Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data Structures Your code must be well formatted and commented. While marks will

ID: 671797 • Letter: D

Question

Data Structures

Your code must be well formatted and commented. While marks will not be assigned for properly formatting and commenting your code, All programs must include the “Essential Features of Program Documentation” (page 122). Details are included throughout Chapter 2 on best practices for documentation.

Q) You just won the lottery – but the prize is paid in a very peculiar way. For the first week, you will receive one cent. For week 2, you will receive 2 cents; 4 cents for week 3; 8 cents for week 4 – and the amount received keeps doubling every week until the end of the year. So at the end of week one, you will have been paid 1 cent; at the end of week two, 3 cents (2 + 1 cents); at the end of week three, the total will be 7 cents (4 + 3 + 1) and so on. Write a program that implements two recursive methods: one that given the week number, it will report the amount to be received for that week (i.e. week 10 pays $5.12); and second method that reports the total amount received to date given the week number (i.e. by the end of week 8, $2.55 has received in total). The program should repeatedly ask the user to indicate the number of weeks and report the results of the two recursive methods. If the user enters 0 weeks, the program will terminate.

Explanation / Answer

#include <stdio.h>
int getAmountForWeek(int weekNumber)
{
    if(weekNumber==0)
    {
     printf("Program Terminated");
    return 0;
    }
    if ( weekNumber!=1 )
        return (2*getAmountForWeek(weekNumber-1));
}

void getTotalAmountTillThatWeek(int weekNumber)
{
int sum=0;
for(int i=1;i<=weekNumber;i++)
{  
        sum=sum+getAmountForWeek(i);
}
float moneyInDollars=(double)sum/100;
    printf("This is the TOTAL amount for weekly money $ %.2f",moneyInDollars," " );
}


int main()
{
    int weeknumber=0;
    do
    {
    printf("Enter week number");
    scanf("%d",&weeknumber);
    if(weeknumber==0)
    break;
    else
    {
            int moneyForWeek=getAmountForWeek(weeknumber);
    float moneyInDOllars=0;
    moneyInDOllars=(double)moneyForWeek/100;
    printf("This is the amount for weekly money $ %.2f",moneyInDOllars," " );
    getTotalAmountTillThatWeek(weeknumber);

    }
    }while(weeknumber!=0);

    return 0;
}