Extra zero in final out put where does it come from? - c#

I wrote this code to order any set of numbers from biggest to smallest, but for whatever reason, the output always has a zero at the end. My question is, where did it come from? (new to coding)
Console.WriteLine("Please enter set of numbers");
int input = Convert.ToInt16(Console.ReadLine());
int counter= 1;
int temp1;
int temp2;
int[] array = new int[input+1];
for (int i = 0; i <=input-1; i++)
{
Console.WriteLine("Please enter entry number " + counter);
int number = Convert.ToInt16(Console.ReadLine());
array[i] = number;
counter++;
}
for (int x = 0; x <= input; x++)
{
for (int i = 1; i <=input; i++)
{
if (array[i - 1] <= array[i])
{
temp1 = array[i - 1];
temp2 = array[i];
array[i - 1] = temp2;
array[i] = temp1;
}
}
}
Console.WriteLine();
for (int i = 0; i<=input; i++)
{
Console.Write(array[i] + " ");
}
Console.ReadLine();

You're inconsistent between whether you're trying to handle input + 1 or input elements. For example:
int[] array = new int[input+1];
for (int i = 0; i <=input-1; i++)
{
Console.WriteLine("Please enter entry number " + counter);
int number = Convert.ToInt16(Console.ReadLine());
array[i] = number;
counter++;
}
You're creating an array with input + 1 elements, but only populating input of them.
In general, it's much more common to use exclusive upper boundaries for loops. For example:
int[] array = new int[input];
for (int i = 0; i < input; i++)
{
// No need for the counter variable at all
Console.WriteLine("Please enter entry number " + (i + 1));
int number = Convert.ToInt16(Console.ReadLine());
array[i] = number;
}

Related

i want to write a console app that get int value from user and put them in a array

i want to write a console app that get int value from user and put them in a array and show sum of the numbers and min and max of them and then print them in the console in order
i write until this point of project but have some bugs...
Console.WriteLine("\n Please Enter the Number of your Numbers: \n");
int k = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Please Enter Your Numbers:");
int[] myArray = new int[k];
int sum = 0;
//INPUT
for (int i = 0; i < k; i++)
{
myArray[i] = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Number [" + (i+1) + "]: " + myArray[i] + " and Next:");
}
//DELETE DUPLICATE ELEMENTS
int[] newArray = myArray.Distinct().ToArray();
//SORT
Array.Sort(newArray);
Console.WriteLine("\n Your Sorted Numbers Without Duplicated ones: ");
foreach (int i in newArray)
{
Console.Write(" | " + i);
}
Console.Write(" |");
i think you are trying to make a program like this:
int i,n;
int[] a = new int[100];
Console.WriteLine("element numbers :");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("please entre your numbers : ", n);
for (i = 0; i < n; i++)
{Console.WriteLine($"element i");
a[i] = Convert.ToInt32(Console.ReadLine());}
int sum = 0;
for (i = 0; i < n; i++)
{sum += a[i];}
Console.WriteLine("sum is : {0}", sum);
int max = a[0];
int min = a[0];
for (i = 1; i < n; i++)
{if (a[i] > max)
{max = a[i];}
if (a[i] < min)
{min = a[i];}}
Console.WriteLine("max is : " + max);
Console.WriteLine("min is : " + min);
Console.WriteLine("element numbers are: ");
for (i = 0; i < n; i++)
{Console.WriteLine(a[i]);}

Displaying an arrays index number

I need help with C#
I'm currently trying to find the minimum value in a 2D array. I have managed to do this however, after I have found the minimum value I want the corresponding values index number (Where it is stored in my 2D array) to be output. For example, I have a 2D and a 1D array. Once the minimum value has been discovered the index value for the 2D array needs to be changed in the 1D array.
The 2d array is c[i,j]
and the 1D array is a[i]
so how would i be able to display the j number in my c array in my a array. For example, if my minimum value was at c[1,5] I want the value in a[5] to be changed from 0 to 1. Any help would be great thanks!
P.S. if ive made this sound really confusing I apologise I'm new to this !
int n = 5, m = 10, MinValue = 100, MaxValue = 1, Total = 0, Sum = 0; //n = number of values m = max value in array MinValue = Lowest number in array
Random Rand = new Random();
int[] A = new int[n + 1];
for (int i = 1; i < n+1; i++) //Reached and unreached array - creation
{
A[i] = 0;
}
A[1] = 1;
int[,] c = new int[n + 1, n + 1]; //Create array
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
c[i, j] = Rand.Next(1, m); //Randomise the array
if (i == j)
{
c[i, j] = 99; // give void spaces the value of 99
}
if (c[i, j] > MaxValue && c[i, j] < 99)
{
MaxValue = c[i, j]; // max value takes the highest value (that isnt 99)
}
}
}
for (int p = 1; p <= n; p++)
{
for (int i = 1; i <= n+1; i++)
{
for (int j = 1; i <= n+1; i++)
{
if (c[i, j] <= MinValue)
{
if (A[i] == 1)
{
if (A[i] == 0)
{
Total = Sum + MinValue;
Sum = Total;
A[i] = 1;
}
}
}
}
}
}
Console.WriteLine("Total Value = " + Total);
Console.WriteLine("");
Console.WriteLine("The tracking array - what has been reached and what hasn't"); // Output reaching array
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
Console.WriteLine("A[" + i + "] = " + A[i].ToString("00") + " ");
//Output the array to screen
}
Console.WriteLine("");
Console.WriteLine("The link length array"); // Output link length array
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
Console.Write("c[" + i + "," + j + "] = " + c[i, j].ToString("00") + " ");
}
Console.WriteLine();
//Output the array to screen
}
Console.WriteLine("");
Console.ReadLine();

How do you find averages of numbers in multidimensional arrays from user input?

This is my first attempt at creating a multidimensional array. I have the user enter the number of students in a class(the rows), and then have them enter the number of scores they will input(the columns). I now want to add all the scores of each individual student and find each of their grade averages. I can't figure out how to separate out the information for each individual student's data. Here is what I have so far:
public static void Main(string[] args)
{
int TotalStudents = 0;
int TotalGrades = 0;
int sum = 0;
Console.WriteLine("Enter the number of students: ");
TotalStudents = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the number of grades: ");
TotalGrades = Convert.ToInt32(Console.ReadLine());
int[,] scoresArray = new int[TotalStudents, TotalGrades];
for (int i = 0; i < TotalStudents; i++)
for (int j = 0; j < TotalGrades; j++)
{
Console.Write("Please enter score {0} for student {1}:", j + 1, i + 1);
scoresArray[i, j] = Convert.ToInt32(Console.ReadLine());
sum = sum + Convert.ToInt32(scoresArray[i,j]);
}
double gradePercent = sum / (TotalGrades * 100);
double gradePer100 = gradePercent * 100;
string gradeLetter = "";
if (gradePer100 >= 90)
{
gradeLetter = "A";
}
else if (gradePer100 >= 80 && gradePer100 < 90)
{
gradeLetter = "B";
}
else if (gradePer100 >= 70 && gradePer100 < 80)
{
gradeLetter = "C";
}
else if (gradePer100 >= 60 && gradePer100 < 70)
{
gradeLetter = "D";
}
else
{
gradeLetter = "F";
}
Console.WriteLine("\nStudent average score is: " + gradePer100);
Console.WriteLine("\nStudent will recieve a " + gradeLetter + " in the class.");
Console.Write("\nPress the [ENTER] key to exit.");
Console.ReadLine();
}
You can create a 2-dimensional array like this :
var array = new int[TotalStudents, TotalGrades];
And then fill it:
for(int i = 0; i < TotalStudents; i++)
for(int j = 0; j < TotalGrades; j++)
{
Console.Write("Please enter score {0} for student {1}", j + 1, i + 1);
array[i, j] = Convert.ToInt32(Console.ReadLine());
}
If the scores are doubles just change new int[TotalStudents, TotalGrades] to new double[TotalStudents, TotalGrades] and Convert.ToInt32(Console.ReadLine()) to Convert.ToDouble(Console.ReadLine())

c#: Project Euler 8. Have some troubles in program

I'm writing a program to solve problem 8 in project Euler, which finds the thirteen adjacent digits in the 1000-digit number that have the greatest product. Firstly, i determine that the 1000-digit number is a string. Then i create a split method that breaks up this string into smaller chars, and converts it to int number. (i use ulong type instead of int)
static ulong split(char input)
{
ulong number = ulong.Parse(Convert.ToString(input));
return number;
}
then i create a method that has ability to check whether what is the biggest product. convert it into int type, find the product of thirteen adjacent digits.
static ulong check(string string1)
{
ulong product = 1;
ulong max = 0;
ulong[] array = new ulong[50];
char[] h1 = string1.ToCharArray();
for (int i = 0; i < string1.Length; i++)
array[i] = split(h1[i]);
for (int i = 0; i < array.Length - 12; i++)
{
for (int j = 0; j < 13; j++)
{
product *= array[i + j]
if (product > max)
max = product;
}
product = 1;
}
return max;
In the main method. i write some code to input the 1000-digit number and write the answer.
string input = #"73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450";
input = input.Replace(" ", "");
input = input.Replace("/r/n", "");
Console.Write(check(input));
Console.ReadKey();
But the program has trouble in the split method. can you explain for me, please ? :(
here is all my program.
string input = #"73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450";
input = input.Replace(" ", "");
input = input.Replace("/r/n", "");
Console.Write(check(input));
Console.ReadKey();
}
static ulong split(char input)
{
ulong number = ulong.Parse(Convert.ToString(input));
return number;
}
static ulong check(string string1)
{
ulong product = 1;
ulong max = 0;
ulong[] array = new ulong[50];
char[] h1 = string1.ToCharArray();
for (int i = 0; i < string1.Length; i++)
array[i] = split(h1[i]);
Console.WriteLine();
for (int i = 0; i < array.Length - 12; i++)
{
for (int j = 0; j < 13; j++)
{
product *= array[i + j];
if (product > max)
{
max = product;
Console.Write(array[i + j] + " ");
}
}
product = 1;
}
return max;
}

How to add elements to an array based on a condition?

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 + " ");

Categories