C++ Programming 1. Write a program that reads a set of integers into an array an
ID: 3665091 • Letter: C
Question
C++ Programming
1. Write a program that reads a set of integers into an array and find the sum of the odd and even integers. Use a sentinel value to end the input because you don’t know the number of entries up front. Display the calculated sums. Find the lowest and highest value in the set of integers and display the results. USE FUNCTIONS. I need a function to gather the required input, return the sum of the odd values via the function and return the sum of the even values via a pass by address variable. I need a function to return the lowest and another function to return the highest value. I need a function to display the calculated sums, the lowest and the highest values in addition to the input data (the contents of the array). I need a function to write the input data to a file whose name is provided by the user from the keyboard. Make sure to call the functions and show how the program works
Explanation / Answer
Here is the code
//The program reads integers from user untill invalid character or q is entered and all the integers are stored in a integer array input of size 100.
#include<iostream>
#include<ctype.h>
#include<string.h>
#include <fstream>
void readInput(int a[], int &i)
{
int c='a';
bool f=true;
while(cin >> c){
a[i] = c;
i++;
}
}
// function calculating sum of odd integers and returning sum
int findOddSum(int a[], int i)
{
int j=0,k=0,sum=0;
for(j=0;j<i;j++)
{
if(a[j]==1) sum+=a[j];
else if(a[j]%2>0)
sum+=a[j];
}
return sum;
}
// function calculating sum of even integers and returning sum using pass by reference
void findEvenSum(int a[], int i, int &evenSum)
{
int j=0,k=0;
evenSum=0;
for(j=0;j<i;j++)
{
if(a[j]%2==0)
evenSum+=a[j];
}
}
int findLowest(int a[], int i)
{
int l=a[0];
int j;
for(j=1;j<i;j++)
{
if(a[j]<l)
l=a[j];
}
return l;
}
int findHighest(int a[], int i)
{
int h=a[0];
int j;
for(j=1;j<i;j++)
{
if(a[j]>h)
h=a[j];
}
return h;
}
//function to display all values
void display(int a[], int i, int oddSum, int evenSum, int lowest, int highest)
{
int j;
cout << "Contents of array are ";
for(j=0;j<i;j++)
{
cout<<a[j]<<" ";
}
cout << endl;
cout << "Odd Sum "<< oddSum<<endl;
cout << "Even Sum "<< evenSum<<endl;
cout << "Lowest Value "<< lowest<<endl;
cout << "Highest Value "<< highest<<endl;
}
// Write integers to file myfile.txt
void writeToFile(int a[], int i)
{
int j;
cout << "Writing to file";
ofstream myfile;
myfile.open("myfile.txt");
for(j=0;j<i;j++)
{
myfile << a[j]<<" ";
}
}
int main(){
int i=0;
int input[100];
int oddSum, evenSum;
cout << "Enter a Integers press q to quit: "<< endl;
/*
Reads Integers from user and store them in array input. Use i as index of array.
of size 100.
*/
readInput(input,i); // read input from user
oddSum=findOddSum(input,i); // calculate odd sum
findEvenSum(input,i, evenSum); // calculate even sum
display(input,i,oddSum,evenSum,findLowest(input,i),findHighest(input,i));
writeToFile(input,i);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.