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...
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
Assignment 3 Chapter 2: Algorithm Discovery and Design More about Pseudocode Design an algorithm that is...
Assignment 3 Chapter 2: Algorithm Discovery and Design More about Pseudocode Design an algorithm that is given a positive integer N and determines whether N is a prime number, that is, not evenly divisible by any value other than 1 and itself. The output of your algorithm is either the message ‘not prime’, along with a factor of N, or the message ‘prime ‘ Many excellent simulations of sorting algorithms are available, examine them if they have questions about this...
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...
Implement the following problem as a self-contained Python program (module). Please write in beginner level code!...
Implement the following problem as a self-contained Python program (module). Please write in beginner level code! Thanks! The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die. After each roll: a) If the player rolls a 1 then the player gets no new points and it becomes the other player’s turn. b) If the player rolls...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT