Using C++ Step 1 Write a program in revarr.cpp that allocates an array of size 1
ID: 3875762 • Letter: U
Question
Using C++
Step 1
Write a program in revarr.cpp that allocates an array of size 10. Reads in array contents and prints the array in reverse. Check out the file in.txt and groundtruth.txt to see the expected input and output.
Compile your code as follows:
Step 2
Execute your code as follows:
$ ./revarr < in.txt > out.txt
Notice that we are using IO redirection to read from in.txt file as opposed to the standard input (or stdin).
Similarly, the output of the program is saved to out.txt file instead of the standard output (or stdout). Step 3
Compare the contents of out.txt and groundtruth.txt using the diff command as follows: $ diff out.txt groundtruth.txt
The above command will have no output if the two files are exactly the same.
We will be using this technique often to test the correctness of your code.
Task 2
Now modify revarr.cpp to create a new program in findeven.cpp that only prints even values in the array. Check your code using in.txt against both groundtruth.txt and groundtruth-even.txt files, i.e., do the following:
$ g++ findeven.cpp -o findeven
Task 3
Now modify findeven.cpp to create a new program sumeven.cpp that sums all even values in the array.
*******************************************************
in.txt
1 2 5 7 9 8 13 15 17 19 21 23 25 27 29 31
groundtruth.txt
19 17 15 13 11 9 7 5 3 1
groundtruthsum.txt
10
groundtrutheven.txt
2 8
Explanation / Answer
//Reverese of array elements in file
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream read;
read.open("in.txt");//input file name
ofstream write;
write.open("groundtruth.txt");//output file name
if(read.fail())
cout << "Cant open input.txt" << endl;
if(write.fail())
cout << "Cant open groundtruthsum.txt" << endl;
int i = 0;
int count = 0;
int A[1000];//provide max size of array
while (read >> A[i]) {
count++;
i++;
}
for (int j = count; j >= 0; j--) {
write << A[j] << " ";
}
read.close();
write.close();
return 0;
}
input:
1 2 1 2
output:
2 1 2 1
//File to add even values from one file to another file and sum to another file
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream read;
read.open("in.txt");//input file name
ofstream write;
write.open("groundtruthsum.txt");//output file name
ofstream write1;
write1.open("groundtrutheven.txt");//output file name
string line,line1;
int EvenSum;//
if(read.fail())
cout << "Cant open input.txt" << endl;
if(write.fail())
cout << "Cant open groundtruthsum.txt" << endl;
if(write1.fail())
cout << "Cant open groundtrutheven.txt" << endl;
if(read.is_open())
{
while(read, line)
{
EvenSum=0;
istringstream sRead(line);
while(sRead >> x)
{
if(x % 2 == 0)
EvenSum += x;
else
break;
}
}
write << EvenSum << endl;
cout << endl;
}
while(read, line1)
{
int i=0 ;
istringstream sRead1(line1);
while(sRead1 >> x)
{
if(i % 2 == 0)
write1<< line1 << endl;
}
}
read.close();
write.close();
write1.close();
system("pause");
return 0;
}
}
input:
1 2 3 4 5 6
output for groundtruthsum is : 12
output for groundtrutheven is : 2 4 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.