Question

1- Write algorithm (in pseudo-code) for the following problem: sum of the series: 1 +2+3+…+N 2-...

1- Write algorithm (in pseudo-code) for the following problem:

sum of the series: 1 +2+3+…+N

2- Convert an algorithm to a flowchart diagram.

3- Counting the primitive operations of the algorithm.

4- Determine algorithm complexity.

5- Write a complete C++ program for the previous algorithm where the program should contain the following:

 Display messages.  Input any number;  Display the series.  Display the sum of the series.

Homework Answers

Answer #1

1. Algorithm()

1 Taking user Input = n

2 Initialize : sum = 0

3 for i = 1 to n do

4 sum =sum + i

5 return sum

2.Flowchart

3.Primitive Operation

Primitive operations are basic computations within an algorithm. evaluating an expression, assigning a value to a variable,calling a method, returning from a method , all these are the examples of primitive operations.

for taking user Input we are assuming that , It takes 1 primitive operation.

Algorithm ()

Number of primitive operations:

1 Taking user Input = n

2 Initialize : sum = 0

3 for i = 1 to n do

4 sum =sum + i

5 return sum

1 ( let)

1

2n

4n (including increment counter)

1

  total primitive Operation: 1+ 1+ 2n +4n + 1 = 6n +3

4.Complexity

Algorithm ()

complexity:

1 Taking user Input = n

2 Initialize : sum = 0

3 for i = 1 to n do

4 sum =sum + i

5 return sum

O(1)

O(1)

O(n)

O(1)

O(1)

Total time complexity = O(1) +O(1) + {O(n) *O(1) } + O(1) = O(n)


5. C++ program :

#include <iostream>

using namespace std;
int main()
{
int n, sum = 0;
// taking user input
cout << "Enter the input : ";
cin >> n;
//Displaying the series
cout << "Series :";
for (int i = 1; i <= n; ++i) {
cout << i<<" " ;
}
//calculating sum of the series using for loop
for (int i = 1; i <= n; ++i) {
sum += i;
}
//Displaying the sum
cout << "\nSum of the Series = " << sum;
return 0;
}

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(Artificial Intelligence) Write pseudo code for the following conditions Genetic algorithm with population size 1
(Artificial Intelligence) Write pseudo code for the following conditions Genetic algorithm with population size 1
1) Write an algorithm to calculate the sum of the following series:           Sum =x-x3/3! +...
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...
Draw flowchart AND write pseudo code User asks you to develop a program to calculate and...
Draw flowchart AND write pseudo code User asks you to develop a program to calculate and print weekly payroll. Calculations must take into account the following: User input ? a. Income tax rate is rate is 15% (.15*salary) b. Social Security tax is 7.65% (.0765*salary) c. Display salary, income tax, social security tax, and net pay net pay=salary-income tax – social security tax
Write an algorithm to calculate the sum of the following series:           Sum =x-x3/3! + x5/5!...
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. very quick please.
Draw flowchart AND write pseudo code for each of the following problems. 1. User asks you...
Draw flowchart AND write pseudo code for each of the following problems. 1. User asks you to develop a program that calculates and then prints interest earned on a bank balance. Program should print interest earned for the same balance when interest is accumulated annually, semiannually and quarterly. Interest earned yearly=balance * rate /100 for annual Interest earned semiannually =balance*rate/2/100 for semi annual Interest earned quarterly= balance*rate/4/100 for quarterly
What will be the expected output of the following pseudo code? Write exactly what would display...
What will be the expected output of the following pseudo code? Write exactly what would display when you execute the statements. Module main() Declare Integer number Display "Enter a number and I will display" Input number Call doubleNumber(number) Display number End Module Module doubleNumber(Integer value) Declare Integer result Set result = value * 2 Display result End Module
11. Write a program to compute the sum of the series 12 + 22 + 32....
11. Write a program to compute the sum of the series 12 + 22 + 32. . . ., such that the sum is doesnot exceed 1000. The program should display how many terms are used in the sum. {3 marks} matlab only 1 to power of 2 , 2 to the power of 2 , 3 to the power of 3 , not 12 + 22 + 32
Consider the following recursive algorithm. Algorithm Mystery(n) if n=1 then Execute Task A; // Requires Θ(1)...
Consider the following recursive algorithm. Algorithm Mystery(n) if n=1 then Execute Task A; // Requires Θ(1) operations else Mystery(n/3); Mystery(n/3); Mystery(n/3); Execute Task B;  //Requires 2n operations end if Let C(n) be the complexity of Mystery(n). Use the method of backward substitution to determine C(n) in three steps. a) Write the recurrence relation for C(n) including the initial condition. b) Write at least two substitution steps for C(n) and identify the pattern. c) Determine the complexity class of the algorithm in...
Answer the following questions: 1. Given the code segments below with n as the problem size,...
Answer the following questions: 1. Given the code segments below with n as the problem size, answer the following questions: //Code Segment 1 (Consider n as a power of 3) int sum = 0; for(int i = 1; i <= n; i = i*3) sum++;​​​​​​​// statement1 //Code Segment 2: (Consider both possibilities for x) if(x < 5){ for(int i = 1; i <= n; i++)    for(int k = 1; k <= i; k++)    sum++;​​​​​// statement2 } else{ for(int...
write a java code. Write a program using loops to compute the sum of the 30...
write a java code. Write a program using loops to compute the sum of the 30 terms of the series below. 91/(3 + 2 + 2) + 92/(4 - 4 + 5) + 93/(5 + 6 - 8) + 94/(6 - 8 + 11) + 95/(7 + 10 + 14) + 96/(8 - 12 - 17) + . . . . Output: Term Ratio Sum 1 13 13 2 18.4 31.4 etc etc
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT