By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

HELP!! Where am I going wrong?

P: n/a
I was recently given this assignment in my introduction to C
programming class. I have been working on this problem for hours and
I am at my wits end. The following code needs to be produced.
Write a function that finds and displays the maximum value in a

two-dimensional array of integers. The array should be declared as a
10-by-20 array of integers in main().

b. Modify this function so that it also displays the row and column
number of the element with the maximum value.<<<

So far I have developed the program up to this point:

#include <stdio.h>

int findMax(int[10][20]);

int main()
{
int val[10][20] =
{23, 54, 35, 11, 10, 94, 88, 56, 13, 14, 99, 63, 28, 94, 77, 45,
12, 19, 91, 76,
24, 55, 36, 16, 19, 95, 89, 57, 18, 34, 92, 64, 29, 91, 44, 32,
89, 11, 98, 88,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67,
76, 18, 98, 10, 48, 19, 89, 13, 18, 14, 13, 84, 85, 19, 87, 92,
91, 18, 19, 67};

printf("The maximum value is %d", findMax(val));
return 0;

}

int findMax(int nums[10][20])
{
int q, pass, temp;

for (pass = 0; pass <= 6; pass++)
{
for (q = 0; q <= 6; q++)
{
if(nums[q][q] > nums[q +1][q + 1])
{
temp = nums[q][q];
nums[q][q] = nums[q+1][q+1];
nums[q + 1][q + 1] = temp;
}
}
}
return(temp);
}

I cannot figure out for the life of me what I am doing or not doing to
make this program work. So I am calling on the gurus for a little
advice.

Thanks,

Shaun
Nov 13 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
shauncarter1 wrote:

[snip - need help with homework]
Write a function that finds and displays the maximum value in a
two-dimensional array of integers. The array should be declared as a
10-by-20 array of integers in main().
qsort each row and then scan the last element of each row for the maximum value.
b. Modify this function so that it also displays the row and column
number of the element with the maximum value.
This is a bit more difficult if you didn't save the original matrix, but since
you've got the row, just scan that row for the value in the original matrix to
get the column. Otherwise...

[snip] int findMax(int nums[10][20])
This should probably be declared as

/* return the maximum value in n x m array nums */
int
findMax(int **nums, size_t n, size_t m)
{
int q, pass, temp;

for (pass = 0; pass <= 6; pass++)
Where does 6 come from?
{
for (q = 0; q <= 6; q++)
{
if(nums[q][q] > nums[q +1][q + 1])
{
temp = nums[q][q];
nums[q][q] = nums[q+1][q+1];
nums[q + 1][q + 1] = temp;
}
}
}
return(temp);
}


You will want to do something like

size_t i, j;
int max = 0;

loop over n
loop over m
update max if nums[i][j] > max
return max

or alternatively declare

typedef struct {
size_t row;
size_t col;
} Index;

and implement

/* return index of maximum value in n x m array nums */
Index findMax(int **nums, size_t n, size_t m);

HTH,

/david

--
FORTRAN was the language of choice
for the same reason that three-legged races are popular.
-- Ken Thompson, "Reflections on Trusting Trust"

Nov 13 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.