C Programming

Discussion in 'General Science & Technology' started by zablon13, Mar 31, 2017.

  1. zablon13 Registered Member

    Messages:
    1
    guys please help me here. am new to programming and i have this question for an assignment:

    An old Arabian legend has it that a fabulously wealthy but unthinking king agreed to give a beggar 1 Shilling and triple the amount for 10 days. Using this information, write, compile, and run a C program that displays how much the king must pay the beggar on each day and the total earned by the beggar at the end of the 10 day period. The output of your program should appear as follows:



    Day Amount Owed Running total earned

    1 1 1

    2 3 4

    3 27 31

    . .

    . .

    10.



    your feedback will highly be appreciated
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. rpenner Fully Wired Staff Member

    Messages:
    4,833
    There is a mismatch in the statement of the problem since 27 is not triple the amount of 3.

    Assuming on day n, the amount \(a_n\) is earned, then on day n+1 the amount \(a_n \times r\) is earned, where in this case, r = 3.

    Thus on day n, \(a_n = a_1 \times r^{n-1}\) and the total earned \(b_n = \sum_{k=1}^{n} a_k = a_1 \frac{r^n -1}{r -1}\)

    So we have:
    \(\begin{array}{r|cc|rr} n & a_n & b_n & \left. a_n \right|_{a_1 = 1, r = 3} & \left. b_n \right|_{a_1 = 1, r = 3} \\ \hline \\ 1 & a_1 & a_1 & 1 & 1 \\ 2 & a_1 r & a_1 \frac{ r^2 - 1 }{ r - 1 } & 3 & 4 \\ 3 & a_1 r^2 & a_1 \frac{ r^3 - 1 }{ r - 1 } & 9 & 13 \\ 4 & a_1 r^3 & a_1 \frac{ r^4 - 1 }{ r - 1 } & 27 & 40 \\ 5 & a_1 r^4 & a_1 \frac{ r^5 - 1 }{ r - 1 } & 81 & 121 \\ 6 & a_1 r^5 & a_1 \frac{ r^6 - 1 }{ r - 1 } & 243 & 364 \\ 7 & a_1 r^6 & a_1 \frac{ r^7 - 1 }{ r - 1 } & 729 & 1093 \\ 8 & a_1 r^7 & a_1 \frac{ r^8 - 1 }{ r - 1 } & 2187 & 3280 \\ 9 & a_1 r^8 & a_1 \frac{ r^9 - 1 }{ r - 1 } & 6561 & 9841 \\ 10 & a_1 r^9 & a_1 \frac{ r^{10} - 1 }{ r - 1 } & 19683 & 29524 \end{array} \)
    This calculation will not overflow a 16-bit or larger integer which is something to consider while programing.
    It is unclear if double-spacing is required or not. It is unclear if the columns should be aligned or if they should be separated by a single space. (These matters don't translate well to the web.)

    Code:
    #include <stdio.h>
    int main(int argc, char **argv) {
      const int payment_day_one = 1;
      const int rate = 3;
      const int days = 10;
      int day = 0;
      int payment = 0;
      int running_sum = 0;
      fprintf(stdout, "%3s %11s %19s\n", "Day", "Amount Owed", "Running total earned");
      for (int i = 0; i < days ; i ++ ) {
          day += 1;
          if ( i == 0 ) {
            payment = payment_day_one ;
          } else {
            payment *= rate ;
          }
          running_sum += payment;
          fprintf(stdout, "%3d %11d %19d\n", day, payment, running_sum);
      }
    } 
    Finally the meaning of the mysterious line for day 3 not matching the English-language portion of the specification is curious. The sequence 1, 3, 27 can be extended in various ways. What if \(a_n = 3^{\frac{n^2-n}{2}}\) ? Then \(b_{10} = 2954462824073397914644\) but this requires more bits of precision (72) than exists in a 64-bit floating point double or 80-bit x86 floating point long double. To get precise numbers in this case, you will need to either use 128-bit IEEE floating point numbers or exact integer arithmetic on 128-bit integers or integers of arbitrary length, none of which is part of widely adopted standards for C or printf.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Dinosaur Rational Skeptic Valued Senior Member

    Messages:
    4,618
    Unless you have some special reason to program in C, I suggest trying Visual Basic.net

    It is more user friendly & I think there is a free Express version. Not sure this is still available. I have been using it for several years.
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Dr_Toad It's green! Valued Senior Member

    Messages:
    1,834
    And in the same breath you advocate using VB? God, man.

    Please Register or Log in to view the hidden image!

     
  8. rpenner Fully Wired Staff Member

    Messages:
    4,833
    Not feeling appreciated.

    I had network issues when I first replied, but today I see that the working of the problem is used by Gary J. Bronson (C++ and visual basic textbooks) and is many times repeated on the web.

    https://books.google.com/books?id=w...bulously wealthy but unthinking king"&f=false

    https://www.assignmentexpert.com/homework-answers/programming-and-computer-science/c/question-67122

    I wonder who rewrote the problem in terms of Shillings instead of cents.

    Here for example is a program that works but should not get full credit for being illegible.
    Code:
    #include <stdio.h>
    int main() {
      int z = 0;
      printf("Day Amount Owed Running total earned\n");
      for (int x = 1, y = 1; x <= 10 ; x ++, y *= 3 ) {
        printf("%d %d %d\n", x, y, (z += y));
      }
    }
    
     

Share This Page