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;
}
thunderrollsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
fridomConnect With a Mentor Commented:
GetAve prototyp:
double GetAve (const student_t student, int size);

but later implemented with:
double GetAve(student_t student[], int size)

the same holds for
PrintAbove

So one time you hand over one structure on the other side you hand over an array of structures. Of course those things are not the same.

Friedrich
0
 
Khanh DoanSenior DeveloperCommented:
How come I can't submit my code ?????????
Error : Question not found (ID : 0) ???????????
0
 
Khanh DoanConnect With a Mentor Senior DeveloperCommented:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

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

void ReadArrays (struct student_t student[], int*size);
void PrintArrays (struct student_t student[], int size);
double GetAve (struct student_t student[], int size);
void PrintAbove (struct student_t student[], int size, double ave);
void sort_name (struct student_t student[], int size, char*alphap[]);
void sort_test (struct 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 ()
{
    struct 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(struct 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 (struct 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(struct 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 (struct 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 (struct student_t student[], int size, char *alphap[])
{
      int fill;
      int index_of_sort;
      char * temp, name[100];
      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(struct 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;
}

I don't understand what do you do in get_min_range2(). And student is a struct but in get_min_range2(), you declare it as a array.
Bonmat86.
0
Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

 
thunderrollsAuthor Commented:
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.
0
 
thunderrollsAuthor Commented:
not "now" i meant "no errors"
0
 
thunderrollsAuthor Commented:
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;
}

0
 
Khanh DoanSenior DeveloperCommented:
Cool, I try to help you but I have a bit misunderstand. Sorry.
Bonmat86.
0
All Courses

From novice to tech pro — start learning today.