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
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...
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.
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...
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
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
Please Write the whole program in assembly i am able to calculate the fibonacci series but...
Please Write the whole program in assembly i am able to calculate the fibonacci series but not sure how to print it in reverse order. Please give a Complete code !! Programming Exercise 1 (10 points): [call it Ass2-Q1.asm] Write an ASM program that reads an integer number N and then displays the first N values of the Fibonacci number sequence, described by: Fib(0) = 0, Fib(1) = 1, Fib(N) = Fib(N-2) + Fib(N-1) Thus, if the input is N...
Write pseudo-code to solve the problem using MapReduce and explain how it works. Each line in...
Write pseudo-code to solve the problem using MapReduce and explain how it works. Each line in the file lists a person’s ID, name, age, and the number of friends he or she has. For example line 1 indicates that the person has ID of 0, his name is Will, his age is 33, and he has 385 friends. Given the file, find out the average number of friends by age. 0,Will,33,385 1,Jean-Luc,26,2 2,Hugh,55,221 3,Deanna,40,465 4,Quark,68,21 5,Weyoun,59,318
Python: Write a program that makes sum of 1, 2, 3, … until the sum is...
Python: Write a program that makes sum of 1, 2, 3, … until the sum is over 1000.