HELP!! Where am I going wrong?

Discussion in 'C Programming' started by shauncarter1, Jul 3, 2003.

  1. shauncarter1

    shauncarter1 Guest

    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
     
    shauncarter1, Jul 3, 2003
    #1
    1. Advertisements

  2. shauncarter1

    David Rubin Guest

    shauncarter1 wrote:

    [snip - need help with homework]
    qsort each row and then scan the last element of each row for 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]
    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)
    Where does 6 come from?
    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
     
    David Rubin, Jul 3, 2003
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.