In the main function you first need to create an integer arr

In the main function, you first need to create an integer array with 10 elements in it. Use a loop to read 10 integers from the user and store the values to the array (in the order of input). For example (in the screenshot below) the first clement in the array stores 23, the next element stores value of 55, then the clement stores the value of 87 and so on. You may assume that the user will only enter integers, but not characters, etc. Once the array is initialized, call a function to get the index number of the largest value in the array. For example, the largest element in the array is 98 (as shown below), so the return value of the function should he 5. This function should take the array you created in the main function as a parameter, the number of valid elements in the array, and return the index of the maximum value in the array. The array should not be changed after the function is called. Write the pre-condition and post-condition for the function. Print the result in the main program after calling the function as below.

Solution

Precondition in this program is that we have to declare an int array of 10 elements and an integer that can store the value returned by the getMax() function.

#include <stdio.h>

/* function declaration */
int getMax(int arr[], int size);

int main () {

/* an int array with 10 elements */
int arr[10],position,i;
printf(\"Enter the elements of array\");

for(i=0;i<10;i++)
{
scanf(\"%d\",&arr[i]);
}
/* pass base address of array as an argument */
position = getMax( arr, 10) ;


printf(\"Position of max element is %d\", position);


return 0;
}

//function definition

int getMax(int arr[], int size) {

int maximum,c,location;

maximum = arr[0];
for (c = 1; c < size; c++)//loop for finding maximum element
{
if (arr[c] > maximum)
{
maximum = arr[c];
location = c;
}
}
printf(\"maximum element%d\",maximum);

//returns the position of maximum element in the array
return location;
}

Post Condition is that the returned value is the index of maximum value found in array.

 In the main function, you first need to create an integer array with 10 elements in it. Use a loop to read 10 integers from the user and store the values to th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site