0

How to refer to a line depending on output?

int person[] = {1,2,3,4,5,6,7,8,9,10}; int pancake; int most = 0; int x = 0; while (person[x] <= 10) { cout << "Person " << person[x] << " are how many pancakes?: "; cin >> pancake; person[x]++; } if (pancake > most) { most = pancake; cout << "Person " << b << " ate the most pancakes."; } I know I didn't make any variable called "b" but I've been trying to have the person there who ate the most pancakes. for example let's say person 6 ate the most pancakes, 9 of them, then I want it to say that person 6 ate the most. but if I rerun the code and person 2 ate most, I want it to say person 2 ate most.

10th Oct 2016, 4:14 AM
Noah V
Noah V - avatar
11 Antworten
+ 1
Since you likely have not changed your If-statement which is of no use. so from cin onwards do: cin >> person[i] //if you use the for-loop as I wrote it if (person[i] > most) { hungriest = i; //declare int hungriest = 0 after the int most=0 at beginning most = person[i]; } } //close for-loop cout << "Person " << hungriest << " was the hungriest and ate " << most << " pancakes!";
10th Oct 2016, 10:26 AM
Grennith
+ 1
well, you could assign 'least' a ridiculous large number - or even better, the maximum number that can be displayed with an integer. include climits (limits.h) and assign your least an initial value of INT_MAX. then simply check if (person[i] < least)
11th Oct 2016, 4:38 AM
Grennith
0
your while-loop looks a bit irritating. the last link in the loop increments the person, not your variable x which you apparently intended to use as an iterator. I'd recommend using a for-loop: for (int i = 0; i < person.length; i++) Inside your while-loop you assign a value entered by the user to pancake in each iteration, thus overwriting the previous value. You should consider using the array to save the values. entering the numbers 1-10 in an array is sort of pointless considering the array starts at 0 to your specified length. Consider person[4] as your 5th person (since an array starts at [0]) and the value (integer in this case) is the amount of pancakes. so it should be cin >> person[i] to check who ate the most, either save the highest value on the fly (within the loop) or bump up the running-time by creating a second loop checking for the highest value
10th Oct 2016, 7:47 AM
Grennith
0
@ Grennith: what do you mean by person.length?
10th Oct 2016, 8:42 AM
Noah V
Noah V - avatar
0
the length of the array. Since c++ does not give you a length-function for arrays either use std::vector or get the size of the array by using (sizeof(person)/sizeof(*person))... or simply hardcode the size of your array for now: 10
10th Oct 2016, 8:46 AM
Grennith
0
I haven't gotten to vectors yet, sizeof gives me the size in bytes and I'm too newb to know how to hardcode. Found this exercise on a website and it said beginner level and only needed: - variables, data types and numerical operators - basic input/output - logic (if / switch statements) - loops (for, while, do-while) - arrays the previous ones were pretty easy
10th Oct 2016, 9:08 AM
Noah V
Noah V - avatar
0
well by hardcode I meant writing for (int i = 0; i < 10; i++)
10th Oct 2016, 9:11 AM
Grennith
0
no matter what I try it keeps showing the last number I entered as instead of highest
10th Oct 2016, 10:13 AM
Noah V
Noah V - avatar
0
ah ok, I understand now. I got the code working and now trying to add another line telling me who ate the least. I've made two new integers, "least" and "nothungry". this time I obviously can't make least=person[i] because it will say that whoever ate most pancakes also ate the least, especially if "least" initial value is 0, because whatever the input, it's always bigger than 0. I don't think i would want to give any other starting value than 0 to "least" because input is different every time. I've been playing around a bit with if(least<person[i]) and some other similar things but none give the wanted result what would you suggest?
11th Oct 2016, 1:00 AM
Noah V
Noah V - avatar
0
Sweet, got it all working. I'm now trying to have a sorted list made after the last line (the one where it says whoever ate the least amount of pancakes) where it will show the one who ate most on top and whoever ate least at the bottom. I've been comparing sorting algorithms and I think the insertion algorithm would be best. This is what my code looks like now: #include <iostream> using namespace std; void insertion_sort (int person[], int length) { int j, temp; for (int i = 0; i < length; i ++) { j = i; while (j > 0 && person[j] < person[j-1]) { temp = person[j]; person[j] = person[j-1]; person[j-1] = temp; j--; } } cout << person[j] << endl; } int main() { int person[10]; int most = 0; int hungriest = 0; int least = INT_MAX; int nothungry = 0; for (int i = 0; i < 10; i++) { cout << "Person " << i << " ate how many pancakes?: "; cin >> person[i]; if (person[i] > most) { hungriest = i; most = person[i]; } if (person[i] < least) { nothungry = i; least = person[i]; } } cout << "Person " << hungriest << " was the hungriest and ate " << most << " pancakes!" << endl; cout << "Person " << nothungry << " was the least hungry and ate " << least << " pancakes!" << endl; void insertion_sort(); } Trying to run this will give me the error: C:\Documents ... 51 error: too few arguments to function 'void insertion_sort(int*, int)'| What am I doing wrong?
12th Oct 2016, 3:23 AM
Noah V
Noah V - avatar
0
you need to start reading guides or whatever - call by reference and call by value
12th Oct 2016, 6:35 AM
Grennith