SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old November 19th, 2002, 10:14 PM
supaben34 supaben34 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Tallahassee
Posts: 55 supaben34 User rank is Corporal (100 - 500 Reputation Level)supaben34 User rank is Corporal (100 - 500 Reputation Level)supaben34 User rank is Corporal (100 - 500 Reputation Level)supaben34 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 h 57 m 51 sec
Reputation Power: 8
Question Reading file inputs and placing values into 2D array

Hey all,
I need to read an input file(any arbitrary file) that has matrix elements in it.

Code:
Ex:
2 3
9 7 5
8 4 10
1 6

2 - int numrows;
3 - int numcols;
{(9,7,5),(8,4,10)} - matrix elements
1 - int start;
6 - int goal;


I need to read the matrix elements and place them in a 2-D array.

Code:
|9 7 5|
|8 4 10|


I am guessing we need to use the numrows and numcols but we can ignore the start and goal variables for now. Would someone please help me along with this? Any feedback is appreciated as always. Thank you.

Reply With Quote
  #2  
Old November 20th, 2002, 06:51 AM
Sulik Sulik is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 10 Sulik User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE

after fopening the file and associating it with a handle

Code:
int num_col, num_row;
int start, goal;
int **arr; 
fscanf(handle, "%d %d \n", num_col, num_row);
arr = new int*[num_row];
for (int i = 0; i < num_row; i++)
{ 
  arr[i] = new int[num_col];
  for (int j = 0; j < num_col; j++)
  {
    fscanf(handle, "%d \n", arr[i][j]);
  }
}
fscanf(handle, "%d %d \n", start, goal);


this should read the needed data in the dinamic array (int **arr)
you will have to delete the array after using it
if you are writing the code in the C, not C++, use

Code:
arr = (int **)malloc(num_row*sizeof(int*));
and
arr = (int*)malloc(num_col*sizeof(int));


instead of new
Hope it'll help

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Reading file inputs and placing values into 2D array


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway