Question

In: Computer Science

1) Write an algorithm to calculate the sum of the following series: Sum =x-x3/3! + x5/5!...

1) Write an algorithm to calculate the sum of the following series:

Sum =x-x3/3! + x5/5! – x7/7! +……. Stop when the term<0.0001.

2) An internet service provider charges its subscribers per month as follows:

Data usage (n), in gbs charges (NIS)

0.0<n<=1.0 250

1.0<n<=2.0 500

2.0<n<=5.0 1000

5.0<n<=10.0 1500

n>10 2000

Write a C program to read the usage(n) from a file and print the charges to be paid by the subscribers.

Your program must include the function calculate that accept the data usage (n ) and returns the charge/month.

Solutions

Expert Solution

1 ans:

#include <stdio.h>

#include <stdio.h>

#include <math.h>

double fact(int n){

double prd=1;

for(int i=1;i<=n;i++){

prd=prd*i;

}return prd;

}

int main(){

double sum=0;

double term;

double x=3.2;

int cnt=1;

int i=0;

while(1){

term=pow(x,cnt)/fact(cnt);

if(term<0.0001){

break;

}

if(i%2==0){

sum=sum+term;

}else{

sum=sum-term;

}

cnt=cnt+2;

i++;

}

printf("%lf\n",sum);

return 0;

}

2 ans:

#include <stdio.h>

#include <stdlib.h>

int usage(double n){

if(n>=0.0 && n<=1.0){

return 250;

}else if(n>1.0 && n<=2.0){

return 500;

}else if(n>2.0 && n<=5.0){

return 1000;

}else if(n>5.0 && n<=10.0){

return 1500;

}else if(n>=10){

return 2000;

}

}

int main(){

FILE *ptr;

double num;

int filecontentlen=5;

if((ptr=fopen("data.txt","r")) != NULL){

for(int i=0;i<filecontentlen;i++){

fscanf(ptr,"%lf", &num);

int cost=usage(num);

printf("usage=%lf and price=%d\n",num,cost);

}

}else{

printf("Error found!");

}

return 0;

}

#data.txt


Related Solutions

Find the derivative of the following function. Do not use limits. f(x)=x5+(x3/3)-5x2+2x-3. Then, calculate the value...
Find the derivative of the following function. Do not use limits. f(x)=x5+(x3/3)-5x2+2x-3. Then, calculate the value of that derivative at x=1.
3. Consider the following recursive algorithm for computing the sum of the following series: S(n) =...
3. Consider the following recursive algorithm for computing the sum of the following series: S(n) = 1/1! + 2/2! + . . . + n/n!. ALGORITHM S (n) //Input: A positive integer n // Procedure: fact(n) returns the factorial of the number passed as parameter //Output: The sum of the series: S(n) = 1/1! + 2/2! + . . . + n/n! if n = 1 return 1 else return S(n − 1) + n/fact(n) Set up and solve a...
The values of X1, X2, X3, X4 and X5 are .1, .2, .08, .2 and .3....
The values of X1, X2, X3, X4 and X5 are .1, .2, .08, .2 and .3. Explicate the meaning of those numbers for the Z score. What are the prospects of this firm? Discuss thoroughly.
write a power series for f(x)=5/(x+3) c=2
write a power series for f(x)=5/(x+3) c=2
Fill in the following function to sum the series of 1 + x/1 + x^2/2 +...
Fill in the following function to sum the series of 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n using Java Program
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3!...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3! /3+4! /4+5! /5 using the function1, • to convert decimal number to binary number using the function2, • to check whether a number is a prime number or not using the function3, • to check whether two given strings are an anagram using the function4. important must do in (Multi-Filing) of c++
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
Write a complete program to sum the following series: (hint: use the for loop) 1 /2...
Write a complete program to sum the following series: (hint: use the for loop) 1 /2 + 1/ 4 + 1 /6 + ⋯ + 1 /100 PS: use C++ language please
Write the algorithm of program that evaluates the series 5-10+7+15+920+11+25+… up to x terms, where the...
Write the algorithm of program that evaluates the series 5-10+7+15+920+11+25+… up to x terms, where the value of x is taken as input from the user. For example, the sum of the given series up to 3 terms should give 2 as output (5-10+7=2), similarly, the sum of this series up to 4 terms should give 17 as output (5-10+7+15=17), Write this in psuedocode
1) Write an algorithm (i.e. the series of steps) to find the solution to a second...
1) Write an algorithm (i.e. the series of steps) to find the solution to a second order non-homogenous ODE (with boundary conditions) using the method of undetermined coefficients. (Note: this algorithm should include at least 1 control structure). 2) Write an if statement in MATLAB that converts an overall final percentage mark grade (1-7).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT