2

You are participating in a game in which players collect points for various solved puzzles. In the end, the player with the highest score wins. You would like to know how far behind the highest-scoring person everyone else is in order to know whether you still have a chance at winning.

Please write a C program that uses a function "behind()" (which you also have to write) in order to help with this task. Your program should first read, from the user input, the number of players participating in the game. There are never more than 10 players in the game. Next, your program should read the current scores of each player and store them in an array. Then you should call the function behind(), to which you pass as a first argument, the array holding the player's scores, and as a second argument the number of players in the game. The function behind should replace the scores stored in the array with the number of points by which each individual player is behind the top-scoring player.

To help you out, the main function of the program has already been written, so your job is simply to write the function behind(), whose protoype is also given to you.

#include <stdio.h>

void behind(int *, int);

int main(void) {
int array[10];
int N, i;

scanf("%d", &N);
for (i=0; i<N; i++) {
    scanf("%d", &array[i]);
}
behind(array, N);
for (i=0; i<N; i++) {
    printf("%d\n", array[i]);
}

return 0;
}

My question is how I write the code to find the max value of the array elements without comparing the last element to something outside the memory block? I have something in mind like:

if(array[i] > array[i+1]) {
max = array[i];
} else {
 max = array[i+1];
}

but realize it goes outside the bounds of the array once reaching the last element.

2 Answers2

2

"How I write the code to find the max value of the array elements without comparing the last element to something outside the memory block?"

You just need to set max before going into the loop to 0. Then you compare if array[i] is greater than max at each iteration. If it is, max gets assigned by array[i]. The loop condition i < len checks whether there are more elements or not in array:

int max = 0;

for (unsigned int i = 0; i < N; i++) {

    if (array[i] > max) {
       max = array[i];
    }    
}

Another thing is that you have first a fixed size array of 10 elements with

int array[10];

and then later ask the user to input the amount of elements:

scanf("%d", &N);

If the user inputs a number above 11 for N, you accessing memory beyond the bounds of the array in the loops.

In this case, a declaration of array as variable length array after the scanf() call would be more appropriate:

int N, i;

scanf("%d", &N);
int array[N];

Since there will never be more than 10 players: "There are never more than 10 players in the game" - You could also leave array of fixed size with 10 elements and analyze the input of N in a loop and ask the user to reinput a valid number between 0 and 10 if N isn't in this range. You use N for the iterations only but it doesn't influence the array size.

int array[10];

while (1) {

   scanf("%d", &N);

   if ( N >= 0 || N <= 10 ) {
       break;
   }  

   printf("Please reinput a valid number!\n");
}

Side notes:

1

how I write the code to find the max value of the array elements without comparing the last element to something outside the memory block?

You can code the behind() something like:

void behind(int *arr, int n) {
    int max_value = 0;

    for (int i = 0; i < n; i++)
        if (arr[i] > max_value)
            max_value = arr[i];
    
    printf("Max value: %d\n", max_value);
}

Rather than defining an array, you may declare the array as a pointer variable, that'd be quite helpful. You can then dynamically allocate the memory and then use it like an array thereafter, also note that you're trying to pass a pointer, so it's better take some benefit of pointer declaration.

The full approach to do that:

#include <stdio.h>
#include <stdlib.h> // for dynamic memory allocation

void behind(int *, int);

int main(void) {
    int *array; // declaring as pointer rather than array
    int N, i;

    scanf("%d", &N);

    if (N > 11) { // must be less than 10
        printf("Must be lesser than or equal to 10!\n");
        return -1;
    }

    // dynamically allocating the required memory
    array = (int *)malloc(sizeof(int) * N);

    for (i = 0; i < N; i++)
        scanf("%d", &array[i]);

    // here we go
    behind(array, N);

    for (i = 0; i < N; i++)
        printf("%d\n", array[i]);

    return 0;
}

void behind(int *arr, int n) {
    int max_value = 0;

    for (int i = 0; i < n; i++)
        if (arr[i] > max_value)
            max_value = arr[i];
    
    printf("Max value: %d\n", max_value);
}

An example output is as follows:

5    // ------------ INPUT
12
45
23
1 
05
Max value: 45 // --- OUTPUT
12
45
23
1
5
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • @DanielPatrickMurray Welcome. And notice that I've used malloc instead of VLA here, so it's supported in higher versions than C99. – Rohan Bari Jul 07 '20 at 06:33