0
public void calculatePercentage(int exam[][])
    {
        int perc = 0;
        for (int i = 0; i < examen.length; i++)
        {
            for (int[] score : exam)
                perc += score;
            perc /= exam.length;
        }
    }

Hello, I am really stuck at this one. I want to create a new mathod calculatePercentages given the parameter exam[][]. The double array exam holds 3 rows of elements. What the method has to do is simple calculate the sum of each row. The answer is probably quite simple, but I just don't know how to do it. For a single array, I guess the code is:

double perc = 0;
    for(int score : exam)
    {
            perc += score;
    }
    perc /= (exam.length);
    return perc;

The exam[][] could look like:

|10 12 18 5 3 | |12 3 5 15 20 | |20 15 13 11 9 |

The output percentage[] should like:

{48,55,68} Each element of percentage[] is the sum of the elements of 1 row of exam[]

5
  • 1
    Show an example input, and an example output, because it's not clear at all. Commented Dec 15, 2012 at 11:37
  • yeah it's unclear, and why the name of the method is calculatePercentage if it is just summing it up? Commented Dec 15, 2012 at 11:39
  • see above, I typed a possible input and output Commented Dec 15, 2012 at 11:41
  • 2
    See jlordo's answer, then. But why the hell are you calling this method calculatePercentage, since it doesn't calculate any percentage? Commented Dec 15, 2012 at 11:46
  • because it are 5 results on /20. Take the sum of these 5 results, makes a result on /100? And there you have a percentage Commented Dec 15, 2012 at 11:51

2 Answers 2

3

The double array exam holds 3 rows of elements. What the method has to do is simple calculate the sum of each row.

The name makes no sense, but it does what you want it to do.

public int[] calculatePercentage(int exam[][]) {
    int[] sums = new int[exam.length];
    for (int i = 0; i < exam.length; i++) {
         int sum = 0;
         for (int j = 0; j < exam[i].length; j++) {
              sum += exam[i][j];
         }
         sums[i] = sum;
    }
    return sums;
}

Also a double array would be double[], you are talking about two dimensional int arrays int[][]

EDIT

Pshemo pointed out a shorter solution is possible:

public int[] calculatePercentage(int exam[][]) {
    int[] sums = new int[exam.length];
    for (int i = 0; i < exam.length; i++) {
         for (int j = 0; j < exam[i].length; j++) {
              sums[i] += exam[i][j];
         }
    }
    return sums;
}

or even just

public int[] calculatePercentage(int exam[][]) {
    int[] sums = new int[exam.length];
    for (int i = 0; i < exam.length; i++)
         for (int j = 0; j < exam[i].length; j++)
              sums[i] += exam[i][j];
    return sums;
}

but I still prefer the first one, for it's readability.

Sign up to request clarification or add additional context in comments.

6 Comments

+1 but you don't need temp sum variable since you already have sums[i] which is 0 at the beginning. You can just for (int j = 0; j < exam[i].length; j++) sums[i]+=exam[i][j];
indeed, 2dimensional array, sorry for that
@jlordo, 2nd line, doesn't it has to be int[] sums = new int[exam[i].length];? Because now you will make an array that is 5 long, and I need one of 3 long
in the second line i can't be resolved to a variable. What the code does is create an array that's size is the amount of rows of exams. Every element will be the sum of the corresponding row, just as in your input output example. So my code is correct, or I have a severe misunderstanding of your question.
@user1873613 in your input output example exam.length is 3, and exam[i].length is 5 for i=0,1,2
|
0
for (int i = 0; i < exam.length; i++) {
        int sum = 0;
        for (int j = 0; j < exam[i].length; j++) {
            sum += exam[i][j];
        }

    }

using for each

for (int x[] : exam) {
      for (int y : x) {

        sum += y;
      }
}

1 Comment

Thank you mohammod hossain, but I already got a solution. I appreciate it of you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.