We help IT Professionals succeed at work.
Get Started

Array of Strings - sorting

thunderrolls
thunderrolls asked
on
733 Views
Last Modified: 2010-04-15
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;
     }

Comment
Watch Question
CEO/Programmer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 16 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE