PDA

View Full Version : c++ code to count the number of rows and coloumns



prabhanjan2906
June 16th, 2009, 04:50 PM
i needed a c++ code to display row and column of any given matrix. the matrix being present in a text file. the objective is to read the matrix from a file and to display the number of rows and columns.

Bachstelze
June 16th, 2009, 04:53 PM
This sounds very much like a school assignment. Anyway, moved to Programming Talk.

prabhanjan2906
June 16th, 2009, 04:56 PM
actually i need to do the same for 2 matrices ad add them..

Zugzwang
June 16th, 2009, 05:14 PM
Convince us that this is not homework. Posting homework questions is against the rules of this forum.

Mirge
June 16th, 2009, 05:42 PM
Show us what you've done so far... so we know where you need help.

prabhanjan2906
June 16th, 2009, 05:45 PM
#include<iostream.h>
#include<ctype.h>
#include<fstream.h>
#include<conio.h>
void main()
{
fstream fptr("kk.txt",ios::in|ios::out);
char ch;
int n=0,m=1,row,col;
clrscr();
fptr.seekg(0,ios::beg);
while(!fptr.eof())
{
fptr.get(ch);
if(ch!='\n')
{
if(isdigit(ch))
{ n=n+1;
if(!fptr.eof())
{
cout<<ch;

}
}

}
if(ch=='\n')
m=m+1;
}
row=m;
col=n/m;

fptr.clear();
cout<<endl<<row<<" "<<col;
}

this will consider only a single digit number. but for a 2 or more digit number it does not work properly

kjohansen
June 16th, 2009, 06:02 PM
unless you have delimiters in the text file you cant do it. How do I know if 101010 is 1,0,1,0,1,0, or 10,10,10 or 101,010, or you get the point. You will need to store the file with commas or spaces or some sort of delimiter...

Mirge
June 16th, 2009, 07:21 PM
#include<iostream.h>
#include<ctype.h>
#include<fstream.h>
#include<conio.h>
void main()
{
fstream fptr("kk.txt",ios::in|ios::out);
char ch;
int n=0,m=1,row,col;
clrscr();
fptr.seekg(0,ios::beg);
while(!fptr.eof())
{
fptr.get(ch);
if(ch!='\n')
{
if(isdigit(ch))
{ n=n+1;
if(!fptr.eof())
{
cout<<ch;

}
}

}
if(ch=='\n')
m=m+1;
}
row=m;
col=n/m;

fptr.clear();
cout<<endl<<row<<" "<<col;
}

this will consider only a single digit number. but for a 2 or more digit number it does not work properly

Please enclose your source code in [ code ]..[ /code ] tags. (no spaces).



#include<ctype.h>
#include<fstream.h>
#include<conio.h>
void main()
{
fstream fptr("kk.txt",ios::in|ios::out);
char ch;
int n=0,m=1,row,col;
clrscr();
fptr.seekg(0,ios::beg);
while(!fptr.eof())
{
fptr.get(ch);
if(ch!='\n')
{
if(isdigit(ch))
{ n=n+1;
if(!fptr.eof())
{
cout<<ch;

}
}

}
if(ch=='\n')
m=m+1;
}
row=m;
col=n/m;

fptr.clear();
cout<<endl<<row<<" "<<col;
}

dwhitney67
June 16th, 2009, 09:13 PM
Say, two numbers separated by a white-space:


#include <fstream>
#include <iostream>

int main()
{
using namespace std;

fstream file("kk.txt", ios::in);

int row = 0;
int col = 0;

file >> row >> col;

cout << "row = " << row << ", col = " << col << endl;
}

It doesn't get any easier than this.

MadCow108
June 16th, 2009, 09:56 PM
or for arbitrary seperation:


#include <sstream>
string line;
while (std::getline(filestream,line))
{
stringstream s(line);
std::cout << "newline" << std::endl;
string token;
while (std::getline(s,token,'-'))
{
std::cout << "token: "<< token << std::endl;
}
}


also drop the .h in the #includes.
c++ standard headers are included without the ending