Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Representational error demonstration program: Second opinion needed!

nested loop

  • Please log in to reply
3 replies to this topic

#1 Yuriy M

Yuriy M

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 27 October 2011 - 06:42 PM

Hi. I have a program that uses loops to demonstrate the problem of representational error. Basically, it involves taking each fraction from 1/2 to 1/30, adding up n copies of 1/n and then comparing the sum to 1. If the sum is equal to 1, an appropriate message is displayed stating that the result is equal to 1. If not, a message will display stating that the result is either less than 1 or greater than 1.

I made my program with the use of nested loops with the outer loop counting from 2 to 30 and the inner loop adding up the number of fractions based on the denomination amount. Ex. 1/2 + 1/2 on first iteration, 1/3 + 1/3 + 1/3 on the second iteration, etc.

Here is the code:

int main(void)
{
    /* Declare variables */
    
    float sum;                                  /* The sum of the fractions */
    int   frac_count,                           /* Counts the number of fractions to be summed in the iteration */
          den_count;                            /* Counts fraction denominations from 2 to 30 */
    
    /* Execute loops to help count denominations and sum up fractions in order to determine representational error */
    
    for (den_count = 2; den_count <= 30; den_count++)
    {
     sum = 0;
     for (frac_count = 1; frac_count <= den_count; frac_count++)
     {
      /* Add up fractions */
      sum += 1 / (float)den_count;
     }   
     /* Compare fractions to 1 and print result */
     if (sum == 1)
      printf("\nAdding n    1/n's gives a result of 1.");
     else if (sum < 1)
      printf("\nAdding n    1/n's gives a result less than 1.");
     else 
      printf("\nAdding n    1/n's gives a result greater than 1.");
    }
    printf("\n");
    return 0;
}


The output looks pretty good to me but I want a second opinion just to make sure that the program is executing the way it is supposed to. Thanks. :)
  • 0
For $1000: Something that is a miserable pile of secrets.

#2 Yuriy M

Yuriy M

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 05 November 2011 - 04:24 PM

If there is no response to this, I'll have to assume that the output is correct and the program is finished.
  • 0
For $1000: Something that is a miserable pile of secrets.

#3 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 07 November 2011 - 03:15 AM

Those fractions are fairly simple, it should provide good output for most cases. If not, there is not much you can do without added complexity.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#4 Yuriy M

Yuriy M

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 07 November 2011 - 08:13 PM

Thanks. I appreciate the response. :)
  • 0
For $1000: Something that is a miserable pile of secrets.





Also tagged with one or more of these keywords: nested loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download