### 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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include #include // is not used. // #include // 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.