1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Adding Arrays to find min and max help please

Discussion in 'Java' started by Bewitched1, Jul 24, 2010.

  1. Bewitched1

    Bewitched1 New Member

    I am working on a project that takes the user input for 10 midTerm1, 10 midTerm2, 10 final exam grades, Then the program is suppose to add the 3 to find a min and max then display the min and max. I can't figure out how to get the 3 arrays into the final array and add them.



    Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class Assign9_Cheshire
    
    {
        public static void main(String[] args)
        {
            //Variable Declarations
            Scanner input = new Scanner(System.in);
    
    
            int averageScore;
            int userInput;
    
    
                //User Prompts midterm1
                int[]midTerm1= new int [10];
                for (int i = 0; i <midTerm1.length; i++){
                    System.out.println("Please enter a MidTerm1 grade:     ");
                    midTerm1[i] = input.nextInt();
                }
                 System.out.println("Thank you next");
                //User prompt midterm2
                int[]midTerm2=new int[10];
                for(int j=0; j<midTerm2.length;j++){
                   System.out.println("Please enter a MidTerm2 grade:    ");
                   midTerm2[j] = input.nextInt();
                }
                System.out.println("Thank you next");
                //User prompt final exam
                int[]finalExam = new int[10];
                for(int k = 0; k<finalExam.length; k++){
                    System.out.println("Please enter a Final Exam grade:   ");
                    finalExam[k] = input.nextInt();
                }
                int [] grades = new int [10];
                int max = 0;
                
               //To display highest score
                for (int t = 0; t < grades.length; t ++)
                {
                    if (grades[t] > max)
                    {
                        max = grades[t];
                    }
                    System.out.print("\nYour highest test score is: " + max);
    
                //To display lowest score
                int min = 100;
                    if(grades[t] < min)
                    {
                        min = grades[t];
                    }
                }
                System.out.print("\nYour lowest test score is: " + min);
    
    
    
              System.out.print("You have entered for midTerm1:    ");
              System.out.println(Arrays.toString(midTerm1));
              System.out.print("You have entered for midTerm2:    ");
              System.out.println(Arrays.toString(midTerm2));
              System.out.print("You have entered for Final Exam:   ");
              System.out.println(Arrays.toString(finalExam));
    
    
                }
    
                }
    
     
  2. virxen

    virxen New Member

    in order to add 3 arrays into one new array
    first they must have the same dimension.
    and the resulting array will be
    result[k]=a[k]+b[k]+c[k] <--for all elements k=0...length

    Code:
    import java.util.Scanner;
    import java.util.Arrays;
    
    public class Assign9_Cheshire
    
    {
        public static void main(String[] args)
        {
            //Variable Declarations
            Scanner input = new Scanner(System.in);
    
    
            int averageScore;
            int userInput;
            [COLOR=Red]int [] grades = new int [10];//define it first of all[/COLOR]
    
                //User Prompts midterm1
                int[]midTerm1= new int [10];
                for (int i = 0; i <midTerm1.length; i++){
                    System.out.println("Please enter a MidTerm1 grade:     ");
                    midTerm1[i] = input.nextInt();
                    grades[i]=midTerm1[i];[COLOR=Red]//you add the first results!!![/COLOR]
                }
                 System.out.println("Thank you next");
                //User prompt midterm2
                int[]midTerm2=new int[10];
                for(int j=0; j<midTerm2.length;j++){
                   System.out.println("Please enter a MidTerm2 grade:    ");
                   midTerm2[j] = input.nextInt();
                  grades[j][COLOR=Red]+[/COLOR]=midTerm2[j];[COLOR=Red]//you add the second results!!![/COLOR]
                }
                System.out.println("Thank you next");
                //User prompt final exam
                int[]finalExam = new int[10];
                for(int k = 0; k<finalExam.length; k++){
                    System.out.println("Please enter a Final Exam grade:   ");
                    finalExam[k] = input.nextInt();
                    grades[k][COLOR=Red]+[/COLOR]=finalExam[k];[COLOR=Red]//last one to add!!![/COLOR]
                }
                
                int max = 0;
                
               //To display highest score
                for (int t = 0; t < grades.length; t ++)
                {
                    if (grades[t] > max)
                    {
                        max = grades[t];
                    }
                    System.out.print("\nYour highest test score is: " + max);
    
                //To display lowest score
                int min = 100;
                    if(grades[t] < min)
                    {
                        min = grades[t];
                    }
                }
                System.out.print("\nYour lowest test score is: " + min);
    
    
    
              System.out.print("You have entered for midTerm1:    ");
              System.out.println(Arrays.toString(midTerm1));
              System.out.print("You have entered for midTerm2:    ");
              System.out.println(Arrays.toString(midTerm2));
              System.out.print("You have entered for Final Exam:   ");
              System.out.println(Arrays.toString(finalExam));
    
    
                }
    
                }
    
    
     
  3. Bewitched1

    Bewitched1 New Member

    That makes sense.. I had changed it up so much I was lost in where I had been and was going....I am pretty Java dumb..:embarasse.that is obvious. Thanks so much for your help. :p
     

Share This Page