-
January 9th, 2014, 10:45 PM
#1
Help with a practice problem!
So I found this practice problem online:
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
And I'm having difficulty sorting the array so that the names of the people and their values (e.g person 4 - 10) remain unchanged.
This is my code so far:
Code:
#include <iostream>
#include <cstdlib>
int main ()
{
int nCakes[10] = {0};
for (int i = 0; i < 10; ++i)
{
std::cout << "Enter the number of pancakes person " << i+1 << " ate."<<std::endl;
std::cin >> nCakes[i];
std::cout << std::endl;
}
for (int iii = 0; iii < 10; iii++)
{
int jjj = iii;
for (int x = iii + 1; x < 10; x++)
{
if (nCakes[x] < nCakes[jjj])
{
jjj = x;
}
}
std::swap(nCakes[iii], nCakes[jjj]);
}
for (int k = 0; k < 10; ++k)
{
std::cout << "Person " << k+1 << " ate " << nCakes[k] << " pancakes"<<std::endl;
}
return 0;
}
Which prints:
Person 1 ate 1
Person 2 ate 3 pancakes etc. Even though person 1 ate, for example, 3 pancakes.
-
January 10th, 2014, 04:45 AM
#2
Re: Help with a practice problem!
Well, it is a big reason for you now to learn debugging, step in in the code with a debugger and see what, where, and why goes wrong or unexpected.
Victor Nijegorodov
-
January 10th, 2014, 08:12 AM
#3
Re: Help with a practice problem!
- your conceptual issue is that you're sorting only a list of pancakes eaten, you're not keeping track of who ate how many. This information is lost in the sort.
- there are better ways to sort in C++ than rolling your own sorter... have a look at std::sort().
-
January 10th, 2014, 08:23 AM
#4
Re: Help with a practice problem!
Typically when you need to group related pieces of information, such as the person number and the number of pancakes they ate, you'd use a struct or class and sort them. That way the grouped information stays together after the sort.
-
January 10th, 2014, 07:12 PM
#5
Re: Help with a practice problem!
Could you please give an example on how to sort a struct, as I am not very confident in using them.
-
January 11th, 2014, 01:10 AM
#6
Re: Help with a practice problem!
Originally Posted by BlurryPi
Could you please give an example on how to sort a struct, as I am not very confident in using them.
Code:
#include <iostream>
struct PersonInfo
{
int personNumber;
int nCakes;
};
using namespace std;
int main()
{
PersonInfo allPersons[10];
for (int i = 0; i < 10; ++i)
{
allPersons[i].personNumber = i+1;
std::cout << "Enter the number of pancakes person " << i+1 << " ate."<<std::endl;
std::cin >> allPersons[i].nCakes;
std::cout << std::endl;
}
}
Run this program. You will see that the person number and the number of cakes eaten are one entity, and not two separate entities. This is an example of how you would/should have started your program.
When you sort, you will be comparing the nCakes entitiy in the structs. If the entities are out of order, then you swap the entire structs that contain the entities. By swapping the entire struct, all of the entities within the struct are also swapped automatically.
You should just write code to convince yourself that if you std::swap() two structs, you will see everything inside the structs also swapped.
Regards,
Paul McKenzie
-
January 11th, 2014, 06:03 AM
#7
Re: Help with a practice problem!
Thank you so MUCH!
It finally works, here's my code:
Code:
#include <iostream>
#include <cstdlib>
struct PersonInfo
{
int personNumber;
int nCakes;
};
int main ()
{
PersonInfo allPersons[10];
for (int i = 0; i < 10; ++i)
{
allPersons[i].personNumber = i+1;
std::cout << "Enter the number of pancakes Person " << i+1 << " ate."<<std::endl;
std::cin >> allPersons[i].nCakes;
std::cout << std::endl;
}
for (int iii = 0; iii < 10; iii++)
{
int jjj = iii;
for (int x = iii + 1; x < 10; x++)
{
if (allPersons[x].nCakes < allPersons[jjj].nCakes)
{
jjj = x;
}
}
std::swap(allPersons[iii].nCakes, allPersons[jjj].nCakes);
std::swap(allPersons[iii].personNumber, allPersons[jjj].personNumber);
}
for (int k = 0; k < 10; ++k)
{
std::cout << "Person " << allPersons[k].personNumber << " ate " << allPersons[k].nCakes << " pancakes"<<std::endl;
}
return 0;
}
Is there anything I could improve upon further, or is it fine?
(thanks so much again!)
-
January 11th, 2014, 06:52 AM
#8
Re: Help with a practice problem!
That may work, but it's not how you do it. You should be swapping the structs themselves, not their members. When you start writing bigger projects, you'd find that approach to be a coding and maintenance nightmare. What if you add a member to a struct later? You'd have to go and find every instance where you swapped members like that, and in a project with hundreds of thousand of lines of code, you'd introduce a lot of bugs.
-
January 11th, 2014, 12:24 PM
#9
Re: Help with a practice problem!
Originally Posted by BlurryPi
Thank you so MUCH!
It finally works, here's my code:
As GCDEF stated, no.
That's why I mentioned in my post that you should first write the code to std::swap two structs -- your code does not do that. What you're doing is still std::swap() on two distinct entities inside the struct. What if the struct contained the persons age, weight, height, address, occupation, previous records of pancakes eaten, etc.? Are you going to write the code to swap all of those entities individually? That's nuts.
Again, take two structs and swap the structs in one statement. You still use std::swap(), but you give it the structs in question, not each entity in the struct.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|