hello ..
i just need a lil guideline... m making a program where i have to apply counting sort on the data which is comma seperated and it is written in a text file like this
59,54,40,79,38
28,98,77,71,74
24,91,56,82,51
74,36,98,29,41
39,10,52,3,24
30,5,4,70,15
29,55,40,56,23
8,94,69,13,37
79,96,54,53,29
12,29,30,99,35
63,87,64,6,6
65,56,80,37,89
total no. of lines are 20 and each line contains 5 elements.
problem is i cant get it how to store all the numbers in an array
i have made the function which will take the data from the file and it will covert it into an int but i want the whole data in an array so that i can apply the counting sort in it..
#include<iostream>
#include<cstdlib>
#include<string>
#include<fstream>
using namespace std;
string getdata(string line,int n)
int recNo=0, prvComma=0, nxtComma;
string out;
for(int i=0; i<line.length(); i++)
{
nxtComma=line.find(',',prvComma);
out=line.substr(prvComma,nxtComma-prvComma);
prvComma=nxtComma+1;
if(recNo++==n)
return out;
}
}
void main()
{
string line;
int i;
int num;
int num1,num2,num3,num4;
double m,n;
int data[5];
ifstream myfile("CS DataSet.txt");
if(! myfile.is_open())
{
cout<<"unable to open"<<endl;
return;
}
for(i=0; i<1; i++)
{
getline(myfile,line);
num=strtod(getdata(line,0).c_str(),NULL);
num1=strtod(getdata(line,1).c_str(),NULL);
num2=strtod(getdata(line,2).c_str(),NULL);
num3=strtod(getdata(line,3).c_str(),NULL);
num4=strtod(getdata(line,4).c_str(),NULL);
}
data[0]=num;
data[1]=num1;
data[2]=num2;
data[3]=num3;
data[4]=num4;
for(i=0;i<5;i++)
{
cout<<data[i]<<endl;
}
myfile.close();
system("PAUSE");
}