Program needs to be written in C#. Thank you Write a console program that allows
ID: 3875755 • Letter: P
Question
Program needs to be written in C#. Thank you
Write a console program that allows a user to enter up to 10 numbers. (The program will stop accepting numbers either when 10 numbers have been entered or when a sentinel value of 0 is entered.) Once the program has finished gathering numbers, it should display the average of the entered numbers and the difference of each number from that average. Use named constants when appropriate. Comment your code. Hints: to do this you'll need two loops. The first loop collects the numbers and stores them in the array (you can also count and accumulate the numbers with this loop). After that loop you can figure out the average (make sure you divide by the actual number of numbers entered, not just 10). The second loop will then display only the numbers entered and the difference mentioned above.
Explanation / Answer
//Rextester.Program.Main is the entry point for your code. Don't change it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
int [] numbers = new int[10];
double averageArray = 0.0;
//Your code goes here
for ( int i = 0; i < 10; i++ ) {
string line = Console.ReadLine();
int j;
if (Int32.TryParse(line, out j)){
Console.WriteLine(j);
numbers[i] = j;
}else{
Console.WriteLine("String could not be parsed.");
}
}
for ( int i = 0; i < 10; i++ ) {
averageArray = averageArray + numbers[i];
Console.WriteLine(" Input Number "+numbers[i]);
}
averageArray = averageArray/10;
Console.WriteLine("Average Number "+ averageArray);
for ( int i = 0; i < 10; i++ ) {
double value1 = (averageArray - numbers[i]);
Console.WriteLine("Difference from average "+ value1);
}
}
}
}
Input
------
10 20 30 40 50 60 70 80 90 100
Output
------------
Input Number 10 Input Number 20 Input Number 30 Input Number 40 Input Number 50 Input Number 60 Input Number 70 Input Number 80 Input Number 90 Input Number 100 Average Number 55 Difference from average 45 Difference from average 35 Difference from average 25 Difference from average 15 Difference from average 5 Difference from average -5 Difference from average -15 Difference from average -25 Difference from average -35 Difference from average -45
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.