Gem4lyf
asked on
Need help w/ array passing and file streams
I'm doing a program that involves both C and inline assembly. My question is, i'm trying load information from the file, store it in an array, and pass it to several functions w/out changing it so that i may do different things to display info on the screen. I've already set up the dataFile = fopen(filename,"r"); line. The first function is a boolean to actually get that data...Then I am supposed to send it to another fucntion to sort it. I'm not totally sure how to go about this. I have the outline also to both the boolean function and the sort function
Boolean getData( FILE* f, int x[], int* cursize, int maxsize )
void sortData( int x[], int cursize )
Any help would be much appreciated
Boolean getData( FILE* f, int x[], int* cursize, int maxsize )
void sortData( int x[], int cursize )
Any help would be much appreciated
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Wow!? - Score already...?
Anyway, in getData, you'll need to do something like:
*cursize = 0;
while (*cursize < maxsize && fscanf(f, "%d", &x[*cursize]) != EOF)
++*cursize;
In your sortData, use qsort, which is a very useful function. It is not trivial, since it needs a compare function, but it is not that hard either. Just look it up in some docs, or ask again...
Anyway, in getData, you'll need to do something like:
*cursize = 0;
while (*cursize < maxsize && fscanf(f, "%d", &x[*cursize]) != EOF)
++*cursize;
In your sortData, use qsort, which is a very useful function. It is not trivial, since it needs a compare function, but it is not that hard either. Just look it up in some docs, or ask again...
ASKER
int main(int argc, char* argv[])
{
int SECRET_NUM = 100; /* WHAT IS THIS FOR? */
int x[MAX];
int cursize;
int avg, highest, lowest, median;
FILE* dataFile; /* internal file pointer for the file */
char filename[100]; /* string to hold the filename */
Boolean loadOK;
/* Ask the user for a filename, then open the data file.
Be sure to check that the open was successful.
If not, display a message, and terminate the program.
*/
printf("Enter your filename > ");
scanf("%s", filename); /* get name from user */
dataFile = fopen(filename,"r");
if (dataFile == NULL)
{
printf("No such file - program terminated\n\a");
exit(1);
}
else
loadOK = getData( dataFile, x, &cursize, MAX );
/* If the load was not OK, we ran out of room.
Display a message, and terminate the program.
*/
sortData( x, cursize );
displayGrades( x, cursize );
avg = getAvg( x, cursize );
otherStats( x, cursize, &highest, &lowest, &median );
displayStats( cursize, avg, highest, lowest, median );
//printf("\n\nSECRET_NUM SHOULD BE 100: %d\n", SECRET_NUM);
return 0;
Boolean getData( FILE* f, int x[], int* cursize, int maxsize )
{
}
void sortData( int x[], int cursize )
{
}
int getAvg( const int x[], int cursize )
{
}
void otherStats( const int x[], int cursize,
int* highest, int* lowest, int* median )
{
}
void displayStats( int numberOfScores, int avg, int highest,
int lowest, int median )
{
}
void displayGrades( const int x[], int cursize )
{
}
}