Question

Write a program with a loop and indexed addressing that calculates the sum of all the...

Write a program with a loop and indexed addressing that calculates the sum of all the gaps

between successive array elements. The array elements are doublewords, sequenced in nonde-

creasing order. So, for example, the array {0, 2, 5, 9, 10} has gaps of 2, 3, 4, and 1, whose sum

equals 10.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;
int sumOfGaps(int arr[],int size){
int sum=0;
for(int i=0;i<size-1;i++)
// finding the difference between 2 elements and adding to sum
sum=sum+(abs(arr[i]-arr[i+1]));
return sum;
}
int main(){
int arr[]={0, 2, 5, 9, 10};
cout<<"Sum of gaps : "<<sumOfGaps(arr,5);
return 0;
}

1 #include<iostream» 2 using namespace std; 3 int sumOfGaps (int arr[],int size){ 4. int sum-0; for(int i-0;i<size-1;i++) /fi

Add a comment
Know the answer?
Add Answer to:
Write a program with a loop and indexed addressing that calculates the sum of all the...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a. write a program that calculates the sum of two numbers x and y and then...

    a. write a program that calculates the sum of two numbers x and y and then the program should call the function by address and prints the sum b. write a program that stores 10 integers provided at run time in an array. the program should prompt the user for a random integer exit in the array, the value must be replaced by a-1

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • Write a complete program in assembly line that:    1. Prompt the user to enter 10...

    Write a complete program in assembly line that:    1. Prompt the user to enter 10 numbers.    2. save those numbers in a 32-bit integer array.    3. Print the array with the same order it was entered.    3. Calculate the sum of the numbers and display it.    4. Calculate the mean of the array and display it.    5. Rotate the members in the array forward one position for        9 times. so the last...

  • Write a program that finds (ANSWER IN C LANGUAGE!): 1. The sum of all elements at...

    Write a program that finds (ANSWER IN C LANGUAGE!): 1. The sum of all elements at even subscripts 2. The sum of all elements at odd subscripts 3. The sum of all elements You are allowed to perform this functionality within main. Main program: ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #include <stdio.h> int main() { /* Declare array variables */ const int NUM_VALS = 4; int userValues[NUM_VALS]; int i; // counter int even = 0; int odd = 0; int sum = 0; /* Initialize...

  • Write a program using loop and array that asks users to input 10 numbers, then calculates...

    Write a program using loop and array that asks users to input 10 numbers, then calculates the average of those numbers. (In C++)

  • Write a program that reads two integer values. It then calculates and displays the sum and...

    Write a program that reads two integer values. It then calculates and displays the sum and average of all values between them, Le. if the first value is vi and the second value is 2, then the program calculates and displays the sum and average of all values in the closed range Iv1,2 Your Program must satisfy the following constraints: A. make sure that v1 is less than 2 B. Use a function named getStats(that takes two integer yalues as...

  • C++. Write a program that copies the contents of one array into another array but in...

    C++. Write a program that copies the contents of one array into another array but in reverse order using pointers.           - in main()                    - define 2 arrays of type int, 10 elements each                              - initialize one array with numbers 1 through 10                              - initialize all elements of the second array to 0                    - call function reverseCopy with both arrays as arguments                    - display the values of array number 2 (reversed order)           - reverseCopy...

  • java CountDigits.java Write a program that calculates and prints, the sum and product of all the...

    java CountDigits.java Write a program that calculates and prints, the sum and product of all the digits in an integer value read from the keyboard.

  • C# : Write a program which calculates Sum, Average of a given array. Array must be...

    C# : Write a program which calculates Sum, Average of a given array. Array must be initialized using initializer. use loops to calculate the same display the output.

  • Please answer this python questions.Thank you! Question Two      Write a program that calculates the amount...

    Please answer this python questions.Thank you! Question Two      Write a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what the salary was for each day, and then show the total pay at the end...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT