problem with arrays and my minimum function

Im trying to learn how to use arrays in C and I'm having some trouble. I think my main problem is understanding the use of pointers. I'm just trying to take the min grade in the text file. Here is my source code:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAX_G 60
#define MAX_N 20


main()
{

FILE* fipoint;
double StudentGrades[20][3];
char StudentNames[20][9];
int x = 0,studentnum1;
void minimum(char names_array, double grades_array);


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);

}



minimum((char)StudentNames,(double)StudentGrades);






system("pause");

}
/*****************************Minimum Function*********************************/
void minimum(char names_array, double grades_array)
{
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);

}

This what my grades.txt file looks like:
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
your minimum function's parameters are not arrays, but just a char and a double. You can't truly pass an array, but only a pointer to an array. You'll also need the length, or the ending pointer.
can you give me an example of how you would do it please. thanks
I must admit, I'm a bit confused with your multidimential arrays, so I'll do something simple. These are just two different ways to do the same thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
void average1(double *grades, int length) {
double total = 0;
    for (int i = 0; i < length; ++i)
        total += grades[i];  // pointers to an array can be indexed just as arrays
cout << "average = " << (total / length) << endl;
}
void average2(double *begin, double *end) {
double total = 0;
    for (double *p = begin; p != end; ++p)
        total += *p;  // pointers to an array can be indexed just as arrays
cout << "average = " << (total / (end - begin)) << endl; // (end - begin) might be bad, maybe someone can tell me if it should be avoided.
}
int main() {
     double gradesArray[] = {80.0, 60.0, 70.0, 75.0, 80.0};
     average1(gradesArray, 5);
     average2(gradesArray, gradesArray + 5);
}

notice that pointers can be referenced like arrays with [], and arrays can be passed as pointers.
Last edited on
thanks...hopefully this will help
Topic archived. No new replies allowed.