nested for loop to compute average of the doubles in each row

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1142 Views - Last Post: 25 October 2015 - 05:54 PM Rate Topic: -----

#1 WhiteHorseTito   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-October 15

nested for loop to compute average of the doubles in each row

Posted 23 October 2015 - 09:54 PM

I am having trouble completing my code that needs to compute the average of each row in this 3x3 array. As of now, everything works and I am able to get a nicely printed table of 3 x 3, however I am struggling to find a solution for calculating the average of each row and outputting it in the console without disrupting my current solution.
My current code:
import java.util.Scanner;

public class Challenge {
    
    public static void main(String[] args){
             // array declared
             double[][] scores = new double[3][3];
             double sum = 0;
             double avg = 0;
             
             // Scanner initialized
             Scanner scnr = new Scanner(System.in);
             
        // User prompt,     
        System.out.println("Enter some numbers: ");
        
        for (int i = 0; i < scores.length; i++) {
            for (int j = 0; j < scores[i].length; j++) {
                sum += scores[i][j];
                avg = sum / i;
                 scores[i][j] = scnr.nextDouble();
            }
        }

        // Printed the array to show its functionality
        System.out.println("Your two dimensional array is printed below!");
        for (double[] a : scores) {
            for (double i : a) {
                System.out.print(i + "\t");
            }
            System.out.println("\n");
            
        }
    }
}  



Is This A Good Question/Topic? 0
  • +

Replies To: nested for loop to compute average of the doubles in each row

#2 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: nested for loop to compute average of the doubles in each row

Posted 23 October 2015 - 10:24 PM

in line 19 you are adding the score to the sum before your have read the score
the calculaion of the average should not be done in the loop (unless you require the running average)
if you need the average of each row you will require three sums one for each row
calculate the average(s) after the loop is complete
Was This Post Helpful? 1
  • +
  • -

#3 WhiteHorseTito   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-October 15

Re: nested for loop to compute average of the doubles in each row

Posted 24 October 2015 - 09:50 AM

View Posthorace, on 23 October 2015 - 10:24 PM, said:

in line 19 you are adding the score to the sum before your have read the score
the calculaion of the average should not be done in the loop (unless you require the running average)
if you need the average of each row you will require three sums one for each row
calculate the average(s) after the loop is complete


I understand, however I tried to work outside of the for loop and I am not getting any results.
My problem is that I am having trouble in approaching the problem the right way.
Calculating the average of each row and printing it out to be specific.
Was This Post Helpful? 0
  • +
  • -

#4 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: nested for loop to compute average of the doubles in each row

Posted 24 October 2015 - 10:19 AM

why not have a 1D array to hold the sum of each row, e.g.
        for (int i = 0; i < scores.length; i++) {
            for (int j = 0; j < scores[i].length; j++) {
                 scores[i][j] = scnr.nextDouble();
                sum[i] += scores[i][j];
            }
        }


then from the sum[] array you work out each average

Edit: fixed order of statements in loop as indicated in post #6

This post has been edited by horace: 24 October 2015 - 11:53 AM

Was This Post Helpful? 1
  • +
  • -

#5 WhiteHorseTito   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-October 15

Re: nested for loop to compute average of the doubles in each row

Posted 24 October 2015 - 10:40 AM

View Posthorace, on 24 October 2015 - 10:19 AM, said:

why not have a 1D array to hold the sum of each row, e.g.
        for (int i = 0; i < scores.length; i++) {
            for (int j = 0; j < scores[i].length; j++) {
                sum[i] += scores[i][j];
                 scores[i][j] = scnr.nextDouble();
            }
        }


then from the sum[] array you work out each average


Interesting, that type of solution did not cross my mind.
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: nested for loop to compute average of the doubles in each row

Posted 24 October 2015 - 10:58 AM

       sum[i] += scores[i][j];           // add ??? to total
       scores[i][j] = scnr.nextDouble(); // get next number 
// vs
       scores[i][j] = scnr.nextDouble();   // get next number
       sum[i] += scores[i][j];             //  add next number to total


The order of those two statements would make a difference
Was This Post Helpful? 0
  • +
  • -

#7 WhiteHorseTito   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-October 15

Re: nested for loop to compute average of the doubles in each row

Posted 24 October 2015 - 11:24 AM

View PostNormR, on 24 October 2015 - 10:58 AM, said:

       sum[i] += scores[i][j];           // add ??? to total
       scores[i][j] = scnr.nextDouble(); // get next number 
// vs
       scores[i][j] = scnr.nextDouble();   // get next number
       sum[i] += scores[i][j];             //  add next number to total


The order of those two statements would make a difference

The only problem now is that my code is broken and the sum is unable to be initialized. After inserting this second array, my code fails to get to this for loop since the sum is unable to be initialized . I've tried initializing the sum array but I am not getting anywhere.
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 835
  • View blog
  • Posts: 6,441
  • Joined: 25-December 13

Re: nested for loop to compute average of the doubles in each row

Posted 24 October 2015 - 11:46 AM

Can you copy and paste here the full text of the error message and the code that causes it.
Was This Post Helpful? 0
  • +
  • -

#9 WhiteHorseTito   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-October 15

Re: nested for loop to compute average of the doubles in each row

Posted 24 October 2015 - 12:28 PM

View PostNormR, on 24 October 2015 - 11:46 AM, said:

Can you copy and paste here the full text of the error message and the code that causes it.


package scorer;
import java.util.Scanner;

public class Scorer {
    
    public static void main(String[] args){
             // array declared
             double[][] scores = new double[3][3];
            // i tried declaring the sum array here but it wasnt
             
             
             // Scanner initialized
             Scanner scnr = new Scanner(System.in);
             
        // User prompt,     
        System.out.println("Enter some numbers human: ");
        
        for (int i = 0; i < scores.length; i++) {
            for (int j = 0; j < scores[i].length; j++) {
                 scores[i][j] = scnr.nextDouble();
                 
            }
        }

        // Printed the array to show its functionality
        System.out.println("Your two dimensional array is printed below!");
        for (double[] a : scores) {
            for (double i : a) {
                System.out.print(i + "\t");
            }
            System.out.println("\n");
            
        }
        
        
        // for loop for sum
        for (int i = 0; i < scores.length; i++) {
             for (int j = 0; j < scores[i].length; j++) {
               scores[i][j] = scnr.nextDouble();
                sum[i] += scores[i][j];
    }
}

    }
}





Error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: variable sum
location: class scorer.Scorer
at scorer.Scorer.main(Scorer.java:44)
Java Result: 1
BUILD SUCCESSFUL (total time: 15 seconds)
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: nested for loop to compute average of the doubles in each row

Posted 24 October 2015 - 12:32 PM

The error tells you that the compiler doesn't recognize the symbol (variable, method name etc.) called 'sum'. I see you used an array sum[] in the last for() loop, but where did you define/declare/initialize it?
Was This Post Helpful? 0
  • +
  • -

#11 WhiteHorseTito   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-October 15

Re: nested for loop to compute average of the doubles in each row

Posted 24 October 2015 - 12:40 PM

View PostGregBrannon, on 24 October 2015 - 12:32 PM, said:

The error tells you that the compiler doesn't recognize the symbol (variable, method name etc.) called 'sum'. I see you used an array sum[] in the last for() loop, but where did you define/declare/initialize it?

I tried intitializing it as a double with the following code
double[] sum = new double [];


this didn't work
Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: nested for loop to compute average of the doubles in each row

Posted 24 October 2015 - 12:41 PM

Post the whole code with the attempted declaration and define "didn't work."
Was This Post Helpful? 0
  • +
  • -

#13 WhiteHorseTito   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-October 15

Re: nested for loop to compute average of the doubles in each row

Posted 24 October 2015 - 12:52 PM

View PostGregBrannon, on 24 October 2015 - 12:41 PM, said:

Post the whole code with the attempted declaration and define "didn't work."


package scorer;
import java.util.Scanner;

/*
 * @author Filip Kuzmanovski
 */
public class Scorer {
    
    public static void main(String[] args){
             // array declared
             double[][] scores = new double[3][3];
             double sum[] = null;

            // i tried declaring the sum array here but it wasnt
             
             
             // Scanner initialized
             Scanner scnr = new Scanner(System.in);
             
        // User prompt,     
             
        System.out.println("Enter some numbers human: ");
        
        for (int i = 0; i < scores.length; i++) {
            for (int j = 0; j < scores[i].length; j++) {
                 scores[i][j] = scnr.nextDouble();
                 
            }
        }

        // Printed the array to show its functionality
        System.out.println("Your two dimensional array is printed below!");
        for (double[] a : scores) {
            for (double i : a) {
                System.out.print(i + "\t");
            }
            System.out.println("\n");
            
        }
        
        
        // for loop for sum
        for (int i = 0; i < scores.length; i++) {
             for (int j = 0; j < scores[i].length; j++) {
               scores[i][j] = scnr.nextDouble();
                sum[i] += scores[i][j];
    }
}

    }
}





Here my works now, sum is initialized but how can I now print the sum or the average of the individual rows. Please explain
Was This Post Helpful? 0
  • +
  • -

#14 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: nested for loop to compute average of the doubles in each row

Posted 24 October 2015 - 12:57 PM

Hmm. Perhaps, but I don't think you've decided what "works" means yet.

What does each element of a row have in common? Use that commonality to create a for() loop to sum the elements of each row.
Was This Post Helpful? 1
  • +
  • -

#15 horace   User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: nested for loop to compute average of the doubles in each row

Posted 24 October 2015 - 01:14 PM

why have you not allocated an array in line 12? if you attempt to use sum[] you will get a nullpointerExcepion
you also have two loops reading your 3*3 array - lines 24 to 28 and lines 43 to 48 - why?
the average calculation would be along the lines of
  sum[0]/3

would give you the average of row 0 - yoou could course use a loop
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2