2 Replies - 446 Views - Last Post: 04 February 2009 - 10:56 PM Rate Topic: -----

#1 richp2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 22-January 09

two dimensional array help

Post icon  Posted 04 February 2009 - 09:55 AM

So I have this program running but the output is wrong. Im trying to display the maximum value of a list of numbers and also display the row and column number of that value. Heres my program:

#include <iostream>
#include <iomanip>
using namespace std;

const int ROWS = 10;
const int COLS = 20;

void display(int [ROWS][COLS]); //function prototype

int main()
{

int i, j;
int val[ROWS][COLS] = { 

10, 20, 121, 214, 142, 123, 1, 2, 3, 91, 94, 84, 499, 955, 294, 194, 5, 5, 6, 33,
30, 12, 124, 233, 213, 333, 2, 9, 2, 33, 94, 84, 949, 982, 654, 194, 5, 5, 6, 33,
99, 11, 333, 122, 342, 111, 4, 4, 3, 11, 94, 84, 949, 482, 424, 194, 5, 5, 6, 33,
33, 33, 211, 434, 432, 444, 2, 2, 1, 44, 94, 84, 949, 992, 324, 194, 5, 5, 6, 33,
94, 84, 949, 942, 294, 194, 5, 5, 6, 33, 94, 84, 949, 542, 294, 194, 5, 5, 6, 33,
95, 24, 249, 249, 491, 985, 3, 8, 3, 98, 10, 20, 121, 321, 142, 123, 1, 2, 3, 91,
30, 12, 124, 123, 213, 333, 2, 9, 2, 33, 94, 84, 949, 948, 298, 194, 5, 5, 6, 33,
99, 11, 333, 122, 342, 111, 4, 4, 3, 11, 94, 84, 949, 482, 944, 194, 5, 5, 6, 33,
33, 33, 211, 434, 432, 444, 2, 2, 1, 44, 94, 84, 949, 482, 754, 194, 5, 5, 6, 33,
94, 84, 949, 942, 294, 194, 5, 5, 6, 33, 94, 84, 949, 999, 724, 194, 5, 5, 6, 33};
display(val);

return 0;
}
void display(int nums[ROWS][COLS])
{
int rowNum, colNum, fmax, maxelemrow, maxelemcol, i, j;
for (rowNum=0; rowNum<ROWS; rowNum++)
{
for(colNum=0; colNum<COLS; colNum++)
cout << setw(4) <<nums[rowNum][colNum];
cout << endl;
}

fmax=nums[0][0];
for (nums[i]; i<ROWS; i++)
for (nums[j]; j<COLS; j++)
	if (nums[i][j] > fmax)
  {	   
		  fmax = nums[i][j];
}
		  maxelemrow = i;
		  maxelemcol = j; 
cout << fmax <<endl;
cout<< "This is row number [" << maxelemrow << "] and column number [" << maxelemcol << "] in the list of numbers" <<endl;
system("pause");
return;
} 



It outputs the table fine but the max number is 10 which is obviously not right. And it outputs [2009288258] for both the row and col rumber. I think it has something to do with the fmax at the end of the program but I'm really not sure. Any help would be extremely useful! Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: two dimensional array help

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,516
  • Joined: 23-August 08

Re: two dimensional array help

Posted 04 February 2009 - 10:15 AM

You need to initialize i and j to 0, for one thing. And you're not setting your maxelemrow and column variables when you find the max.
Was This Post Helpful? 0
  • +
  • -

#3 richp2004  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 22-January 09

Re: two dimensional array help

Posted 04 February 2009 - 10:56 PM

View PostJackOfAllTrades, on 4 Feb, 2009 - 09:15 AM, said:

You need to initialize i and j to 0, for one thing. And you're not setting your maxelemrow and column variables when you find the max.


Wow that was surprisingly easy to fix :) Thank you very much! I really appreciate it :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1