-
January 30th, 2009, 05:09 PM
#1
two-dimensional arrays (need help)
So I have this program running but the output is wrong. Im trying to display the maximum value of a list of numbers. 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, 982, 724, 194, 5, 5, 6,
33};
display(val);
return 0;
}
void display(int nums[ROWS][COLS])
{
int rowNum, colNum, fmax, max, i;
for (rowNum=0; rowNum<ROWS; rowNum++)
{
for(colNum=0; colNum<COLS; colNum++)
cout << setw(4) <<nums[rowNum][colNum];
cout << endl;
}
fmax=nums[0][0];
if (nums[ROWS][COLS] > max)
max = nums[ROWS][COLS];
cout << max;
system("pause");
return;
}
It outputs the table fine but the max number is way off. I think it has something to do with the fmax at the end of the program. Any help would be useful Thanks
-
January 30th, 2009, 05:14 PM
#2
Re: two-dimensional arrays (need help)
You don't have any code to calculate the max. You simply take the larger of nums[0][0] and nums[10][20]. Since (10,20) is an invalid index for nums, there's no telling what might be there.
Oh, and never under any circumstances use a variable, macro, or function named "max" in your program. Same goes for min. It's stupid, but Microsoft has destroyed the usefulness of those names for everyone with their macros. Any code using those names probably won't build correctly in Visual Studio.....
-
February 2nd, 2009, 10:38 PM
#3
Re: two-dimensional arrays (need help)
So what additional code is needed?
-
February 2nd, 2009, 11:34 PM
#4
Re: two-dimensional arrays (need help)
I'm not going to just give you the answer. However, as a hint, it will resemble the code to output the matrix except with an if statement where the cout is now.
-
February 3rd, 2009, 06:27 PM
#5
Re: two-dimensional arrays (need help)
In the last if statement??
-
February 3rd, 2009, 06:37 PM
#6
Re: two-dimensional arrays (need help)
I've already told you the last if statement in the code posted above is bogus. Get rid of it.
It occurs to me that since you've chosen to allocate the 2D array in one contiguous block of memory, you *could* use std::max_element() to help you out here. However, you should probably write manual code first to understand what that would be doing internally.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|