Latest Computer Forum Topics:

  • Nintendo "Hand-Held" ? (8 posts)
  • my laptop has no firewire input :{?? (5 posts)
  • Does slow computer mean I need new motherboard??? (7 posts)
  • PCIe x16 2.0?? (5 posts)
  • TDU Problems?? (2 posts)
  • Anyone heard of MS Office Ultimate 2007 for ??!!? (5 posts)
  • Windows Defender??? (3 posts)
  • working with adobe files??? (5 posts)
  • Viewing thumbnails on a website??? (2 posts)
  • Dreamweaver or Fireworks??? (2 posts)
  • what all needs to be completed in Kingdom Hearts for there to be the music video at the end? (3 posts)
  • Air Mouse??? (3 posts)
  • Ramdac?????????? (2 posts)
  • Broadband problem with XP?? (5 posts)
  • HDMI Monitor?? (3 posts)
  •  
    Author Message

    Grapes

    Members


    Online status

    494 posts

    Location: Puerto Rico
    Occupation: Plastic surgeon
    Age:

    #209840   2008-03-22 23:15 GMT      
    I have this program that loops through once you click yes at the end of it. when you click no the output comes up as the average of your input.
    In other words.. First loop through I put in 80, it asks me if I want to loop again, I say yes then I put in 70. This time I click no so it gives me an output of the average of 70 and 80.
    I'm guessing I need an array for this program.. But I just can't seem to figure it out..

    Any suggestions?

    Freedom

    Members


    Online status

    495 posts

    Location: Dominica
    Occupation: Négociant
    Age:

    #209841   2008-03-22 23:24 GMT      
    Use an ArrayList ;)

    List<Integer> numberList = new ArrayList<Integer>();

    When you click yes and add a number, have this:
    numberList.add(value);
    You won't need to cast the int as an Integer because Java automatically autoboxes (converts primitives to Objects).

    When you click no, try this:
    int total = 0;
    for (int i : numberList) {
    total += i;
    }
    int average = total / numberList.size();

    Hopefully that helps Good luck

    DawnsAwaken

    Members


    Online status

    521 posts

    Location: Azerbaijan
    Occupation: Lifeguard
    Age:

    #209842   2008-03-23 00:10 GMT      
    try this let for no u assume as 0
    int no,sum=0,cnt=0;
    while(1)
    {
    no= number u r typing
    if (no!=0)
    {
    sum=sum+no ;
    cnt=cnt+1;
    }
    else
    {
    break;
    }
    } //end of while

    avg=sum/cnt;
    //display avg


    }

    oddperson

    Members


    Online status

    470 posts

    Location: Botswana
    Occupation: Web designer
    Age:

    #209843   2008-03-23 00:24 GMT      
    A good way to solve computer programs is to plan them out before you begin coding. This gives you the time and space to make mistakes and get it straight where you are going- like a route map so you dont get lost.

    Your program needs to:
    - accept values,
    - store values (the numbers read in, the total so far, and a count of how many values have been read in so far)
    - loop and process the values
    - output the average

    Here is some sample pseudocode that should give you an idea of what to do here

    Begin Program
    initialize inputPrompt = "no"
    initialize total = 0
    initialize count = 0
    intialize average = 0.0

    Output prompt Yes / No //these are your buttons

    While buttonClick not equal to "No" //this is the loop

    Output prompt "enter a number"
    accept and store input number //this is your input
    count = count + 1 //this is your processing
    total = input + total
    average = total / count
    Output prompt Yes / No //the user is prompted with the buttons again

    End While
    output average //output
    End Program

    You would use an array if you know how many numbers the user will enter, if its just 2 then just have 2 variables (if you need to keep a record of what they were).

    You would use an arraylist if you dont know how many numbers the user will enter because it will expand to accomodate them, although it will not store primitive values like int. I think this is just a procedural programming problem you have been set anyway.
    > 1 <