Write a program that will take the sum of the odd-indexed array elements in array of 50 integers completely filled.  This is my C++ Codes but when I entered the 50 integers, the sum of the odd-indexed are wrong. Can you fix the errors? Please still use header files iostream header files. This is a topic under Data Structures: Arrays.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter7: Arrays
Section7.4: Arrays As Arguments
Problem 5E
icon
Related questions
Question
100%

Problem:

Write a program that will take the sum of the odd-indexed array elements in array of 50 integers completely filled. 

This is my C++ Codes but when I entered the 50 integers, the sum of the odd-indexed are wrong. Can you fix the errors? Please still use header files iostream header files. This is a topic under Data Structures: Arrays.

//Start of program
#include <iostream>

using namespace std;

int main()
{
//Declare array and initialize variables
int a[50], sum = 0, i;

//Prompt the user
cout << "Enter 50 values of integer array: \n";

//For loop
for (i = 0; i < 50; i++)
{
cin >> a[i];
}

//Odd-indexed numbers
for (i = 0; i < 50; i++)
{
if (a[i] % 2 != 0) //If the remainder when element at i is divided by 2 yields a non-zero value
{
continue; //Iteration repeated wherein element at i is incremented and tested
}
sum += a[i]; //Statement executed and iteration continued
}

//Prompt the message with the sum of odd-indexed numbers
cout << "The sum of odd-indexed numbers are: " << sum << endl;

//End of program
return 0;
}

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Array
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++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr