Clicky

Here is my problem:

Space needs to be reserved for 10 elements.
An exchange sort algorithm is needed to sort the data.
Quit does not need to appear in the displayed results.

Thanks for any advice.
 #include <iostream> #include <string.h> using namespace std;   struct record         {        char lastName[15];            float sales;            };    int main(){     record obj[5];     for(int i=0; i<5; i++){             cout<<"\n\tPlease enter last name(or type quit to display data):";             cin>>obj[i].lastName;               if( strcmp( obj[i].lastName, "quit" ) == 0 ){                          //ListEntries(int num);            break;                }             cout<<"\n\tPlease enter sales:";             cin>>obj[i].sales;                     }      for(int y=0; y<5; y++){           cout<< obj[y].lastName <<  "\t" << obj[y].sales << endl;             }                            system("pause");           return 0;         }                             
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 

Select allOpen in new window

asked 12/13/2011 12:40

talk2eric's gravatar image

talk2eric ♦♦


13 Answers:
Try this:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
#include <iostream>
#include <string.h>
using namespace std;


struct record 
       {
       char lastName[15];    
       float sales;    
       }; 


int main(){
    record obj[5];
    char cmd[15];
    for(int i=0; i<5; i++){
            cout<<"
	Please enter last name(or type quit to display data):";
            cin>>cmd;  
            if( strcmp( cmd, "quit" ) == 0 ){ 
            
           //ListEntries(int num);
           break;    
           }
            strcpy(obj[i].lastName, cmd);
            cout<<"
	Please enter sales:";
            cin>>obj[i].sales;        
            }

    for(int y=0; y<5; y++){
          cout<< obj[y].lastName <<  "	" << obj[y].sales << endl;
            }
           
       
       system("pause");   
       return 0; 
       }
link

answered

StingRaY's gravatar image

StingRaY

I get weird results at the end.
link

answered 2011-12-13 at 20:49:43

talk2eric's gravatar image

talk2eric

How weird? Can you show me the result?
link

answered 2011-12-13 at 20:57:20

StingRaY's gravatar image

StingRaY

That's probably because you don't keep track of how many names were entered.  You always print all 5, even if you only enter 1.  And since you don't clear the structs, you are likely getting a bunch of garbage.
link

answered 2011-12-13 at 20:58:55

ubound's gravatar image

ubound

Here are my inputs, and the displayed results:


        Please enter last name(or type quit to display data):f

        Please enter sales:6

        Please enter last name(or type quit to display data):g

        Please enter sales:4

        Please enter last name(or type quit to display data):h

        Please enter sales:2

        Please enter last name(or type quit to display data):quit
f       6
g       4
h       2
·áfu0¿p 2.92348e+032
        3.92364e-043
Press any key to continue . . .
link

answered 2011-12-13 at 21:05:25

talk2eric's gravatar image

talk2eric

How do you clear the structs?
link

answered 2011-12-13 at 21:11:44

talk2eric's gravatar image

talk2eric

I am thinking I should use num instead of 5, so it would be i<num instead of i<5. num would be incremented according to the number of entries.
link

answered 2011-12-13 at 21:13:02

talk2eric's gravatar image

talk2eric

Try this
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
#include <iostream>
#include <string.h>
using namespace std;

struct record {
	char lastName[15];
	float sales;
};

int main() {
	record obj[5];
	char cmd[15];
	for (int m = 0; m < 5; m++) {
		strcpy(obj[m].lastName, "");
		obj[m].sales = 0.0;
	}
	for (int i = 0; i < 5; i++) {

		cout << "
	Please enter last name (or type quit to display data):";
		cin >> cmd;
		if (strcmp(cmd, "quit") == 0) {
			break;
		}
		strcpy (obj[i].lastName, cmd);
		cout << "
	Please enter sales:";
		cin >> obj[i].sales;
	}
	for (int y = 0; y < 5; y++) {
		cout << obj[y].lastName << "	" << obj[y].sales << endl;
	}
      	system("pause");
	return 0;
}
link

answered 2011-12-13 at 21:19:52

StingRaY's gravatar image

StingRaY

Or this, as an alternative:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
#include <iostream>
#include <string.h>
using namespace std;

struct record {
        char lastName[15];
        float sales;
};

int main() {
        record obj[5];
        char cmd[15];
        int reccount = 0;
        for (int i = 0; i < 5; i++) {

                cout << "
	Please enter last name (or type quit to display data):";
                cin >> cmd;
                if (strcmp(cmd, "quit") == 0) {
                        break;
                }
                reccount++;
                strcpy (obj[i].lastName, cmd);
                cout << "
	Please enter sales:";
                cin >> obj[i].sales;
        }
        for (int y = 0; y < reccount; y++) {
                cout << obj[y].lastName << "	" << obj[y].sales << endl;
        }
        system("pause");
        return 0;
}
link

answered 2011-12-13 at 21:20:53

StingRaY's gravatar image

StingRaY

Now, I am trying to adapt the following sort algorithm to rearrange the data.

// Bubble Sort Function for Descending Order
void BubbleSort(apvector <int> &num)
{
      int i, j, flag = 1;    // set flag to 1 to start first pass
      int temp;             // holding variable
      int numLength = num.length( );
      for(i = 1; (i <= numLength) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (numLength -1); j++)
         {
               if (num[j+1] > num[j])      // ascending order simply changes to <
              {
                    temp = num[j];             // swap elements
                    num[j] = num[j+1];
                    num[j+1] = temp;
                    flag = 1;               // indicates that a swap occurred.
               }
          }
     }
     return;   //arrays are passed to functions by address; nothing is returned
}



I get a few error messages such as variable or field `BubbleSort' declared void
link

answered 2011-12-13 at 21:22:09

talk2eric's gravatar image

talk2eric

To demonstrate where I placed the bubble sort algorithm, please see the following:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
#include <iostream>
#include <string.h>
using namespace std;

struct record {
        char lastName[15];
        float sales;
};


int main() {
        record obj[5];
        char cmd[15];
        int reccount = 0;
        for (int i = 0; i < 5; i++) {

                cout << "
	Please enter last name (or type quit to display data):";
                cin >> cmd;
                if (strcmp(cmd, "quit") == 0) {
                        break;
                }
                reccount++;
                strcpy (obj[i].lastName, cmd);
                cout << "
	Please enter sales:";
                cin >> obj[i].sales;
        }
        
        // Bubble Sort Function for Descending Order 
void BubbleSort(apvector <int> &num)
{
      int i, j, flag = 1;    // set flag to 1 to start first pass
      int temp;             // holding variable
      int numLength = num.length( ); 
      for(i = 1; (i <= numLength) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (numLength -1); j++)
         {
               if (num[j+1] > num[j])      // ascending order simply changes to <
              { 
                    temp = num[j];             // swap elements
                    num[j] = num[j+1];
                    num[j+1] = temp;
                    flag = 1;               // indicates that a swap occurred.
               }
          }
     }
     return;   //arrays are passed to functions by address; nothing is returned
}


        for (int y = 0; y < reccount; y++) {
                cout << obj[y].lastName << "	" << obj[y].sales << endl;
        }
        system("pause");
        return 0;
}
link

answered 2011-12-14 at 20:01:56

talk2eric's gravatar image

talk2eric

I think you cannot place the function like that. Moreover, the bubble sort function you posted is for a vector of integer not your record.

You have to move the function out of main() function and adjust it to work with your record struct.
link

answered 2011-12-14 at 20:03:38

StingRaY's gravatar image

StingRaY

Check this out:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
#include <iostream>
#include <string.h>
using namespace std;

struct record {
        char lastName[15];
        float sales;
};

// Bubble Sort Function for Descending Order 
void BubbleSort(record num[], int numLength) {
      int i, j, flag = 1;    // set flag to 1 to start first pass
      record temp;             // holding variable
      //int numLength = sizeof(num) / sizeof(record); 
      for(i = 1; (i <= numLength) && flag; i++)
     {
          flag = 0;
          for (j=0; j < (numLength -1); j++)
         {
               if (num[j+1].sales > num[j].sales)      // ascending order simply changes to <
              { 
                    strcpy(temp.lastName, num[j].lastName);             // swap elements
                    temp.sales = num[j].sales;
                    strcpy(num[j].lastName, num[j+1].lastName);
                    num[j].sales = num[j+1].sales;
                    strcpy(num[j+1].lastName, temp.lastName);
                    num[j+1].sales = temp.sales;
                    flag = 1;               // indicates that a swap occurred.
               }
          }
     }
     return;   //arrays are passed to functions by address; nothing is returned
}

int main() {
        record obj[5];
        char cmd[15];
        int reccount = 0;
        for (int i = 0; i < 5; i++) {

                cout << "
	Please enter last name (or type quit to display data):";
                cin >> cmd;
                if (strcmp(cmd, "quit") == 0) {
                        break;
                }
                reccount++;
                strcpy (obj[i].lastName, cmd);
                cout << "
	Please enter sales:";
                cin >> obj[i].sales;
        }
        
        BubbleSort(obj, reccount);

        for (int y = 0; y < reccount; y++) {
                cout << obj[y].lastName << "	" << obj[y].sales << endl;
        }
        system("pause");
        return 0;
}


This is how to integrate your bubble sort into your code.
link

answered 2011-12-14 at 20:22:25

StingRaY's gravatar image

StingRaY

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×36
×1
×1

Asked: 12/13/2011 12:40

Seen: 384 times

Last updated: 12/15/2011 11:32

Categories