heres my main

int main(){
    
    int hours[50], cnt = 0; 
    char grades[50], gpaForOneClass[50];
    string course[50];  
    float gpa = 0.0, totalHours = 0, gpaSum = 0;
    gradevalue in;
    
    
    while (1)
    {
          cout<<"\nEnter a course, the letter grade received, and the credit hours. "<<endl;
          cin >> course[cnt];
      
          if (course[cnt] == "quit" || course[cnt] == "QUIT")  break;//ends input when quit or QUIT is typed
          
          cin >> grades[cnt];            
          cin >> hours[cnt];
         
          in = convert(in, grades[cnt]);  //converts letter input into right value
          gpaForOneClass[cnt] = grade(in, hours[cnt]);  //gets gpa total for one class
          op_grade(in);                //outputs grade letter
          gpaSum += gpaForOneClass[cnt];  //totals up gpa
          totalHours = totalHours + hours[cnt];  //totals up hours
          cnt = cnt + 1;
       
    }
          
    
    
    gpa = gpaAvg(gpaSum, totalHours);  //calls gpa function
    header();  //calls header of table
    
    for (int b = 0; b < cnt; b++)  //puts the information into the table
    {
        bsort(course, cnt);           // calls bsort function
        cout<<course[b]<<" "<<hours[b]<<" "<<grades[b]<<endl;
    }
    
    cout<<"The total GPA is "<<gpa<<" And you attempted "<<totalHours<<" hours."<<endl; 
    cin>>cnt;
    
    return 0;
}

and here is my bsort function:

void bsort(string course[], const int cnt)   //This bubblesort is used to put the courses into
{                                 //alphabetical order
      int i = 0, j = 0;
      string temp;
      
      for (i = 1; i < cnt; i++);
      {
         for (j = 0; j < cnt - i; j++);
         {
             if (course[j] > course[j+1])
             {
                       temp = course[j];
                       course[j] = course[j+1];
                       course[j+1] = temp;
             }
         }
      }        
}

The problem is that it will sort the course string. However, i need the attatched parallel arrays to be sorted along with it.

Example Input:

phys112 C 3
cs110 A 4

Example Output:

cs110 C 3
phys112 A 4

I need the grades and the hours to go along with the corresponding input.

I have no clue what I need to do now to do this that wouldn't be a pain in the butt. I know I could save it all into one array, get them output sorted, then break it back into seperate arrays....

Please tell me there is an easier way than this because even though I know you can do it that way, I dont know exactly how to make the code for that.

Thanks.

>Please tell me there is an easier way than this
Put your data into a structure, create an array of structure objects, and sort that:

struct course {
  string name;
  int hours;
  char gpa;
  char grades;
};

course courses[50];

...

bsort(courses, cnt);

Then you need to change bsort to work with your structure:

void bsort(course courses[], const int cnt)
{
      int i = 0, j = 0;
      course temp;
      
      for (i = 1; i < cnt; i++);
      {
         for (j = 0; j < cnt - i; j++);
         {
             if (courses[j].name > courses[j+1].name)
             {
                       temp = courses[j];
                       courses[j] = courses[j+1];
                       courses[j+1] = temp;
             }
         }
      }        
}

Thanks narue, but that returned a ton of errors for me...
and im not sure but i dont think we were supposed to use structs yet..
my other program thats due thursday uses them and classes.... Its a continuation of this program.

i did this instead with my sort:

void bsort(string courses[], int hours[], char grades[], const int cnt)   //This bubblesort is used to put the courses into
{                                 //alphabetical order
      int i = 0, j = 0, temper;
      string temp;
      char tmp;
      
      for (i = 0; i < cnt; i++);
      {
         for (j = 0; j < cnt - i; j++);
         {
            
             if (courses[j] > courses[j+1])
                 {
                       temp = courses[j];
                       courses[j] = courses[j+1];
                       courses[j+1] = temp;
                       
                       temper = hours[j];
                       hours[j] = hours[j+1];
                       hours[j+1] = temper;
                       
                       tmp = grades[j];
                       grades[j] = grades[j+1];
                       grades[j+1] = tmp;
                 }  
            
         }      
             
         
      }        
}

and where i call it:

for (int b = 0; b <= cnt ; b++)
    {
        bsort(courses, hours, grades, cnt);           // calls bsort function
    }

cnt is the variable i use to get the elements of the array.

I am curious how to run my loops for it to do more than just the swap of array element 0 and array element 1. I know i could further expand by doing more if statements j+1, j+2, j+3 etc. but it seems there is a sorter way of this...

I tried some other possibilities that ranged from just elements 1 and 2 swapping to getting stuck in endless loops to nothing happening but none for the exact output i was desiring.

Thanks.

>but that returned a ton of errors for me...
That's because you just pasted the snippets I gave and expected it to work.

>i dont think we were supposed to use structs yet.
Then you have to do it the hard way. Sort the courses array and mirror the swaps on all of your other arrays.

Thanks everybody for the help.

I did have to swap all of them narue, all 3 arrays.

And i figured out why it wouldnt sort.

I needed a while loop instead of one of the for loops...

Now its on to changing things to classes...

Classes are a pain arent they? im gettin tons of messages but I am gonna work with it a bit more before i post to see if i can get anywhere...

:)

>Classes are a pain arent they?
Classes are a pain in that they require more "glue" code to work than is required without them. Once the idioms are learned, classes are a useful tool when used judiciously.

>Please tell me there is an easier way than this
Put your data into a structure, create an array of structure objects, and sort that:

struct course {
  string name;
  int hours;
  char gpa;
  char grades;
};

course courses[50];

...

bsort(courses, cnt);

Then you need to change bsort to work with your structure:

void bsort(course courses[], const int cnt)
{
      int i = 0, j = 0;
      course temp;
      
      for (i = 1; i < cnt; i++);
      {
         for (j = 0; j < cnt - i; j++);
         {
             if (courses[j].name > courses[j+1].name)
             {
                       temp = courses[j];
                       courses[j] = courses[j+1];
                       courses[j+1] = temp;
             }
         }
      }        
}

what does that nested loop do?

what is the output going to be like in that nested loop with the if statement?

First, I think those semicolons should be removed.

for (i = 1; i < cnt; i++);
      {
         for (j = 0; j < cnt - i; j++);
         {

Second, why not throw in some debugging output statements and see for yourself?

#include <stdio.h>

void show(const int *array, int cnt)
{
   int i;
   for (i = 0; i < cnt; ++i)
   {
      printf("%2d ", array[i]);
   }
   putchar('\n');
}

void asort(int *array, int cnt)
{
   int i, j, temp;
   for ( i = 1; i < cnt; i++ )
   {
      printf("i = %2d: ", i);
      show(array, cnt);
      for ( j = 0; j < cnt - i; j++ )
      {
         printf(" j = %2d: %2d > %2d?\n", j, array[j], array[j+1]);
         if ( array[j] > array[j+1] )
         {
            printf("swapping %d with %d\n", array[j], array[j+1]);
            temp = array[j];
            array[j] = array[j+1];
            array[j+1] = temp;
         }
      }
   }
   printf("i = %2d: ", i);
   show(array, cnt);
}

int main(void)
{
   int data[] = {42,-1,0,5,3,9,-7,1,20,13};
   asort(data, sizeof data / sizeof *data);
   return 0;
}

Second, why not throw in some debugging output statements and see for yourself?

how is that done?

how is that done?

::smacks forehead:: Look up. ;) I'll edit and hightlighting to my last post.

[edit]The output of the code from my last post is as follows.

i =  1: 42 -1  0  5  3  9 -7  1 20 13 
 j =  0: 42 > -1?
swapping 42 with -1
 j =  1: 42 >  0?
swapping 42 with 0
 j =  2: 42 >  5?
swapping 42 with 5
 j =  3: 42 >  3?
swapping 42 with 3
 j =  4: 42 >  9?
swapping 42 with 9
 j =  5: 42 > -7?
swapping 42 with -7
 j =  6: 42 >  1?
swapping 42 with 1
 j =  7: 42 > 20?
swapping 42 with 20
 j =  8: 42 > 13?
swapping 42 with 13
i =  2: -1  0  5  3  9 -7  1 20 13 42 
 j =  0: -1 >  0?
 j =  1:  0 >  5?
 j =  2:  5 >  3?
swapping 5 with 3
 j =  3:  5 >  9?
 j =  4:  9 > -7?
swapping 9 with -7
 j =  5:  9 >  1?
swapping 9 with 1
 j =  6:  9 > 20?
 j =  7: 20 > 13?
swapping 20 with 13
i =  3: -1  0  3  5 -7  1  9 13 20 42 
 j =  0: -1 >  0?
 j =  1:  0 >  3?
 j =  2:  3 >  5?
 j =  3:  5 > -7?
swapping 5 with -7
 j =  4:  5 >  1?
swapping 5 with 1
 j =  5:  5 >  9?
 j =  6:  9 > 13?
i =  4: -1  0  3 -7  1  5  9 13 20 42 
 j =  0: -1 >  0?
 j =  1:  0 >  3?
 j =  2:  3 > -7?
swapping 3 with -7
 j =  3:  3 >  1?
swapping 3 with 1
 j =  4:  3 >  5?
 j =  5:  5 >  9?
i =  5: -1  0 -7  1  3  5  9 13 20 42 
 j =  0: -1 >  0?
 j =  1:  0 > -7?
swapping 0 with -7
 j =  2:  0 >  1?
 j =  3:  1 >  3?
 j =  4:  3 >  5?
i =  6: -1 -7  0  1  3  5  9 13 20 42 
 j =  0: -1 > -7?
swapping -1 with -7
 j =  1: -1 >  0?
 j =  2:  0 >  1?
 j =  3:  1 >  3?
i =  7: -7 -1  0  1  3  5  9 13 20 42 
 j =  0: -7 > -1?
 j =  1: -1 >  0?
 j =  2:  0 >  1?
i =  8: -7 -1  0  1  3  5  9 13 20 42 
 j =  0: -7 > -1?
 j =  1: -1 >  0?
i =  9: -7 -1  0  1  3  5  9 13 20 42 
 j =  0: -7 > -1?
i = 10: -7 -1  0  1  3  5  9 13 20 42

I dunno dave...i know im not on your level when it comes to coding
but you lose me when i ask you for an explanation and you give me more code

what do you mean by debugging statements (in english not code please)?

and

How do they work ?

I dunno dave...i know im not on your level when it comes to coding
but you lose me when i ask you for an explanation and you give me more code

Sorry, I guess I didn't know where you were coming from. And I guess I understand code better than explanations about code.

what do you mean by debugging statements (in english not code please)?

By "debugging statements" I mean statements like cout or printf that you use to show the value of some variable(s) at specific locations in the code.

How do they work ?

They print information that you want to know.


Please compile and run the code I posted earlier to see what I mean. When one finishes with the debugging phase, these printf lines would be deleted.

Please compile and run the code I posted earlier to see what I mean. When one finishes with the debugging phase, these printf lines would be deleted.

Seriously, I wish I could dave, but I don't own a computer and never have. I use public library computers right now. It sucks, but you have to do what you can, you know?

It won't be forever though as soon as these two degrees i got start paying off I'll be able to buy my own ;)

Oh thanks for the explanation

im still a little confused mainly by this

printf("%2d ", array[i]);

i've asked you this before but what does the %2d or %d mean and do?

Seriously, I wish I could dave, but I don't own a computer and never have. I use public library computers right now. It sucks, but you have to do what you can, you know?

Oh, yeah. Sorry, I'm not feeling well today.

im still a little confused mainly by this

printf("%2d ", array[i]);

i've asked you this before but what does the %2d or %d mean and do?

The %d means print an int, The %2d means print an int using at least 2 characters -- padded with leading spaces. I just did that to make things line up neater.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.