need help figuring out what im missing program wont run //Program prompts user f
ID: 3543870 • Letter: N
Question
need help figuring out what im missing program wont run
//Program prompts user for any number vales
//(up to 20)
//and averages them
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DebugSix04
{
class Program
{
static void Main(string[] args)
{
const int QUIT = 999;
int[] numbers = new int [20];
int x = 0;
int num;
double average;
double total = 0;
string inString;
Console.Write("Please enter a number or " +
QUIT + " to quit...");
inString = Console.ReadLine();
num = Convert.ToInt32(inString);
while ((x < numbers.Length) && num != QUIT)
{
numbers[x] = num;
total += numbers[x];
++x;
Console.Write("Please enter a number or " + QUIT + " to quit...");
inString = Console.ReadLine();
num = Convert.ToInt32(inString);
}
Console.WriteLine("The numbers are:");
for (int y = 0; y < x; ++x)
Console.Write("{0,6}", numbers[y]);
average = total / numbers.Length;
Console.WriteLine();
Console.WriteLine("The Average is {0}", average);
}
}
}
Explanation / Answer
CHANGED CODE IN BOLD LETTERS :
Here you are trying to compare a string with a numeric value and which is always a false expression to evaluate .
Instead first you check the string then convert it like following
and please chek if this is working because wntire program is not here i cant check if you are facing any problem please post here :
//Program prompts user for any number vales
//(up to 20)
//and averages them
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DebugSix04
{
class Program
{
static void Main(string[] args)
{
const int QUIT = 999;
int[] numbers = new int [20];
int x = 0;
int num;
double average;
double total = 0;
string inString;
Console.Write("Please enter a number or " +
QUIT + " to quit...");
inString = Console.ReadLine();
while ((x < numbers.Length) && num != QUIT)
{
num = Convert.ToInt32(inString);
numbers[x] = num;
total += numbers[x];
++x;
Console.Write("Please enter a number or " + QUIT + " to quit...");
inString = Console.ReadLine();
}
Console.WriteLine("The numbers are:");
for (int y = 0; y < x; ++x)
Console.Write("{0,6}", numbers[y]);
average = total / numbers.Length;
Console.WriteLine();
Console.WriteLine("The Average is {0}", average);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.