Masnad Nihit - 7 months ago 63

C Question

I am trying to get 5 different grades from judges, and putting them in a array.

Then I try to find the max and the min element from the array.

example {2,3,4,5,6}

max= 6 , min = 2.

After that I have to delete 6 and 2, and then add 3+4+5 which gives me a sum of 12.

How do delete and then add the sum? Since it looks like I am getting errors while deleting from the array .Here is the code

`#include <stdio.h>`

int main()

{

int judge1, judge2,judge3,judge4, judge5,i,max,min;

int size = 4;

printf("Judge one grade");

scanf("%d", &judge1);

printf("Judge one grade");

scanf("%d", &judge2);

printf("Judge one grade");

scanf("%d", &judge3);

printf("Judge one grade");

scanf("%d", &judge4);

printf("Judge one grade");

scanf("%d", &judge5);

int a[] = {judge1, judge2, judge3, judge4, judge5};

max=a[0];

for(i=1;i<size;i++){

if(max<a[i])

max=a[i];

}

min=a[0];

for(i=1;i<size;i++){

if(min>a[i])

min=a[i];

}

for (i=0;i < size ;i++) {

if(a[i] == min || a[i] == max){

a[i] = a[i+1];

}

}

for (i=0;i < size ;i++) {

int sum = 0;

sum+= a[i];

printf("%d\n", sum);

}

return 0;

}

Answer

Instead of looping through the array again, use the index. Note that in your code, you reset sum inside the loop so it will only show the last value and is not in scope when the loop is finished (so it will disappear). You also print inside the loop instead of after the full sum has been calculated.

```
int imin = 0, imax = 0;
max=a[0];
for(i=1; i<size; i++){
if(max<a[i]) {
imax = i;
max=a[i];
}
}
min=a[0];
for(i=1; i<size; i++){
if(min>a[i]) {
min=a[i];
imin = i;
}
}
a[imin] = 0; // Does not contribute to the sum
a[imax] = 0; // Does not contribute to the sum
int sum = 0;
for (i=0; i<size; i++) {
sum += a[i];
}
printf("sum = %d\n", sum);
```