Thread: Nested Loops Help

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    1

    Nested Loops Help

    Here are the instructions:


    .
    - the outer loop processes vegetable categories
    - the inner loop processes seasons within a vegetable category.
    There are the same number of seasons for each vegetable category.



    For a particular vegetable category in the outer loop, the inner loop will process all seasons, one by one.
    - Input the number of seeds, the number of plants and the unit price of a plant.
    - Keep a running total of:
    - the growing ratio of plants to seeds
    - the total selling price of all plants
    At the end of the inner loop report the growing ratio and total selling price for that category


    After all categories have been processed, report the following:
    - The category of the best growing group (having the highest ratio)
    - The category of the best selling price (having the largest sales)







    SAMPLE DATA
    Code:
    Enter the number of the veg catagory :
    2
    Enter the number of the seasons :
    2
    ----------------------------------------------------------
    catagory 1 :
    season 1 :
    Enter Number of Seeds :
    13
    Enter Number of Plants :
    11
    Enter UnitPrice :
    1.2
    season 2 :
    Enter Number of Seeds :
    14
    Enter Number of Plants :
    10
    Enter UnitPrice :
    2.1
    growing ratio for catagory 1 : 78.02%
    total sold price for catagory 1 : 34.20
    ----------------------------------------------------------
    catagory 2 :
    season 1 :
    Enter Number of Seeds :
    12
    Enter Number of Plants :
    11
    Enter UnitPrice :
    1.1
    season 2 :
    Enter Number of Seeds :
    14
    Enter Number of Plants :
    12
    Enter UnitPrice :
    1.4
    growing ratio for catagory 2 : 88.69%
    total sold price for catagory 2 : 28.90
    ----------------------------------------------------------
    ----------------------------------------------------------
    The best growing group is Catagory 2, with the best growing ratio : 88.69%
    The best total selling price is Catagory 1, with the price : 34.20
    Press any key to continue . . .

    Here is my sample code:


    Code:
    #include <iostream>
    #include <iomanip>
    
    
    using namespace std;
    
    
    int main()
    {
        int num_of_veg_categories, num_of_seasons, seeds, plants, total_seeds, total_plants;
        double unit_price, ratio_perc, total_price;
        double bestRatio = 0; // ***set up for Ratio comparison
        double bestAmount = 0; // ***set up for total sold price comparison
    
    
        total_seeds=0;
        total_plants=0;
        total_price=0;
    
    
        cout << setprecision(2) << fixed;
        cout << "Enter the number of veg category: ";
        cin >> num_of_veg_categories;
        cout << "Enter the number of seasons: ";
        cin >> num_of_seasons;
    
    
        for (int i = 1; i <= num_of_veg_categories; i++) //*****Errors
        {
            cout << "Category" << i << ":" << endl;
            for (int j = 1; j <= num_of_seasons; j++)
            {
                cout << "Season" << j << ":" << endl;
                cout << "Enter the number of seeds: ";
                cin >> seeds;
                cout << "Enter the number of plants: ";
                cin >> plants;
                cout << "Enter the unit price: ";
                cin >> unit_price;
    
    
                total_seeds+=seeds;
                total_plants+=plants;
                total_price+= plants*unit_price;
            }// ***seasons
    
    
            ratio_perc= ( (double) total_plants / total_seeds) / num_of_seasons*100;
    
    
            if (ratio_perc > bestRatio)
            {
                 bestRatio = ratio_perc;
            }
    
    
            if (total_price > bestAmount)
            {
                 bestAmount = total_price;
            }
    
    
        } //***outer loop
    }
    Why isn't my program actually working????

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'm assuming that your "Errors" comment means you're getting a compiler error at that point.
    You should tell us what that error is, because it compiles just fine for me.
    Code:
    $ g++ -Wall foo.cpp
    $ ./a.out 
    Enter the number of veg category: 2
    Enter the number of seasons: 2
    Category1:
    Season1:
    Enter the number of seeds: 13
    Enter the number of plants: 11
    Enter the unit price: 1.2
    Season2:
    Enter the number of seeds: 14
    Enter the number of plants: 10
    Enter the unit price: 2.1
    Category2:
    Season1:
    Enter the number of seeds: 12
    Enter the number of plants: 11
    Enter the unit price: 1.1
    Season2:
    Enter the number of seeds: 14
    Enter the number of plants: 12
    Enter the unit price: 1.4
    As for the rest of the problem, you're missing cout statements for your actual results.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested Loops Help.
    By etricity in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2014, 03:32 AM
  2. Help nested for loops
    By dals2002 in forum C Programming
    Replies: 14
    Last Post: 03-14-2008, 01:18 AM
  3. nested for loops
    By akub3 in forum C Programming
    Replies: 2
    Last Post: 04-01-2004, 06:21 AM
  4. nested for loops/
    By o0o in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 10:19 AM
  5. nested for loops
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 11:44 AM

Tags for this Thread