e: added highest/lowest value
New problem found:
if 2 persons eat the same highest/lowest amount everything goes wrong.
updated code to try to fix
e2:
just noticed that i don't get the same result in using C++Shell and from MVS C++
if i set that they ate from 0 -> 9. On C++Shell i get 9 max and 1 min while on MSV i get 8 max and 0 min
updated code with changes
e3: just noticed my sorting is completley messed up, trying to fix it.
updated code to see where everything is going wrong
#include <iostream>
usingnamespace std;
int main()
{
int person[10] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int ate[11];
int highest;
int lowest;
for (int x = 0; x < 10; x++)
{
cout << "How many pancakes did person " << person[x] << " eat? ";
cin >> ate[x];
system("CLS");
}
for (int x = 0; x < 10; x++)
{
cout << "ate[" << x << "]" << " has value " << ate[x] << " at start."<< endl; //see if all identifiers has the right value
}
//sort ate by highest value
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (ate[j] < ate[j + 1])
{
highest = ate[j];
ate[j] = ate[j + 1];
ate[j + 1] = highest;
}
}
cout << "ate[" << i << "]" << " after highest sort " << ate[i] << endl; // prints out the changes after sorting has occured. it should show higest at top, while lowest bottom.
}
cout << "The person who ate most pancakes ate: " << highest << " pancakes." << endl;
//sort ate by lowest value
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (ate[j] > ate[j + 1])
{
lowest = ate[j];
ate[j] = ate[j + 1];
ate[j + 1] = lowest;
}
}
cout << "ate[" << i << "]" << " after lowest sort " << ate[i] << endl; // prints out the changes after lower sorting has occured. it should show lowest at top, while highest bottom.
}
cout << "The person who ate the least pancakes ate: " << lowest << " pancakes." << endl;
system("pause");
return 0;
}
i haven't learned about structs yet, are they easier to use when handling this type of code?.
the exercise says that i should be able to do this by using:
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays
so it seems i have serious logic flaws :S, guess i'll restart on a empty project.
are they easier to use when handling this type of code?.
They make handling bunch of related variable an order of magnitude easier. Actually if you have to use parallel arrays (where you must support relations between different arrays), you done something wrong.
the exercise says that i should be able to do this by using:
It is for base excercise, not for star additions.
Base excercise can be easily solved with only one array, as you do not need to change it, only find highest and lowest values. Additional question require you to shift array around and this leads to the need to store person number alongside number of pancakes eaten.