• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

summing each row in a 2-dimensional array

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am doing a project where I have to sum each row in a 2-dimensional array. The thing is that this must be done in a separate method, then returned to the main method as a new array. I know how to sum each row in a 2-dimensional array, but I don't know how to create a new array with this.

Here is what I have so far:



So I guess what I need help with is how to get the sum of each row into the new array sum[]. Any help?

[ June 16, 2008: Message edited by: J Calabrese ]

[ June 16, 2008: Message edited by: J Calabrese ]
[ June 16, 2008: Message edited by: J Calabrese ]
 
J Calabrese
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I edited some things I had done wrong in the main post.

And what do you mean bill? I don't think my variables are hurting anything, as I need to somehow get the sum of each row into a new array.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about my original reply ending up here. I was replying to a post about declaring variables inside an array and it apparently got deleted while I was writing my response, so it somehow ended up here.

Since I'm here, a couple of quick observations:

1. In your declaration of sumIt, you should pass in an int[][], not an int[]. I know it works as is, but you should make it explicit since the code inside the method expects an int[][].

2. When you declare the return array (sum[]), do you know the size will always be 8? If it's not, is there somewhere you can get the size?

3. Are you intentionally returning sum inside the for loop, or is this a typo?

Hope this helps.
 
J Calabrese
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. I thought of that, and fixed it.

2. yes it is always 8

3. It was most definitely a typo. I have fixed your recommendations and this is what I have now:



edit: That worked, which is wonderful. Thanks so much for your help.

[ June 16, 2008: Message edited by: J Calabrese ]
[ June 16, 2008: Message edited by: J Calabrese ]
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bill Cruise:
Sorry about my original reply ending up here. I was replying to a post about declaring variables inside an array and it apparently got deleted while I was writing my response, so it somehow ended up here...


It was my post. I don't know how and why it got deleted?
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to repost your question I'll reply to it. I don't want to confuse things by replying to your question in this thread.
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One small correction:
Replace your inner for loop for j like this:


instead of


In the normal case when all the inner array elements have fixed length i.e. 8 it will work.
But when you reduce or increase the length of elements (which is feasible in Java) then ArrayIndexOutofBoundsException will be thrown.

For ex:
Replace the 2nd row element by removing 4 from there.
Now it will look like:


Now compile & run the program with the following inner loop for j:


Run it:


2) Also it would be better if you could fix the array length like this:



Regards.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you saying it is normal for all members to have the same length? It is not a two-dimensional array, which doesn't actually exist in Java. It is an array of arrays, so the individual member arrays are often different lengths/
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your correction sir!
Regards.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ramya narayanan:
Thanks for your correction sir!
Regards.



You're welcome
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey i'm new to programming and I was clicking around. I started playing with this program and to print it out i am getting confused.. this is what i have so far.. if anyone can help that'd be awesome! and i'm trying to give it a box look with day of the week and the employee 1-8 on the side like this

SunMonTueWedThurFri Sat Total
Employee 1243458834

Employee 2734334428

Employee 3334332220

Employee 4934734131

Employee 5354363832

Employee 6344634428

Employee 7374838437

Employee 8635927941





public class Proj4
{ /** Main method */
/** Main method */
public static void main(String args[]) {

//creating 2-dimensional array for employee's hours
int[][] hours = {
{2, 4, 3, 4, 5, 8, 8},
{7, 3, 4, 3, 3, 4, 4},
{3, 3, 4, 3, 3, 2, 2},
{9, 3, 4, 7, 3, 4, 1},
{3, 5, 4, 3, 6, 3, 8},
{3, 4, 4, 6, 3, 4, 4},
{3, 7, 4, 8, 3, 8, 4},
{6, 3, 5, 9, 2, 7, 9}};

int[] sum1 = sumIt(hours);



}


public static int[] sumIt(int[][] numbers){

int sum[] = new int[8];

for (int i = 0; i < numbers.length; i++){
int total = 0;
for (int j = 0; j < numbers[0].length; j++)
total += numbers[i][j];

sum[i] = total;
}
public static void Printit(int employee[][])
{
String days="Emp Sun Mon Tue Wed Thur Fri Sat Total Hours";
int row=0,col=0;
String spaces=" ";
String spaces2=" ";

System.out.println(days);
System.out.println("==============================================");
for(row=0;row<=7;++row)
{
System.out.print(spaces);
for(col=0;col<=8;++col)
{
System.out.print(employee[row][col]+spaces2);
}
System.out.println();
}
}
}
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you two doing the same course?

Ryan: please continue discussion on the thread you started yourself; it causes no end of confusion if you get the same question twice.
 
reply
    Bookmark Topic Watch Topic
  • New Topic