After sort need specific index to print with value

I cant´figure out how to cout when sorted which person ate most pancakes. I need som help here.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #include <iostream>

using namespace std;

// Constants
const int PERSONS = 10;

int main()
{

	int pancakes[PERSONS];

	int i = 0;
	int numOfElements = 0;
	int input;

	cout << "Please type in how many pancakes the persons have eaten: ";
	
	while (i < PERSONS)
	{
		numOfElements++;
		cin >> input;
		pancakes[i] = input;
		i++;
		if (i == (PERSONS))
		{
			cout << "Thank you!" << endl;
		}
		
	}

	for(int i = 0; i < numOfElements; ++i)
	{
		cout << "Person # " << (i + 1) << " ate " << pancakes[i] << endl;
	}

	// Lowest value in array

	int leastPancakes = pancakes[0];                
	
	for(int i = 0; i < numOfElements; ++i)
	{
           if(pancakes[i] < leastPancakes)	// and the Lowest one
		   {
				leastPancakes = pancakes[i];
				
				if(leastPancakes == pancakes[i])
				{
					 cout << "Person " << (i + 1) << " ate least pancakes: " << leastPancakes << '\n';	
				}
		   }
	}

	for(int i = 0; i < numOfElements; i++)
	{
		for(int j = 0; j < numOfElements; j++)
		{
			if(pancakes[j]<pancakes[j+1])
			{
				int hold=pancakes[j];
				pancakes[j]=pancakes[j+1];
				pancakes[j+1]=hold; 
			}
		} 
	} 
	cout<<"Sorted Array is: "<<endl;
 
	for(int i = 0; i < numOfElements; i++)
	{
		// This aint working cout << "Person " <<  << " ate " << pancakes[i] << " pancakes." << endl; 
	}
	
    return 0;
}





See my comments in the code below. This code works.

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
62
63
64
65
#include <iostream>

using namespace std;

// Constants
const int PERSONS = 10;

int 
main()
{
    int pancakes[PERSONS];

    int i = 0;
    int numOfElements = 0;
    int input;

    cout << "Please type in how many pancakes the persons have eaten: ";

    // Note the simplified loop here.  Just use the index and
    // set the number of elements when the loop exits. Actually, you
    // know the number of elements ahead of time
    while (i < PERSONS) {
	cin >> pancakes[i++];
    }
    numOfElements = i;
    cout << "Thank you!" << endl;

    for (int i = 0; i < numOfElements; ++i) {
	cout << "Person # " << (i + 1) << " ate " << pancakes[i] << endl;
    }

    // Lowest value in array

    // Track the index of the person who ate the fewest.  That way you
    // know both the person number and how many they ate.
    int leastIndex = 0;

    for (int i = 1; i < numOfElements; ++i) {
	if (pancakes[i] < pancakes[leastIndex]) {
	    leastIndex = i;
	}
    }

    cout << "Person " << (leastIndex + 1) << " ate least pancakes: "
	 << pancakes[leastIndex] << '\n';

    for (int i = 0; i < numOfElements; i++) {
	// since you can swap item j with j+1, you have to end this
	// loop 1 earlier.
	for (int j = 0; j < numOfElements-1; j++) {
	    if (pancakes[j] < pancakes[j + 1]) {
		int hold = pancakes[j];
		pancakes[j] = pancakes[j + 1];
		pancakes[j + 1] = hold;
	    }
	}
    }
    cout << "Sorted Array is: " << endl;

    for (int i = 0; i < numOfElements; i++) {
	cout << "Person " << i+1 << " ate " << pancakes[i] << " pancakes." << endl; 
    }

    return 0;
}
If you sort an array properly, no matter what numbers you input, the lowest value should be the first element and the highest value should be the last element.

for example: cout << arr[0] // the lowest value
cout << arr[9] // The highest value.

So, whatever those numbers are, once you've sorted the array properly, the correct numbers should appear.
Yes it´s sorting almost alright, but if person number 4 ate most pancakes I want it to sort like this

Person 4 ate 10 pancakes
Person 2 ate 8 pancakes and so on...

1
2
3
  for (int i = 0; i < numOfElements; i++) {
	cout << "Person " << i+1 << " ate " << pancakes[i] << " pancakes." << endl; 
    }
I think now understand what you mean. You want the numbers to correspond with the correct person. What you need to is a parallel array. Here is what I was able to do. You can modify this easily if you wish

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <iomanip>

using namespace std;

void SelectionSort(string [], int pancakes[], int);

const int people = 10;

int main()
{
    int pancakes[people];
    string person[people] = {"Jamie", "Jordan", "Peele", "Ash", "Luis",
                             "Jimmy", "Kurt", "Kaiser", "Anthony", "Ryan"};

    for(int i=0; i<10; i++)
    {
        cout<< "How many pancakes did " << person[i] << " eat? ";
        cin >> pancakes[i];

    }

    cout << "\n" << "Names " << "\t";
    cout << "        Total pancakes eaten" << endl;
    cout << "-----------------------------------";


    SelectionSort(person, pancakes, people);

    return 0;
}

void SelectionSort(string person[], int pancakes[], int SIZE)
{
    int i;
    int j;
    int max;

    for (i = 0; i < SIZE - 1; i++)
    {
        max = i; // The intial subscript or the first element.

        for (j = i + 1; j < SIZE; j++)
        {
            if (pancakes[j] > pancakes[max])  /* If this element is greater,
                                           then it is the new maximum */
            {
                max = j;
            }
        }
        // Swap both variables at the same time.
        int temp = pancakes[i];
        pancakes[i] = pancakes[max];
        pancakes[max] = temp;

        string tempString = person[i];
        person[i] = person[max];
        person[max] = tempString;
    }

    for(i=0; i<10; i++)
    {

        cout << "\n" << left << setw(9) << person[i] << "\t";
        cout << pancakes[i] << endl;
    }
    cout << "-----------------------------------";
    cout << "\n\n" << person[0] << " eat the most pancakes. " << endl;
    cout << person[9] << " eat the least pancakes. " << endl;
    cout << pancakes[0] << " was the largest amount. " << endl;
    cout << pancakes[9] << " was the smallest amount. " << endl;


}
Last edited on
Topic archived. No new replies allowed.