Input/output for array

Sep 29, 2014 at 7:20am
Hey,
So I've started learning C++ and I'm doing beginner excercises by Blitz Coder.
One of them is like that:
Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10)
Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.

★ Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.

★★★★ Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.
i.e.
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes


I'm only at the first part and I'm stuck.. I can't figure out what I'm doing wrong, because the user input is not being saved to the array :?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

using namespace std;
int A[10];
int main()
{
int i=0;

    while (i<10)
    {
    i++;
    printf("Person number ");
    printf("%d", i);
    printf(" please insert how many pancakes you ate for breakfast \n");
    scanf("%d", &A[i]);
    }
    
    return 0;
}


If possible, don't complete the program, just give me few tips/hints..

And I'm gonna jump ahead - for second part of task, how can I find the person with least pancakes eaten in the array.
And how can I make order in the array for third part (going from biggest number to smallest?

Thank you all in advance!!
Sep 29, 2014 at 7:31am
Firstly..
So I've started learning C++

but i see no c++ in your code.
http://www.cplusplus.com/doc/tutorial/basic_io/

for the least pancakes thing, define an int called 'least' and initialise to 1000 or some high number like that.
Then we you read in your number you can do something like:

1
2
3
4
5
6
7
8
int least = 10000;

// read in number

if(number < least)
{
   number = least;
}


So at the end of the loop that 'least variable' will have retained the smallest number.


Sep 29, 2014 at 8:06am
When 'i' equals to 9, you're accessing A[10].
Sep 29, 2014 at 8:23am
When 'i' equals to 9, you're accessing A[10].

Yes, I know that it starts from 0 and goes to 9 (for A[10] array)
But how do I store values in the array correctly?
Sep 29, 2014 at 9:38am
increment i after line 15 and change line 13 to
1
2
printf("%d", i+1);


that is if you want to keep using crappy C methods of course.
Sep 29, 2014 at 5:08pm
Mutexe, I didn't quite understoon the first reply that you wrote, could you specify it?
I fixed the first part. Thanks for that suggestion :)
Sep 30, 2014 at 9:17am
morning,
I was only saying that you are using C-Style input/ouput constructs like scanf and printf, rather than the c++ 'equivalents' cin and cout.

the c-style stuff isn't that safe:
http://stackoverflow.com/questions/4515884/why-we-do-not-use-printf-and-scanf-in-c

Sep 30, 2014 at 10:40am
mutexe wrote:
but i see no c++ in your code.

Technically, using namespace std; counts as C++, I guess. Of course, that's also a bad habit :P
Last edited on Sep 30, 2014 at 10:41am
Sep 30, 2014 at 12:25pm
aye :)
Topic archived. No new replies allowed.