The program needs to: Read input integers from a file into an array A; there wil
ID: 3818903 • Letter: T
Question
The program needs to:
Read input integers from a file into an array A; there will be up to 50 elements.
Then fill two new arrays with elements selected from A as indicated below.
Array A5 will contain all the elements of A that are multiples of 5.
Array Aodd will contain all the elements of A that are odd integers.
Output the contents of these arrays, with labels, to a file...
How do I fix the program below to have an array containing 50 elements and reading from a file?
Program below :
#include <iostream>
#include <fstream>
using namespace std;
ifstream infile;
ofstream outfile;
int main()
{
infile.open("unknown.txt");
outfile.open("list.txt");
int new_num;
int min;
infile >> new_num;
min = new_num;
while (infile)
{
if (new_num < min)
min = new_num;
infile >> new_num;
}
outfile << "Minimum number is " << min << endl;
infile.close();
outfile.close();
return 0;
}
Explanation / Answer
unknown.txt (Save this fuile under D Drive.Then the path of the file pointing to it is D:\unknown.txt)
12
23
34
45
56
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
43
44
45
46
47
48
49
50
51
52
53
54
55
54
55
56
57
58
__________________
Program:
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
//Creating an Arrays
int A5[50];
int Aodd[50];
//Declaring variables
int num,i=0,j=0;
int lenA5=0;
int lenAodd=0;
//defines an input stream for the data file
ifstream dataIn;
//Defines an output stream for the data file
ofstream dataOut;
//Opening the input file
dataIn.open("D:\unknown.txt");
if(dataIn.bad())
{
cout<<"** File Not Found **"<<endl;
return 1;
}
else
{
//Readiing the numbers from the file
while(!dataIn.fail())
{
//reading each number
dataIn>>num;
/* if the number is multiple of 5
* populate the value into an A5 array
*/
if(num%5==0)
{
A5[i]=num;
lenA5++;
i++;
}
/* if the number is odd
* populate the value into an Aodd array
*/
if(num%2!=0)
{
Aodd[j]=num;
lenAodd++;
j++;
}
}
}
dataIn.close();
//creating and Opening the output file
dataOut.open("D:\list.txt");
//Writing the Array Elements of A5 to the output file
dataOut<<"Array Elements which are multiplies of 5 are :"<<endl;
for(int i=0;i<lenA5;i++)
{
dataOut<<A5[i]<<" ";
}
//Writing the Array Elements of Aodd to the output file
dataOut<<endl<<"Array Elements Which are odd numbers :"<<endl;
for(int i=0;i<lenAodd;i++)
{
dataOut<<Aodd[i]<<" ";
}
//Closing the output file.
dataOut.close();
return 0;
}
________________
list.txt (We can See this file under D Drive.As we specified the path of the output file as D:\list.txt)
Array Elements which are multiplies of 5 are :
45 15 20 25 30 35 40 45 50 55 55
Array Elements Which are odd numbers :
23 45 15 17 19 21 23 25 27 29 31 33 35 37 39 43 45 47 49 51 53 55 55 57
_________________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.