|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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; } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > matrix pointer |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|