Jump to content

Representational error demonstration program: Second opinion needed!

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Yuriy M

Yuriy M

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
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. :)
For $1000: Something that is a miserable pile of secrets.

#2
Yuriy M

Yuriy M

    Learning Programmer

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

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
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.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
Yuriy M

Yuriy M

    Learning Programmer

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




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users