I've figured out how to do most of what I need. I realize that the variables used in the for loops will not be reachable outside of the loop, but I need to display the sum of the integers the user puts in. So,
step 1: Asks user for input of number of integers.
Step 2: Runs through getting each integer.
Step 3: Then displays all input.
And step 4: should have the sum of step 3...and that's
where my issue is.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Dynamic_Entry
{
class Program
{
static void Main()
{
Console.Write("How many integers are in your list? ");
int k = Convert.ToInt32(Console.ReadLine());
int[] a = new int[k];
int sum = 0;
for (int i = 0; i < a.Length; i++)
{
Console.Write("Please enter an integer: ");
a[i] = Convert.ToInt32(Console.ReadLine());
}
for (int n = 0; n < a.Length; n++)
{
Console.WriteLine("{0, 5}", a[n]);
}
Console.WriteLine("-----");
sum += [] a;
Console.Write("{0, 5}", sum);
Console.ReadLine();
}
}
}
Any help on how to get the sum from outside the loop? If I put the hyphen line inside the last loop, it'll keep putting lines after every number...I only need the line at the end, with the sum under it. Thanks!
Since you can used linq, replace the sum with bellow
Console.WriteLine("-----");
sum = a.Sum();
Console.Write("{0, 5}", sum);
You may use
a.Sum()
method of Linq.
And your code should look like following
static void Main()
{
Console.Write("How many integers are in your list? ");
int k = Convert.ToInt32(Console.ReadLine());
int[] a = new int[k];
int sum = 0;
for (int i = 0; i < a.Length; i++)
{
Console.Write("Please enter an integer: ");
a[i] = Convert.ToInt32(Console.ReadLine());
}
for (int n = 0; n < a.Length; n++)
{
Console.WriteLine("{0, 5}", a[n]);
}
Console.WriteLine("-----");
Console.Write("{0, 5}", a.Sum());
Console.ReadLine();
}
Related
This was a small problem the teacher gave us at school. We were asked to make a program which keeps asking the user to input test scores until he inputs -99 to stop the program. The values are stored in an array and the highest, lowest and average scores are displayed.
The problem I have with my code is that whenever I run it it always gives me a value of 0 for lowest scores even though there are no zeros in the input. Can anyone please point out the mistake in my code?
Code:
static void Main(string[] args)
{
int[] za = new int[100];
scores(za, 0);
}
public static void scores(int[] ar, int x)
{
Console.Write("Please enter homework score [0 to 100] (-99 to exit): ");
int a = Convert.ToInt16(Console.ReadLine());
if (a != -99)
{
ar[x] = a;
x++;
scores(ar, x);
}
else
{
Console.Clear();
Console.WriteLine("Homework App");
int[] arr = new int[x];
foreach (int l in arr)
{
arr[l] = ar[l];
}
int lowest = arr[0];
for (int i = 1; i < arr.Length; i++)
{
if (lowest > arr[i]) { lowest = arr[i]; }
}
int highest = arr[0];
for (int j = 1; j < arr.Length; j++)
{
if (highest < arr[j]) { highest = arr[j]; }
}
double sum = 0;
double nums = 0;
for (int k = 0; k < arr.Length; k++)
{
sum = sum + arr[k];
nums++;
}
double average = sum / nums;
Console.WriteLine("Highest Score: {0}", highest);
Console.WriteLine("Lowest Score: {0}", lowest);
Console.WriteLine("Average Score: {0}", average);
Console.ReadLine();
}
}
When you're copying items, don't use a foreach loop and then the element stored in the array as the index that you're inserting to. Instead, use a for loop with a counter variable.
You should change this:
int[] arr = new int[x];
foreach (int l in arr)
{
// You're using the wrong value for the index here.
// l represents the value of the item stored in `arr`, which is `0`
arr[l] = ar[l];
}
To this:
int[] arr = new int[x];
for (var counter = 0; counter < x; counter++)
{
arr[counter] = ar[counter];
}
I have written out a program that declares an array of 10 integers, takes input from the user and puts them in the array and then accepts out parameters for the highest value, lowest value, sum of all values, and the average.
The main method displays all the stats. I don't know why, but I get the error
k does not exist in the current context - line 51 column 5
when I have previously declared it before that given line. Any help would be appreciated.
using System;
namespace IntegerStatistics
{
class Program
{
static void Main()
{
Console.Clear();
// Declaring variables
int[] userArray = FillArray();
int ArrayHighest = 0;
int ArrayLowest = 0;
int ArraySum = 0;
int ArrayAverage = 0;
Calculations(userArray, out ArrayHighest, out ArrayLowest, out ArraySum, out ArrayAverage);
Console.WriteLine("The lowest value in the array is {0}, while the highest is {1}.", ArrayLowest, ArrayHighest);
Console.WriteLine("The array has a sum of {0} and averages out to {1}.", ArraySum, ArrayAverage);
Console.ReadLine();
}
private static int[] FillArray()
{
int[] intArray = new int[10];
int numbersEntered = 0;
int intTemp = 0;
string strTemp = "";
for(int k = 0; k < 10; ++k)
{
Console.WriteLine("Enter a whole number or 999 to quit: ");
strTemp = Console.ReadLine();
while(!int.TryParse(strTemp, out intTemp))
{
Console.WriteLine("Input was not in the correct format");
Console.Write("Please enter a valid number: ");
strTemp = Console.ReadLine();
}
}
if(intTemp != 999)
{
intArray[k] = intTemp;
++numbersEntered;
}
else
{
k = 10;
}
Array.Resize(ref intArray, numbersEntered);
return intArray;
}
private static void Calculations(int[] intArray, out int Highest, out int Lowest, out int intSum, out int average)
{
intSum = 0;
Array.Sort(intArray);
Lowest = intArray[0];
Array.Reverse(intArray);
Highest = intArray[0];
Array.Reverse(intArray);
for(int k = 0; k < intArray.Length; ++k)
{
intSum += intArray[k];
}
average = intSum / intArray.Length;
}
}
}
The line I'm specifically having issues with is:
if (intTemp != 999)
{
intArray[k] = intTemp;
++numbersEntered;
}
else
{
k = 10;
}
I think you meant to place the if-else block inside the for-loop.
Since you want to check if the input number is 999 for each iteration, you could write your for-loop like this:
for(int k = 0; k < 10; ++k)
{
Console.WriteLine("Enter a whole number or 999 to quit: ");
strTemp = Console.ReadLine();
while(!int.TryParse(strTemp, out intTemp))
{
Console.WriteLine("Input was not in the correct format");
Console.Write("Please enter a valid number: ");
strTemp = Console.ReadLine();
}
if(intTemp != 999)
{
intArray[k] = intTemp;
++numbersEntered;
}
else
{
k = 10;
}
}
The loop variable k goes out of scope when the code leaves the for loop. So, you can use variable k only within the loop.
So, you need to move your if-else inside your for loop. Change your FillArray() method to
private static int[] FillArray()
{
int[] intArray = new int[10];
int numbersEntered = 0;
int intTemp = 0;
string strTemp = "";
for (int k = 0; k < 10; ++k)
{
Console.WriteLine("Enter a whole number or 999 to quit: ");
strTemp = Console.ReadLine();
while (!int.TryParse(strTemp, out intTemp))
{
Console.WriteLine("Input was not in the correct format");
Console.Write("Please enter a valid number: ");
strTemp = Console.ReadLine();
}
if (intTemp != 999)
{
intArray[k] = intTemp;
++numbersEntered;
}
else
break;
}
Array.Resize(ref intArray, numbersEntered);
return intArray;
}
Also, I'd suggest changing ArrayAverage to double type. e.g. 2, 3 => average 2.5
double ArrayAverage = 0; //average need not be whole number
Additionally, with Linq you can shorten your Calculations method as
//using System.Linq;
private static void Calculations(int[] intArray, out int Highest, out int Lowest, out int intSum, out double average)
{
Lowest = intArray.Min();
Highest = intArray.Max();
intSum = intArray.Sum();
average = intArray.Average();
}
There is my code:
static void Main(string[] args)
{
int i;
int[] numbers= new int[5];
for (i = 0; i < 5; i++)
{
Console.Write("Insert 5 numbers:");
numeros[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("\n");
for (i = 0; i < 5; i++)
{
Array.Sort(numeros);
Console.WriteLine("Ascendant numbers {0}",numbers[i]);
Array.Reverse(numbers);
Console.WriteLine("Descendant numbers {0}", numbers[i]);
}
Console.ReadLine();
}
So at this moment for each loop my app prints my string per item of my array, followed by the number.
What I want is, to print the string only once, followed by the complete array in a single line.
You can (1) Sort and reverse only once (2) Print the string outside the loop (3) Print only the digit inside loop (4) use Console.Write to print digits in the same line. Something like
Array.Sort(numeros);
Console.WriteLine("Numeros ingresados de forma ascendente :");
for (int i = 0; i < 5; i++)
{
Console.Write("{0} ", numeros[i]);
}
Array.Reverse(numeros);
Console.WriteLine(Environment.NewLine + "Numeros ordenados de forma descendente :");
for (int i = 0; i < 5; i++)
{
Console.Write("{0} ", numeros[i]);
}
N.B. I do not understand the language you are printing, so not sure the meaning remains same after code change.
This question already has answers here:
Find the average. largest, smallest number and mode of a int array
(3 answers)
Closed 7 years ago.
Hi I am trying to make a program that ask the user to enter up to 25 numbers and then displays the average for them but when It asks for the number it just keeps asking for numbers over ans over again and doesn't display the average.
Any guidance would be appreciated.
static void Main(string[] args)
{
Console.WriteLine("enter the amount of numbers you would like to find the average and mean of: ");
int arraylength = Int32.Parse(Console.ReadLine());
int[] AverageArray = new int[25];
//filling the array with user input
for (int i = 0; i < AverageArray.Length; i++)
{
Console.Write("enter the numbers you wish to find the average for: ");
AverageArray[i] = Int32.Parse(Console.ReadLine());
}
//printing out the array
Console.WriteLine("here is your array: ");
for (int i = 0; i < AverageArray.Length; i++)
{
Console.WriteLine(AverageArray[i]);
}
Console.WriteLine(FindAverage(AverageArray));
}
public static double FindAverage(int[] averageNumbers)
{
int arraySum = 0;
for (int i = 0; i < averageNumbers.Length; i++)
arraySum += averageNumbers[i];
return arraySum / averageNumbers.Length;
}
}
Well you already have arrayLength, why not use that as your array size indicator? Also, your average routine will lose precision. You are returning double, but your code has everything as int.
static void Main(string[] args)
{
Console.WriteLine("enter the amount of numbers you would like to find the average and mean of: ");
int arraylength = Int32.Parse(Console.ReadLine());
int[] AverageArray = new int[arraylength];
//filling the array with user input
for (int i = 0; i < arraylength; i++)
{
Console.Write("enter the numbers you wish to find the average for: ");
AverageArray[i] = Int32.Parse(Console.ReadLine());
}
//printing out the array
Console.WriteLine("here is your array: ");
for (int i = 0; i < arrayLength; i++)
{
Console.WriteLine(AverageArray[i]);
}
Console.WriteLine(FindAverage(AverageArray));
}
public static double FindAverage(int[] averageNumbers)
{
// change to 'double' otherwise you will lose precision
double arraySum = 0;
for (int i = 0; i < averageNumbers.Length; i++)
arraySum += averageNumbers[i];
return arraySum / averageNumbers.Length;
}
I am trying to ask the user to input 10 numbers. After receiving the numbers, I am storing them in an array followed by printing the array. I came up with the following code to do the task but it is not printing the array.
Also feel that I may have rattled on way too much code for a simple task. Do note that I am very new to c# thus not familiar with advanced stuff or possibly even most of basic stuff. Even the "convert.toInt32", I adopted from reading around and not taught in class yet.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test_Array
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int h;
int i;
int j;
Console.WriteLine("Please input 10 numbers. Press 'ENTER' after each number.");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = Convert.ToInt32(Console.ReadLine());
d = Convert.ToInt32(Console.ReadLine());
e = Convert.ToInt32(Console.ReadLine());
f = Convert.ToInt32(Console.ReadLine());
g = Convert.ToInt32(Console.ReadLine());
h = Convert.ToInt32(Console.ReadLine());
i = Convert.ToInt32(Console.ReadLine());
j = Convert.ToInt32(Console.ReadLine());
int[] newArray = {a,b,c,d,e,f,g,h,i,j};
Console.WriteLine(newArray);
Console.ReadLine();
}
}
}
use a for loop.
int[] newArray = new int[10];
for (int i = 0; i < newArray.Length; i++)
{
newArray[i] = Convert.ToInt32(Console.ReadLine());
}
You can use the same loop to display as well:
for (int i = 0; i < newArray.Length; i++)
{
Console.WriteLine(newArray[i]);
}
The ToString method of arrays (which is what Console.WriteLine is calling in your code) isn't overloaded to print out the contents of the array. It leaves the basic object implementation of just printing the type name.
You need to manually iterate the array and print out the individual values (or use a method that will do that for you).
I.e.
foreach(var item in array)
Console.WriteLine(item)
or
Console.WriteLine(string.Join("\n", array));
static void Main(string[] args)
{
int[] rollno = new int[10];
Console.WriteLine("Enter the 10 numbers");
for (int s = 0; s < 9; s++)
{
rollno[s] = Convert.ToInt32(Console.ReadLine());
rollno[s] += 110;
}
for (int j = 0; j < 9; j++)
{
Console.WriteLine("The sum of first 10 numbers is : {0}", rollno[j]);
}
Console.ReadLine();
}
}
}
You could simplify things a lot with the following:
static void Main(string[] args)
{
int newArray = new int[10];
Console.WriteLine("Please input 10 numbers. Press 'ENTER' after each number.");
for (int i = 0; i < 10; i++) {
newArray[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("The values you've entered are:");
Console.WriteLine(String.Join(", ", newArray));
Console.ReadLine();
}