Note that there are some explanatory texts on larger screens.

plurals
  1. POjava array returned with incorrect values
    primarykey
    data
    text
    <p>I am a TA for a java class at my university and a student confronted me with a very odd problem today in lab. I looked over it for about an hour and had the other TA in lab do the same, yet we could not find a problem.</p> <p>Effectively what we are doing here is creating 3 arrays, passing them into a new method. Modifying the value of those array in the new method and returning back to the original method. We are not using the return statement to return any of the arrays to the original method. Instead we are levering, what I can only describe being from a C background as pass-by-reference. However upon returning to the original method that values have changed to some incorrect values.</p> <p>In this specific example we have three arrays called: "exams", "quizzes" and "labs." Each of these arrays are of size 1,000 and are initialized to -1. Inside the first method "calcGrade" we create these arrays and initialize them. Then we pass all three arrays to the second method which captures the amount of exams, quizzes and labs the user has and then stores the actual exam, quiz and lab grade values to the arrays.</p> <p>METHOD 1 (calcGrade)</p> <pre><code>exams quizzes labs -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 . . . . . . . . . </code></pre> <p>METHOD 2 (getScores)</p> <pre><code>exams quizzes labs 90 80 90 -1 80 90 -1 -1 -1 -1 -1 -1 . . . . . . . . . </code></pre> <p>BACK to METHOD 1 (calcGrades)</p> <pre><code>exams quizzes labs 80 90 90 -1 -1 90 -1 -1 -1 -1 -1 -1 . . . . . . . . . </code></pre> <p>Can anyone think of any reason this could be happening? I am honestly stumped and I don't want him to lose credit for something that doesn't seem to be wrong...</p> <p>Here is the code (please note that there are several println statements in there for debugging purposes):</p> <pre><code> import java.util.Scanner; public class CSE1341Grade{ public static void main(String [] args){ Scanner input = new Scanner(System.in); System.out.println("What is your first name? "); String first = input.nextLine(); System.out.println("What is your last name? "); String last = input.nextLine(); calcGrade(first, last); } public static void calcGrade(String first, String last){ int base = 1000; int[] quizzes = new int [base]; int[] exams = new int [base]; int[] labs = new int [base]; for(int x = 0; x &lt; base; x++) { quizzes[x] = -1; exams[x] = -1; labs[x] = -1; } int[] countarr = getScores(quizzes, exams, labs); System.out.println("EXAMS:"); for(int x = 0; x &lt; countarr[0]; x++) System.out.println(exams[x]); System.out.println("QUIZ:"); for(int x = 0; x &lt; countarr[1]; x++) System.out.println(quizzes[x]); System.out.println("LABS:"); for(int x = 0; x &lt; countarr[2]; x++) System.out.println(labs[x]); for(int x = 0; x &lt; countarr.length; x++) System.out.println(countarr[x]); //System.out.println("----"); double examAvg =0.0; for(int i=0;i&lt;countarr[0];i++){ //adding together scores examAvg+=exams[i]; //System.out.println(examAvg); } //System.out.println("----"); double quizAvg=0.0; for(int i=0;i&lt;countarr[1];i++){ //adding together scores quizAvg+=quizzes[i]; //System.out.println(quizAvg); } //System.out.println("----"); double labAvg=0.0; for(int i=0;i&lt;countarr[2];i++){ //adding together scores labAvg+=labs[i]; //System.out.println(labAvg); } examAvg = examAvg/countarr[0]; quizAvg = quizAvg/countarr[1]; labAvg = labAvg/countarr[2]; double totalAverage = (.5 * examAvg) + (.35 * quizAvg) + (.1 *labAvg) + 5.0; System.out.println("Total Score: " +totalAverage);//display average String grade = ""; if (totalAverage &gt;= 90) grade = "A"; else if (totalAverage &gt;= 80) grade ="B"; else if (totalAverage &gt;= 70) grade = "C"; else grade = "F"; System.out.println(first + " " + last + " your grade is a: " + grade); //letter grade } public static int [] getScores(int [] exams, int [] quizzes, int [] labs){ Scanner input = new Scanner(System.in); int [] countArray = new int[3]; //holding numbers of exams quizzes labs System.out.println("How many exam grades do you have? "); countArray[0] = input.nextInt(); System.out.println("How many quiz grades do you have? "); countArray[1] = input.nextInt(); System.out.println("How many lab grades do you have?" ); countArray[2] = input.nextInt(); System.out.println(countArray[0] + ", " + countArray[1] + ", " + countArray[2]); for(int counter = 0; counter &lt; countArray[0]; counter++){ //every exam score System.out.printf("Enter Exam" + " " + (counter + 1) + " " + "score: "); exams[counter]=input.nextInt(); System.out.println(exams[counter]); } System.out.println("----"); for(int counter = 0; counter &lt; countArray[1]; counter++){ //every quiz score System.out.printf("Enter Quiz" + " " + (counter + 1) + " " + "score: "); quizzes[counter]=input.nextInt(); System.out.println(quizzes[counter]); } System.out.println("----"); for(int counter = 0; counter &lt; countArray[2]; counter++){ //every lab score System.out.printf("Enter Lab" + " " + (counter + 1) + " " + "score: "); labs[counter]=input.nextInt(); System.out.println(labs[counter]); } System.out.println("----"); System.out.println("EXAMS:"); for(int x = 0; x &lt; countArray[0]; x++) System.out.println(exams[x]); System.out.println("QUIZ:"); for(int x = 0; x &lt; countArray[1]; x++) System.out.println(quizzes[x]); System.out.println("LABS:"); for(int x = 0; x &lt; countArray[2]; x++) System.out.println(labs[x]); System.out.println("************************"); return countArray; //return back to calc grade } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload