Would it be possible to merge two arrays without using concat function or copy? I tried using copy but I'm looking for a method where it you don't need to use a function to merge two arrays. Thank you.
using System;
namespace console;
{
class Program
{
public static void Main()
{
try {
string input = "Enter the elements [{0}]: ";
//User input for integer
Console.Write("Please enter the first size of the array: ");
int yourfirstArray = Convert.ToInt32(Console.ReadLine());
int i;
string[] yourArrays = new string[yourfirstArray];
for (i = 0; i != yourArrays.Length; i++)
{
Console.Write(input, i, yourArrays[i]);
yourArrays[i] = Console.ReadLine();
}
Console.Write("Please enter the second size of the array: ");
int yoursecondArray = Convert.ToInt32(Console.ReadLine());
string[] myArrays = new string[yoursecondArray];
int j;
for (j = 0; j != myArrays.Length; j++)
{
Console.Write(input, j, yourArrays[j]);
yourArrays[j] = Console.ReadLine();
}
string[] thisMerged = new string[myArrays.Length + yourArrays.Length];
myArrays.CopyTo(thisMerged, 0);
yourArrays.CopyTo(thisMerged, myArrays.Length);
for (int k = 0; k != myArrays.Length; k++)
{
Console.Write("", i + myArrays[i], "", j + yourArrays[j]);
}
Console.ReadLine();
}
catch
{
Console.Write("Please input the correct data value.");
Console.ReadLine();
}
}
}
}
Related
I am making a project and I got a problem when I was taking data from the user using an array of objects.
When I run the program it asks for the data to be entered as many time as I want. But it only prints the data which user entered the first time.
No, I cannot use lists, I can only use arrays. It's the demand of the problem
using System;
namespace agentX
{
class Application
{
private static int x;
static void Main(string[] args)
{
Console.WriteLine("Enter the number of passengers");
x=int.Parse(Console.ReadLine());
Customer[] S = new Customer[x];
for (int i = 0; i < x; i++)
{
S[i] = new Customer();
S[i].SetInfo();
}
for (int j = 0; j < x; j++)
{
S[j].printInfo();
}
}
}
class Customer
{
//private data members
private int rollno;
private string name;
private int age;
//method to set student details
public void SetInfo()
{
Console.WriteLine("Enter the name ");
this.name=Console.ReadLine();
Console.WriteLine("Enter the roll number");
this.rollno = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the age");
this.age = int.Parse(Console.ReadLine());
}
public void printInfo()
{
Console.WriteLine("\r\nStudent Record: ");
Console.WriteLine("\tName : " + this.name);
Console.WriteLine("\tRollNo : " + this.rollno);
Console.WriteLine("\tAge : " + this.age);
Console.ReadKey();
}
}
}
From your printInfo method you want to move the last line Console.ReadKey(); out to your Main under the last loop. Reason being is that Console.ReadKey() blocks the loop until you press a key.
public void printInfo()
{
Console.WriteLine("\r\nStudent Record: ");
Console.WriteLine("\tName : " + this.name);
Console.WriteLine("\tRollNo : " + this.rollno);
Console.WriteLine("\tAge : " + this.age);
}
static void Main(string[] args)
{
Console.WriteLine("Enter the number of passengers");
x=int.Parse(Console.ReadLine());
Customer[] S = new Customer[x];
for (int i = 0; i < x; i++)
{
S[i] = new Customer();
S[i].SetInfo();
}
for (int j = 0; j < x; j++)
{
S[j].printInfo();
}
Console.ReadKey();
}
This program will sort numbers and depending if they are greater than or less than 100, put them into their own array.
The problem I have is printing this array. I would like the last number on the line, to not be followed by a space. I have tried many many times to get this to work now and figured I'd ask here.
I know of Console.Write("\b"); but I prefer to find a way of editing the loop so I don't have to do this. Here is the code:
using System;
using System.Linq;
class SplitArray
{
public static void Main(string[] args)
{
int[] myArray = GetNumbersFromConsole();
int[] smallNumbers = new int[myArray.Length];
int[] bigNumbers = new int[myArray.Length];
int bigIndex = 0;
int littleIndex = 0;
for (int i = 0; i < myArray.Length; i++)
{
if(myArray[i] > 100)
{
bigNumbers[bigIndex++] = myArray[i];
}
else if(myArray[i] < 100)
{
smallNumbers[littleIndex++] = myArray[i];
}
}
Console.Write("Big: ");
for (int i = 1; i < bigIndex; ++i)
{
Console.Write(bigNumbers[i]);
Console.Write(" ");
}
Console.WriteLine();
//Console.WriteLine($"{bigNumbers[0]}");
Console.Write("Little: ");
for (int i = 0; i < littleIndex; i++)
{
Console.Write($"{smallNumbers[i]}");
Console.Write(" ");
}
Console.ReadLine();
}
static int[] GetNumbersFromConsole()
{
int count = int.Parse(Console.ReadLine());
int[] result = new int[count];
for (int i = 0; i < count; ++i)
{
result[i] = int.Parse(Console.ReadLine());
}
return result;
}
}
You could just capture your Console.Write(" "); in an if statement.
if(i != littleIndex - 1)
{
Console.Write(" ");
}
littleIndex - 1 is the last time your loop executes, so this will just prevent it from adding the trailing white space. Just do the same for your big numbers as you're printing them out.
There is a builtin utility string.Join
var str = string.Join(" ",bigNumbers);
Console.WriteLine("Big: " + str);
I was playing around some more and just thought I'd try posting the first object in the array before running this loop (also changed loop so the first item didn't get printed twice).
Thank you everyone - I solved it! :)
using System;
using System.Linq;
namespace Arrays
{
class SplitArray
{
public static void Main(string[] args)
{
int[] myArray = GetNumbersFromConsole();
int[] smallNumbers = new int[myArray.Length];
int[] bigNumbers = new int[myArray.Length];
int bigIndex = 0;
int littleIndex = 0;
for (int i = 0; i < myArray.Length; i++)
{
if(myArray[i] > 100)
{
bigNumbers[bigIndex++] = myArray[i];
}
else if(myArray[i] < 100)
{
smallNumbers[littleIndex++] = myArray[i];
}
}
Console.Write("Big: ");
Console.Write($"{bigNumbers[0]} ");
for (int i = 1; i < bigIndex; i++)
{
Console.Write(bigNumbers[i]);
if (i != bigIndex - 1)
{
Console.Write(" ");
}
}
Console.WriteLine();
Console.Write("Little: ");
Console.Write($"{smallNumbers[0]} ");
for (int i = 1; i < littleIndex; i++)
{
Console.Write($"{smallNumbers[i]}");
if (i != littleIndex - 1)
{
Console.Write(" ");
}
}
Console.ReadLine();
}
static int[] GetNumbersFromConsole()
{
int count = int.Parse(Console.ReadLine());
int[] result = new int[count];
for (int i = 0; i < count; ++i)
{
result[i] = int.Parse(Console.ReadLine());
}
return result;
}
}
}
static void Main(string[] args)
{
int m, count = 0;
Console.WriteLine("Enter the Limit : ");
m = int.Parse(Console.ReadLine());
int[] a = new int[m];
Console.WriteLine("Enter the Numbers :");
for (int i = 0; i < m; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}
foreach (int o in a)
{
if (o == 1)
{
count++;
}
}
Console.WriteLine("Number of 1s in the Entered Number : "+count);
Console.ReadLine();
}
here get each value into the array, and check each value equal to one are not. But i need this task without using an array. Could you please help us.
Just check the input when it's entered, without storing it:
static void Main(string[] args)
{
int m, count = 0;
Console.WriteLine("Enter the Limit : ");
m = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Numbers :");
for (int i = 0; i < m; i++)
{
if(Console.ReadLine() == "1")
count++;
}
Console.WriteLine("Number of 1's in the Entered Number : "+count);
Console.ReadLine();
}
You can simply keep a count where you add it to the array
int m, count = 0;
Console.WriteLine("Enter the Limit : ");
m = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Numbers :");
for (int i = 0; i < m; i++)
{
count += Console.ReadLine() == "1" ? 1 : 0;
}
Console.WriteLine("Number of 1's in the Entered Number : "+count);
Console.ReadLine();
You could use LINQ and remove the for loop as well as the array.
Console.WriteLine("Enter the Limit : ");
int m = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the Numbers :");
int count =
Enumerable
.Range(0, m)
.Select(n => Console.ReadLine())
.Where(x => x == "1")
.Count();
Console.WriteLine("Number of 1's in the Entered Number : " + count);
Console.ReadLine();
I would advice using more meaningful variable names and adding input validation Int32.TryParse instead Convert.ToInt32.
Just do the if (o == 1) check in the first for and forget about the second one.
You can use List instead of array.
code is here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberofOnes
{
public class Program
{
static void Main(string[] args)
{
int m, count = 0;
Console.WriteLine("Enter the Limit : ");
m = int.Parse(Console.ReadLine());
List<int> a = new List<int>();
Console.WriteLine("Enter the Numbers :");
for (int i = 0; i < m; i++)
{
a.Add( Convert.ToInt32(Console.ReadLine()));
}
foreach (int o in a)
{
if (o == 1)
{
count++;
}
}
Console.WriteLine("Number of 1's in the Entered Number : " + count);
Console.ReadLine();
}
}
}
I'm working on this simple C# program adding elements to an array. I allow the user to enter 5 numbers, and if the user enters an INVALID valid I have a message for that. My issue is that whether the users enters an invalid number or not I still want to add 5 numbers to my array.
My code works, but let's say the user enters 3 numbers and then 2 words and I end up having ONLY 3 numbers, but I want the 5 numbers no matter what. What am I doing wrong?
Here's my code:
int[] numbers = new int[5];
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Enter a number: ");
string c = Console.ReadLine();
int value;
if (int.TryParse(c, out value))
{
numbers[i] = value;
}
else
{
Console.WriteLine("You did not enter a number\n");
}
}
for (int i = 0; i < numbers.Length; i++ )
{
Console.Write(numbers[i] + " ");
}
You can reduced increment count by 1, when user inputs wrong/no number.
Also note, you are code currently reading input only for 4(not 5 as question description says.) numbers.
int[] numbers = new int[4];
for (int i = 0; i < 4; i++)
{
Console.WriteLine("Enter a number: ");
string c = Console.ReadLine();
int value;
if (int.TryParse(c, out value))
{
numbers[i] = value;
}
else
{
i--;
Console.WriteLine("You did not enter a number\n");
}
}
for (int i = 0; i < numbers.Length; i++ )
{
Console.Write(numbers[i] + " ");
}
try using do-while
int[] numbers = new int[4];
int i = 0;
do
{
Console.WriteLine("Enter a number: ");
string c = Console.ReadLine();
int value;
if (int.TryParse(c, out value))
{
numbers[i] = value;
i++;
}
else
{
Console.WriteLine("You did not enter a number\n");
}
} while (i < 5);
Console.WriteLine("\nYour entered numbers are\n");
for (int j = 0; j < numbers.Length; j++ )
{
Console.Write(numbers[j] + " ");
}
You could use while loop here. See the below code
int[] numbers = new int[5];
int i = 0;
while (i < 5) {
Console.WriteLine ("Enter a number: ");
string c = Console.ReadLine ();
int value;
if (int.TryParse (c, out value)) {
numbers[i] = value;
i++;
} else {
Console.WriteLine ("You did not enter a number\n");
}
}
for (i = 0; i < numbers.Length; i++) {
Console.Write (numbers[i] + " ");
}
You can reduce the code using while loop. Also its better to change the last for loop to foreach
int[] numbers = new int[5];
int i = 0;
while (i < 5)
{
Console.WriteLine("Enter a number: ");
string c = Console.ReadLine();
int value;
if (!int.TryParse(c, out value)) continue;
numbers[i] = value;
i++;
}
foreach (int t in numbers)
Console.Write(t + " ");
I have a program here that need some improvements. This Program inputs 5 elements in an Array and Removes if any duplicates. It works but the problem is that it sets every duplicate to zero. I don't want to display zero. I want it completely destroyed and eliminated. I don't want that duplicate element to appear. This is what I have so Far! Could Use some help. Thank You.
// Gurpreet Singh
// Duplicate Program
using System;
class duplicate
{
static void Main()
{
const int Array_Size = 5;
int [] number = new int [Array_Size];
int i;
for ( i = 0; i < Array_Size; i++)
{
Console.Write("Element " + i + ": ");
number[i] = Int32.Parse(Console.ReadLine());
if (number[i] < 9 || number[i] > 101)
{
Console.WriteLine("Enter Number between 10 - 100");
number[i] = Int32.Parse(Console.ReadLine());
}
}
for (i = 0; i < Array_Size; i++)
{
for (int j = 0; j < Array_Size; j++)
{
if (i != j)
{
if (number[j] == number[i])
number[j] = 0;
}
}
}
Console.WriteLine("Duplicate Removed:");
for (i = 0; i < Array_Size; i++)
{
Console.WriteLine("Element " + i + " " + number[i]);
}
Console.ReadLine();
}
}
The easiest way is to use Linq's Distinct method:
number = number.Distinct().ToArray();
This will return a new array without any duplicates.
The duplicate is displayed as zero, since you assign the value of the duplicate to be zero, in the line,
if(number[j]==number[i])
number[j]=0
to delete the element from the array, use the following code:
if(number[j]==number[i])
{
int k=j;
while(k<Array_Size-1)
{
number[k]=number[k+1];
k++;
}
Array_Size--;
}
the statement Array_Size--; is done so that the last element is not repeated twice
This is my complete code in which I put some double-for-loop statement to
prevent it from inserting the duplicated integers in an array.
Have a look.
class Program
{
static void Main(string[] args)
{
const int ARRAY_SIZE = 5;
int[] ArrayTable = new int[ARRAY_SIZE];
int Element=0;
int a;
for(a=0; a<ArrayTable.Length;a++)
{
Console.Write("Please Enter an integer (between 10-100): ");
Element = Int32.Parse(Console.ReadLine());
while (Element < 10 || Element > 100)
{
Console.Write("Try again (between 10-100): ");
Element = Int32.Parse(Console.ReadLine());
}
ArrayTable[a] = Element;
for (int b = 0; b < a; b++)
{
while (ArrayTable[a] == ArrayTable[b])
{
Console.Write("Integer Duplicated!\nTry again: ");
Element = Int32.Parse(Console.ReadLine());
ArrayTable[a] = Element;
Console.WriteLine();
while (Element < 10 || Element > 100)
{
Console.Write("Try again (between 10-100): ");
Element = Int32.Parse(Console.ReadLine());
ArrayTable[a] = Element;
}
}
}
}
for (int c = 0; c < ArrayTable.Length; c++)
{
Console.Write("{0} ", ArrayTable[c]);
}
}