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.
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;
}
Step by step
Solved in 3 steps with 1 images