Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: SumAndAverage Program Help

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default SumAndAverage Program Help

    Solved! Answer is down below.
    -----
    Almost have it figured out... However, every time the program loops, I think the sum and average get reset to sum=0 and average=1, and I cannot figure out a way around it. Any help would be greatly appreciated!

    Write a program that reads an unspecified number of integers from the keyboard. When the user enters a ‘0’ this signifies the end of data entry and doesn’t itself count as one of the entered values. The program must compute the sum of all the numbers and the average value of the numbers. The program must be named “SumAndAverage”.

    public class SumAndAverage {
    public static void main(String [] args) {
     
    int number;
    int sum=0;
    int average=1;
    int count = 1;
     
    System.out.print("Enter number: ");
    number = Keyboard.readInt();
     
    while (number != 0) {
    System.out.print("Enter number: ");
    number = Keyboard.readInt();
    sum = sum number;
    average = sum / count;
    count +;
    }
    count = count -1;
    System.out.println("The sum of these " count " numbers is: " sum);
    System.out.println("The average of these " count " numbers is: " average);
     
    }
    }
    Last edited by Raiderofrice; October 4th, 2009 at 10:11 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: SumAndAverage Program Help

    The average at the start shouldn't be 1, it should be 0. Also, count should start at 0. There are a few syntax errors inside your while loop, they're probably typo's when you were posting. The real problem could be because of round-off error. In integer math, 3/5 = 0, not 0.6. It's also better to only compute the average once the total sum has been calculated.

    public class SumAndAverage
    {
         public static void main(String[] args)
         {
              double average = 0;
              double sum = 0;
              int count = 0;
              while(true)
              {
                   System.out.print("Enter number: ");
                   int number = new Scanner(System.in).nextInt();
                   if (number == 0)
                   {
                        break;
                   }
                   sum += number;
                   count++;
              }
              average = sum / count;
              System.out.println(The sum of these " + count + " numbers is: " + sum);
              System.out.println("The average of these " + count + " numbers is: " + average);
         }
    }

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: SumAndAverage Program Help

    why is it that the variable average must be initialized into '0' not '1'? ive checked the program (the second one)
    and theres nothing logically wrong..

    it will change the value of the average in a memory space after executing this part...
    average = sum / count;

    the value of average wont be '1' nor '0' anymore...

    but please kindly explain why is it should be initialized into '0' not '1'? (getting curious about this again)
    whats the difference... hmmm

  4. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: SumAndAverage Program Help

    Hey, thanks for your input for the code. I tweaked with it and it finally works! Here is what I have:

    public class SumAndAverage {
        public static void main(String [] args) {
     
    // Object declaration
            int number = 1;
            int sum = 0;
            int average = 0;
            int count = 0;
     
    // Indefinite while loop
            while (number != 0) {
                count++;
                System.out.print("Enter number: ");
                number = Keyboard.readInt();
                 sum = sum + number;
                 average = sum / count;
            }
     
    // Results
            count = count - 1;
            average = sum / count;
            System.out.println("The sum of these " + count + " numbers is: " + sum);
            System.out.println("The average of these " + count +" numbers is: " + average + " and " + sum%count + " / " + count);      
        }
    }