General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 20th, 2002, 08:39 PM
isabelle isabelle is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 1 isabelle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question matrix pointer

Hi! Can anyone please help me out with this program? PLEASE! I have written some of the codes but it might e totally wrong....HELP!!!!!!!!

-------------------------------------------------------------------------------

Write a function with prototype that takes the 1st argumnet a C-string and 2 int which are passed by reference. Inside the function open up the file referred to by the argument filename.

float* readmatrix(char[] filename, int& rows, int& columns);

the function should open the file and check to see if the open was successful. In the event the open fails return to the calling function the value NULL. The program should terminate with an error message if NULL is returned.

The text file when opened has the following form:

2 3
12.0 3.5 44.5
12.9 1.0 5.6

the file describes a matrix. 1st line contains 2 int values. The 1st value of the line is the number of rows and the 2nd is the number of columns. The file will then have a number of lines which is equivalent to the number of rows. Each line is guaranteed to have a number of columns asindicated by the first value. Assume that the data file will always contain data file of type float for the elements of the matrix.

Allocate a 1 dimensional array dynamically with dimensions specified on the first line of the input file-this will be the matrix. For example : float* matrix = new float[ROWS*COLUMNS];

The function should also return NULL in the event it cannot allocate memory fot the resulting matrix. Once the matrix is created, populate each element with the data from the file. The format of the file how it would appear in the matrix.

An example on how the 1 dimensional array would look after being populated is:-
-------------------------------------------------
| 12.0 | 3.5 | 44.5 | 12.9 | 1.0 | 5.6 |
-------------------------------------------------

to access the 2nd element of the matrix on the 2nd row (which is 1.0), use the following formula:
multiply the number of rows minus 1 * the number of columns on each row, then add the coulmn number minus one. So to access the 1st element on the 2nd row, we would go (((2 - 1 )* 3) + 2 - 1 which yields and index 4 in the array. We subtract 1 from the index because the array occupies element 0...(n-1) where n is the number of elements that form the matrix.

Once this is done, close the stream associated with the file and return the pointer to the matrix to the caller. Be sure that the variables coulmns and rows which are passed by reference to communicate to the callinf function the dimensions of the array which models a matrix.




float* readmatrix(char[] filename, int& rows, int& columns)
{
cout<<"Enter the 1st file: ";
cin>>fileName;

ifstream fileExist(fileName);

if(!fileExist)
{
cerr << "No such file!"<<endl;
return NULL;
}

cin.getline(ch,'\n');

float* matrix = new float [rows*columns];

if(matrix==NULL)
{
return 1;
}

return matrix;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > matrix pointer


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 1 hosted by Hostway