Home > Blockchain >  Want to access arr[n] in printf but the given n is above the actual elements in an array? [C]
Want to access arr[n] in printf but the given n is above the actual elements in an array? [C]

Time:12-12

CSS student here. I was given an exercise by my prof but I don't know how to solve this problem. A specific n is wanting to be accessed in printf but the given elements in the array are below the n that is asked.

This is the code I wrote but in this paticular test it's not giving me the right solution. Any tips?

#include <stdio.h>

int max(int arr[], int n) {

    int numMax = 0, indexMax = 0;

    for (int i = 0; i <= n; i  ) {
        if (arr[i] >= numMax) {
            numMax = arr[i];
            indexMax = i;
        }
    }

    return indexMax;
}




int main () {

    int arr[5]={-88, -91, -45, -90, -13};
    printf("The index of the highest number is: %d\n", max(feld, 5));
    // solution: 5

    return 1;
}

CodePudding user response:

Your array is called arr and not feld.
In your function you can initialize numMax with the first value of the array and then loop through it to test the following ones.

#include <stdio.h>

int max(int arr[], int n) 
{
    int numMax = arr[0], indexMax = 0;

    for (int i = 1; i < n; i  ) 
    {
        if (arr[i] >= numMax) 
        {
            numMax = arr[i];
            indexMax = i;
        }
    }
    return indexMax;
}

int main(void)
{
    int arr[5] = {-88, -91, -45, -90, -13};
    printf("The index of the highest number is: %d\n", max(arr, 5));

    return 0;
}

CodePudding user response:

With: for (int i = 0; i <= n; i ), the OP program is stepping out of the boundaries of the array. (zero based indexing is tricky for beginners.)

The array elements aren't going anywhere.
Simply pick the last element, and update that pick if a higher value is found during a scan toward the 0th element.

int maxVal( int arr[], int n ) {
    int maxInd = --n;

    while( --n >= 0 )
        if( arr[n] > arr[MaxInd] ) maxInd = n;

    return maxInd;
}

Fewer variables to keep track of is always an advantage.

The function returns the index, not the value.

printf("The index of the highest number is: %d\n", max(arr, 5) );

EDIT:
Let's visit main() to improve it a bit.

int main( void ) { // more conventional

    // the compiler counts more accurately than most people:
    int arr[] = { -88, -91, -45, -90, -13 };
    size_t nElem = sizeof arr/sizeof arr[0];

    // Notice that maxVal() should return a 'size_t', too.
    // Use the appropriate format specifier
    // The name "maxVal()" is misleading. Fix that...
    printf("The index of the highest number is: %sz\n", maxValInd( arr, nElem ) );

    return 0; // 0 means all good, non-zero indicates an error occurred.
}

Now, since that uses size_t (better for non-negative values like the number of elements in an array or bytes in a file), we should improve the function, too:

size_t maxValInd( int arr[], size_t n ) {
    size_t maxInd = 0; // pick 0th as first pick...

    while( --n > 0 ) // test all values down to, but not, arr[0].
        if( arr[n] > arr[MaxInd] ) maxInd = n;

    return maxInd;
}

NB: size_t is an unsigned datatype that will underflow if decremented below zero. Handle with care to avoid infinite-loops.

CodePudding user response:

int arr[6] and passing 5 as argument to max should do the work.

  •  Tags:  
  • c
  • Related