Link to home
Start Free TrialLog in
Avatar of Gem4lyf
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
ASKER CERTIFIED SOLUTION
Avatar of obg
obg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gem4lyf
Gem4lyf

ASKER

Thank you for the advice on reading it...The data file that is being used is a txt file. The thing about it, that I need to do it in assembler but I figured, it would be easier to see it in C first b4 i did it in assembler. So far i have initialized my variables, set up my functions(i have a skeleton of my program). I'm trying to work w/ fscanf because that is the another function I have to call in my assembler language. here's what I have so far...
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 )
{

}


}
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...