[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Structure/Arrays- fixing errors

Posted on 2006-06-19
9
Medium Priority
?
265 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
  • 3
  • 3
7 Comments
 
LVL 24

Accepted Solution

by:
fridom earned 600 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 600 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
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!

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

873 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