0

why my output is not coming correctly if I give my inputs ?

#include <stdio.h> int main() { int n,sum=0; double median; printf("enter number of elements"); scanf("%d",&n); int arr[100],freq[100]; printf("enter %d elements : \n",n); for (int i=0;i<n;i++) { sum+=arr[i]; } double mean = (double)sum; if(n%2==0) { int mid1 = n/2; int mid2 = mid1-1; median=(arr[mid1]-arr[mid2])/2.0; } int max_count=0,mode=-1; for(int i=0;i<n;i++) { freq[i]=1; for (int j=0;j<n;j++) { if(arr[i]==arr[j]) { freq[i]++; } } if(freq [i]>max_count) { max_count = freq[i]; mode = arr[i]; } } printf("mean : %2lf \n",mean); printf("median : %2lf \n",median); printf("mode : %d \n",mode); }

18th Oct 2023, 4:12 PM
Mahaswetha Imayavaramban
Mahaswetha Imayavaramban - avatar
1 Antwort
+ 2
Mahaswetha Imayavaramban the calculation for mean is incomplete. Divide by number of elements. The logic for median is incomplete. It is missing the case when number of elements is odd.
18th Oct 2023, 4:42 PM
Brian
Brian - avatar