Here is my code, the aim is to use dynamic memory and pointers. The program siccesfuly reads in data from a text file into the dynamic array(matrix). The size of the array is also in the text file, so thats how the size of the array is known. I am required to pass the pointer to the matrix(1 dimensional array just for now) to the caller so it can be used in a print function. Using "return matrix;" does not seem to work. Any ideas? Also i am not allowed to change any of the function headers, and can assume the data file will also contain data of type float.

main.cpp

#include <iostream>
#include <fstream>
#include "matrix-1.h"
using namespace std;



int main ()
{

	char filename[10];
	
	cout << "Enter filename to open: " << endl;
	cin >> filename;

	int rows;
	int columns;
	
	

	readmatrix(filename, rows, columns);
	
	printmatrix(matrix, columns, rows);//main dosnt recognise matrix
	


	return 0;
}

matrix-1.cpp

#include <iostream>
#include <fstream>
#include "matrix-1.h"
using namespace std;



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

	ifstream ins;
	
		

	ins.open(filename, ios::in | ios::out);

	if(ins.good())
	{

		
		ins >> rows >> columns;

		float* matrix = new float[rows * columns];
		
		for (int i = 0; i < (rows * columns); i++)
		{
			ins >> matrix[i];		
		}
		
		return matrix; //if i put this at the end it isnt in scope

	}
	else
	{
		cout << "the shit didnt work" << endl;		
		return NULL;
	}
	
	cout << rows << endl;
	cout << columns << endl;
	
	ins.close();
	
	return 0;
}

matrix.cpp

#include <iostream>
#include <fstream>
#include "matrix-1.h"
using namespace std;


//the matrix pointer isnt getting passed to this function.
void printmatrix(float matrix[], int columns, int rows)
{
	
	
	for (int i = 0; i < (rows * columns); i++)
		{
			cout << matrix[i]<< endl;
		}
	return 0;
}

matrix-1.h

float* readmatrix(char[], int&, int&);
void printmatrix(float[], int, int);

I suspect there's a reason why readmatrix has a return type of float*. I'd consider using the return type back in main() by declaring a float * in main() that can accept the pointer value returned and can then pass it to printmatrix(). For thouroughness sake I'd also delete the memory the matrix uses when the memory in the matrix is no longer needed

I did realise that lol and it now works :) My next problem is reading in 2 matricies and then multipliying them. To do this i am trying to copy the contents of the original array into another array called matrix a, and then i will delete the first array and read in data from another file and copy it to an array called matrixb, these will then be used to multiply. Any ideas on how i can copy these arrays since strcpy() dosnt work.

>>Any ideas on how i can copy these arrays since strcpy() dosnt work.

strcpy() is just for C style strings. I'd use loops and do it by hand, though you may be able to use something like memcpy().

float* x = NULL;
x = readmatrix(filename, rows, columns);

float* matrixa = new float(6);
	for (int i =0; i < 6; i++)
	{	
		*x[i] = *matrixa[i];
	
	}

I tried this but when i ouput i get garbage. How exactly would i be able to do this?

float* matrixa = new float(6);

you probably wanted

float* matrixa = new float[6];

i got that above code to work like this

float* matrixa = new float(6);
	for (int i =0; i < 6; i++)
	{	
		matrixa[i] = x[i];
	
	}

However when i try to delete the pointer x, and then call the read function again, and then copy the data from another file into matrixb using the same method, i get core dumped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.