dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Assignment help with while loops

  1. #1
    Join Date
    Oct 2015
    Posts
    4

    Assignment help with while loops

    Hello:

    I am doing an assignment for my computer science class, and was just hoping some on could check my work.

    Here is a the assignment:

    The population of a town A is less than the population of town B. However, the population of town A is growing faster than the population of town B. Write a program that prompts the user to enter the population and growth rate of each town. The program outputs after how many years the population of town A will be greater than or equal to the population of town B and the populations of both the towns at that time. (A sample input is: Population of town A = 5000, growth rate of town A = 4%, population of town B = 8000, and growth rate of town B = 2%.)

    and here is my code:

    #include <iostream>
    using namespace std;

    int main()
    {
    //variables
    int pop_A, pop_B;
    int year = 0; //counter variable
    double growthrate_A, growthrate_B, newpop_A, newpop_B;

    //Input
    cout << "Enter the population of town A and town B: ";
    cin >> pop_A >> pop_B;

    cout << "Enter the growth rate town A and town B (growth rate B must be less than growth rate A): ";
    cin >> growthrate_A >> growthrate_B;

    //process

    //error checking
    while (pop_A >= pop_B)
    {
    cout << "Error: Population of Town A must be less than town B./n Please try again.";
    cin >> pop_A;
    }

    //error checking
    while (pop_A <= 0)
    {
    cout << "Error: Value must be greater than zero.\n Please try again.";
    cin >> pop_A;
    }

    //error checking
    while (pop_B <= 0)
    {
    cout << "Error: Value must be greater than zero. \n Please try again.";
    cin >> pop_B;
    }

    //error checking
    while (growthrate_A <= 0)
    {
    cout << "Error: Value must be greater than zero. \n Please try again.";
    cin >> growthrate_A;
    }

    //error checking
    while (growthrate_B <= 0)
    {
    cout << "Error: Value must be greater than zero. \n Please try again.";
    cin >> growthrate_B;
    }

    //error checking
    while (growthrate_A <= growthrate_B)
    {
    cout << "Error: The growth rate of town A must be greater than the growth rate of town B. \n Please try again.";
    cin >> growthrate_A;
    }

    // calculating the new population by means of a while loop nested within an if-statement
    if (growthrate_A > growthrate_B)
    {
    newpop_A = ((growthrate_A / 100) * pop_A) + pop_A;
    newpop_B = ((growthrate_B / 100) * pop_B) + pop_B;

    while (pop_A <= pop_B)
    {
    year += 1;

    pop_A = ((growthrate_A / 100) * pop_A) + pop_A;
    pop_B = ((growthrate_B / 100) * pop_B) + pop_B;
    }
    //output
    cout << "The number of years it will take for town A population to be equal or greater than the population of town B is: " << year << endl;
    }
    system("pause");
    return 0;
    }


    Thanks in advance. I greatly appreciate your input.
    Attached Files Attached Files

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,027

    Re: Assignment help with while loops

    When posting code, please use code tags. Go Advanced, select the formatted code and click '#'.

    Code:
    // calculating the new population by means of a while loop nested within an if-statement
    if (growthrate_A > growthrate_B)
    This test is not required as at this point in the code it is know that this is true due to the proceeding while loop.

    Code:
    newpop_A = ((growthrate_A / 100) * pop_A) + pop_A;
    newpop_B = ((growthrate_B / 100) * pop_B) + pop_B;
    You calculate these values but never use them.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.1)

  3. #3
    Join Date
    Jun 2015
    Posts
    208

    Re: Assignment help with while loops

    Quote Originally Posted by nicbarker View Post

    while (pop_A <= pop_B)
    {
    year += 1;

    pop_A = ((growthrate_A / 100) * pop_A) + pop_A;
    pop_B = ((growthrate_B / 100) * pop_B) + pop_B;
    }
    It's considered good style to declare variables close to where they're used and not miles away so I would put "year" just before the above loop.

    Another minor issue is that the right hand side of the expressions will evaluate to a double which is then assigned to an int. This is a narrowing conversion which will result in a "possible loss of data" compiler warning (which can be supressed with a cast). Also note that the 100 is an int whereas 100.0 is a double (so you use the intended literal type).

    In my view the input part of the program is a little quirky. After giving clear instructions I would prompt for all 4 values in one go. If there's an error I would explain the problem and then give the user the chance to retry (enter all 4 values again), or to quit the program. That's clean and sufficient for a program like this.

    Finally a real bug - the loop may never finish! It can happen under several circumstances most notably when ((growthrate_A / 100) * pop_A) is too small to increment pop_A. The loop will go on forever. This can also happen if pop_A and pop_B are incremented with the same amount. The problem is the rounding (from double to int).
    Last edited by tiliavirga; October 27th, 2015 at 09:35 AM.

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

    Re: Assignment help with while loops

    I'd consider the order you're performing your error checks too. You're checking that Pop_A is >= Pop_B, but later on giving users a chance to reenter their values. While that test may pass initially, it could fail later and you'll never know. You need to perform all your relevant validity checks each time a value is changed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)