Link to home
Start Free TrialLog in
Avatar of thunderrolls
thunderrolls

asked on

Structure/Arrays- fixing errors

II'm very new to C programming, but this is also my last program...I have this program pretty much written, i just have a several errors which I can't fix for some reason. The compiler shows:

incompatible type for argument 1 of 'GetAve'
incompatible type for argument 1 of 'PrintAbove'
warning:passing arg 3 of 'sort_test' from incompatible pointer type
warning: passing arg 1 of 'gets' discards qualifiers from pointer-target type
subscripter value is neither array nor pointer
warning:passing arg 1 of 'gets' discard qualifiers from pointer target type
conflicting types for 'GetAve'
previous delcaration of "GetAve'
conflicting types for 'PrintAbove'
previous declaration of 'PrintAbove'
'name' undeclared (first use in this function)
(Each undeclared identifier is reported only once...
warning: assignment of read-only member 'Test'
warning: assignment of read-only memeber 'Test'
warning: assignment of read-only member 'ID'
warning: data definition has no type or storage class




*******************************************************************************
Revising prior program assignment of  6/7/06 (Using array of structure)
Program takes student names, ID numbers and corresponding test scores;
calculates the average, sorts by name and grades; displays the students'
names and ID numbers who exceeded the class average.
*******************************************************************************/
#include <stdio.h>
typedef struct
{
        int ID;
        char name[30];
        int Test;
}student_t;

void ReadArrays (const student_t student[], int*size);
void PrintArrays (const student_t student[], int size);
double GetAve (const student_t student, int size);
void PrintAbove (const student_t student, int size, double ave);
void sort_name (const student_t student[], int size, char*alphap[]);
void sort_test (const student_t student[], int size, char*alphap);
int get_min_range (char*alphap[], int first, int last);
int get_min_range2 (int Test[], int first, int last);


int main ()
{
    student_t student[30];
    int size;
    double ave;
    char*alphap[30];

    ReadArrays (student, &size);
    PrintArrays (student, size);
    ave = GetAve (student, size);
    printf("\nThe average test score is %.2f\n", ave);
    PrintAbove(student, size, ave);
    sort_name(student, size, alphap);
    sort_test(student, size, alphap);

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

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

     for (i=0; i<size; i++)
         total_test += student[i].Test;

     ave = total_test/size;

   return ave;
}
void PrintAbove (const student_t student[], int size, double ave)
{
    int i=0;
    printf("Students Above Average... \n");
    printf("Name                   ID               Test Score \n");
    printf("----                  ----              ----------\n");
    for (i=0; i < size; i++)
     {
         if (student[i].Test > ave)
         printf("%-15s       %d         %10d \n", student[i].name, student[i].ID, student[i].Test);
     }
}
void sort_name (const student_t student[], int size, char*alphap[])
{
     int fill;
     int index_of_sort;
     char * temp;
     int i, tempID;
     int tempmid;

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

     for (fill = 0; fill < size-1; fill++)
          index_of_sort = get_min_range(alphap, fill, size-1);

          if (fill != index_of_sort)
          {
           temp = alphap[index_of_sort];
           alphap[index_of_sort] = alphap[fill];
           alphap[fill] = temp;
           tempmid = student[index_of_sort].Test;
           student[index_of_sort].Test = student[fill].Test;
           student[fill].Test = tempmid;
           tempID = student[index_of_sort].ID;
           student[index_of_sort].ID = student[fill].ID;
           student[fill].ID = tempID;
   }
  }
  printf("Sorted by Names:\n");
  printf("Name                   ID               Test Score \n");
  printf("----                  ----              ----------\n");

  for (i=0; i < size; i++)
       printf("%-15s       %d         %10d\n", student[i].name, student[i].ID, student[i].Test);
  }
int get_min_range (char*alphap[], int first, int last)
{
     int i;
     int small = first;
     for (i = first; i <= last; i++)
         if (strcmp(alphap[small], alphap[i]) > 0)
             small = i;

           return small;
}
void sort_test (const student_t student[], int size, char*alphap[])
{
     int fill, index_of_sort, i;
     int tempmid, tempID;
     char *temp;

     for (fill = 0; fill < size-1; fill++)
          index_of_sort = get_min_range2(student, fill, size-1);

          if (fill != index_of_sort)
          {
              temp = alphap[index_of_sort];
              alphap[index_of_sort] = alphap[fill];
              alphap[fill] = temp;
              tempmid = student[index_of_sort].Test;
              student[index_of_sort].Test = student[fill].Test;
              student[fill].Test = tempmid;
              tempID = student[index_of_sort].ID;
              student[index_of_sort].ID = student[fill].ID;
              student[fill].ID = tempID;
   }
}
  printf("Sorted by Grades:\n");
  printf("Name                   ID               Test Score \n");
  printf("----                  ----              ----------\n");

  for (i=0; i < size; i++)
       printf("%-15s       %d         %10d\n", alphap[i],student[i].ID, student[i].Test);
}
 int get_min_range2 (int Test[], int first, int last)
{
      int i;
      int small = first;
      for (i = first; i <= last; i++)
          if (Test[small] < Test[i])
              small = i;

           return small;
}
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
How come I can't submit my code ?????????
Error : Question not found (ID : 0) ???????????
SOLUTION
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 thunderrolls
thunderrolls

ASKER

Bonmat86, when I run the program you posted, there is now errors however the student names in sorted by grade are displayed as symbols, and the sorting it inccorrect. I'll try to play around with it.
not "now" i meant "no errors"
I fixed my program up. I have no more errors just a bunch of warning. The program runs, however the names do not sort under "sorted by names" the IDs and grades do.

here it is: I didn't make many changes that bonmat86 said because my professor told us to follow an example and did some of this program with us....
so from what I have now, I just need to make the names sort. Here it is:

#include <stdio.h>

typedef struct
{
        int ID;
        char name[30];
        int Test;
} student_t;

void ReadArrays (const student_t student[], int*size);
void PrintArrays (const student_t student[], int size);
double GetAve (const student_t student[], int size);
void PrintAbove (const student_t student[], int size, double ave);
void sort_name (const student_t student[], int size, char*alphap[]);
void sort_test (const student_t student[], int size, char*alphap[]);


int main ()
{
    student_t student[30];
    int size;
    double ave;
    char*alphap[30];

    ReadArrays (student, &size);
    PrintArrays (student, size);
    ave = GetAve (student, size);
    printf("\nThe average test score is %.2f\n", ave);
    PrintAbove(student, size, ave);
    sort_name(student, size, alphap);
    sort_test(student, size, alphap);

    system ("PAUSE");
    return 0;
}

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

     while (student[i].ID !=  -1)
     {
          i++;
          printf("Please enter student name, ID, and test score>\n");
          fflush(stdin);
          gets(student[i].name);
          scanf("%d %d", &student[i].ID, &student[i].Test);
     }
     *size = i;
}

void PrintArrays (const student_t student[], int size)
{
     int i;
     printf("\nID:  Name:                   Test Score:\n");
     printf("      ------                   -----------\n");

     for (i=0; i < size; i++)
     {
          printf("%-s, %d        %d \n", student[i].name, student[i].ID, student[i].Test);
     }
     printf("\nThe class has %d student(s)\n", size);
}

double GetAve(const student_t student[], int size)
{
     int i, total_test = 0;
     double ave=0;

     for (i=0; i < size; i++)
          total_test += student[i].Test;

     ave = total_test/size;

     return ave;
}

void PrintAbove (const student_t student[], int size, double ave)
{
    int i;
    printf("Students Above Average:\n");
    printf("Name                   ID               Test Score \n");
    printf("----                  ----              ----------\n");

    for (i=0; i < size; i++)
     {
          if (student[i].Test > ave)
          printf("%-15s       %d         %10d \n", student[i].name, student[i].ID, student[i].Test);
     }
}

void sort_name (const student_t student[], int size, char *alphap[])
{
     int fill;
     int index_of_sort;
     char * temp;
     int i, tempID;
     int tempmid;

     for(i=0; i < size; i++)
          alphap[i] = &student[i].name;

     for (fill = 0; fill < size-1; +++fill)
     {
          index_of_sort = get_min_range(alphap, fill, size-1);

          if (fill != index_of_sort)
          {
               temp = alphap[index_of_sort];
               alphap[index_of_sort] = alphap[fill];
               alphap[fill] = temp;
               tempmid = student[index_of_sort].Test;
               student[index_of_sort].Test = student[fill].Test;
               student[fill].Test = tempmid;
               tempID = student[index_of_sort].ID;
               student[index_of_sort].ID = student[fill].ID;
               student[fill].ID = tempID;
          }
     }

     printf("Sorted by Names:\n");
     printf("Name                   ID               Test Score \n");
     printf("----                  ----              ----------\n");

     for (i=0; i < size; i++)
          printf("%-15s       %d         %10d\n", student[i].name, student[i].ID, student[i].Test);
}

int get_min_range (char*alphap[], int first, int last)
{
     int i;
     int small = first;
     for (i = first; i <= last; i++)
          if (strcmp(alphap[small], alphap[i]) > 0)
               small = i;

     return small;
}

void sort_test(const student_t student[], int size, char *alphap[])
{
     int fill, index_of_sort, i;
     int tempmid, tempID;
     char *temp;

     for (fill = 0; fill < size-1; ++fill)
     {
          index_of_sort = get_min_range2(student, fill, size-1);

          if (fill != index_of_sort)
          {
               temp = alphap[index_of_sort];
               alphap[index_of_sort] = alphap[fill];
               alphap[fill] = temp;
               tempmid = student[index_of_sort].Test;
               student[index_of_sort].Test = student[fill].Test;
               student[fill].Test = tempmid;
               tempID = student[index_of_sort].ID;
               student[index_of_sort].ID = student[fill].ID;
               student[fill].ID = tempID;
          }
     }

     printf("Sorted by Grades:\n");
     printf("Name                   ID               Test Score \n");
     printf("----                  ----              ----------\n");

     for (i=0; i < size; i++)
          printf("%-15s       %d         %10d\n", alphap[i], student[i].ID, student[i].Test);
}

int get_min_range2 (const student_t student[], int first, int last)
{
     int i;
     int small = first;
     for (i = first; i <= last; i++)
          if (student[small].Test < student[i].Test)
               small = i;

     return small;
}

Cool, I try to help you but I have a bit misunderstand. Sorry.
Bonmat86.