In: Computer Science
1.Write a C++ program using control structures, arrays, functions to calculate the number of dollars, quarters, dimes, nickels, and pennies in a given amount of money. The input should be a floating-point value representing a decimal value. Example: 63.87 à 63 [dollars and 87 cents] should output 63 dollars, 3 quarters, 1 dime, 0 nickels, and 2 pennies.
2. In trigonometry the cosine function can be calculated using cos(x) = 1 – x 2 /2! + x 4 /4!- x 6 /6! + x8 /8!- … Write a C++ program that uses a function to calculate the cos of a positive value (radians) by summing the 1st 15 terms of this series. You will need to write a separate function to calculate a factorial. Write another function that calculates the relative error between using 15 terms and 5 terms. The relative error can be calculated as |cosine(x, 15) – cosine(x, 5)|/cosine(x, 15).
question 2 solution
// CPP program to find the
// sum of cos(x) series
#include <bits/stdc++.h>
using namespace std;
const double PI = 3.142;
double fact(double c, int i)
{
c = c * (2 * i - 1) * (2 * i);
return c;
}
double cosXSertiesSum(double x,
int n)
{
double res = 1;
double sign = 1, c = 1,
pow = 1;
for (int i = 1; i < n; i++)
{
sign = sign * -1;
c = fact(c,i);
pow = pow * x * x;
res = res + sign *
pow / c;
}
return res;
}
double error(double x)
{
double a = cosXSertiesSum(x,15) - cosXSertiesSum(x,5);
if(a<0)
{
a = a - 2*a;
}
return a/cosXSertiesSum(x,15);
}
// Driver Code
int main()
{
double x;
cout<<"Enter the value in
radians"<<endl;
cin>>x;
int n = 15;
cout << cosXSertiesSum(x, n)<<endl;
cout<<error(x);
}
question 1 solution
#include<iostream>
using namespace std;
struct Money{
int dollars;
int cents;
};
int print(Money);
Money cent(Money);
int main()
{
struct Money m;
m.dollars = 63;
m.cents = 87;
print(m);
}
Money cent(Money m)
{
cout<<"Quaters "<<m.cents/25<<endl;
m.cents = m.cents-(25*(m.cents/25));
cout<<"Dime "<<m.cents/10<<endl;
m.cents = m.cents-(10*(m.cents/10));
cout<<"Nickels "<<m.cents/5<<endl;
m.cents = m.cents-(5*(m.cents/5));
cout<<"Pennies "<<m.cents/1;
}
int print(Money m){
cout<<"Dollars are "<<m.dollars<<endl;
cent(m);
}