473,588 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading a matrix file and storing it in array

So here is my problem once... i have a file called
network.txt ...its a 13X13 matrix...but the network.txt can go up to
100X 100.
I should read the text file and store it in array [j][k].

0110000000000
0001000000000
0001000000000
0000110000000
0000001000000
0000001000000
0000000100000
0000000010000
0000000001000
0000000000110
0000000000001
0000000000001
0000000000000

I struggled doing it in C...can somebody help with this in C++

Apr 7 '06 #1
4 11655
In article <11************ **********@j33g 2000cwa.googleg roups.com>,
pa****@gmail.co m wrote:
So here is my problem once... i have a file called
network.txt ...its a 13X13 matrix...but the network.txt can go up to
100X 100.
I should read the text file and store it in array [j][k].

0110000000000
0001000000000
0001000000000
0000110000000
0000001000000
0000001000000
0000000100000
0000000010000
0000000001000
0000000000110
0000000000001
0000000000001
0000000000000

I struggled doing it in C...can somebody help with this in C++


You should not store it in a "array[j][k]" is the answer. Unless, of
course, it's a homework assignment and the teacher says you must... Is
that the case?
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 7 '06 #2
pa****@gmail.co m wrote:
So here is my problem once... i have a file called
network.txt ...its a 13X13 matrix...but the network.txt can go up to
100X 100.
I should read the text file and store it in array [j][k].

0110000000000
0001000000000
0001000000000
0000110000000
0000001000000
0000001000000
0000000100000
0000000010000
0000000001000
0000000000110
0000000000001
0000000000001
0000000000000

I struggled doing it in C...can somebody help with this in C++


I do not know what actual problem you are to solve .. but how about
you read every line into a string and store those in a vector? So you
are left with:

std::vector <std::string> matrix;

and you can access it like:

matrix [0] [1];

to get the second character of the first line (a '1' in your
example).

hth
--
jb

(reply address in rot13, unscramble first)
Apr 7 '06 #3
pa****@gmail.co m wrote:
So here is my problem once... i have a file called
network.txt ...its a 13X13 matrix...but the network.txt can go up to
100X 100.
I should read the text file and store it in array [j][k].


What have you tried so far?
It's probably easier to use nested vectors versus that array, unless
you have some other reason. Unfortunately, you've given us virtually no
information about the actual problem you're trying to solve. What is
the purpose of the matrix? How will it be used after you create it?
These are important design questions that must be dealt with before any
implementation.


Brian
Apr 7 '06 #4
notjim
1 New Member
Keeping it simple Is this what you have in mind:

void Foo::loadFile(s td::string filename){

fooFile.open(fi lename.c_str()) ;

int input;
char next;
int fooC=0;

mFoo.push_back( std::vector<int >());

while(!fooFile. eof()){

fooFile >> input;

mFoo[fooC].push_back(inpu t);

fooFile.get(nex t);
if(next=='\n'){
fooC++;
mFoo.push_back( std::vector<int >());
}
}

}

where fooFile is an ifstream and mFoo is a std::vector<std ::vector<int> >
Apr 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2197
by: SB | last post by:
Hello. I have an input file which is laid out in the following manner... Name Day 1 am time 1 am time 2 appointment pm time 1 pm time 2 appointment Day 2
3
9500
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
10
8706
by: Duncan M Gunn | last post by:
Hi, I need to store the following matrix of values: T U V A 2 1 1 B 2 - - C - - 2 Where (-) is an empty cell.
10
1827
by: Nevets Steprock | last post by:
I'm writing a web program where one of the sections is supposed to output a correlation matrix. The typical correlation matrix looks like this: ..23 ..34 .54 ..76 .44 .28 ..02 .77 .80 .99 I've written code to calculate the correlation data and it is populated in a vector like this:
15
2539
by: Shuch | last post by:
Hi all, i m trying to read from a file and then copy it into an array...my code is as follow..it runs fine but i cant understand y it doesnt show me any output?? here is my code... using namespace std; int Adj; //The matrix of adjacent nodes int curr; //it gives the current index to retrieve the neigbours of a node int n=100;
10
2766
by: nuke1872 | last post by:
Hello guys, I have a file names network.txt which contains a matrix. I want to read this matrix as store it as an array. I am new to stuff like these...can anybody help me out !! Thanks nuke
20
5205
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've used various techniques ( loop unrolling, loop jamming...) and tried some matrix libraries : newmat (slow for large matrix) , STL (fast but ..not usefull) , hand coding (brain consuming...), and recently Meschach...
10
7491
by: bodowpin | last post by:
Hello. I am trying to read a text file that contains 1 2 3 it just looks like that. I was able to read it and assign each number to a matrix element (or array element). It was reading it all fine and I was trying to change the elements to int variables a,b,c so that I could say matrix = {a,b,c}; At some point during tweaking all the matrix elements became 0 and I have lost myself.
5
2085
by: slizorn | last post by:
hi, well this is the file i have to read into the system... <matrix> rows = 2 cols = 2 1 2 2 4 </matrix>
0
7929
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7987
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6634
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5729
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2372
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.