Skip to content

Instantly share code, notes, and snippets.

@jkasun
Last active January 9, 2019 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkasun/70f7ebe23a679dab0d3d1daf8a66f095 to your computer and use it in GitHub Desktop.
Save jkasun/70f7ebe23a679dab0d3d1daf8a66f095 to your computer and use it in GitHub Desktop.
Assignment
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const int MAXLENGTH = 100; // Max length of the string
int numOfStudents = 2;
int numberOfSubjects = 2;
// Calculate the sum, average of each subject
void calculateTotalAndAverage(int studentMarks[numOfStudents][numberOfSubjects], char subjectTitles[numberOfSubjects][MAXLENGTH])
{
int classTotal = 0;
for (int i = 0; i < numberOfSubjects; i++)
{
// Calculating the total for each subject
int total = 0;
for (int j = 0; j < numOfStudents; j++)
{
total = total + studentMarks[j][i];
}
// add the subject total to class total
classTotal = classTotal + total;
// Printing results
printf("Sum of the marks for %s is %i\n", subjectTitles[i], total);
printf("Average of the marks for %s is %f\n", subjectTitles[i], total / (float)numOfStudents);
printf("\n");
}
float average = classTotal / (float)(numberOfSubjects * numOfStudents);
printf("Average of the class is : %f\n", average);
}
void calculateRank(char studentNames[numOfStudents][MAXLENGTH], int studentMarks[numOfStudents][numberOfSubjects])
{
int studentTotals[numOfStudents];
// Calculating the sum of each student
for (int i = 0; i < numOfStudents; i++)
{
int studentTotal = 0;
for (int j = 0; j < numberOfSubjects; j++)
{
studentTotal = studentTotal + studentMarks[i][j];
}
// printf("Total for %s is %i\n", studentNames[i], studentTotal);
studentTotals[i] = studentTotal;
}
for (int i = 0; i < numOfStudents - 1; i++)
{
// Last i elements are already in place
for (int j = 0; j < numOfStudents - i - 1; j++)
{
// Sorting the arrays (Bubble sort)
if (studentTotals[j] < studentTotals[j + 1])
{
// Swapping rank
int temp = studentTotals[j];
studentTotals[j] = studentTotals[j + 1];
studentTotals[j + 1] = temp;
// Swapping name
char *tempName = (char *)malloc((strlen(studentNames[j]) + 1) * sizeof(char));
strcpy(tempName, studentNames[j]);
strcpy(studentNames[j], studentNames[j + 1]);
strcpy(studentNames[j + 1], tempName);
free(tempName);
}
}
}
// The header
printf("RANK\tNAME\t\tTOTAL\n");
for (int i = 0; i < numOfStudents; i++)
{
// Printing ranks
printf("%i\t%s\t\t%i\n", i + 1, studentNames[i], studentTotals[i]);
}
}
int main()
{
// contains the name of students
char studentNames[numOfStudents][MAXLENGTH];
// contains the marks of each student
int studentMarks[numOfStudents][numberOfSubjects];
// contains the name of subjects
char subjectTitles[numberOfSubjects][MAXLENGTH];
printf("\n---------------------------------------------\n");
for (int i = 0; i < numberOfSubjects; i++) {
printf("Enter the name of subject %i: ", i + 1);
scanf("%s", subjectTitles[i]);
}
printf("\n---------------------------------------------\n");
for (int i = 0; i < numOfStudents; i++)
{
int marks[numberOfSubjects];
printf("Enter student name: ");
scanf("%s", studentNames[i]);
for (int j = 0; j < numberOfSubjects; j++)
{
printf("Enter Marks for %s: ", subjectTitles[j]);
scanf("%i", &studentMarks[i][j]);
}
printf("\n---------------------------------------------\n");
}
calculateTotalAndAverage(studentMarks, subjectTitles);
printf("\n---------------------------------------------\n");
calculateRank(studentNames, studentMarks);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment