Write a short program in C++ to read in a list of integers from a file. Each int
ID: 3709174 • Letter: W
Question
Write a short program in C++ to read in a list of integers from a file. Each integer should be placed in one of three arrays – the positive even numbers go into an array called evenNum, the positive odd numbers into an array called oddNum and the negatives into an array called negNum. Print the contents (with indexes) of each array – properly labeled – after the entire file has been read (print only those values you input from the file – not the unused elements).
Note that 0 is not positive or negative and should not be stored anywhere, yet is a valid value in the file.
You may assume the file contains no more than 30 integers – however, they may all be even, odd or negative and the file may contain less than 30 integers.
Connect to my input file by declaring the file name as a constant string, as in:
const string FILENAME = "/home/fac/ohsh/submit/18sq1230/files/lab4input.dat";
You must name your file lab4.cpp
Sample Output:
evenNum[0]: 2
evenNum[1]: 14
OR
evenNum array:
index 0: 2
index 1: 14
Explanation / Answer
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
int num1, i = 0, evenCount = 0, oddCount = 0, negCount = 0;
int evenNum[30], oddNum[30], negNum[30];
ifstream infile;
infile.open("lab4input.dat", ios::in);
while (! infile.eof()) {
infile >> num1;
i++;
if (num1 < 0) {
negNum[negCount] = num1;
negCount++;
} else if ((num1 % 2) == 0) {
evenNum[evenCount] = num1;
evenCount++;
} else if ((num1 % 2) != 0) {
oddNum[oddCount] = num1;
oddCount++;
}
}
infile.close();
cout << "NEGATIVE numbers are :: " << endl;
for(int i = 0; i < negCount - 1 ; i++) {
cout << "negNum[" << i << "] = " << negNum[i] << endl;
}
cout << "EVEN numbers are :: " << endl;
for(int i = 0; i < evenCount - 1 ; i++) {
cout << "evenNum[" << i << "] = " << evenNum[i] << endl;
}
cout << "ODD numbers are :: " << endl;
for(int i = 0; i < oddCount - 1 ; i++) {
cout << "oddNum[" << i << "] = " << oddNum[i] << endl;
}
return 0;
}
/********** input file = lab4input.dat **************
10
-20
30
-40
50
-60
70
-80
90
100
11
22
33
44
55
66
77
88
99
101
-10
20
-30
40
-50
60
-70
80
-90
-100
*********************************************/
/************************ OUTPUT OF PROGRAM *******************
NEGATIVE numbers are ::
negNum[0] = -20
negNum[1] = -40
negNum[2] = -60
negNum[3] = -80
negNum[4] = -10
negNum[5] = -30
negNum[6] = -50
negNum[7] = -70
negNum[8] = -90
negNum[9] = -100
EVEN numbers are ::
evenNum[0] = 10
evenNum[1] = 30
evenNum[2] = 50
evenNum[3] = 70
evenNum[4] = 90
evenNum[5] = 100
evenNum[6] = 22
evenNum[7] = 44
evenNum[8] = 66
evenNum[9] = 88
evenNum[10] = 20
evenNum[11] = 40
evenNum[12] = 60
ODD numbers are ::
oddNum[0] = 11
oddNum[1] = 33
oddNum[2] = 55
oddNum[3] = 77
oddNum[4] = 99
**************************************************************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.