Beginner Exercises (not Homework)

Pages: 12
Nov 24, 2015 at 11:37pm
I think that the issue lies on lines 19-27 and i know it's probably a logic error but for whatever reason I am unable to see it. I have used the debugger and I understood what was going on i know that it was skipping some person and I didn't know why it was doing that so I don't know how to fix it. One other thing is that the exercise is the Pancake Glutton and it is the last modification.

For those who are curious:
http://www.cplusplus.com/forum/articles/12974/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>;
#include <array>;
#include <string>;

int main() {
	std::array<int, 10> person;
	std::array<int, 10> numberPancakesEaten;
	std::array<int, 10> numberOfPerson;
	std::string end;
	int k = 0;
	
	for (int i = 0; i < 10; i++) {
		std::cout << "How many pancakes did person ";
		std::cout << i;
		std::cout << " eat?";
		std::cin >> person[i];
	}

	for (int z = 0; z < 10; z++) {
		for (int j = 0; j < 10; j++) {
    		if (person[j] > person[z]) {
				numberPancakesEaten[k] = person[j];
				numberOfPerson[k] = j;
	    	}
                //new code
	    	if (numberPancakesEaten[k] == person[j]) {
	    	    person[j] = -1;
	    	}
	    	//end of new code
	        }
		k++;
	}

	for (int x = 0; x < 10; x++) {
		std::cout << "Person ";
		std::cout << numberOfPerson[x];
		std::cout << ": ate ";
		std::cout << numberPancakesEaten[x];
		std::cout << " pancakes.\n";
	}
	
	std::getline(std::cin, end);
	std::getline(std::cin, end);
}


edit: Forgot that you can edit and run. So I deleted the input and output.

edit 2: did some more trouble shooting and found that the person #7 is left out sometimes not sure what is going on. Code is now updated

Not sure how to fix any help would be appreciated.
Last edited on Nov 25, 2015 at 9:56am
Nov 24, 2015 at 11:49pm
Why on line 19 are you setting z to one?
Nov 24, 2015 at 11:51pm
oops i changed that on my code on my visual studios... I originally thought that i didn't want to repeat, but later realized that it would not work correctly...

Sorry about that. Let me fix it. Good catch.
Last edited on Nov 24, 2015 at 11:56pm
Nov 24, 2015 at 11:56pm
No problem: Good luck!
Nov 25, 2015 at 9:57am
Anyone have any ideas how to fix the logic error?
Nov 25, 2015 at 11:05am
Why do you make it so complicated?
You could create a struct to store the number of the person and number of pancakes.
1
2
3
4
5
struct PancakeInfo
{
  int NumberOfPerson;
  int NumberOfPancakes Eaten;
}

Then you need only one array with the structs.
Finding the min and max of this array should be easy.
Then you sort the array in descending order and dump it on the screen.
Nov 25, 2015 at 11:58am
I was trying to follow the rules of the problem:
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays


No where in this list does it say "Structs"

So...
I am trying to do it the way they intended.

However, what you said is very valid and helpful.
Last edited on Nov 25, 2015 at 12:04pm
Nov 25, 2015 at 12:01pm
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <array>

struct Person
{
	int index;
	int cakes;
};

int main()
{
	std::array<Person, 10> person;
	
	//INPUT
	for (int i = 0; i < 10; i++) {
		std::cout << "How many pancakes did person ";
		std::cout << i;
		std::cout << " eat? ";
		std::cin >> person[i].cakes;
		person[i].index = i;
	}

	//MAXIMUM
	int maximum = person[0].cakes;
	int maxPerson = 0;

	for (int i = 0; i < 10; i++)
	{
		if (maximum < person[i].cakes)
		{
			maximum = person[i].cakes;
			maxPerson = i;
		}
	}
	std::cout 
		<< "Maximum eaten by person " << maxPerson 
		<< " who ate " << person[maxPerson].cakes
		<< std::endl;

	system("pause");

	return 0;
}
Nov 25, 2015 at 12:07pm
I see.

By the way the one that i am working on:
★★★★ 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


@kemort: Although, I appreciate the full blown answer, it wasn't quite what I was looking for.

I just wanted to have help in using my code and getting some insight on what I was missing or not understanding.
Last edited on Nov 25, 2015 at 12:32pm
Nov 25, 2015 at 1:08pm
closed account (48T7M4Gy)
I just wanted to have help in using my code and getting some insight on what I was missing or not understanding.
That's OK I did it for my own interest. :]
Nov 25, 2015 at 3:29pm
@kemort: Does your code even produce the correct output? When I ran your program (with the following modifications) it didn't seem to produce the correct results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
	std::array<Person, 10> person{10,3,6,4,33,21,44,54,14,18};
/*	
	//INPUT
	for (int i = 0; i < 10; i++) {
		std::cout << "How many pancakes did person ";
		std::cout << i;
		std::cout << " eat? ";
		std::cin >> person[i].cakes;
		person[i].index = i;
	}
*/
...


Result: Maximum eaten by person 3 who ate 54
The 54 is correct by it should be person 8.

Nov 25, 2015 at 3:47pm
Put this on line 22 so that your code looks like this:
1
2
3
4
5
6
7
8
9
              
    		if (person[j] > person[z]) {
                     std::clog << "Setting numberPancakesEaten[ " << k << " ] to " <<
                        "person[ " << j << " ] (" << person[ j ] << ")." << std::endl;

		numberPancakesEaten[k] = person[j];
				numberOfPerson[k] = j;
	    	}


Put this on line 27 so that your code looks like this:

1
2
3
4
5
6
	    	if (numberPancakesEaten[k] == person[j]) {
                    std::clog << "Setting person[ " << j << " ] to -1 " <<
                          " (" << person[ j ] << ")." << std::endl;

	    	    person[j] = -1;
	    	}


When you run your code, run it like this:

1
2
3

main 2>main.out


You will see what your code is doing in the file main.out and that should help you redesign your code. I hope it helps!
Nov 25, 2015 at 3:59pm
No where in this list does it say "Structs"

So...
I am trying to do it the way they intended.


Sorry,

I failed to understand that requirement means also limit of what you are allowed to use.
Nov 25, 2015 at 4:08pm
@Thomas1965: it's OK i forgot to write out the requirements, which was my fault. You did nothing wrong. In hindsight, I should have added the list of what things it required and the link to the page. :]

@kemort: Sorry about sounding harsh i just wanted to get a better understanding on what I wasn't understanding. Yeah I think that might work and I think I will mark this as solved. If I still have trouble I'll just unmark it as resolved.

Thanks everyone for all your help!

- Hirokachi
Nov 25, 2015 at 4:26pm
Hirokachi

IMO the list of the requirements:

Suggested Study Order:

• variables, data types, and numerical operators
• basic input/output
• logic ( if statements, switch statements )
• loops ( for, while, do-while )
• arrays
• pseudo random number generation
• strings & string functions
• functions
• structures/classes
• enumerated data
• file input/output
• pointers
• sorting
• linked lists
• advanced classes
• recursion

Shouldn't be taken as absolutes. For example if you were to follow this list to the letter you probably shouldn't be using std::array yet, unless you consider std::array, std::vector, std::string, etc to be discussed in the first point. I myself think std::vector and std::string should be used very early, std::array not a big fan.

I myself would recommend functions be learned right after loops since I consider functions more important than the following items.

Keep us informed on your progress with the program.

Edit: Also the last part of this exercise could be considered sorting since you need to show the data in "sorted" order maximum to minimum.
Last edited on Nov 25, 2015 at 4:34pm
Nov 25, 2015 at 10:25pm
That is a very good point... I just never thought of it like that.
Nov 25, 2015 at 10:43pm
closed account (48T7M4Gy)
@kemort: Does your code even produce the correct output?
@jlb Oops! You've jumped the gun. Of course it is correct. Even!

When I ran your program (with the following modifications) it didn't seem to produce the correct results.
There is nothing wrong with my code or the perfectly accurate results I produce with it. The answer to your error lies in your sample array.
Nov 25, 2015 at 11:21pm
And what is wrong with the sample array?
Nov 25, 2015 at 11:53pm
closed account (48T7M4Gy)
And what is wrong with the sample array?

Nothing that I am aware of, but there again I didn't write it.
Nov 26, 2015 at 6:26pm
What is this sample array that we are talking about?

I am not sure if you are asking about my code or not.

Please give me the line number and i am sorry that i didn't put any comments in the code.
Pages: 12