Solved

Structure/Arrays- fixing errors

Posted on 2006-06-19
9
261 Views
Last Modified: 2010-04-15
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;
}
0
Comment
Question by:thunderrolls
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
9 Comments
 
LVL 24

Accepted Solution

by:
fridom earned 150 total points
ID: 16940003
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
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16941203
How come I can't submit my code ?????????
Error : Question not found (ID : 0) ???????????
0
 
LVL 10

Assisted Solution

by:Khanh Doan
Khanh Doan earned 150 total points
ID: 16941204
#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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:thunderrolls
ID: 16947348
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
 

Author Comment

by:thunderrolls
ID: 16947353
not "now" i meant "no errors"
0
 

Author Comment

by:thunderrolls
ID: 16947564
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
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16947706
Cool, I try to help you but I have a bit misunderstand. Sorry.
Bonmat86.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SCANF - LIMIT THE NUMBER OF CHARARACTERS 1 68
List out all word 7 351
C++ :Change value from  DisableCMD registry 4 77
MySQL recovery 7 54
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question