| | |
please help me with min,max and avg (java)
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 3
Reputation:
Solved Threads: 0
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
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
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.
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.
0
#5 Oct 15th, 2009
•
•
•
•
can anyone please help me start the min,max and avg off?
with some examples. ill appreciate that a lot
Java Syntax (Toggle Plain Text)
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.
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Sep 2008
Posts: 1,598
Reputation:
Solved Threads: 202
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
pair comparisons
Last edited by BestJewSinceJC; Oct 15th, 2009 at 4:25 pm.
Out.
![]() |
Similar Threads
- Min/Max value and average array help!! (C++)
- Average/max/min (C)
- min/max of a mixed type list (Python)
- min and max values (C++)
Other Threads in the Java Forum
- Previous Thread: HELP ! Java code errors
- Next Thread: how to use "all" cmd prompt command in java ?
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation binary blackberry block bluetooth character chat class classes client code compile component consumer database desktop developmenthelp eclipse error event exception fractal freeze ftp game givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jmf jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number online page print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner screen server set singleton size sms sort sql string swing template textfields threads time title tree tutorial-sample update windows working






