Solved

Array of Strings - sorting

Posted on 2006-06-15
16
674 Views
Last Modified: 2010-04-15
This is a revison of a program. First I had to write a program with functions that read a class of up to 30 students' ID and test score (using parallel arrays). Calculating the average of the test and display the students' ID and test score whose test was above the average. Now I am in the process of revising this program so that we now include the students' names into an array of strings. Both IDs and names should be printed. Adding two sort functions so that one function sorts data by names, and the other function sorts data by test scores. Use "gets();".
This is C programming...I have this so far. Teacher says I'm on the right track and I have the right set-up. Can someone help me finish this? I am following an example, so that is why variables may not make sense. I think you need a flag in there somewhere too. Please help.

#include <stdio.h>
#include <string.h>

void ReadArrays(char name[][30], int ID[], int Test[], int *size);
void PrintArrays (char name[][30], int ID[], int Test[], int size);
double GetAve(int Test[], int size, double avg_test);
void PrintAbove(char name[][30], int ID[], int Test[], double avg_test, int size);
void sort_name (char name[][30], int Test[], int size);
void sort_test (char name[][30], int Test[], int size);


int main ()
{
    char name[30][30];
    int Test[30];
    int ID[30];
    int i,size;
    double avg_test=0;

    ReadArrays (name, ID, Test, &size);
    PrintArrays(name, ID, Test, size);
    avg_test=GetAve(Test, size, avg_test);
    sort_name(name, Test, size);
    sort_test(name, Test, size);
    printf("\nThe average test score is %.2f\n", avg_test);

    PrintAbove (name, ID, Test, avg_test, size);

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

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



}
double GetAve(int Test[], int size, double test_avg)
{
     int i=0;
     double total_test = 0;
     for (i=0; i<size; i++)
         total_test += Test[i];
     test_avg = (double) total_test/(double) size;

   return test_avg;
}
void PrintAbove (char name[][30], int ID[], int Test[], double avg_test, int size )
{   int i=0;
    printf("Students Above Average... \n");
    printf("Name          ID          Test Score \n");
    printf("----         ----        -----------\n");
    for (i=0; i < size; i++)
     {    if (Test[i] > avg_test)
              puts(name[i]);
              printf("%d        %d \n", ID[i], Test[i]);
     }
}
void sort_name (char name[][30], int Test[], int size)
{
  int fill,size, Test;
  int index_of_min,mid;
  char * temp;
  char * alphap[30];
  char * name[30][30];
  int i;
  double tempmid;
  int test[30];

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

  }
  for (fill = 0; fill < size - 1; ++fill)
  {
    index_of_min = get_min_range(alphap, fill, size, size - 1);
    if (fill != index_of_min)
  {
        temp = alphap[index_of_min];
        alphap[index_of_min] = alphap[fill];
        alphap[fill] = temp;
        tempmid = mid[index_of_min];
        mid[index_of_min] = mid[fill];
        mid[fill] = tempmid;
   }
  }
  printf("Sorted by Names:\n");
  for (i=0; i < size; i++)
       printf("%-25s %d\n", alphap[i], *mid[i]);
  }
int sort_test (char *alphap[], int first, int last)
  {
   int i;
   int small = first;
   for (i = first; i <= last; i++)
       if (strcmp(alphap[small], alphap[small] > 0)
           small = i;

           return small;
     }

0
Comment
Question by:thunderrolls
  • 8
  • 3
  • 3
  • +2
16 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16917399
>>    int i,size;
Your i is unferenced you can delete it
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16917405
>> void sort_name (char name[][30], int Test[], int size)
>> {
>>  int fill,size, Test;
You function parameter contains a variable size.
In the next line you are declaring again. You better delete the next one.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16917408
Same with Test.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16917412
Same with name
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16917419
>>   alphap[i] = &name[i];
>>   test[i] = &Test[i];

It should have been
alphap[i] = name[i];
test[i] = Test[i];

0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16917420
Your this function is missing
get_min_range(...)
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16917426
Actually there are lots of compilation error.
Why don't you compile and find the compilation errors
and post the code when it is error free.
0
 

Author Comment

by:thunderrolls
ID: 16917676
can you help me with the sorting of the names and the test scores, that's really where I'm clueless...I can fix errors, but the sorting i dont understand for this program...
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16917821
Fix the errors first.
0
 
LVL 24

Accepted Solution

by:
fridom earned 500 total points
ID: 16918046
fflush(stdin) invoked undefined behaviour
*never* use gets always use fgets
and it seems you ahve to many indirections for some variables.


sorting can be done with the qsort function.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void print_str_array(char arr[][20], size_t len) {
  int i = 0;
  for (i = 0; i < len; i++){
    printf("%s ", arr[i]);
  }
  putchar('\n');
}

int main(void) {
  char to_sort[3][20];
  strcpy(to_sort[0], "one");
  strcpy(to_sort[1], "two");
  strcpy(to_sort[2], "three");


  printf("array before sort: ");
  print_str_array(to_sort, 3);
  qsort(to_sort, 3, 20, strcmp);
  printf("array after sort: ");
  print_str_array(to_sort, 3);

 

  return 0;

}

Friedrich
0
 
LVL 1

Expert Comment

by:tushar_comp
ID: 16925479
hi,
you can use quicksort , it is fast, u don't have to write ur own function,
and use compare function to do sorting of any data type
u will get lot of ref of quicksort on google search


if its useful
let me know

Tushar
 
0
 
LVL 20

Expert Comment

by:alainbryden
ID: 16925773
really though. Import a library, and you can easily sort the string using qsort(char *arr1, arr1.length, strcmpare(char, char, int, int))
0
 

Author Comment

by:thunderrolls
ID: 16927170
My professor told us to add fflush (stdin) in there. So I guess I have to use it...and also we are suppose to used selction sort i think...we were never taught quick sort.
0
 
LVL 24

Expert Comment

by:fridom
ID: 16928648
Then tell you professor:
Ifstream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

So the behavior is undefind in C.

Friedrich
0
 

Author Comment

by:thunderrolls
ID: 16931212
I emailed my professor and he said we need fflush, he's not helping me much. Thanks for all your help, everyone. I give up.
0
 
LVL 24

Expert Comment

by:fridom
ID: 16932047
fflush is ok but you want it to apply after the printf in this form
fflush(stdout);

Friedrich
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Test against App 49 137
Pointer in one class to member in another 6 121
SQL handling single and double quotes 3 96
C language IDE – Compilers installation 14 71
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…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

810 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