Thread: C++ bubblesort and structure help

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    Question C++ bubblesort and structure help

    Hi,

    at school we had to make a program where you first set a memberlist of 10members in a structure ( name, age, shoemeasure and gender) then we had to bubblesort or shellsort according to age or shoemeassure. but nobody could find the right solution
    i hope some of you guys know the right answer
    we work with the DEV-C++ v4.9.9.2 compiler.

    btw sry for my bad english

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    What have you tried so far?
    What about what you've tried isn't working?
    What would you like to try within the program that you don't know how to do/use?
    What of all of the above answers can't you find online, in your textbook, or in your library?

    No one is going to code this for you - ask better, more specific questions, and help will be more forthcoming...
    There is a difference between tedious and difficult.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    2
    here's the code i had... sry it's in dutch i'm from belgium...
    i hope you guys could find the mistakes in it

    Code:
    #include <iostream>
    #include <conio.h>
    #include <string.h>
    using namespace std;
    const int MAXSIZE = 10;
    
    void bubbleSort(int getallen[10], int size);
    void bubble_Sort(int getallen[10], int size);
    void swap(int& x, int& y);
    
    struct ledenlijst 
    {
        char naam [10][30];
        char voornaam [10][10];
        int leeftijd[10];
        int schoenmaat[10];
        char ras[10][10];
        
    };
    
    int main ()
    {
        int i=0;
        ledenlijst a;
        
        for (i=1; i<11; i++)
        {
            cout<<i<<"\n:  Naam: ";
            cin>>a.naam[i];
            cout<<"   Tel.nr. ";
            cin>>a.voornaam[i];
            cout<<"   leeftijd: ";
            cin>>a.leeftijd[i];
            cout<<"   Schoenmaat: ";
            cin>>a.schoenmaat[i];
            cout<<"   Ras :";
            cin>>a.ras[i];
        }
                   
        system ("PAUSE");   
         
        cout<<"Kies sorteren via leeftijd('L') of via schoenmaat('S') ";
         if ( getch() == 'L' )
         {
              bubbleSort( a.leeftijd , MAXSIZE);
              
              for (int i=1; i<11; i++)
              {
                  cout<<"\n"<<i<<"\n:  Naam: "<< a.naam[i]<<endl;
                  cout<<"   Tel.nr. "<< a.voornaam[i]<<endl;
                  cout<<"   leeftijd: "<< a.leeftijd[i]<<endl;
                  cout<<"   Schoenmaat: "<< a.schoenmaat[i]<<endl;
                  cout<<"   Ras :"<< a.ras[i]<<endl;
                  getch();
              }
              
         }
         if ( getch() == 'S' )
         {
              bubble_Sort(a.schoenmaat, MAXSIZE);
              
              for (int i=1; i<11; i++)
              {
                  cout<<"\n"<<i<<"\n:  Naam: "<< a.naam[i]<<endl;
                  cout<<"   Tel.nr. "<< a.voornaam[i]<<endl;
                  cout<<"   leeftijd: "<< a.leeftijd[i]<<endl;
                  cout<<"   Schoenmaat: "<< a.schoenmaat[i]<<endl;
                  cout<<"   Ras :"<< a.ras[i]<<endl;
              }
                           
         }
         getch();
    }      
    
         
    
     
    void bubble_Sort(int schoenmaat[], int size)
    {
         int last = size - 2;
         int isChanged = 1;
    
         while (last >= 0 && isChanged)
         {
               isChanged = 0;
    
               for (int i = 0; i <= last; i++)
                     if (schoenmaat[i] < schoenmaat[i+1])
                     {
                           swap(schoenmaat[i], schoenmaat[i+1]);
                           isChanged = 1;
                     }
               last--;
         }
    }
    
    void bubbleSort(int leeftijd[], int size)
    {
         int last = size - 2;
         int isChanged = 1;
    
         while (last >= 0 && isChanged)
         {
               isChanged = 0;
    
               for (int i = 0; i <= last; i++)
                     if (leeftijd[i] < leeftijd[i+1])
                     {
                           swap(leeftijd[i], leeftijd[i+1]);
                           isChanged = 1;
                     }
               last--;
         }
    }
    
    void swap(int& x, int& y)
    {
         int temp;
         temp = x;
         x = y;
         y = temp;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Standard deviation, bubblesort and others
    By peter_pan in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2002, 10:24 PM