Error in the program

I am trying to get the average of 20 integers as long as it is not equ

Page 1 of 1

5 Replies - 1111 Views - Last Post: 18 September 2008 - 05:08 PM Rate Topic: -----

#1 mshelia   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-August 08

Error in the program

Post icon  Posted 18 September 2008 - 11:28 AM

Can someone please help with this. I keep getting errors.


using System;

public class AverageNumbers

{

public static void Main()

{

const int QUIT = 999;

int numbers = new int[20];

int num;

string inString;





// Ask user to enter a number or QUIT to exit 

Console.Write("Please enter a number or " + QUIT + " to quit...");

inString = Console.ReadLine();

num = Convert.ToInt32(intString);



int x;

double total;



x = 0;

double total;

while((x < numbers.Length))

{

numbers[x] = num;

++x;

total += numbers[x];

Console.Write("Please enter a number or " + QUIT + " to quit...");

inString = Console.ReadLine();

numbers = Convert.ToInt32(inString);

}

Console.WriteLine("The numbers are:");

for(int y = 0; y < x; ++y)

Console.Write("{0,6}", numbers[y]);



double average; 

average = total / numbers.Length;

Console.WriteLine();

Console.WriteLine("The average is {0}", average);

Console.Read();

}

}



Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Error in the program

#2 Mach1Guy   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: Error in the program

Posted 18 September 2008 - 11:44 AM

remember when displaying code such as above to put all the code between the code tags when posting. makes it easier to read ;)
 

This post has been edited by Mach1Guy: 18 September 2008 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15427
  • View blog
  • Posts: 61,802
  • Joined: 12-June 08

Re: Error in the program

Posted 18 September 2008 - 11:48 AM

View Postmshelia, on 18 Sep, 2008 - 01:28 PM, said:

Can someone please help with this. I keep getting errors.

using System;

public class AverageNumbers

{

public static void Main()

{

const int QUIT = 999;

int numbers = new int[20];

int num;

string inString;





// Ask user to enter a number or QUIT to exit 

Console.Write("Please enter a number or " + QUIT + " to quit...");

inString = Console.ReadLine();

num = Convert.ToInt32(intString);



int x;

double total;



x = 0;

double total;

while((x < numbers.Length))

{

numbers[x] = num;

++x;

total += numbers[x];

Console.Write("Please enter a number or " + QUIT + " to quit...");

inString = Console.ReadLine();

numbers = Convert.ToInt32(inString);

}

Console.WriteLine("The numbers are:");

for(int y = 0; y < x; ++y)

Console.Write("{0,6}", numbers[y]);



double average; 

average = total / numbers.Length;

Console.WriteLine();

Console.WriteLine("The average is {0}", average);

Console.Read();

}

}


What sort of errors?

Maybe something about the double total; not being instantiated?

How about that same variable being declared twice? (line 35 and 41)

Divide by zero potential in line 71?

Line 49 increment should probably happen AFTER line 51?

This post has been edited by modi123_1: 18 September 2008 - 11:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 Mach1Guy   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 89
  • Joined: 04-December 06

Re: Error in the program

Posted 18 September 2008 - 12:01 PM

int x = 0;
int total = 0;



also be sure to check the denominator before dividing so you do not divide by zero...ex: if (numbers.Length > 0)

This post has been edited by Mach1Guy: 18 September 2008 - 12:03 PM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Error in the program

Posted 18 September 2008 - 12:44 PM

num = Convert.ToInt32(intString);

It's not intString, it's inString you want.
numbers = Convert.ToInt32(inString);

Here you're trying to set an integer array to the value of an integer.
Was This Post Helpful? 0
  • +
  • -

#6 jacobjordan   User is offline

  • class Me : Perfection
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Error in the program

Posted 18 September 2008 - 05:08 PM

What line are you receiving errors on, and what is the error message?

We cannot help you if you don't be specific.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1