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.
#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.
#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.
constexprint 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";
}