Link to home
Start Free TrialLog in
Avatar of thunderrolls
thunderrolls

asked on

Array of Strings - sorting

This is a revison of a program. First I had to write a program with functions that read a class of up to 30 students' ID and test score (using parallel arrays). Calculating the average of the test and display the students' ID and test score whose test was above the average. Now I am in the process of revising this program so that we now include the students' names into an array of strings. Both IDs and names should be printed. Adding two sort functions so that one function sorts data by names, and the other function sorts data by test scores. Use "gets();".
This is C programming...I have this so far. Teacher says I'm on the right track and I have the right set-up. Can someone help me finish this? I am following an example, so that is why variables may not make sense. I think you need a flag in there somewhere too. Please help.

#include <stdio.h>
#include <string.h>

void ReadArrays(char name[][30], int ID[], int Test[], int *size);
void PrintArrays (char name[][30], int ID[], int Test[], int size);
double GetAve(int Test[], int size, double avg_test);
void PrintAbove(char name[][30], int ID[], int Test[], double avg_test, int size);
void sort_name (char name[][30], int Test[], int size);
void sort_test (char name[][30], int Test[], int size);


int main ()
{
    char name[30][30];
    int Test[30];
    int ID[30];
    int i,size;
    double avg_test=0;

    ReadArrays (name, ID, Test, &size);
    PrintArrays(name, ID, Test, size);
    avg_test=GetAve(Test, size, avg_test);
    sort_name(name, Test, size);
    sort_test(name, Test, size);
    printf("\nThe average test score is %.2f\n", avg_test);

    PrintAbove (name, ID, Test, avg_test, size);

   system ("PAUSE");
   return 0;
}
void ReadArrays(char name[][30], int ID[], int Test[], int *size)
{
     int i = 0;
     printf("Please enter student name, ID, and test score (-1 -1 -1 to stop)>\n");
     fflush(stdin);
     gets(name[i]);
     scanf("%d %d", &ID[i],&Test[i]);

     while (ID[i] !=  -1)
     {
         i++;
         printf("Please enter student name, ID, and test score>\n");
         fflush(stdin);
         gets(name[i]);
         scanf("%d %d", &ID[i], &Test[i]);
     }
      *size = i;
}
void PrintArrays (char name[][30], int ID[], int Test[], int size)
{
    int i;
    for (i=0; i < size; i++)
    puts(name[i]);
    printf("ID %d has test score of %d \n", ID[i], Test[i]);
    printf("The class has %d student(s)\n", size);



}
double GetAve(int Test[], int size, double test_avg)
{
     int i=0;
     double total_test = 0;
     for (i=0; i<size; i++)
         total_test += Test[i];
     test_avg = (double) total_test/(double) size;

   return test_avg;
}
void PrintAbove (char name[][30], int ID[], int Test[], double avg_test, int size )
{   int i=0;
    printf("Students Above Average... \n");
    printf("Name          ID          Test Score \n");
    printf("----         ----        -----------\n");
    for (i=0; i < size; i++)
     {    if (Test[i] > avg_test)
              puts(name[i]);
              printf("%d        %d \n", ID[i], Test[i]);
     }
}
void sort_name (char name[][30], int Test[], int size)
{
  int fill,size, Test;
  int index_of_min,mid;
  char * temp;
  char * alphap[30];
  char * name[30][30];
  int i;
  double tempmid;
  int test[30];

  for(i=0; i < size; i++)
  {
   alphap[i] = &name[i];
   test[i] = &Test[i];

  }
  for (fill = 0; fill < size - 1; ++fill)
  {
    index_of_min = get_min_range(alphap, fill, size, size - 1);
    if (fill != index_of_min)
  {
        temp = alphap[index_of_min];
        alphap[index_of_min] = alphap[fill];
        alphap[fill] = temp;
        tempmid = mid[index_of_min];
        mid[index_of_min] = mid[fill];
        mid[fill] = tempmid;
   }
  }
  printf("Sorted by Names:\n");
  for (i=0; i < size; i++)
       printf("%-25s %d\n", alphap[i], *mid[i]);
  }
int sort_test (char *alphap[], int first, int last)
  {
   int i;
   int small = first;
   for (i = first; i <= last; i++)
       if (strcmp(alphap[small], alphap[small] > 0)
           small = i;

           return small;
     }

Avatar of rajeev_devin
rajeev_devin

>>    int i,size;
Your i is unferenced you can delete it
>> void sort_name (char name[][30], int Test[], int size)
>> {
>>  int fill,size, Test;
You function parameter contains a variable size.
In the next line you are declaring again. You better delete the next one.
Same with Test.
Same with name
>>   alphap[i] = &name[i];
>>   test[i] = &Test[i];

It should have been
alphap[i] = name[i];
test[i] = Test[i];

Your this function is missing
get_min_range(...)
Actually there are lots of compilation error.
Why don't you compile and find the compilation errors
and post the code when it is error free.
Avatar of thunderrolls

ASKER

can you help me with the sorting of the names and the test scores, that's really where I'm clueless...I can fix errors, but the sorting i dont understand for this program...
Fix the errors first.
ASKER CERTIFIED SOLUTION
Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

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
hi,
you can use quicksort , it is fast, u don't have to write ur own function,
and use compare function to do sorting of any data type
u will get lot of ref of quicksort on google search


if its useful
let me know

Tushar
 
really though. Import a library, and you can easily sort the string using qsort(char *arr1, arr1.length, strcmpare(char, char, int, int))
My professor told us to add fflush (stdin) in there. So I guess I have to use it...and also we are suppose to used selction sort i think...we were never taught quick sort.
Then tell you professor:
Ifstream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

So the behavior is undefind in C.

Friedrich
I emailed my professor and he said we need fflush, he's not helping me much. Thanks for all your help, everyone. I give up.
fflush is ok but you want it to apply after the printf in this form
fflush(stdout);

Friedrich