how to display the highest and lowest mark holder in an array
hi! i need to display the highest and lowest mark holding name in an array. could any one please give me just a line to get me started? i already know how to find the highest and lowest number but i don't know how to display the name.
any help is appriciated! :D
P.S. i have reposted this post because of tags :)
Code:
// Program No. 23
// Christmas Project Homework
class ProgramNo23{
public static void main (String args[]){
String names [] = new String [10];
int marks [] = new int [10];
int size = 0;
int option = 0;
int sum = 0;
int total = 0;
int high = 0;
int low = 100;
int high2 = 0;
int low2 = 100;
double avg = 0;
do{
System.out.println("1. Enter Name & Mark");
System.out.println("2. Edit a Mark");
System.out.println("3. Display Name, Marks & Grades");
System.out.println("4. Display Statistics");
System.out.println("5. Display Passes");
System.out.println("6. Display Failures");
System.out.println("7. Exit");
System.out.println();
System.out.println("---------------------------------");
System.out.println();
System.out.print("Enter option: ");
option = Keyboard.readInt();
System.out.println();
switch(option){
case 1: // Enter Names & Marks
for(int i = 1; i < 11; i++){
System.out.print("Please Enter Name "+i+": ");
String n = Keyboard.readString();
System.out.print("Please Enter Mark "+i+": ");
int m = Keyboard.readInt();
System.out.println();
names[size] = n;
marks[size] = m;
size++;
}
break;
case 2: // Edit a Mark
{
if(size!=0)
{
boolean found = false;
System.out.println("Enter name to edit: ");
String m = Keyboard.readString();
System.out.println();
for(int p = 1;p < size; p++){
if(names[p].equalsIgnoreCase(m)){
System.out.println("Name Found");
System.out.println();
System.out.print("Enter new mark: ");
int newMark = Keyboard.readInt();
System.out.println();
}
}
if(found == false) System.out.println("Name not found");
System.out.println();
}
else System.out.println("Directory Empty");
break;
}
case 3: // Display Name, Mark and Grade
{
for(int x = 0; x < size; x++){
System.out.println(names[x]+": ");
if(marks[x] >= 90) System.out.print("Grade A: ");
if((marks[x] >= 80)&&(marks[x] < 90)) System.out.print("Grade B: ");
if((marks[x] >= 70)&&(marks[x] < 80)) System.out.print("Grade C: ");
if((marks[x] >= 60)&&(marks[x] < 70)) System.out.print("Grade D: ");
if((marks[x] >= 50)&&(marks[x] < 60)) System.out.print("Grade E: ");
if(marks[x] < 50) System.out.print("Grade F: ");
System.out.println(marks[x]);
System.out.println();
}
System.out.println();
}
break;
case 4: // Display Statistics
for(int s = 0; s < marks.length; s++){
sum += marks[s];
}
for(int f = 0; f < marks.length; f++){
if(marks[f] > high) high = marks[f];
if(marks[f] < low) low = marks[f];
}
for(int w = 0; w < marks.length; w++){
if()
if()
}
total = sum;
avg = (total/10);
System.out.println("Highest Mark: "+high);
System.out.println("Average Mark: "+avg);
System.out.println("Lowest Mark: "+low);
System.out.println("Sum of Marks: "+total);
System.out.println();
break;
}
}
while(option!=7);
}
}
Re: how to display the highest and lowest mark holder in an array
It depends on where you store your names. Please post complete code samples, that is whole methods or classes that contain no errors - if they are not part of your question. You may also edit your post afterwards by using the EDIT" button below your post.
Re: how to display the highest and lowest mark holder in an array
i have now posted the whole program. i'm very sorry for all my misunderstandings but i am new here. thanks for all your help!
Re: how to display the highest and lowest mark holder in an array
you could create a variable to store the index of the marks array (assuming the name would be at the same index of the name array) and then just call the name at that index:
Code:
case 4: // Display Statistics
for(int s = 0; s < marks.length; s++){
sum += marks[s];
}
// Something like this
int markHigh, markLow;
for(int f = 0; f < marks.length; f++){
if(marks[f] > high) {
high = marks[f];
markHigh= f;
}
if(marks[f] < low) {
low = marks[f];
gradeLow = f;
}
}
then you can call the index of the name array
Code:
// put these with your output of the grades
String nameHigh, nameLow;
nameHigh = name[markHigh];
nameLow = name[markLow];
this is just a thought and my apoligies in advance if its not what you need, im kinda new to java myself
goodluck
Russ
Re: how to display the highest and lowest mark holder in an array
My suggestion is to store the index instead of the real value of "high" "average" and so on. So if you have the index you may reference name[indexHigh] or marks[indexHigh] whenever you like.
However with that method you need to take great care that always the same index references the same item in both arrays - it would be probably better to use a list to store those elements like e.g. Map<String, int>.