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

    Help with a C++ program

    Hi,

    I need to write a C++ program to do the following things, and i have displayed my attempted code. Please let me know if there is more effective way to do this.

    - I need to calculate the % based on the input values (run-time)
    - The percentage value will keep on increasing based on the input values.

    For eg

    If the input is 30, then it after completing the 1st operation it should display 1 % .... and after completing 2nd operation it should display 2 % and so on, and when it completes 30th operation it will display 100 %.

    Note: the input values is a random number.


    My logic

    Case 1: If the input is 30

    1/30 = 0.03 and multiply the result by 100 then the value is 3 %
    .........
    30/30 = 1 result will be 100 %

    It is ok to have the % is some increasing order, rather than more presisely with 1%,2% etc.

    But problem is my logic will not work for this case

    Case 2: If the input is for eg 300

    then 1/300 = 0.033 and multiply the result by 100 then the value is 0.33, in this case i should multiply by 1000 instead of 100 to get the round value.

    Any idea on how to deal with this.

    Thanks in advance.

  2. #2
    Join Date
    Dec 2009
    Posts
    145

    Re: Help with a C++ program

    Let's say you input N
    so your operation needs to perform N times
    for 1 your percentage becomes 1x100/N
    for 2...........................................2x100/N
    .....
    ..................................................Nx100/N

    You slip your mind in your second case into something illogical.

    PS. (Brainer needs to know what are you thinking at that moment.)

  3. #3
    Join Date
    Oct 2009
    Posts
    577

    Re: Help with a C++ program

    To add to above comment:

    You have a few wrong assumptions:

    In your first case you don't have to display 1%, 2%, 3% ... but 3.33%, 6.67%, 10.00, ... 100%.

    You see there are roundings necessary if you always have two decimals right of the decimal point?

    In your second case the factor truly is 0.033 (actually 0.033333333333333...). So your sequence is 0.033%, 0.067%, 0.100%, ..., 99.967%, 100% if you would use a fraction of 3 decimals or 0.03, 0.07, 0.10, ..., 99.97%, 100% if you hold onto the 2 decimals. Probably best would be if you determine the number of fractional digits from the input, i. e. from the numbers of decimals the input has.

  4. #4
    Join Date
    Jun 2010
    Posts
    72

    Re: Help with a C++ program

    If this is related to GUI, Progressbar seems to accept only integral values, but static text can be set with any dec-degit number you'd love to, and feel free to spare its limited dec-digits on console applications

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