Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
#include <stdio.h>
#include <string.h>
void bubble_sort_grades(char [], int); 
int main(void)
{
    int menuswitch=1;
    int amountofstudents;
    int i;
    int z;
    char studentinfo[100];
    char fname[50][100];
    char lname[50][100];
    char grade[50][100];
    printf("Enter Amount of Students: ");
    scanf("%d ", &amountofstudents);
    for (i=0;i<amountofstudents;i++)
    {
        fgets(studentinfo, sizeof(studentinfo), stdin);
        strcpy(fname[i], strtok(studentinfo, " "));
        strcpy(lname[i], strtok(NULL, " "));
        strcpy(grade[i], strtok(NULL, " "));
    }
        while (menuswitch==1)
        {
            int answer;
            printf("Enter 1 for Alphabetical Sort (First Name) \n");
            printf("Enter 2 for Alphabetical Sort (Last Name) \n");
            printf("Enter 3 for Score Sort \n");
            printf("Enter 0 to exit \n");
            printf("Enter choice now: ");
            scanf("%d", &answer);
            if (answer==1)
            {
                bubble_sort_grades(grade,amountofstudents);
                printf("%s\n", grade[0]);
                printf("%s\n", grade[1]);
            }
            if (answer==2)
            {
                printf("In 2 \n");
            }
            if (answer==3)
            {
                printf("In 3 \n");
            }
            if (answer==0)
            {
                printf("Ending Program \n");
                menuswitch=0;
            }
        }
}
void bubble_sort_grades(char grades2[], int amount)
{
    int c, d , t;
    for (c=0; c<(amount); c++)
    {
        for (d=0; d<amount-1; d++)
        {
            if (grades2[c]>grades2[d+1])
            {
                t=grades2[d+1];
                grades2[d+1]=grades2[d];
                grades2[d]=t;
            }
        }
    }
}

Sorry for asking another questions but I need help on bubble sorting. I created a function to bubble sort the grades of students from the input. Yet, when I do this I get only the first grade sorted instead of the array.

   Input:
    John Smith 86
    Victor Jones 76

Output: 68 76

share|improve this question
1  
And what where you expecting as output? –  zubergu Oct 15 '13 at 21:42
    
The Wikipedia article has a good explanation of bubble sort. You should be able to implement yours using the examples there. en.wikipedia.org/wiki/Bubble_sort –  Jim Mischel Oct 15 '13 at 21:46
    
i want it to be in increasing order 76 then 86 instead its just sorting the first string in the array (86) –  Anthony Devoz Oct 15 '13 at 21:51
    
Just to get this to build, I had to change some of your prototypes to char grade[50][100];. Did it build this way for you? I think the code you have here does not match what you are actually using. –  ryyker Oct 15 '13 at 21:55

3 Answers 3

up vote 3 down vote accepted

You have two main problems here.

Problem 1: Incorrect array indexing

As @TWhite already pointed out, the parameter for your bubble-sort function has the wrong type. You've declared your arrays to be of type char[50][100], which means it allocates 50*100 characters as a single large block in memory. If your memory for grade is allocated at baseAddr, then grade[0] is at baseAddr+0, grade[1] is at baseAddr+100, grade[2] is at baseAddr+200, etc. If you don't tell bubble_sort_grades the last dimension of your 2D-array, then it has no way calculate these indices. Changing the signature of bubble_sort_grades to void bubble_sort_grades(char[][100], int) would fix that problem.

Problem 2: You're storing c-strings but treating them like ints

The grade array is an array of c-strings (char*). It stores characters, not ints. That means that this line is totally wrong: if (grades2[c]>grades2[d+1]) (Side note: Notice that you're using c instead of d as the first index, which is also an error). If you want to compare strings, you should instead use strcmp, since comparing two char* values will with the > operator just do a pointer comparison. However, using strcmp requires that all the grades be 2 digits (e.g. 05 instead of 5), otherwise the string "9" will be greater than "80". Since the grades are c-strings, that also means t=grades2[d+1] is totally incorrect since you're storing a char* into an int. You'd need to create a temporary buffer char t[100], then use strcpy instead of copying things around by assignment.


I like @chux's suggestion of using a struct. Using structs has the added benefit of automatically (correctly) handling copying the whole struct when you use the = operator. I was going to suggest something similar, and actually suggest using the built-in qsort routine, but I realize this is probably homework and you may not have covered structs yet. In that case, it's probably easier to just change your grade array to store ints instead of c-strings.

share|improve this answer

A number of issues

Instead of

char fname[50][100];
char lname[50][100];
char grade[50][100];

Use structure

typedef struct {
  char fname[100];
  char lname[100];
  char grade[100];
} Student_t;
Student_t Student[50];

Inside bubble_sort_grades(), usestrcmp() to compare names.

void bubble_sort_grades(Student_t Student[], int amount) {
  int c, d;
  for (c = 0; c < (amount); c++) {
    for (d = 0; d < amount - 1; d++) {
      if (strcmp(Student[d].grade, Student[d+1].grade) > 0) {
        Student_t t;
        t = Student[d];
        Student[d + 1] = Student[d];
        Student[d] = t;
      }
    }
  }
}

There area other significant issues, but this should get the OP going.

share|improve this answer
    
Just nitpicking here, but it's a good idea to keep in mind that the _t suffix for types is reserved by POSIX. It's hardly going to matter in this particular case, but liberal use of _t may lead to problems later. –  dreamlax Oct 15 '13 at 22:21
    
@dreamlax Look like I should check into that. Any reference suggestions? –  chux Oct 15 '13 at 22:23
    
See section 2.2.2 here. EDIT, updated version here (same restriction applies). –  dreamlax Oct 15 '13 at 22:27

One issue is your :

void bubble_sort_grades(char [], int); 

Should be changed to :

void bubble_sort_grades(char *[], int); 

Consider using char *[] for all your arrays.

share|improve this answer
2  
Maybe you meant char (*)[100]? –  zubergu Oct 15 '13 at 21:57
1  
TWhite: I like your ninja cow icon— or should I say ninnyuu! –  DaoWen Oct 15 '13 at 22:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.