Confuzzled again..
Ok here we go.. Im doing this program for an assignment and I just cant figure out this dynamic memory allocation. So if anyone can help me, I need to convert this program to use dynamic memory allocation.. please
#include <stdio.h>
#include <math.h>
#include <conio.h>
int userinput(void);
int scan(char *input);
char word[80];
int main()
{
int loop;
int i = 0;
int mark[1000];
int total = 0;
float mean, scale;
printf("How many students are there: ");
loop = userinput();
while(i<loop)
{
printf("Enter mark for student %d: ",i + 1);
mark = userinput();
total = total + mark;
i++;
}
mean = total/loop;
if(mean<50)
{
i = 0;
scale = 50/mean;
total = 0;
printf("\nScaled Mark\n", i+1);
while(i<loop)
{
mark = mark * scale;
printf("%d\n", mark);
total = total + mark;
i++;
}
mean = total/loop;
printf("\nAverage mark = %.2f",mean);
}else{
printf("\nAverage mark = %.2f",mean);
}
getch();
}
int userinput(void)
{
gets(word);
return scan(word);
}
int scan(char *input)
{
int number;
int decimalPtFound = 0;
int i = 0;
while(input != '\0')
{
if(input >= '0' && input <= '9'){
} else if(input == '.'){
decimalPtFound ++;
if(decimalPtFound > 1){
printf("Invalid character '.'\n\n");
main();
}
}else{
printf("Invalid character '%c'\n\n", input);
main();
}
i++;
}
sscanf(input,"%d",&number);
return number;
}
Also if someone wants to help me figure out a way to loop the same student mark if a wrong character is entered
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement