Mar 30, 2009 at 1:44am UTC
I'm pretty new to C and it seems like the only thing that is breaking my code is my lack of knowledge of pointers. Here is my code: At the bottom is my grades.txt file. I would appreciate any kind of help...thanks
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
main()
{
FILE* fipoint;
double StudentGrades[20][3];
char StudentNames[20][9];
int x = 0,studentnum1;
double* grades;
char* names;
grades=StudentGrades;
names=StudentNames;
void minimum(char names_array[20][9], double grades_array[20][3]);
void maximum(char names_array[20][9], double grades_array[20][3]);
void average(int studentnum,double grades_array[20][3]);
/************************Task 1&2*************************************/
fipoint=fopen("grades.txt","r");
if(fipoint==NULL)
{
printf("Cannot open file!\n");
}
else
{
while(fscanf(fipoint,"%s",&StudentNames[x])>0)
{
fscanf(fipoint,"%lf", &StudentGrades[x][0]);
fscanf(fipoint,"%lf", &StudentGrades[x][1]);
fscanf(fipoint,"%lf", &StudentGrades[x][2]);
printf("%s %lf %lf %lf\n", StudentNames[x], StudentGrades[x][0],
StudentGrades[x][1], StudentGrades[x][2]);
x++;
}
fclose(fipoint);
}
/********************Task 3**************************************/
minimum((char)StudentNames,(double)StudentGrades);
maximum((char)StudentNames,(double)StudentGrades);
/********************Task 5**************************************/
printf("Which students average would you like to see?(1-20)\n\n");
scanf("%d",&studentnum1);
average(studentnum1,(double)StudentGrades);
system("pause");
}
/*****************************Minimum Function*********************************/
void minimum(char names_array[20][9], double grades_array[20][3])
{
int row=0,colmn=0,minrow=0;
int count=0;
double mini=0;
while(row<20){
if (grades_array[row][colmn]<=grades_array[row][colmn+1]){
mini==grades_array[row][colmn];
colmn++;
}
else {
mini==grades_array[row][colmn+1];
colmn++;
}
if(colmn==1){
row++;
colmn=0;
}
}
printf("Student %d had the lowest grade of %lf\n\n",minrow+1,mini);
}
/*****************************Maximum Function*********************************/
void maximum(char names_array[20][9], double grades_array[20][3])
{
int row2=0,colmn2=0,maxrow=0;
int count2=0;
double max=0;
while(row2<20){
if (grades_array[row2][colmn2]>=grades_array[row2][colmn2+1]){
max==grades_array[row2][colmn2];
colmn2++;
}
else {
max==grades_array[row2][colmn2+1];
colmn2++;
}
if(colmn2==1){
row2++;
colmn2=0;
}
}
printf("Student %d had the lowest grade of %lf\n\n",maxrow+1,max);
}
/**************************Average Function***********************************/
void average(int studentnum,double grades_array[20][3])
{
float aver, high, middle, low;
while(studentnum<1 || studentnum>20){
printf("This student does not exist");
}
if(grades_array[studentnum][0]>=grades_array[studentnum][1] && grades_array[studentnum][0]>=grades_array[studentnum][2]){/*****If first column is greatest**/
high=grades_array[studentnum][0]*.40;
if(grades_array[studentnum][1]>=grades_array[studentnum][2]){
middle=grades_array[studentnum][1]*.35;
low=grades_array[studentnum][2]*.25;
}
else{
middle=grades_array[studentnum][2]*.35;
low=grades_array[studentnum][1]*.25;
}
}
else if(grades_array[studentnum][1]>grades_array[studentnum][0] && grades_array[studentnum][1]>grades_array[studentnum][2]){/*****If second column is greatest**/
high=grades_array[studentnum][1]*.40;
if(grades_array[studentnum][0]>grades_array[studentnum][2]){
middle=grades_array[studentnum][0]*.35;
low=grades_array[studentnum][2]*.25;
}
else{
middle=grades_array[studentnum][2]*.35;
low=grades_array[studentnum][0]*.25;
}
}
else if(grades_array[studentnum][2]>grades_array[studentnum][0] && grades_array[studentnum][2]>grades_array[studentnum][1]){/*****If third column is greatest**/
high=grades_array[studentnum][2]*.40;
if(grades_array[studentnum][0]>grades_array[studentnum][1]){
middle=grades_array[studentnum][0]*.35;
low=grades_array[studentnum][1]*.25;
}
else{
middle=grades_array[studentnum][0]*.35;
low=grades_array[studentnum][1]*.25;
}
}
aver=high+middle+low;
printf("Student %d had and an average of %f",studentnum,aver);
}
Student1 77 68 86
Student2 50 60 35
Student3 45 87 90
Student4 60 87 90
Student5 34 56 4
Student6 1 2 3
Student7 67 75 81
Student8 23 45 90
Student9 1 50 100
Student10 34 65 86
Student11 38 98 78
Student12 67 54 97
Student13 99 98 87
Student14 17 71 7
Student15 64 86 87
Student16 56 78 65
Student17 23 45 76
Student18 69 96 87
Student19 45 78 88
Student20 80 81 90
Mar 30, 2009 at 1:52am UTC
Umm... what is the problem you're having? Compiler error? If so what error and what line. Output not what you're expecting? If so what are you expecting and what are you getting. What exactly do you want us to help you with?
Also please put your code in code tags. Makes it so much easier to read. (see # button on the side where you post)
Last edited on Mar 30, 2009 at 1:53am UTC
Mar 30, 2009 at 2:02am UTC
yea im getting compiler errors and warnings. I'm trying to figure out why i'm getting the errors. Its a lot of errors so it might be best to copy and paste and then run. A lot of the errors r the same so i should close to figuring it out