how to keep only 2 rows of matrix

How can I keep just 2 rows using modular arithmetic at the time of 2 D array, meaning I do not need to store the entire table, since I have a lot of items. This array will be used for knapsack problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

cin>>sizeDesired >> number;

  //create dynamic 2D array;
   	int **arr = new int*[sizeDesired];
	for(int i = 0; i < sizeDesired; ++i)
	{
		arr[i] = new int[number];
	}

//process some data
for (int k=0; k<sizeDesired; k++)
{
for (int n =0; n<number n++)
{
cin>> arr [k] [n];
//after 2 roows are full, I need to create 1 new row and delete the top row.
// how do I do that? Thanks!
}
}
	
Last edited on
help!!!
In your example you've already created all the row, so why do you ask how to create a new one ?
Maybe, I should not create the entire matrix before, instead just generate new row once the first two rows are full and then delete the first row.

what do you think?

Thanks!
Why not simply reuse a row instead of deleting+creating one ?
Last edited on
thank you
Topic archived. No new replies allowed.