Question

In: Computer Science

1) Write a function equation(A, B) that returns a value C where A + B =...

1) Write a function equation(A, B) that returns a value C where A + B = C

a. Write the equation function:

b: Write the output of your function for a few values:

2) Write a non-recursive function called fact(n) that computes n! Try computing fact(n) for large values. Can you find the maximum value for n your program will compute correctly?

a) Write your fact function:

b) Write the output of your function for a few values:

3)

  1. Write a function called printThem(n) which print the numbers from 1 to n with the following conditions:

  • In place of multiples of 3 print three.

  • In place of multiples of 5, print five.

  • If a number is divisible by both 3 and 5 print fifteen.

  • Do not print multiples of 7 (even if a multiple of 3)

Display 15 numbers per line. Include sample output for n=50. Note that 50 should be the parameter when you call your printThem function, not part of the function itself.

a) Print Them(n) function:

b) Write your output function

4)

  1. Write a function makeChange(amount) that for a given amount of money makes change for that amount in nickels, dimes, and quarters. You can assume the amount will be 100 cents or less. For example:

Change for 100:

  nickels: 4

  dimes: 3

  quarters: 2

When you are done, think about how you could calculate all possible ways to make change for that amount. That increases the complexity considerably, so it is not required for full credit.

a) Write the output of your makeChange function

b) Write your makeChange function:

*****************************************************************************************************************************************

Please answer these questions in C and write comments on why some actions are being done! Please give which function to write to each problem as well as the output function!! Thank you so much!

Solutions

Expert Solution

//C program

#include<stdio.h>

int equation(int a , int b){
   return a+b;
}

int fact(int n){
   if(n==0)return 1;
   int f = 1;
  
   while(n>0){
       f*=n;
       n--;
   }
   return f;
}

int printThem(int n){
   int i;
   int count=0;
   for(i=1;i<=n;i++){
       if(i%7==0)continue; //if number is divisible by 7 then print nothing
       else if(i%3==0&&i%5==0)printf(" fifteen "); //if number is divisible by 3 and 5 then print fifteen
       else if(i%3==0)printf(" Three "); //if number is divisible by 3 then print three
       else if(i%5==0)printf(" five "); //if number is divisible by 5 then print five
       else printf("%d ",i); //else print number
       count++;
       if(count==15)printf("\n");//in a line only 15 numbers are possible
   }
}

void makeChange(int amount){
   int cents_count=0,nickels_count=0, dimes_count=0,quarters_count=0;
   printf("\nChange for : %d\n",amount);
   while(amount>=25){
       amount-=25;
       quarters_count++;
   }
   while(amount>=10){
       amount-=10;
       dimes_count++;
   }
   while(amount>=5){
       amount-=5;
       nickels_count++;
   }
   while(amount>0){
       amount--;
       cents_count++;
   }
   printf("Cent : %d\n",cents_count);
   printf("Nikel : %d\n",nickels_count);
   printf("Dime : %d\n",dimes_count);
   printf("Quarter : %d\n",quarters_count);
}

int main(){
   printf("\n3+4 : %d\n",equation(3,4));
   printf("\n5+5 : %d\n",equation(5,5));
   printf("\n20+50 : %d\n",equation(20,50));
  
   printf("\nFactorial of 5 : %d\n",fact(5));
   printf("\n\n");
   printThem(50);
   printf("\n\n");
   makeChange(98);
   return 0;
}

//sample output


Related Solutions

C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x...
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x contains an odd number of 1s (in its binary representation) and 0 otherwise. You can consider x as a four byte integer, i.e. w = 32 bits. Constraint: Your solution can use at most 12 arithmetic, bitwise, and logical operations.
In C++, write a function that returns the average of all the values stored in an...
In C++, write a function that returns the average of all the values stored in an integer array (called arr) of size n. The return type of the function should be double. Test this function in the main program.
IN C++ The Quadratic equation is used to solve for the variable x where the function...
IN C++ The Quadratic equation is used to solve for the variable x where the function evaluates to zero. However there is actually two answers returned by the quadratic equation. Can we solve for the quadratic equation using just one call to a function you write? Hint : create a function that does not 'return' anything. Takes three parameters a,b,c as double.s   And had two more parameters that are passed by reference to two doubles that are the two answers...
C++ Suppose  getAcctNum() returns the account number of a given Account variable.  Write a function that returns a...
C++ Suppose  getAcctNum() returns the account number of a given Account variable.  Write a function that returns a boolean value to find if a given account number appears in an array of Account objects.
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns...
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns the highest similarity score found in the genome as a double. Note: the term genome refers to the string that represents the complete set of genes in an organism, and sequence to refer to some substring or sub-sequence in the genome. Your function MUST be named findBestSimScore Your function should take two parameters in this order: a string parameter for the genome (complete set...
Write a Python function that returns a list of keys in aDict with the value target....
Write a Python function that returns a list of keys in aDict with the value target. The list of keys you return should be sorted in increasing order. The keys and values in aDict are both integers. (If aDict does not contain the value target, you should return an empty list.) This function takes in a dictionary and an integer and returns a list. def keysWithValue(aDict, target): ''' aDict: a dictionary target: an integer ''' # Your code here
16. Write a function that returns the start value of a hailstone sequence that contains the...
16. Write a function that returns the start value of a hailstone sequence that contains the largest value that was reported by largestInAnyHS(n). Write a contract, then an implementation, of a function that takes exactly one parameter, an integer n, and returns the start value from 1 to n of the hailstone sequence that contains the largest value. The heading must be int startHSWithLargest(int n) This function must not read or write anything. Modify your main function so that it...
1- Write a function f(n,a,b) that generates n random numbers # from Uniform(a,b) distribution and returns...
1- Write a function f(n,a,b) that generates n random numbers # from Uniform(a,b) distribution and returns their minimum. # Execute the function with n=100, a=1, b=9. 2- Replicate the function call f(100,1,9) 100 thousand times # and plot the empirical density of the minimum of 100 indep. Unif(1,9)'s 3-Use the sampling distribution from (b) to find 95% confidence # interval for the minimum of 100 independent Unif(1,9)'s. Please solve in R
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT