// C programming, PLEASE COMPILE a code to match with the EXPECTED VALUE // MY O
ID: 3861488 • Letter: #
Question
// C programming, PLEASE COMPILE a code to match with the EXPECTED VALUE
// MY OUTPUT IS WRONG
void minmax(int a[], int len, int *minpos, int *maxpos)
{
*minpos=*maxpos=a[0];
for(int i=1; i<len; i++)
{
if(*minpos > a[i])
*minpos=a[i];
else if(*maxpos<a[i])
*maxpos=a[i];
}
}
#include <stdio.h>
void minmax(int a[], int len, int* min, int* max);
int main() {
int a[] = { 1, 4, -1, -1, 9, 9, -2, 14, -10 };
int min = 42;
int max = 1729;
minmax(a, 0, &min, &max);
printf("min max: %d %d ", min, max);
printf("Expected: 42 1729 ");
minmax(a, 6, &min, &max);
printf("min max: %d %d ", min, max);
printf("Expected: 2 4 ");
minmax(a, 9, &min, &max);
printf("min max: %d %d ", min, max);
printf("Expected: 8 7 ");
int b[] = { -1, -4, -9 };
minmax(b, 3, &min, &max);
printf("min max: %d %d ", min, max);
printf("Expected: 2 0 ");
return 0;
}
/*
OUTPUT
min max: 1 1
Expected: 42 1729
min max: -1 -1
Expected: 2 4
min max: -10 -1
Expected: 8 7
min max: -9 -1
Expected: 2 0
*/
Explanation / Answer
In this program your logic is correct but your Expectation is wrong, I explain bellow
our array is int a[] = { 1, 4, -1, -1, 9, 9, -2, 14, -10 };
your first expectation printf("Expected: 42 1729 "); is correct, but,
your second expectation printf("Expected: 2 4 "); is not correct, our array a[] does not contain element 2 so how you expect minimum as 2, and we have element 9 in a[] in first 6 elements so how you expect 4 as max.
in the same way the third expectation printf("Expected: 8 7 "); and fourth expectation printf("Expected: 2 0 "); are also not correct
here is the correct program with output
Program:-
#include <stdio.h>
void minmax(int a[], int len, int* min, int* max);
int main()
{
int a[] = { 1, 4, -1, -1, 9, 9, -2, 14, -10 };
int min = 42;
int max = 1729;
minmax(a, 0, &min, &max);
printf("min max : %d %d ", min, max);
printf("Expected: 42 1729 ");
minmax(a, 6, &min, &max);
printf("min max : %d %d ", min, max);
printf("Expected: -1 9 "); //code changed here because your Expectation is wrong
minmax(a, 9, &min, &max);
printf("min max : %d %d ", min, max);
printf("Expected: -10 14 "); //code changed here because your Expectation is wrong
int b[] = { -1, -4, -9 };
minmax(b, 3, &min, &max);
printf("min max : %d %d ", min, max);
printf("Expected: -9 -1 "); //code changed here because your Expectation is wrong
return 0;
}
void minmax(int a[], int len, int *minpos, int *maxpos)
{
int i;
//add if condition here
if(len>0) //if len do not greater than 0 means that you dont want to search for min and max in array so no need to assign a[0] to *minpos and *maxpos
*minpos=*maxpos=a[0];
for(i=1; i<len; i++)
{
if(*minpos > a[i])
*minpos=a[i];
else if(*maxpos<a[i])
*maxpos=a[i];
}
}
Output:-
min max : 42 1729
Expected: 42 1729
min max : -1 9
Expected: -1 9
min max : -10 14
Expected: -10 14
min max : -9 -1
Expected: -9 -1
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.