Data Structures and Algorithms Discussion Board
September 10, 2010, 02:48:54 PM *
Welcome, Guest. Please login or register.
Login with username, password and session length
News: Looking for a reliable webhosting provider? Read HostGator review to find 7 arguments in support of HostGator.
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Grades average program! [Urgent]  (Read 3338 times)
Hitman
Newbie
*

Rating: 0
Offline Offline

Posts: 3


« on: March 11, 2009, 08:30:28 AM »

I am very beginner and trying to do my class homework. An assignment is to input 10 grades, remove the biggest and the lowest (to avoid cheating) and then calculate an average. Pleazzze, help!!!!
Logged
Denis
Newbie
*

Rating: 0
Offline Offline

Posts: 21


« Reply #1 on: March 11, 2009, 09:18:11 AM »

Did you give it a try?
Logged
Hitman
Newbie
*

Rating: 0
Offline Offline

Posts: 3


« Reply #2 on: March 12, 2009, 03:22:00 AM »

Yes. What I done yet:
Code:
import java.util.Scanner;

public class GradesProgram {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int[] grades = new int[10];
int i;
System.out.println("Enter 10 grades: ");
for (i = 0; i < 10; i++) {
grades[i] = kb.nextInt();
}
int sum = 0;
for (i = 0; i < 10; i++) {
sum = sum + grades[i];
}
double av = sum / 10;
System.out.print("Average is " + av);
}
}

But how to remove minimum and maximum grades?Huh
Logged
Denis
Newbie
*

Rating: 0
Offline Offline

Posts: 21


« Reply #3 on: March 15, 2009, 04:59:51 AM »

Good attempt. All you should do is to track minimum and maximum grades, while looping and discard them then. Also, (sum / 10) returns an integer result, while you probably need double one. The code:

Code:
import java.util.Scanner;

public class GradesProgram {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int[] grades = new int[10];
int i;
System.out.println("Enter 10 grades: ");
for (i = 0; i < 10; i++) {
grades[i] = kb.nextInt();
}
int sum = 0;
int min = 100;
int max = -100;
for (i = 0; i < 10; i++) {
sum = sum + grades[i];
if (grades[i] < min)
min = grades[i];
if (grades[i] > max)
max = grades[i];
}
sum -= min;
sum -= max;
double av = (double)sum / 8;
System.out.print("Average is " + av);
}
}

Also, using an array in this program is not necessary. You can sum grades and track extreme values in the input loop:

Code:
import java.util.Scanner;

public class GradesProgram {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int grade;
int i;
int sum = 0;
int min = 100;
int max = -100;
System.out.println("Enter 10 grades: ");
for (i = 0; i < 10; i++) {
grade = kb.nextInt();
sum = sum + grade;
if (grade < min)
min = grade;
if (grade > max)
max = grade;
}
sum -= min;
sum -= max;
double av = (double)sum / 8;
System.out.print("Average is " + av);
}
}

Hope it helps.
Logged
Hitman
Newbie
*

Rating: 0
Offline Offline

Posts: 3


« Reply #4 on: March 15, 2009, 05:55:30 AM »

It works well. Thanx! Grin
Logged
Algolist.net Editor
Administrator
Newbie
*****

Rating: 0
Offline Offline

Posts: 8


« Reply #5 on: March 17, 2009, 09:44:14 AM »

Topic has been split, last post by zaini can be found at iterative, stack based method [Urgent]
« Last Edit: November 21, 2009, 04:20:39 AM by Algolist.net Editor » Logged
Pages: [1]
  Print  
 
Jump to:  

 
Partners Ads        video conversion        mobile djs