Need help with a Data Structures program

1.Create a C++ project with the program file below
2.Run the original program on the original data and see that your output is as below.
**note that the first number is the count of numbers remaining in the file
3.Make modifications to the program so that
**You find and print out all the pairs of numbers from the file that add up to zero.
**for each pair, print the two numbers, then print their their indices in order - all four values on 1 line
**do not include the count when looking for pairs
**when all pairs are found, print how many pairs there were
**you may add functions to your program
**you will almost surely add an array
**do not change anything in the main function before or after the comment lines marking the
boundaries of change

•Here is the output of the original program:
10
4
-10
5
3
29
-3
0
75
-3
10
It took 15 clock ticks to process the file

•Here is what the output should be like after modification:
-10 10 1 9
3 -3 3 5
3 -3 3 8
n pairs = 3
It took 15 clock ticks to process the file
**Note that the time will not be 15 ticks - this is just an example**

The .cpp file is as follows:
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;

#include <fstream>
using std::ifstream;
#include <cstdlib> // for exit function

#include <ctime> // for timing in ticks

// This program reads values from the file 'example.dat'
// and echoes them to the display until a negative value
// is read.

int main() {
clock_t t; // for keeping time in clock ticks
// see http://www.cplusplus.com/reference/ctime/clock/
t = clock();

ifstream indata; // indata is like cin

///// code below will be changed by you /////
int num; // variable for input value
indata.open("data.txt"); // opens the file
if(!indata) { // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}
indata >> num;
while ( !indata.eof() ) { // keep reading until end-of-file
cout << num << endl;
indata >> num; // sets EOF flag if [b]no val
ue found
}
///// code above will be changed by programmer /////

indata.close();
t = clock() - t;
cout << "It took " << t << " clock ticks to process the file" << endl;


return 0;
}[/b]
Last edited on
And? What are you asking?
I'm asking for the code that will allow the checking of pairs and printing it out only if its sum is 0. At least that's what I think is being asked.
Topic archived. No new replies allowed.