0% found this document useful (0 votes)
2 views

My Code 02

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

My Code 02

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

My Code 02

// main.c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAX_COURSES 10 // Maximum number of courses you can input

// Function to calculate the weighted CGPA


float calculateWeightedCGPA(float cgpas[], int credits[], int n) {
float totalWeightedCGPA = 0.0;
int totalCredits = 0;
int i; // Declare loop variable here

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


totalWeightedCGPA += cgpas[i] * credits[i]; // Weighted CGPA
totalCredits += credits[i]; // Total Credits
}

return totalWeightedCGPA / totalCredits;


}

// Function to calculate mean CGPA


float calculateMean(float cgpas[], int n) {
float sum = 0.0;
int i; // Declare loop variable here

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


sum += cgpas[i];
}
return sum / n;
}

// Function to calculate variance


float calculateVariance(float cgpas[], float mean, int n) {
float variance_sum = 0.0;
int i; // Declare loop variable here

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


variance_sum += pow(cgpas[i] - mean, 2);
}
return variance_sum / n;
}

// Function to calculate standard deviation


float calculateStandardDeviation(float variance) {
return sqrt(variance);
}

int main() {
int n;

// Ask for the number of courses


printf("Enter the number of courses: ");
scanf("%d", &n);

// Declare arrays to store course details


char courseName[MAX_COURSES][100];
char courseCode[MAX_COURSES][20];
int credits[MAX_COURSES];
float cgpas[MAX_COURSES];

// Input course details


int i; // Declare loop variable here
for (i = 0; i < n; i++) {
printf("\nEnter details for Course %d\n", i + 1);
printf("Course Code: ");
scanf("%s", courseCode[i]);
printf("Course Name: ");
scanf(" %[^\n]%*c", courseName[i]); // To allow spaces in course name
printf("Credits: ");
scanf("%d", &credits[i]);
printf("CGPA: ");
scanf("%f", &cgpas[i]);
}

// Calculate Total Credits


int totalCredits = 0;
for (i = 0; i < n; i++) {
totalCredits += credits[i];
}

// Calculate Weighted CGPA


float weightedCGPA = calculateWeightedCGPA(cgpas, credits, n);

// Calculate Mean CGPA


float mean = calculateMean(cgpas, n);

// Calculate Variance
float variance = calculateVariance(cgpas, mean, n);

// Calculate Standard Deviation


float standardDeviation = calculateStandardDeviation(variance);
// Display results
printf("\nResults:\n");
printf("Total Credits: %d\n", totalCredits);
printf("Weighted CGPA: %.2f\n", weightedCGPA);
printf("Mean CGPA: %.2f\n", mean);
printf("Variance: %.2f\n", variance);
printf("Standard Deviation: %.2f\n", standardDeviation);

return 0;
}

You might also like