Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

//

// Created by Tatenda on 5/16/2024.


//
#include <stdio.h>

int main() {
// array of marks
float marks[] = {78, 89, 90, 67, 88, 90, 76, 89, 91, 78};

// print the marks


printf("\n Marks: ");
for (int i = 0; i < 10; i++){
printf("%.2f, ", marks[i]);
}

// number of marks in the array


int numOfMarks = sizeof(marks) / sizeof(marks[0]);

// sort the marks in ascending order


for (int i = 0; i < 10; i++){
for (int j = 0; j < 9; j++) {
if (marks[j] > marks[j + 1]) { // if the current mark is greater than the
next mark
// swap the marks to sort them
float temp = marks[j];
marks[j] = marks[j + 1];
marks[j+1] = temp;
}
}
}

// print the sorted marks


printf("\nSorted marks: ");
for (int i = 0; i < 10; i++){
printf("%.2f, ", marks[i]);
}
printf("\n");

// calculate the mean, median and mode


float sum = 0;
// calculate the sum of all marks
for (int i = 0; i < 10; i++){
sum += marks[i];
}

float average = sum/numOfMarks; // calculate the mean by dividing the sum by the
number of marks
float median;
// int maxIndex = numOfMarks - 1; // calculate the maximum index of the marks array
// int middleIndex = maxIndex/2; // calculate the middle index of the sorted marks
// float median = marks[middleIndex]; // calculate the median by getting the middle
value of the sorted marks

if (numOfMarks % 2 == 0) { // if the number of marks is even calculate the


median by getting the average of the two middle values
median = (marks[(int)((numOfMarks / 2) - 1)] + marks[numOfMarks / 2]) / 2.0;
} else { // if the number of marks is odd, calculate the median by getting the
middle value
median = marks[(int)(numOfMarks / 2)];
}

int maxCount = 0; // variable to keep track of the maximum count of a mark


float mode = -1;

int isCounted[10] = {0}; // array to keep track of the marks that have been
counted, 0 means not counted, 1 means counted
// loop through the marks to find the mode
for(int i = 0; i < 10; i++){
if (isCounted[i] == 1){ // if the mark has already been counted, skip it
continue;
}
int count = 1;
// loop through the rest of the marks to find the count of the current mark
for (int j = i + 1; j < 10; j++){
if (marks[j] == marks[i]){
count++;
isCounted[j] = 1;
}
}
// if the count of the current mark is greater than the maximum count, update
the maximum count and mode
if (count > maxCount){
maxCount = count;
mode = marks[i];
}
}

// print the mean, median and mode


printf("Mean: %.2f\n", average);
printf("Median: %.2f\n", median);
printf("Mode: %.2f\n", mode);

You might also like