Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following C language code aimed to compute 1+2+3 . . . +n using divide and conquer approach, where n is an integer number. But

image text in transcribedThe following C language code aimed to compute 1+2+3 . . . +n using divide and conquer approach, where n is an integer number. But the code provides correct answer when n is 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, . . .. Correct the code (i.e., the sum function) so that it provides correct answer when n is 1, 2, 3, 4, 5, 6, 7, 8, 9, . . ..

1. (14 marks) The following language code aimed to compute 1+2+3 ... tn using divide and conquer approach, where n is an integer number. But the code provides correct answer when n is 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, .... Correct the code (i.e., the sum function so that it provides correct answer when n is 1, 2, 3, 4, 5, 6, 7, 8, 9, .... #include int sum(int n) if (n==1) return 1; if (n>1) return 2*sum (n/2)+(n/2)*(n/2); int main() int n; printf( "Enter the value of n: "); scanf("%d", &n); int res=sum(n); printf ("Output: %d", res)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management System MCQs Multiple Choice Questions And Answers

Authors: Arshad Iqbal

1st Edition

1073328554, 978-1073328550

More Books

Students also viewed these Databases questions

Question

What is the measure of Arc CB? ? 60 B 84 60 90 67

Answered: 1 week ago