Solved

Structure/Arrays- fixing errors

Posted on 2006-06-19
9
255 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
9 Comments
 
LVL 24

Accepted Solution

by:
fridom earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
#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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:thunderrolls
Comment Utility
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
Comment Utility
not "now" i meant "no errors"
0
 

Author Comment

by:thunderrolls
Comment Utility
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
Comment Utility
Cool, I try to help you but I have a bit misunderstand. Sorry.
Bonmat86.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now