CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2014
    Posts
    3

    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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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().

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  5. #5
    Join Date
    Jan 2014
    Posts
    3

    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.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help with a practice problem!

    Quote Originally Posted by BlurryPi View Post
    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

  7. #7
    Join Date
    Jan 2014
    Posts
    3

    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!)

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help with a practice problem!

    Quote Originally Posted by BlurryPi View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured