# Thread: help.. what to do with this?

1. Member Join Date
Dec 2009
Posts
8
Rep Power
0

## help.. what to do with this?

Hi,
I'm a newbie here and I need some help.

Problem:
Create a two-dimensional array program to store student grades on multiple exams. This is used by a professor to store and analyze a set of student grades. Each row of the array represents a single student's grades for the entire course, and each column represents a grade on one of the exams the students took during the course.

The output should be:

Student1
Test1: 87
Test2: 96
Test3: 70
Student2
Test1: 68
Test2: 87
Test3: 90
.......
.......
.......
.......
***Summary***

________Test1__Test2__Test3__Average
Student1:__87___96_____70_____84.33
Student2:__68___87_____90_____81.67
Student3:__94___100____90_____94.67
Student4:__100__81_____82_____87.67
.......
.......
.......

50-59:
60-69: *
70-79: *
80-89: ****
90-99: ****
100: **

And here's what I came up with...

Java Code:
```import java.io.*;
public static void main(String[]args)throws Exception{

DataInputStream k=new DataInputStream(System.in);
float ave[]=new float ;
int i, j;
float a=0;

for (i=0;i<10;i++){
System.out.println("Student "+(i+1));
for (j=1;j<4;j++){
System.out.print("Test "+j+" ");
do{
System.out.print("Test "+j+" ");
}
else
a=a+stud[i][j];
}
ave[i]=a/3;
a=0;
}
System.out.println("\n***Summary***\n");
System.out.println("\t\tTest1\tTest2\tTest3\tAverage");
for (i=0;i<10;i++){
System.out.print("Student "+(i+1)+"\t");
for(j=1;j<4;j++){
System.out.print(stud[i][j]+"\t");
}
System.out.println(ave[i]);
}

}
}```
and my real problem comes here:
How do I get the Lowest & Highest grades and that overall distribution thingy?
I know its a long problem, but please, someone help me. :(  Reply With Quote

2. Member Join Date
Dec 2009
Location
Rio de Janeiro
Posts
38
Rep Power
0

## Man, was this exercise proposed by your teacher? He's trying to make you learn Java without the concept of OO.

2) Check out the code below and use it to make yours. Don't copy and paste... your teacher will not like it hehe

Java Code:
```	public static void main(String[]args)throws Exception{

double numbers[][] = {{25, 39, 47.8, 6}, {3, 78, 68, 95}};
int[] amount = new int;

//Now we'll assume the first number is the lowest
double lowest = numbers;
//Now we'll assume the first number is the highest
double highest = numbers;
for(double[] a : numbers){
for(double b : a){
if (b < lowest){
lowest = b;
}else if (b > highest){
highest = b;
}

if (b < 50){
continue;
}

if (b < 60){
amount++;
} else if (b < 70){
amount++;
} else if (b < 80){
amount++;
} else if (b < 90){
amount++;
} else if (b < 100) {
amount++;
} else if (b == 100){
amount++;
}
}
}

System.out.println("Lowest: " + lowest);
System.out.println("Highest: " + highest);

System.out.print("50 - 59: ");
for(int i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();

System.out.print("60 - 69: ");
for(int i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();

System.out.print("70 - 79: ");
for(int i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();

System.out.print("80 - 89: ");
for(int i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();

System.out.print("90 - 99: ");
for(int i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();

System.out.print("100: ");
for(int i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();
}```  Reply With Quote

3. Member Join Date
Dec 2009
Posts
8
Rep Power
0

## haha. yeah, this was proposed by my teacher, and we haven't even reached OO in our course yet.. anyway thanks for the reply.
but uhmm... since you said not to copy and paste.. would you mind explaining abit on how to get the lowest and highest numbers part, coz i know my teacher would surely ask me where i got this code
Java Code:
`for(double[] a : numbers)`  Reply With Quote

4. Member Join Date
Dec 2009
Location
Rio de Janeiro
Posts
38
Rep Power
0

## Java Code:
```//Now we'll assume the first number is the lowest
double lowest = numbers;
//Now we'll assume the first number is the highest
double highest = numbers;
for(double[] a : numbers){
for(double b : a){
if (b < lowest){
lowest = b;
}else if (b > highest){
highest = b;
}```
This part of the code is responsible for find lowest and highest values. The first thing we do is to assume that the first value in the array is the lowest and the highest value. The lines below do exactly this:

Java Code:
```double lowest = numbers;
double highest = numbers;```
Now that we have an reference to compare the other numbers, we will go to each element in the array and we'll do two comparisons with it:
1) If the element we are visiting now is lower than the lowest, we have now a new lowest element;
2) If the element we are visiting now is higher than the highest, we have now a new highest element;
These two steps are presented here:

Java Code:
```if (b < lowest){
lowest = b;
}else if (b > highest){
highest = b;
}```
Consider "b" being the element we are visiting now.

I think you're having problem with the following commands:

Java Code:
```for(double[] a : numbers){
for(double b : a){```
If you want, you can change these two lines to

Java Code:
```for (int i = 1; i < numbers[i-1].length; i++) {
double a[] = numbers[i];
for (int r = 0; r < a.length; r++) {
double b = a[r];```
The two pieces of code above work at the same way, but I think you are more familiar with the latter! =)

Using this new way, your code would be something familiar to it:

Java Code:
```                double[][] numbers = { { 1.2, 2 }, { 7, 4 } };

double lowest = numbers;
double highest = numbers;

for (int i = 1; i < numbers[i-1].length; i++) {
double a[] = numbers[i];
for (int r = 0; r < a.length; r++) {
double b = a[r];

if (b < lowest) {
lowest = b;
} else if (b > highest) {
highest = b;

}
}
}```
ok?  Reply With Quote

5. Member Join Date
Dec 2009
Posts
8
Rep Power
0

## Ok,, so here's what i came up with (and still got some problems with it). Please take a look, and tell me if this is good enough. :(

Java Code:
```import java.io.*;
public static void main(String[]args)throws Exception{
DataInputStream k=new DataInputStream(System.in);
double[] ave=new double ;
int[] amount=new int;
int i, j;
double s=0;
for (i=0;i<10;i++){
System.out.println("Student "+(i+1));
for (j=1;j<4;j++){
System.out.print("Test "+j+" ");
}
ave[i]=s/3;
s=0;
}
System.out.println("\n***Summary***\n");
System.out.println("\t\tTest1\tTest2\tTest3\tAverage");
for (i=0;i<10;i++){
System.out.print("Student "+(i+1)+"\t");
for(j=1;j<4;j++){
}
System.out.println(ave[i]);
}
System.out.println();
for(double b : a){
if (b < lowest){
lowest = b;
}else if (b > highest){
highest = b;
}

if (b < 50){
continue;
}

if (b < 60){
amount++;
} else if (b < 70){
amount++;
} else if (b < 80){
amount++;
} else if (b < 90){
amount++;
} else if (b < 100) {
amount++;
} else if (b == 100){
amount++;
}
}
}
System.out.println("Lowest: " + lowest);
System.out.println("Highest: " + highest);
System.out.println();
System.out.print("50 - 59: ");
for(i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();
System.out.print("60 - 69: ");
for(i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();
System.out.print("70 - 79: ");
for(i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();
System.out.print("80 - 89: ");
for(i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();
System.out.print("90 - 99: ");
for(i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();
System.out.print("100: ");
for(i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();
}
}```
I'm not familiar with the Scanner thingy, so i thought i should just stick with the DataInputStream :)  Reply With Quote

6. ## DataInputStream is not compatible.Just use
below or Scanner class
for a beginner this below method is always good.
-----------------------------------------------

Why u have to use that many forloops?
Make it as single method and use it wherever u want.Dont repeat the code.  Reply With Quote

7. Member Join Date
Dec 2009
Location
Rio de Janeiro
Posts
38
Rep Power
0

## The lowest number is not working because I'm unable to find out what's the first grade that is valid in your array. I thought it was grade;

Try to find it out and then change it in the code and it will work properly.

=)  Reply With Quote

8. Member Join Date
Dec 2009
Posts
8
Rep Power
0

## DataInputStream is not compatible.Just use
below or Scanner class
for a beginner this below method is always good.
-----------------------------------------------
Thanks. I'll be using this then.

Why u have to use that many forloops?
Make it as single method and use it wherever u want.Dont repeat the code.
Why you ask? maybe because i don't know how to..lol

The lowest number is not working because I'm unable to find out what's the first grade that is valid in your array. I thought it was grade;

Try to find it out and then change it in the code and it will work properly.

=)
that's what i've bee trying to find out for the past 3 hours!!! perhaps you guys can help me out with this? :D  Reply With Quote

9. Member Join Date
Dec 2009
Location
Rio de Janeiro
Posts
38
Rep Power
0

## Now it's working!

Java Code:
```public class StudentGrades{
public static void main(String[]args)throws Exception{
//DataInputStream k=new DataInputStream(System.in);
Scanner k = new Scanner(System.in); //I created it

double[][] grade=new double; //I created it
double[] ave=new double ;
int[] amount=new int;
int i, j;
double s=0;
for (i=0;i<10;i++){
System.out.println("Student "+(i+1));
//for (j=1;j<4;j++){
for (j=0;j<3;j++){ //I created it
System.out.print("Test "+j+" ");
grade[i][j] = k.nextInt(); //I created it
}
ave[i]=s/3;
s=0;
}
System.out.println("\n***Summary***\n");
System.out.println("\t\tTest1\tTest2\tTest3\tAverage");
for (i=0;i<10;i++){
System.out.print("Student "+(i+1)+"\t");
//for(j=1;j<4;j++){
for(j=0;j<3;j++){ //I created it
}
System.out.println(ave[i]);
}
System.out.println();
for(double b : a){
if (b < lowest){
lowest = b;
}else if (b > highest){
highest = b;
}

if (b < 50){
continue;
}

if (b < 60){
amount++;
} else if (b < 70){
amount++;
} else if (b < 80){
amount++;
} else if (b < 90){
amount++;
} else if (b < 100) {
amount++;
} else if (b == 100){
amount++;
}
}
}
System.out.println("Lowest: " + lowest);
System.out.println("Highest: " + highest);
System.out.println();
System.out.print("50 - 59: ");
for(i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();
System.out.print("60 - 69: ");
for(i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();
System.out.print("70 - 79: ");
for(i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();
System.out.print("80 - 89: ");
for(i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();
System.out.print("90 - 99: ");
for(i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();
System.out.print("100: ");
for(i = 0; i < amount; i++){
System.out.print("*");
}
System.out.println();
}
}```  Reply With Quote

10. Member Join Date
Dec 2009
Posts
8
Rep Power
0

## GREAT! Thanks a lot..
Problem Solved.. :)  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•