Results 1 to 17 of 17
  1. #1
    SweetLD215 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default Help with while iterative procedure

    Hey there,

    I could use some help with my homework once again.

    Here's the question:

    Write a Java program which uses a while iterative procedure to allow the user to enter a list of numbers terminated by 0, and which totals them, finds their average and displays their total and average.

    I'm not completely sure how to write the code to produce both sum and average, but I googled and researched and gave it a shot and... well, my code definitely is not right. If anyone can explain how to write this and maybe let me know where I'm going wrong, I'd really appreciate it.

    import java.util.Scanner;
    public class SwitchStatement {
    public static void main(String[] args) {
    Scanner inp = new Scanner (System.in);
    int x = 1;
    int sum = 0;
    System.out.println ("Please enter a sequence of integer numbers ending with 0");
    while (x != 0) {
    x = inp.nextInt();
    sum +=x;
    }
    System.out.println ( "Sum of integers is " + sum);

    int count; // The number of positive integers.

    count = 0;
    System.out.println(x);
    while (x != 0) {
    count++; // Count the input by adding 1 to count.
    x = ((int)sum) / count;
    System.out.println ( "Average of integers is " +x);
    }
    }
    }


    When I run the prorgram, I can enter the integers, and get the sum, but I can't get the average to display.
    Here's what it looks like:

    Please enter a sequence of integer numbers ending with 0
    21
    46
    0

    Sum of integers is 67
    0


    Any help/explanation would be really appreciated. Thank you!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    You should be counting the number of numbers while they're being read in, not afterwards.

  3. #3
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Also, your variable 'x' is never reset. At the end of your first while loop, 'x' will be equal to zero (which is what is printed out) and your second while loop is skipped entirely because of this.
    Sincerely, Joshua Green
    Please REP if I help :)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    Oh, and use CODE tags, because I completely missed that bit!
    :)

  5. #5
    SweetLD215 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    Thanks for responding!

    I'm still not getting it. I'm fairly new to Java so hopefully I don't sound completely stupid :)

    I've edited the code but it still isn't running properly. I understood moving the count piece of the code and did that. I'm not quite sure how to reset my 'x' variable. I gave it a shot, but it didn't seem to do the trick so maybe I didn't quite understand.

    Sorry about the code tags. I didn't know what that was but I figured it out.



    Java Code:
    import java.util.Scanner;
    public class SwitchStatement{
    	public static void main (String[] args){
    		Scanner mg = new Scanner (System.in);
    		int x = 1;
    		int sum = 0;
    		System.out.println ("Please enter a sequence of integer numbers ending with 0");
    		while (x != 0){
    			x = mg.nextInt();
    			sum +=x;		
    		}
    		System.out.println ("Sum of integers is " +sum);
    		
    		int count;
    		count = 0;
    		count++;				
    		int avg = (x/count);
    
    		while (x != 0){
    		x = mg.nextInt();
    
    		}
    		System.out.println("Average of integers is " +avg);
    	}
    }
    When I run it, I get this:

    Please enter a sequence of integer numbers ending with 0
    72
    84
    0

    Sum of integers is 156
    Average of integers is 0
    Last edited by SweetLD215; 10-12-2010 at 11:26 PM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    What is count supposed to represent?

    Say I entered three nonzero numbers, what value do you expect count to have? Where, in your code, do you intend that it gets that value?

  7. #7
    SweetLD215 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    pbrockway2 thank you! Your voice was in my head as I kept playing with the code. It was when you asked what value I wanted count to have and where the code was supposed to find it. I kept thinking "three, but I have no idea" so finally... I was like... "ok, I'm stupid. x is where it gets it" So... a bit of trial and error and I think I got it.

    I nearly did a EUREAKA dance! LOL

    Even though it runs correctly if someone wouldn't mind confirming that it looks good, that would be wonderful!


    Java Code:
     import java.util.Scanner;
     public class SwitchStatement{
    static Scanner mg = new Scanner (System.in);
    
    public static void main (String[] args){
    
     int x = 1;
     int sum = 0;
     int avg = 0;
     int count=x;
     count++;
    
     System.out.println ("Please enter a sequence of integer numbers ending with 0");
     while (x != 0){
     x = mg.nextInt();
     sum +=x;
    avg = (sum/count);
    
    }
     System.out.println ("Sum of integers is " +sum);
     System.out.println("Average of integers is " +avg);
    }
    }

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I agree in that code a count of 3 would be useful. But wouldn't count end up as two in the code you posted?

    Java Code:
    int x = 1;
    int sum = 0;
    int avg = 0;
    int count=x;  // count is now 1
    count++;      // count is now 2
                  // count is never changed from this point on
    The problem is that the user enters all these numbers, but count never changes. That can't be right: count should increment (get bigger by 1) for every nonzero number that is entered.

  9. #9
    SweetLD215 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    Ahhhh ok, that totally makes sense. Hmm, I'll have to research to figure out how to make the count keep going. I'm not really sure how to do that one.
    Last edited by SweetLD215; 10-13-2010 at 01:15 AM.

  10. #10
    SweetLD215 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    Ok so I'm thinking that maybe putting the count++; in the loop and taking the sum and average part out of it would help but now that creates some other issue and it won't sum or average anything.

    I'm feeling kind of stupid right now being unable to figure this out.

  11. #11
    SweetLD215 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    I think I got it!

    I hope... I got it.

    Java Code:
     import java.util.Scanner;
     public class SwitchStatement{
     static Scanner mg = new Scanner (System.in);
    
    public static void main (String[] args){
    
     int x = 1;
     int sum = 0;
     int avg = 0;
     int count = 0;
     
     System.out.println ("Please enter a sequence of integer numbers ending with 0");
     while (x != 0){
     x = mg.nextInt();
     sum +=x;
     count++;
    }
     
     count--;
     avg = sum/count;
     
     System.out.println ("Sum of integers is " +sum);
     System.out.println("Average of integers is " +avg);
    }
    }

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    sum +=x;
    count++;

    This in the loop looks good to me.

    I'm not sure about that count-- after the loop though. Why do you do that? Also you could put an extra line of output at the end just to check things. (Although if this an assignment you might take it out later)

    Java Code:
    System.out.println ("Number of integers entered was " + count);
    System.out.println ("Sum of integers is " +sum);
    System.out.println("Average of integers is " +avg);

    Then try with lots of input. At this point you have a reasonable looking piece of code and you should be trying to break it: to make sure there is nothing wrong with it.

  13. #13
    SweetLD215 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    Yes, it is for an assignment.

    I put the count--; in because it kept counting the zero at the end as part of the sequence when averaging the numbers. I wanted to somehow make it not count the zero, and this was all I could come up with.
    Is this not a good way to do that?

    I just entered in the piece of code you gave me and tried using a string of 9 numbers then a string of 14 numbers and both seem to work. =)

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    OK, I hadn't actually run the code at any point. And I can see you're right: the zero gets "counted" so you have to take one off to allow for that. Well done!

  15. #15
    SweetLD215 is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    Thank you! It was a lot of searching to figure that one out.

    Now I'm aiming for some extra credit assignments and hopefully not pull my hair out.

    I really appreciate your help. You've been amazing!

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome.

  17. #17
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Glad you got it figured out, sorry I would have helped more but I was away from my computer all week. Gratz.
    Sincerely, Joshua Green
    Please REP if I help :)

Similar Threads

  1. Iterative help
    By Jnoobs in forum New To Java
    Replies: 5
    Last Post: 10-06-2010, 08:26 PM
  2. Help needed to turn a recursive method into iterative
    By mercyfulfate in forum New To Java
    Replies: 5
    Last Post: 07-16-2010, 06:13 PM
  3. Replies: 0
    Last Post: 02-03-2010, 08:20 AM
  4. stored procedure
    By sankarigopi in forum JDBC
    Replies: 1
    Last Post: 11-13-2008, 04:53 PM
  5. Iterative Algorithms
    By Zosden in forum Algorithms
    Replies: 1
    Last Post: 07-05-2008, 06:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •