Study of dynamic memory allocation. A program to accept input into a dynamically allocated array of floats to store the marks of students of a class and determine their grades and the topper.

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

void inputMarks(float *, int);
void showGrades(float *, int);
int getTopper(float *, int);

int main(void)
{
int i, cnt;
float *marks;

printf("\nEnter the number of students : ");
scanf("%d", &cnt);

marks = (float *) malloc(sizeof(float) * cnt);

if(marks == NULL)
{
printf("\nMemory allocation failed");
return 1;
}

inputMarks(marks, cnt);
showGrades(marks, cnt);
i = getTopper(marks, cnt);

printf("\n\nThe topper is student number %d with %f marks", i + 1, marks[i]);

free(marks);

return 0;
}

void inputMarks(float *p, int sz)
{
int i;
printf("\nEnter marks for %d students :\n", sz);
for(i = 0 ; i < sz ; i++)
{
printf("Marks %d : ", i + 1);
scanf("%f", p);
p++;
}
}

void showGrades(float *p, int sz)
{
int i;
printf("\nDetermine the grades :\nS.No.\tGrades\n");
for(i = 0 ; i < sz ; i++)
{
if(p[i] >= 60.0)
printf("\n%d\tGrade A", i + 1);
else if(p[i] >= 50.0 && p[i] < 60.0)
printf("\n%d\tGrade B", i + 1);
else if(p[i] >= 40.0 && p[i] < 50.0)
printf("\n%d\tGrade C", i + 1);
else
printf("\n%d\tFAIL", i + 1);
}
}

int getTopper(float *p, int sz)
{
int i, top = 0;
for(i = 0 ; i < sz ; i++)
{
if(p[i] > p[top]) top = i;
}
return top;
}

No comments:

Post a Comment

kiss on google ads if you are anonymous because your ip is trackable.thank you.

......from.admin