Problem 13 Vrite a function that takes a list and computes the alternating sum of all its elements. An alternating sum alternates between dding and subtracting elements. The example returns -2 because 1 – 4+9 – 16 +9 – 7 +4 – 9+11. Your function should eturn the alternating sum of a list. xample IO: lt-sum ([1.4.9.16.9.7.4.9.11]) #returns

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 16PE
icon
Related questions
Question
100%

python problem

Problem 13
Write a function that takes a list and computes the alternating sum of all its elements. An alternating sum alternates between
adding and subtracting elements. The example returns -2 because 1 – 4+9 – 16+9 – 7+4 –- 9+ 11. Your function should
return the alternating sum of a list.
Example IO:
alt-sum ([1,4 ,9 , 16 ,9 , 7 ,4 ,9 ,11]) #returns -2
Transcribed Image Text:Problem 13 Write a function that takes a list and computes the alternating sum of all its elements. An alternating sum alternates between adding and subtracting elements. The example returns -2 because 1 – 4+9 – 16+9 – 7+4 –- 9+ 11. Your function should return the alternating sum of a list. Example IO: alt-sum ([1,4 ,9 , 16 ,9 , 7 ,4 ,9 ,11]) #returns -2
Expert Solution
Step 1

PROGRAM CODING:

#include<iostream.h>
using namespace std;

int alternate_sum(int ar[], int size)
 {
    int sign = 1, sum = 0;
    for (int a = 0; a < size; ++a)
 {
        sum += sign*arr[a];
        sign *= -1;
    }
    return sum;
}

 

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Fundamentals of Boolean Algebra and Digital Logics
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr