Hey im quite new to java and my friends have been helping me out
however ive stumbled upon a question im not quite sure how to solve.
i need to find the max,min and avg of the total 'responses'
im not quite sure how to bring the values from the 'response' over since the numbers arent generated until a user inputs a few numbers.

my work so far:

import java.util.*;
public class fluffpuff {

public int board[];
    public void runsurvey() {
}
    {
        Scanner in = new Scanner(System.in);

        //user:
        int choice = 0;
        int surveyed =0;
        int response = 0;

        //total:
        int average;
        int max;
        int min;

//How many people are to be surveyed?
while(choice==0)
    {
    System.out.print("How many people are to be surveyed?");
    surveyed = in.nextInt();
    if(surveyed<0) {
        System.out.println("Illegal input, must be >= 1. Try again");
    } else { 
        choice++;
    }
}

board = new int[surveyed];

//Please enter response
int counter = 0;
while(choice==1)
{
    while(counter<surveyed)
    {
        Scanner reader = new Scanner(System.in);
        System.out.print("Please enter response: ");
        response = reader.nextInt();
        if(response<0) {
            System.out.println("Illegal input, must be 0..9, Try again");
    } else { 
        board[counter] = response;
        counter++;
    }
    }
    choice++;
}

displayboard(surveyed);

}


//Graph of response

public void displayboard(int surveyed)
{
    int row;
    int count;

    row = 0;
    while (row < surveyed)
    {
        count=0;
        while(count<board[row]) {
            System.out.print("*");
            count++;
        }
        row++;
        System.out.println("");
    }


//Max

//Min

//Average

Edited 3 Years Ago by Dani: Formatting fixed

one way you can do it is to have methods which determines min, max and ave. and everytime the user inputs a number it calls this methods.
1. for the min/max if its the first run obviously any number from the user is the starting value. then for succeeding inputs, compare if that is less/greater than mini and max respectively.

2. for the average get the cumulative sum of the user inputs and devide this by the number of responses so far.

Note: if you are only concern with the min, max and ave values of the response, avoid storing the responses to an array of values. This is very inefficient in terms of execution and takes a lot of memory if the responses are significantly high.

would i be able to be given a small example, im not expecting it to be solved, i would appreciate it if you could start me off.
thanks

can anyone please help me start the min,max and avg off?
with some examples. ill appreciate that a lot

can anyone please help me start the min,max and avg off?
with some examples. ill appreciate that a lot

Assuming that you have a while loop from which you read the numbers from the keyboard, then:

Scanner in = new Scanner(System.in);

int sum = 0;
int count = 0;
int min=0;
int max=0;

System.out.println("Enter numbers");
while (in.hasNextInt()) {

  int curr = in.nextInt();
  System.out.println("Number given: "+curr+". Next:");

  sum = sum + curr;
  count++;
}

System.out.println("Sum of all numbers: "+sum);
System.out.println("Numbers given: "+count);

You have what you need to calculate the average. As for the min, max, initialize them with first number you will enter. Think how you will use the "count" to accomplish that.
Then you have all you need to use Jocamps advice:

then for succeeding inputs, compare if that is less/greater than mini and max respectively.

Not that the OP is interested, but in case anyone is, finding the minimum and then finding the maximum takes (n-1) comparisons each for a total of 2n-1 comparisons. The same can be done in about 3n/2 comparisons by doing both at once using pair comparisons.

pair comparisons

Edited 7 Years Ago by BestJewSinceJC: n/a

Comments
good point! this is how i would do it anyway :)
This article has been dead for over six months. Start a new discussion instead.