please help me with min,max and avg (java)

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 3
Reputation: dannyboiii is an unknown quantity at this point 
Solved Threads: 0
dannyboiii dannyboiii is offline Offline
Newbie Poster

please help me with min,max and avg (java)

 
0
  #1
Oct 14th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 120
Reputation: Jocamps is an unknown quantity at this point 
Solved Threads: 7
Jocamps's Avatar
Jocamps Jocamps is offline Offline
Junior Poster
 
0
  #2
Oct 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: dannyboiii is an unknown quantity at this point 
Solved Threads: 0
dannyboiii dannyboiii is offline Offline
Newbie Poster
 
0
  #3
Oct 15th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 3
Reputation: dannyboiii is an unknown quantity at this point 
Solved Threads: 0
dannyboiii dannyboiii is offline Offline
Newbie Poster
 
0
  #4
Oct 15th, 2009
can anyone please help me start the min,max and avg off?
with some examples. ill appreciate that a lot
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,678
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #5
Oct 15th, 2009
Originally Posted by dannyboiii View Post
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:
  1. Scanner in = new Scanner(System.in);
  2.  
  3. int sum = 0;
  4. int count = 0;
  5. int min=0;
  6. int max=0;
  7.  
  8. System.out.println("Enter numbers");
  9. while (in.hasNextInt()) {
  10.  
  11. int curr = in.nextInt();
  12. System.out.println("Number given: "+curr+". Next:");
  13.  
  14. sum = sum + curr;
  15. count++;
  16. }
  17.  
  18. System.out.println("Sum of all numbers: "+sum);
  19. 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.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
1
  #6
Oct 15th, 2009
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
Last edited by BestJewSinceJC; Oct 15th, 2009 at 4:25 pm.
Out.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC