HELP!! Where am I going wrong?

S

shauncarter1

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.
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
 
D

David Rubin

shauncarter1 wrote:

[snip - need help with homework]
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[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top