C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 17th, 2012, 09:32 AM
Reflexa Reflexa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 6 Reflexa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 56 sec
Reputation Power: 0
C++:Load two demensional array from file?

Hello there guys,I need to load a two dimensional array from file in my program.
The matrix in the file is like:

M N
A11 A12 ....A1N
.....................
Am1 Am2 Amn

Where M and N are the size of the matrix and they are expected to change when i want to show a smaller part of that matrix for example.
Also another question:
We have the same matrix and i want to cut it in two different matrixes - lets say its 10 rows and i want to cut in in the fifth row so the two new will show A - matrix from numbers above that row and B - matrix from numbers below that row.

Any clues will be highly appreciated.

Reply With Quote
  #2  
Old December 19th, 2012, 12:21 AM
Reflexa Reflexa is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 6 Reflexa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 56 sec
Reputation Power: 0
Any idea how to assign M and N to a "a" and "b" variables?
How getline reads the lines in the text file?Are they readed as one line or every char is read separately?Can i start reading from the second row when i assign the two variables from the first to the array(just skip first row)?
Please any guidance will be very much appreciated!

Reply With Quote
  #3  
Old December 19th, 2012, 03:04 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,839 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 18 h 45 m 47 sec
Reputation Power: 1774
An example.
Code:
$ cat bar.txt
3 2
1 2
3 4
5 6
$ cat bar.cpp
#include <iostream> 
#include <fstream>
#include <vector>
using namespace std; 

int main(void) 
{
  vector<vector<int> > mat;
  ifstream fin("bar.txt");
  int rows, cols;
  if ( fin >> rows >> cols ) {
    for ( int r = 0 ; fin && r < rows ; r++ ) {
      vector<int> row;
      for ( int c = 0 ; fin && c < cols ; c++ ) {
        int temp;
        if ( fin >> temp ) {
          row.push_back(temp);
        }
      }
      mat.push_back(row);
    }
  }
  cout << "Number of Rows = " << mat.size() << endl;
  cout << "Number of Cols = " << mat[0].size() << endl;
  return 0;     
}
$ ./a.out 
Number of Rows = 3
Number of Cols = 2
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C++:Load two demensional array from file?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap