I have a program which sorts numbers written for a text file.
For example input.txt contains 4 2 0 3 4 1 2 0 7.
The fist number is the number of the data points, and the following are the (x,y) coordinates.

Here's my code:

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

int main ()
{
string num;
int i,j,x,y,temp,temp1;
ifstream infile;
infile.open ("input.txt");
getline(infile,num); // Saves the line in STRING.
cout << num << endl;
cout << "No of points listed: " << num[0] << endl; // It will output 4
for(x=2,y=4;x<=num.length(),y<=num.length();x=x+4,y=y+4)
{
cout << "Points : [" << num[x] << "," << num[y]<< "]" << endl;
// It will output (2,0) (3,4) (1,2) (0,7)
};
cout << " Sorted " << endl;
for(i=num.length()-1;i>0;i--)
{
for(j=2;j<i;j+=4)
{
if (num[j]>num[j+4])
{
temp = num[j];
temp1= num[j+2];
num[j]=num[j+4];
num[j+2]=num[j+6];
num[j+4] = temp;
num[j+6] = temp1;
}
}
}
for(x=2,y=4;x<=num.length(),y<=num.length();x=x+4,y=y+4)
{
if(num[x]==num[x+4])
{
if(num[y]>num[y+4])
{
temp = num[y];
num[y]=num[y+4];
num[y+4]=temp;
}}
cout<<"Points: ["<< num[x]<<","<< num[y]<<"]";
// It will output sorted (0,7)(1,2)(2,0)(3,4)
cout<<endl;

}
system ("pause");
}``````

The only problem is when i use digits with tens or hundreds places.
For example the input.txt contains: 3 22 10 6 17 13 5
The correct output should be: (22,10)(6,17)(13,5) sorted (6,17)(13,5)(22,10)
However the output is: (2, )(0,6)(1, )(3,5) sorted (0,6)(1, )(2, )(3,5)

I think the cause of this is the program reads the space in the input file as an element of the string and a two digit number two elements which is why there are blank spaces and missing numbers.

Is there a better way to code my program so that the spaces will not count as elements and numbers with two or more digits will only count as one element in the string.

Thank you guys very much =))

Ok, I solved the part in which the code sees the spaces and 2 digit numbers as elements and 2 elements respectively, However i have no idea on how to sort the data points.
Here's my input data: 10 9 9 10 10 8 8 1 1 7 7 2 2 6 6 3 3 5 5 4 4
Here's my code

``````#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
struct Point
{
int x, y ;
};
int main ()
{
ifstream infile("input.txt");
int n_points ;
infile >> n_points ;
cout << "No. of points: " << n_points << endl;
// outputs No.of points: 10
int points_read = 0 ;
while ( infile && points_read < n_points )
{	Point p ;
infile >> p.x >> p.y ;
cout << "Points: (" << p.x << ", " << p.y << ") " << endl;
// outputs Points: (9,9)(10,10)(8,8)(1,1)(7,7)(2,2)(6,6)(3,3)(5,5)(4,4)
}
system("pause");
}``````

Thanks again guys =))

why aren't you just reading the data as integers instead of strings? It would be a lot simpler and you won't have to worry about spaces. I'd put the data into a 2d array of ints which would then be easy to sort.

``````#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <list>
#include <map>
#include <fstream>

using namespace std;

struct Point
{
int x,y;
};

bool myobject(Point x1,Point x2)
{
return (x1.x<x2.x);
}
int main()
{
ifstream infile;
infile.open("input.txt");
int n_points = 0;

infile>>n_points;

vector<Point> myvector;
vector<Point>::iterator it;

for (int i = 1; i <= n_points; i++)
{
Point p;
infile>> p.x >> p.y;
myvector.push_back(p);
}

sort(myvector.begin(),myvector.end(),myobject);
for (it = myvector.begin(); it!=myvector.end(); it++)
{
Point q = *it;
cout<< " " << q.x << " " << q.y;
}

return 0;
}``````

Here, it works. If you have any questions, please ask

commented: Good deal. Now he doesn't have to do his own homework. -4

Thanks for your help, can you explain how the sorting works. I know the "sort" function sorts the whole line of vector. So how did it sort the y-axis even though it is in line with the x-axis.

Again thanks very much for your help, just want to clarify some stuff.

``sort(myvector.begin(),myvector.end(),myobject);``

This line will sort myvector from begin to end using the compare function myobject. So myobject will determine how the items are sorted.

``````bool myobject(Point x1,Point x2)
{
return (x1.x<x2.x);
}``````

It can be seen that myobject sorts two Point based on x-axis only :

``x1.x<x2.x``

Hope this will clarify your prob :)

Is there any way to convert the sorted outputs to an array form since Im more comfortable in using arrays instead of pointers.

Vector is not pointer, it is a dynamic array which has no size limit. You can access element normally as in array. Vector is only different from array such that you do not have to specify the size, it will dynamically grow as you push_back item.

You can read more to understand about vector in the link above

i tried accessing the elements in the vector by

``cout << myvector[1];``

as an example. However an error comes out saying

``no match for 'operator<<' in 'std::cout << (&myvector)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = Point, _Alloc = std::allocator<Point>](1u)'``

can you explain what does this mean =)

``````for (int i = 0 ; i < myvector.size(); i++)
{
cout<<myvector[i].x<<" "<<myvector[i].y<<" ";
}``````

This code is equivalent to the output in the previous piece of code. Because the vector item is a struct, you have to specify the field you want to output. If the vector item is only integer, then myvector is enough

Thanks for all your help, the my program is now working fine

Just replace the output part. Here's the full code

``````#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <list>
#include <map>
#include <fstream>

using namespace std;

struct Point
{
int x,y;
};

bool myobject(Point x1,Point x2)
{
return (x1.x<x2.x);
}
int main()
{
ifstream infile;
infile.open("input.txt");
int n_points = 0;

infile>>n_points;

vector<Point> myvector;
vector<Point>::iterator it;

for (int i = 1; i <= n_points; i++)
{
Point p;
infile>> p.x >> p.y;
myvector.push_back(p);
}

sort(myvector.begin(),myvector.end(),myobject);
for (int i = 0 ; i < myvector.size(); i++)
{
cout<<myvector[i].x<<" "<<myvector[i].y<<" ";
}

return 0;
}``````

I have further expanded the program into a cubic splines program which outputs the equation for the cubic splines.
Here is the code:

``````#include <iostream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <list>
#include <map>
#include <fstream>
#include <math.h>
#include <cstdlib>
using namespace std;

struct Point
{
double x,y,h,c,b,d,s;
};

bool myobject(Point x1,Point x2)
{
return (x1.x<x2.x);
}
int main()
{
ifstream infile;
infile.open("input.txt");
int n_points = 0;

infile>>n_points;
cout << "No. of points: " << n_points << endl;
vector<Point> myvector;
vector<Point>::iterator it;

for (int i = 1; i <= n_points; i++)
{
Point p;
infile>> p.x >> p.y;
myvector.push_back(p);
cout << "Points: (" << p.x << ", " << p.y << ") " << endl;
}

sort(myvector.begin(),myvector.end(),myobject);
cout << "Sorted" << endl;;
for (it = myvector.begin(); it!=myvector.end(); it++)
{
Point q = *it;
cout << "Points: (" << q.x << ", " << q.y << ") " << endl;
};

double A[n_points][n_points], temp[1][1];
int  ctr, ctr2, next=0, check = 1;

for( ctr=0; ctr<(n_points-1); ctr++)
{
myvector[ctr].h = myvector[ctr+1].x - myvector[ctr].x;
}

for(ctr=1; ctr<(n_points-1); ctr++)
{
myvector[ctr].c = 3*(((myvector[ctr+1].y-myvector[ctr].y)/(myvector[ctr+1].x-myvector[ctr].x))-((myvector[ctr].y-myvector[ctr-1].y)/(myvector[ctr].x-myvector[ctr-1].x)));
}
myvector[0].c=myvector[n_points-1].c=0;
for(ctr=0; ctr<n_points; ctr++)                // set all initially to zero
{
A[ctr][next] = 0;
if(ctr==(n_points-1) && next != (n_points-1))      //next column
{
next = next + 1;
ctr = -1;
}
}
A[0][0]=A[n_points-1][n_points-1]=1;                 // set 1
for(ctr=1; ctr<(n_points-1); ctr++)                // assign the values
{
A[ctr][ctr] = 2*(myvector[ctr-1].h+myvector[ctr].h);
A[ctr][ctr-1] = myvector[ctr-1].h;
A[ctr][ctr+1] = myvector[ctr].h;
}

//Start of Gauss Elimination
next=1;
for (ctr=1; ctr<3 ; ctr++)                         // Setting diagonals to 1 and creating upper triangle of matrix
{
for(ctr2=3; ctr2>-1; ctr2--)
{
A[ctr][ctr2] = A[ctr][ctr2] / A[ctr][ctr];
if(check==1)
{
myvector[next].c = myvector[next].c /A[ctr][ctr];
next++;
check = 0;
}
}
temp[0][0] = A[ctr+1][ctr];
check = 1;

for(ctr2=1; ctr2<3; ctr2++)
{
A[ctr+1][ctr2] =  (-1*A[ctr][ctr2]*temp[0][0]) + A[ctr+1][ctr2];
if (check==1)
{
myvector[ctr2+1].c = (-1*myvector[ctr2].c*temp[0][0]) + myvector[ctr2+1].c;
check = 0;
}
}
check = 1;
}

for (ctr=2; ctr>0 ; ctr--)             //  Lower trinagle to complete identity matrix
{
temp[0][0] = A[ctr-1][ctr];
for(ctr2=2; ctr2>0; ctr2--)
{
A[ctr-1][ctr2] = (-1*A[ctr][ctr2]*temp[0][0]) + A[ctr-1][ctr2];
if (check==1)
{
myvector[ctr2-1].c = (-1*myvector[ctr2].c*temp[0][0]) + myvector[ctr2-1].c;
check = 0;
}
}
check = 1;
}                                      // after this for loop is finished, the c coeffecients (c[n]) would already-
// -be solved
A[0][0] = A[2][3] = 0;                 // can automatically be set to zero

for(ctr=0; ctr<n_points-1; ctr++)                   // b and d coeffecients
{
myvector[ctr].b = ((myvector[ctr+1].y-myvector[ctr].y)/myvector[ctr].h) - (myvector[ctr].h/3)*(2*myvector[ctr].c+myvector[ctr+1].c);
myvector[ctr].d = (myvector[ctr+1].c-myvector[ctr].c) / (3*myvector[ctr].h);
}
// a coeffecients are automatically assigned to y[n]
cout<< "The cubic splines are:\n";
for(ctr=0; ctr<n_points-1; ctr++)
{
cout<<"s"<<ctr+1<<" = "<<myvector[ctr].y<<" + ("<<myvector[ctr].b<<"(x-"<<myvector[ctr].x<<")) + "<<"("<<myvector[ctr].c<<"(x-"<<myvector[ctr].x<<")^2) + ("<<myvector[ctr].d<<"(x-"<<myvector[ctr].x<<")^3)\n";
}
system("pause");
}``````

Is there a way to input certain "x" values whose "y" values can be calculated by based by the equation of the cubic splines.
I've tried

``````double FO(double X)
{
return myvector[ctr].y + myvector[ctr].b*(X - myvector[ctr].x) +  myvector[ctr].c*(pow(X - myvector[ctr].x,2)) + myvector[ctr].d*(pow(X - myvector[ctr].c,3));
}``````

However there are errors such as

``````a function-definition is not allowed here before '{' token
expected `,' or `;' before '{' token``````

If i add a ';' before '{', the error then becomes

```X' undeclared (first use this function)``

Isn't it already declared.

Thanks for all you help =))

Please PLEASE post the grade you get for handing in tungnk1993's work/suggestions. I'd really like to know your instructor's comments.