C/pls check my programme for me.
Expert: Prince M. Premnath - 12/4/2007
QuestionQUESTION: Hi Prince M. Premnath,
If you would only have time to answer abt 4 to 6 questions on my C classes, you would have helped me a great deal. the first problem is this one: This code is abt getting the grades, sum and average of a number of students. I couldn't understand why it's not compiling. could you check it for me?
#include<stdio.h>
#include<string.h>
int sum(int *s);
int average(int *sum, int n);
void print_info(int *s, int*ave);
int main(void){
int grade, max,i;
int sum, n,m;
char name[m];
printf("How many students do you want to enter?");
scanf("%d",n);
name[m]= name[n];
for(i=0;i<name[n];i++);
printf("enter name of student:");
scanf("%s",&name[i]);
printf("%s",name[i]);
printf("enter the grade:");
scanf("%d",grade);
sum =0;
max = grade;
printf("enter name of student:");
scanf("%s",&name[i]);
printf("%s",name[i]);
printf("\n");
printf("enter the grade:");
scanf("%d",grade);
if(grade>max)
max = grade;
sum +=grade;
sum = int sum(int *s);
average = int average(int *sum, int n);
void print_info(int *s, int*ave);
getchar();
}
int sum(int *s){
int u;
u = *s++;
return(sum);
}
int average(int *sum, int n){
int ave_score;
ave_score = (*s/n);
return(ave_score);
}
void print_info(int *s, int*ave){
printf("%d",*s);
printf("%d",*ave);
}
ANSWER: Hi dear Henry I found too many errors, both logical and syntactical !! I suggest you to learn the basic constructs of C programming Language ( arrays , pointers , loops and so on ..) because i spot most errors with this constructs !
If i intend to correct the errors in this program , then sure you will say "Who's programme is this ?" but i don't like to dis appoint you ! if you wish , specify your question clearly , let me help you out in writing code !
For assistance just post a follow up ( Feel free )
With Thanks and Regards!
Prince M. Premnath
---------- FOLLOW-UP ----------
QUESTION: Hi,
Thanks alot. Exactly my problem. I just don't know where my errors are coming from. Now, i think i have an idea. Vould you write out the code but a bit clearer. such that i can study it and understand. I actually have been grappling with how to use functions, pointers and arrays correclty plus loops. But i am certain that with this problem, all that could be answered. Please, when using any function with pointers as parameters , kindly state why . I know already why but got more confused why it's not working for me. Thanks alot. I will be expecting your own simplified version of the programme.Please, explain more on the logical errors of this programme when writing yours using comments.
AnswerHi dear Henry !
here im presenting you a simple program developed using the help of structures , you can also develop solution for this prob using arrays too instead of using structures ..
#include<stdio.h>
#include<string.h>
/* define student record */
struct student
{
char name[30];
int marks[5]; /* for 5 subjects */
float avg , total;
int grade;
};
void main()
{
int n ; /* how many students */
struct student stud[100];
/* you can store information about a maximum of 100 students*/
int i , j;
float temp;
printf("Enter how many students :");
scanf("%d" , &n);
for( i = 0 ; i < n ; i++)
{
printf("Enter student name :");
scanf("%s" , stud[i].name);
printf("Enter student grade :");
scanf("%d" , &stud[i].grade);
printf("Enter 5 subject mark :");
for(j = 0 ; j<5 ; j++)
scanf("%d" , &stud[i].marks[j]);
/* calculate total and average for that particular student */
temp = 0;
for( j = 0 ; j < 5 ; j++)
temp += stud[i].marks[j];
stud[i].total = (float)temp;
stud[i].avg = (float)temp/5.0;
}
printf("Print student info :\n");
for( i = 0 ; i < n ; i++)
{
printf(" student name :%s\n" , stud[i].name);
printf(" student grade :%d\n" , stud[i].grade);
printf("5 subject mark :");
for(j = 0 ; j <5 ; j++)
printf(" %d" , stud[i].marks[j]);
printf("Total = %f\n" , stud[i].total);
printf("Average %f\n" , stud[i].avg);
}
}
if you spot any bugs with this prog kindly report it to me , feel free to post follow ups
Thanks and Regards !
Prince M. Premnath