Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Adding Arrays to find min and max help please (http://www.go4expert.com/forums/adding-arrays-min-max-help-t22805/)

Bewitched1 24Jul2010 21:11

Adding Arrays to find min and max help please
 
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));


            }

            }


virxen 25Jul2010 02:45

Re: Adding Arrays to find min and max help please
 
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;
        int [] grades = new int [10];//define it first of all

            //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];//you add the first results!!!
            }
            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]+=midTerm2[j];//you add the second results!!!
            }
            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]+=finalExam[k];//last one to add!!!
            }
           
            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));


            }

            }


Bewitched1 25Jul2010 03:22

Re: Adding Arrays to find min and max help please
 
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


All times are GMT +5.5. The time now is 11:54.