New To Java - Help with arrays

I would like some help with a question on arrays

I am supposed to find the mean and the mode of a 2 D array

I created a nested for loop the totaled the array = that to int total

and then I set int average = total / array.length, and it is not correctly averaging the array,

my professor said that array.length won't work for a 2D array only a single array. she suggested a counter but I am not sure on how to code this?

Can someone help me please?

I have no idea on how to find the mode.

[534 byte] By [itec_girla] at [2007-11-26 23:09:14]
# 1
Simple. Declare a variable before the for loop and initialise to zero. Inside the loop everytime you had a value to the total, you also increment your counter by one.
floundera at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 2

Thank you for the reply i wanted to make sure this is what you meant

is this code correct ?

int counter = 0;

for (int rowNumber[] : Studentgrades)

{

for (int columnNumber : rowNumber)

total += columnNumber;

counter++;

int average = total / counter;

average = Mean;

}

itec_girla at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 3
it complies but i still get a zero for an anser that is why i asked thanks again
itec_girla at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 4
Basically.Is the declaration of average inside the loop? If so you only need to do it once, outside the loop once you have added all the values. Also, after assinging average a value, why are you immediately reassigning it to be Mean?
floundera at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 5

Hear is my whole code for the project:

public class assignmentone

{

public static void main (String args[])

{

int Studentgrades[][] = { {88, 99, 89, 88, 77},

{90, 89, 70, 78, 100},

{94, 74, 86, 89, 63},

{99, 76, 66, 89, 64},

{89, 76, 89, 88, 56},

{98, 76, 89, 88, 99},

{98, 89, 66, 88, 89} };

int Maxgrade = Studentgrades[0][0];

int Mingrade = Studentgrades[0][0];

int Mean = 0;

int Mode = 0;

int total = 0;

int grade = 0;

int counter = 0;

for (int Testgrade[]: Studentgrades)

{

for (int grades: Testgrade)

{

if (grades < Mingrade)

Mingrade = grades;

if (grades > Maxgrade)

Maxgrade = grades;

total += grades;

counter++;

int average = total / counter;

average = Mean;

}

}

System.out.printf("The max grade is %s\n",Maxgrade);

System.out.printf("The min grade is %s\n",Mingrade);

System.out.printf("The mean is %s\n", Mean);

System.out.printf("The Mode is %s", Mode);

}

}

no reason on the changing it to mean, i could delete that save a variable lol

itec_girla at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 6
it complies and the Max and Min come back correctly but the Mean still says zero
itec_girla at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 7
Remember, the right hand side of an assignment statement the value is assigned to the left hand side!Message was edited by: flounder
floundera at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 8
that worked, when i changed average = mean to mean = average, it works fine now thank you so much, it is the simple that get me all the time, thanks again
itec_girla at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 9
Two things, you are still doing the calculation inside the loop, so it does the calculation everytime around instead of ONCE outside the loop. Also, why bother with the average variable?Mean = total / counter;
floundera at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 10
Okay, thanks i did get rid of the average variable and just change it to Mean = total/ counter; but i'm not sure of what you mean doing the calculation inside the loop , how do i move it outside the loop
itec_girla at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 11

Okay, thanks i did get rid of the average variable

and just change it to Mean = total/ counter; but i'm

not sure of what you mean doing the calculation

inside the loop , how do i move it outside the loop

for (...) {

// inside the loop

}

// outside the loop

Why are you calculating the average after every item (inside the loop) and then doing nothing with it, rather than just calculating it once at the end (outside the loop)?

jverda at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 12
oh okay i see what you mean i will change that thanks a bunch
itec_girla at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 13
Any thoughts on how to do the mode?
itec_girla at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 14

Any thoughts on how to do the mode?

What part are you having trouble with?

Do you know what mode is and how to do it "manually"?

If yes, then have you taken a stab at doing it in Java? What part is leaving you stuck? (I think I know, but it'll be better if you can explain it yourself.)

jverda at 2007-7-10 14:04:13 > top of Java-index,Java Essentials,New To Java...
# 15

yes, i know the mode is the number that repeats itself the most i tried this

if (Studentgrades[x - 1] == Studentgrades[x])

{

temp++;

}

else

{

if (temp > elements)

{

tempMode = Studentgrades[x - 1];

elements = temp;

temp = 1;

}

else

{

temp = 1;

}

}

if (temp > elements)

tempMode = Studentgrades[x - 1];

Mode = tempMode;

but it does not compile

itec_girla at 2007-7-21 19:22:23 > top of Java-index,Java Essentials,New To Java...
# 16

1) When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

2) Don't just say "it doesn't compile." Paste in the exact, complete error message.

3) I'm not sure what problem you're having here, but here's one thing you'll have to address: To know which value occurs the most, you'll have to keep a running total of how may times each value occurs. So if you find a 5, you'll have to look up where you're storing 5's count and increment it by one. Do you know how to do that? I see a couple of approaches. The simplest two are a Map or an array.

jverda at 2007-7-21 19:22:23 > top of Java-index,Java Essentials,New To Java...
# 17

oh okay sorry, i will use those from now on, this is my first time on this site

the error is :

assignmentone.java:52: incompatible types

found: int[]

required: int

tempMode = Studentgrades[x - 1];

^

assignmentone.java:63: incompatible types

found: int[]

required: int

tempMode = Studentgrades[x - 1];

^

2 errors

-jGRASP wedge2: exit code for process is 1.

-jGRASP: operation complete.

and no, i don't know how to do that, i thought it was something like that i wanted to write it where it stored the values and counted it and return the highest count but i am not sure as to how to code that ( first year java student , sorry , i feel dumb sometimes ) the book as an example of frequency but iv dont understand it

thanks for the response and help

itec_girla at 2007-7-21 19:22:23 > top of Java-index,Java Essentials,New To Java...
# 18
Studentgrades is a 2D array, correct? Therefore Studentgrades[somevalue] is an array itself. Therefore you cannot assign an array to an int.
floundera at 2007-7-21 19:22:23 > top of Java-index,Java Essentials,New To Java...
# 19
yes, Studentgrades is an 2d array, so i need to change that from int to int []
itec_girla at 2007-7-21 19:22:23 > top of Java-index,Java Essentials,New To Java...