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:
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.
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>
usingnamespace 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.