• C

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

thunderrollsAsked:
Who is Participating?
 
fridomConnect With a Mentor CEO/ProgrammerCommented:
fflush(stdin) invoked undefined behaviour
*never* use gets always use fgets
and it seems you ahve to many indirections for some variables.


sorting can be done with the qsort function.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void print_str_array(char arr[][20], size_t len) {
  int i = 0;
  for (i = 0; i < len; i++){
    printf("%s ", arr[i]);
  }
  putchar('\n');
}

int main(void) {
  char to_sort[3][20];
  strcpy(to_sort[0], "one");
  strcpy(to_sort[1], "two");
  strcpy(to_sort[2], "three");


  printf("array before sort: ");
  print_str_array(to_sort, 3);
  qsort(to_sort, 3, 20, strcmp);
  printf("array after sort: ");
  print_str_array(to_sort, 3);

 

  return 0;

}

Friedrich
0
 
rajeev_devinCommented:
>>    int i,size;
Your i is unferenced you can delete it
0
 
rajeev_devinCommented:
>> 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.
0
Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

 
rajeev_devinCommented:
Same with Test.
0
 
rajeev_devinCommented:
Same with name
0
 
rajeev_devinCommented:
>>   alphap[i] = &name[i];
>>   test[i] = &Test[i];

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

0
 
rajeev_devinCommented:
Your this function is missing
get_min_range(...)
0
 
rajeev_devinCommented:
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.
0
 
thunderrollsAuthor Commented:
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...
0
 
rajeev_devinCommented:
Fix the errors first.
0
 
tushar_compCommented:
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
 
0
 
alainbrydenCommented:
really though. Import a library, and you can easily sort the string using qsort(char *arr1, arr1.length, strcmpare(char, char, int, int))
0
 
thunderrollsAuthor Commented:
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.
0
 
fridomCEO/ProgrammerCommented:
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
0
 
thunderrollsAuthor Commented:
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.
0
 
fridomCEO/ProgrammerCommented:
fflush is ok but you want it to apply after the printf in this form
fflush(stdout);

Friedrich
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.