Using arrays

I am trying to teach myself to code in C++ and I am having issues with using arrays. I saw some practice problems that I am working on from this site and the problem is asking for user to enter in a value from 9 different people and it wants me to compare that value and see which individual ate the most and output which highest value to the least. So my question is how do I store the input into an array. I know I can use an if statement and while loop to go through the list I am just confused on how to store the value from each person into the array so I can retrieve it and output the highest value.
Let's say you have an array of 10 integers. You declare it like this: int numbers[10];
You access an element of the array like this: numbers[0], numbers[1]...numbers[9]
Since the number of elements is fixed you can easily use a for loop.
Try first to input and output all the values, finding the highest will be the second step.
Last edited on
arrays work off an index value that starts at 0 and goes to size-1.

for for 10 people,

int array[10];

array[0] = 1;
array[1] = 32;
array[9] = x; // 9 is the LAST one. array[10] is not valid!
..

which can be done easily in a for loop over the index.

it would be wise to learn vectors instead, they are 'better arrays' for most programming uses.

Last edited on
#include <iostream>
#include <utility>
#include <algorithm>
using namespace std;

int pancake[10] = {};

void selectsort(int array[], int n){
//pos_min is short for position of min
int pos_min,temp;

for (int i=0; i < n-1; i++)
{
pos_min = i;//set pos_min to the current index of array

for (int j=i+1; j < n; j++)
{

if (array[j] < array[pos_min])
pos_min=j;
//pos_min will keep track of the index that min is in, this is needed when a swap happens
}

//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
temp = array[i];
array[i] = array[pos_min];
array[pos_min] = temp;
}
}
}

int main () {


for (int i = 0;i < 11;i++) {
cout << "how many pancakes did person " << i << " eat?" << endl;
cin >> pancake[i];

selectsort(pancake);

}

}

This is the code I was trying to write but when I attempt to call the function in main it produces an error.
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
#include <iostream>
#include <utility>
// <algorithm> is not used.
// #include <algorithm>

// prefer omitting this line.
// using namespace std;

// no need for a global variable.
// int pancake[10] = {};

// note the use of indentation to improve code clarity.
void selectsort(int* array, int n){
    // pos_min is short for position of min
    // prefer to initialize variables.
    int pos_min = 0;
    
    for (int i = 0; i < n-1; ++i)
    {
        pos_min = i;//set pos_min to the current index of array
    
        for (int j = i+1; j < n; ++j) {
            if (array[j] < array[pos_min])
                pos_min=j;
        }
    
        //if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
        if (pos_min != i)
            std::swap(array[i], array[pos_min]);
    }
}

int main () {
    // use the constant integer named sz to denote the number of elements 
    // in the array, instead of the magic number 10.  
    constexpr int sz = 10;
    // this array contains exactly sz elements (not sz + 1 elements)
    int pancake[sz];

    // sz (10) not sz + 1 (11).  
    for (int i = 0; i < sz/* 11 */; ++i) {
        // humans number things starting from 1
        std::cout << "how many pancakes did person " << (i + 1) << " eat?\n";
        std::cin >> pancake[i];
    }
    
    // sort the array outside the loop, only once.
    // you need to pass the size, too.
    selectsort(pancake, sz);
    
    // print all the elements in the array
    for (int i = 0; i < sz; ++i)
        std::cout << "Person " << i << " ate " << pancake[i] << " pancakes.\n";
}


Live demo:
http://coliru.stacked-crooked.com/a/174e59dda63634bb
I tried the code and it worked like a charm. I appreciate the feedback and I undestand why the code I originally did wasn't working.
Topic archived. No new replies allowed.