Homework 24 Solution

Write a program that will read in a file of points in a format like this:

`N = 6`
`X:      1       1.5     2       3.1     6       8.3`
`Y:      5.5     5.2     5.1     5.2     5.7     6.6`

and output them as ordered pairs, i.e. like this:

`(1,5.5) (1.5,5.2) (2,5.1) (3.1,5.2) (6,5.7) (8.3,6.6)`

Turn In a screen capture of your program running on the file Input.txt and the source code for your program. (Hint: This is most easily done with two arrays.)

Solution

// KGS
// Hmwk 24
// This programs reads in a file of points and prints them as ordered pairs

#include <iostream>
#include <fstream>
using namespace
std;

int main() {
ifstream fin("input.txt");
if (!fin){
cout << "Could not open data file." << endl;
exit (1);
}

int size;
char junk;
fin >> junk >> junk >> size;

double* x;
x = new double[size];
fin >> junk >> junk;
for(int i=0;i<size;i++)
fin >> x[i];

double* y;
y = new double[size];
fin >> junk >> junk;
for(i=0;i<size;i++)
fin >> y[i];

//Print as ordered pairs
for(i=0;i<size;i++)
cout << '(' << x[i] << ',' << y[i] << ") ";
cout << endl;

delete [] x;  // deallocate dynamic memory

delete [] y;
return 0;
}