I just got a program that does forward substitution. It is working fine but I would like to make it so the user can enter size of both 2D n x n array and a n x 1 array and then be able to enter the elements of the arrays. Since the 2D array is square and 1D array shares the same row size, only one number would need to be entered.

ie: Enter size of array: 4
Enter elements of 2D array of size 4 x 4: {1,2,2,5}.....
Enter elements of 1D array of size 4: {1,2,....

How can I do this? I read something about 'new' but don't understand it well.

Also, I have the 2D array being passed to functions where

void print2array(double intarray[][4], int lim)

Is it possible to have the intarray[][n]? Where n is the number entered above?

Recommended Answers

All 10 Replies

For 1D (n * 1):

int* array = new int[n];

It will create an array with the size of n

For 2D (n * m):

int ** array = new int * [n];
for (int i = 0; i < n; i++) array[i] = new int[m];

It will create a 2D array with size of n * m;

Do not forget to delete them after using them.

so if i have

int main()
{
	double L[4][4] = {{100,0,0,0},{1.5,101.5,0,0},{2.5,2.5,102.5,0},{3,3,3,103}};
	double b[4] = {98.5,101,102,103.5};
	double x[4] = {0,0,0,0};

you are saying to do:

int main()
{
cin << n;
double* b = new double[n];
double ** L = new double * [n];
for (int i = 0; i < n; i++) L[i] = new double[n];
cout << "Enter elements of matrix L using format {{1,2,3..},{4,5,6..},...}}" << endl;
cin >>  L;	
L = {{100,0,0,0},{1.5,101.5,0,0},{2.5,2.5,102.5,0},{3,3,3,103}};
b = {98.5,101,102,103.5};

but what do you mean delete? Thanks!

You should read elements of array one by one from input

int main()
{
cout << "Enter the size of array" << endl;
cin >> n;
double* b = new double[n];
double ** L = new double * [n];
for (int i = 0; i < n; i++) L[i] = new double[n];
cout << "Enter elements of matrix L using format 1 2 3 4 5 6 ... (n * n elements)" << endl; 
// Of course user can use "Enter" after each row to make its shape clear for himself
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> L[i][j];

cout << "Enter elements of matrix b using format 1 2 3 4 5 6 ... (n elements)" << endl; 
// Of course user can use "Enter" after each row to make its shape clear for himself
for (int i = 0; i < n; i++)
cin >> b[i];

...
}

An input sample:
Enter the size of array
4
Enter elements of matrix L using format 1 2 3 4 5 6 ... (n * n) elements
100 0 0 0
1.5 101.5 0 0
2.5 2.5 102.5 0
3 3 3 103
Enter elements of matrix b using format 1 2 3 4 5 6 ... (n elements)
98.5 101 102 103.5

great thanks a lot.

is there a way to pass a two dimensional array to the function with

function( array[][n]...)

instead of having to write in the number each time?
Thread: User input for matrix size

To pass an array to a function you should do as follows:

1D:

void function(double * array);

2D:

void function(double ** array);

but I need to pass the two arrays to the function, perform an operation to yield a new 1D array. I got the function to work with the arguments array[ ][4]

this is the important function, where I need to return the array, x. This works no problem, I create x in the main filled with 0's and this function returns the array I need (minus the stuff I took out to not give away the code).

void forwardsubst(double lower[][4], double b[], double x[], int lim)
{
	x[0]= b[0]/lower[0][0];
	for()
	{
		double sum = 0;
		for()
		{
			sum = sum + (lower[j][k]/x[k]);
		}
		x[j] = (b[j] - sum)/lower[j][j];
	}
}

You are saying the proper syntax would be

void forwardsubst(double ** lower, double * b, double * x, int lim)

Exactly.

Thanks a lot attefeh, runs great now. I can LU decomp till my hearts content now :)

You're welcome :).

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.