Sorting Arrays

ok, im doing the pancake glutton problem found on http://www.cplusplus.com/forum/articles/12974/ . i plagiarized code from wikipedia (the code i pasted below) and im wondering: is there a way to sort values in an array without having to use the <algorithm> header?

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <algorithm>
 
int main() {
  int array[] = { 23, 5, -10, 0, 0, 321, 1, 2, 99, 30 };
  int elements = sizeof(array) / sizeof(array[0]); 
  std::sort(array, array + elements);
  for (int i = 0; i < elements; ++i) 
     std::cout << array[i] << ' ';
}


edit: also, how do i keep the persons number AND the amount of pancakes paired up? once i sort the amount of pancakes, then i lose which person it belonged to.
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
/*
Pancake Glutton
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays

Write a program that asks the user to enter the number of
 pancakes eaten for breakfast by 10 different people 
(Person 1, Person 2, ..., Person 10)
Once the data has been entered the program must analyze the
 data and output which person ate the most pancakes for breakfast.

- Modify the program so that it also outputs which person ate
 the least number of pancakes for breakfast.

---- Modify the program so that it outputs a list in order of
 number of pancakes eaten of all 10 people.
i.e.
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes

*/

#include <iostream>
#include <algorithm>


using namespace std;

int ate[] = {0,0,0,0,0,0,0,0,0,0};

void ask(int num)
{
    num++;
    cout << "Person " << num << ": ";
    num--;
    cin >> ate[num];
}

int main()
{
    int a = 0;

    cout << "How many pancake were eaten by 10 people?\n";

    while (a < 10) {
    ask(a);
    a++; }

    /*
    a = 0;
    while (a < 10) {
    cout << ate[a];
    a++; }
    */

    int elements = sizeof(ate) / sizeof(ate[0]);
    sort(ate, ate + elements);

    /*
    for (int i = 0; i < elements; ++i)
    cout << ate[i] << ' ';
    */

    return 0;
}
Last edited on
is there a way to sort values in an array without having to use the <algorithm> header?
Sure, just define your own sort algorithm (I suppose that using qsort from cstdlib is not what you want)

how do i keep the persons number AND the amount of pancakes paired up?
You can put them together in a struct.
Actually I still wonder if you are a business application developer the STL algorithm sort function is good enough. They run fast and accurate based on my business logic. Why do I want to code my own sort function instead ? It will be very time consuming and not very convincing to my end users on the total man-days effort needed to deliver a solution to them.

However if say you work for Dinkumware which sells C++ STL for a living, then you have all the valid reason to build your own sort function and try to make it "faster" than your competitors C++ STL.

But based on my observation, the total number of business application developers out-numbered the total number of "infrastructure plumbing" developers.
Topic archived. No new replies allowed.