Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Russd772

Thread: how to display the highest and lowest mark holder in an array

  1. #1
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default 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 :)


    Java 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);
    		}
    	}
    Last edited by blackbonzai075; 01-03-2013 at 07:53 PM. Reason: i did not copy the whole program sorry :/

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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.
    I like likes!

  3. #3
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default 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!

  4. #4
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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:
    Java 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
    Java 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
    Sierra likes this.

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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>.
    I like likes!

Similar Threads

  1. how to display the highest mark holder in an array
    By blackbonzai075 in forum New To Java
    Replies: 3
    Last Post: 01-03-2013, 11:09 PM
  2. Getting/Printing the Highest to lowest
    By micolord in forum New To Java
    Replies: 12
    Last Post: 09-14-2011, 04:44 PM
  3. Need Lowest and Highest decimal point for 0.73 and 0.76
    By maran.ramar in forum New To Java
    Replies: 5
    Last Post: 11-15-2010, 01:58 AM
  4. cant get highest and lowest to display
    By tracey in forum New To Java
    Replies: 2
    Last Post: 05-16-2010, 08:48 AM
  5. Replies: 21
    Last Post: 11-05-2008, 02:31 PM

Posting Permissions

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