Wrong output while printing a multidimensional array - c++

i have some trouble while printing this pseudo-multidimensional array , with elements that are set already.
And the point of it is to swap the first and third row and 2nd and 4th column, but the output looks weird...
#include <iostream>
using namespace std;
int main()
{
int arr[12]= {
1,2,3,4,5,6,7,8,6,4,5,3
};
cout << "Before change: "<<endl;
for (int row=0;row<3;row++){
for (int col=0;col<4;col++){
cout << arr[row*col]<<" ";
}
cout <<endl;
}
cout << "After the row change: "<<endl;
for (int row=2;row>=0;row--){
for(int col=0;col<4;col++){
cout<<arr[row*col]<<" ";
}
cout<<endl;
}
cout << "After the column change: "<<endl;
int temp;
for(int row=0;row<3;row++){
temp=arr[row*1];
arr[row*1]=arr[row*3];
arr[row*3]=temp;
for (int col=0;col<4;col++){
cout<<arr[row*col]<<" ";
}
cout<<endl;
}
Instead of having an output like this:
1 2 3 4
5 6 7 8
6 4 5 3
6 4 5 3
5 6 7 8
1 2 3 4
6 3 5 4
5 8 7 6
1 4 3 2
i get this :
1 1 1 1
1 2 3 4
1 3 5 7
1 3 5 7
1 2 3 4
1 1 1 1
1 1 1 1
1 4 3 2
1 7 5 3

You wrong how to calculate the element inside the array arr[row*col] will be always 0 for the first row ( row = 0). So you have to do something like this:
#include <iostream>
using namespace std;
int main()
{
int arr[12] = {
1, 2, 3, 4, 5, 6, 7, 8, 6, 4, 5, 3
};
**int dimCol = 4;**
cout << "Before change: " << endl;
for (int row = 0; row < 3; row++){
for (int col = 0; col < 4; col++){
cout << arr[**(row*dimCol) + col**] << " ";
}
cout << endl;
}
cout << "After the row change: " << endl;
for (int row = 2; row >= 0; row--){
for (int col = 0; col < 4; col++){
cout << arr[**(row*dimCol) + col**] << " ";
}
cout << endl;
}
cout << "After the column change: " << endl;
int temp;
for (int row = 0; row < 3; row++){
temp = arr[row * 1];
arr[row * 1] = arr[row * 3];
arr[row * 3] = temp;
for (int col = 0; col < 4; col++){
cout << arr[**(row*dimCol) + col**] << " ";
}
cout << endl;
}
}
The formule will be: array[row*numberOfColumn + Column]

You got a wrong argument definition:
Instead of
array[row*col]
write this
array[row*4 + col]
So the formula is:
array[row*total_col + col]

The loop you are using multiplies each time your variable with Zero that's why you are getting 1 at the beginning of every line as your first element is 1, and arr[0] is 1.
*and your line 1 is
1 1 1 1*
because value of outer loop is zero and any value of variable of inner loop multiplied will result in 0.
the reason you are not getting correct output is your logic to print all element is not correct.
go for
array[row*4 + col]

Related

Write a C++ program which will print (half pyramid) pattern of natural numbers

The task:
Write a C++ program which will print (half pyramid) pattern of natural numbers.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
I have tried using this code, but its not giving the output:
#include <iostream>
using namespace std;
int main()
{
int rows, i, j;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
cout << j << " ";
}
cout << "\n";
}
return 0;
}
OUTPUT:
Enter number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
j in your inner loop is the 1 based index of the element in the current row (1,2,3 etc.).
Instead of printing it, you should print a counter that is increased over all the iterations.
Something like:
#include <iostream>
int main()
{
int rows, i, j;
std::cout << "Enter number of rows: ";
std::cin >> rows;
int n = 1;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
std::cout << n << " ";
n++; // for next iteration
}
std::cout << "\n";
}
return 0;
}
Output example:
Enter number of rows: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
A side note: better to avoid using namespace std - see here Why is "using namespace std;" considered bad practice?.

Why is the ILS_best_p not giving me the vector I want?

I would like to store the smallest value of compute_evaluation_function in current_best_eval, and the corresponding vector that gives that smallest value in ILS_best_p, I've succeeded with the current_best_eval, but can't seem to get it for the ILS_best_p even though they're under the same if statement.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <ctime>
#include <cmath>
using namespace std;
long int **read_matrix(ifstream &input, long int n);
double ran01(long int *idum);
long int *generate_random_vector(long int n);
unsigned long int compute_evaluation_function(long int n);
void first_2_opt_symmetric (long int n);
void swap(long int pos_1, long int pos_2, long int *);
void perturbation(long int pert_size);
long int *p;//the vector generated from generate_random_vector()
long int **d;//one of the matrices read from file
long int **f;//one of the matrices read from file
long int n;
long int actual_solution_value;
long int new_eval;
long int current_eval;
#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
int main()
{
string name;
long int optimum;
ifstream infile;
long int initial_eval;
long int *ILS_best_p;
long int new_vs_current;
new_vs_current = INT_MAX;
long int current_best_eval;
current_best_eval = INT_MAX;
long int new_current_eval;
long int best_eval;
long int improvement;
long int i;
long int j;
infile.open("nug12.dat");
infile >> name;
cout << "Name: " << name << "\n";
infile >> n;
cout << "Rows and Columns: " << n << "\n";
infile >> optimum;
cout << "Optimum solution: " << optimum << "\n";
d=read_matrix(infile, n);
f=read_matrix(infile, n);
cout << endl;
p=generate_random_vector(n);
compute_evaluation_function(n);
cout << endl;
first_2_opt_symmetric (n);
cout << endl;
initial_eval = compute_evaluation_function(n);
cout << endl << endl;
for (i = 0; i < 500; i++) //ILS loop
{
if (new_eval < current_eval)
{
new_vs_current = new_eval;
cout << "iteration 1st = " << i+1 << " " << endl;
}
else if (current_eval < new_eval)
{
new_vs_current = current_eval;
cout << "iteration 2nd = " << i+1 << " " << endl;
}
cout << "iteration = " << i+1 << " " << endl;
cout << " Vector before perturbation = ";
for (j = 0; j < n; j++)
{
cout << p[j] << " ";
}
cout << endl << endl;
cout << endl << "current best eval = " << current_best_eval << endl;
perturbation(3);
cout << endl << endl;
cout << endl << "current best eval = " << current_best_eval << endl;
cout << " Vector after perturbation = ";
for (j = 0; j < n; j++)
{
cout << p[j] << " ";
}
cout << endl << endl;
cout << endl << "current best eval = " << current_best_eval << endl;
first_2_opt_symmetric (n);
new_current_eval = compute_evaluation_function(n);
cout << endl << "current best eval = " << current_best_eval << endl;
cout << endl;
cout << " Vector after local search = ";
for (j = 0; j < n; j++)
{
cout << p[j] << " ";
}
cout << endl << endl;
cout << endl << "current best eval = " << current_best_eval << endl;
cout << " Vector p = ";
for (j = 0; j < n; j++)
{
cout << p[j] << " ";
}
cout << endl << endl;
if (new_current_eval < current_best_eval)
{
new_eval = new_current_eval;
cout << "iteration 3rd = " << i+1 << " " << endl;
}
else if (current_best_eval <= new_current_eval)
{
new_eval = current_best_eval;
cout << "iteration 4th = " << i+1 << " " << endl;
}
if (new_eval < new_vs_current)
{
current_best_eval = new_eval;
cout << "iteration 5th = " << i+1 << " " << endl;
ILS_best_p = p;
}
else if (new_vs_current < new_eval)
{
current_best_eval = new_vs_current;
cout << "iteration 6th = " << i+1 << " " << endl;
ILS_best_p = p;
}
cout << endl << "current best eval = " << current_best_eval << endl;
}
cout << endl << "current best eval = " << current_best_eval << endl;
cout << "ILS best vector = ";
for (i = 0; i < n; i++)
{
cout << ILS_best_p[i] << " ";
}
return 0;
}
long int **read_matrix(ifstream &input, long int n)
{
/*
FUNCTION: read instance matrices
INPUT: instance file name, instance size
OUTPUT: d-matrix and f-matrix
(SIDE)EFFECTS: none
COMMENTS: read the distance matrix and flow matrix
*/
long int i, j;
long int **matrix = new long int *[n];
for (i = 0; i < n; i++)
{
matrix[i] = new long int[n];
for (j = 0; j < n; j++)
{
if( !(input >> matrix[i][j]) )
{
cerr << "Error reading at " << i << j << endl;
exit(1);
}
}
}
return matrix;
}
double ran01(long int *idum)
{
/*
FUNCTION: returns a pseudo-random number
INPUT: a pointer to the seed variable
OUTPUT: a pseudo-random number uniformly distributed in [0,1]
(SIDE)EFFECTS: changes the value of seed
*/
long k;
double ans;
k =(*idum)/IQ;
*idum = IA * (*idum - k * IQ) - IR * k;
if (*idum < 0 )
{
*idum += IM;
}
ans = AM * (*idum);
return ans;
}
long int *generate_random_vector(long int n)
{
/*
FUNCTION: generates a random vector
INPUT: vector dimension
OUTPUT: returns pointer to vector, free memory when vector is not needed anymore
(SIDE)EFFECTS: none
*/
long int i, j, help;
long int *v;
srand(time(0));
long int seed=rand();
v = new long int[ n ];
for ( i = 0 ; i < n; i++ )
{
v[i] = i;
}
for ( i = 0 ; i < n-1 ; i++)
{
j = (long int) ( ran01( &seed ) * (n - i));
assert( i + j < n );
help = v[i];
v[i] = v[i+j];
v[i+j] = help;
}
return v;
}
unsigned long int compute_evaluation_function(long int n)
{
/*
FUNCTION: compute evaluation function
INPUT: pointer to solution
OUTPUT: evaluation function
(SIDE)EFFECTS: none
COMMENTS: none
*/
long int i, j;
unsigned long obj_f_value;
obj_f_value = 0;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
obj_f_value += d[i][j] * f[p[i]][p[j]];
}
}
cout << obj_f_value;
return obj_f_value;
}
void first_2_opt_symmetric (long int n)
{
/*
FUNCTION: first improvement 2-opt local search for symmetric instances
INPUT: pointer to some initial assignment
OUTPUT: none
(SIDE)EFFECTS: initial assignment is locally optimized, dlb is used to increase the search speed, the dlb value is changed to false if item change it's location during perturbation
COMMENT: neighborhood is scanned in random order, use of register for faster computation
*/
long int improvement = 1;
long int improve_item = 0;
long int u, v, i, j, k;
long int tmp;
long int *x;
improvement = 1;
x = generate_random_vector(n);
while (improvement)
{
improvement = 0;
for ( i = 0 ; i < n ; i++ )
{
u = x[i];
improve_item = 0;
for ( j = 0 ; j < n ; j++ )
{
v = x[j];
if (u == v)
continue;
tmp = 0;
for ( k = 0 ; k < n ; k++ )
{
if ( (k != u) && (k != v) )
{
tmp += (d[u][k] - d[v][k]) * (f[p[v]][p[k]] - f[p[u]][p[k]]);
}
}
if (tmp < 0)
{
improvement = 1;
improve_item = 1;
swap (u, v, p);
}
}
}
}
delete []x;
}
void swap(long int pos_1, long int pos_2, long int *p)
{
/*
FUNCTION: swap position of two items in a vector
INPUT: position 1, position 2, pointer to vector
OUTPUT: none
(SIDE)EFFECTS: none
COMMENTS: none
*/
long int tmp;
tmp = p[pos_1];
p[pos_1] = p[pos_2];
p[pos_2] = tmp;
}
void perturbation(long int pert_size)
{
/*
FUNCTION: apply random perturbation
INPUT: pointer to solution, perturbation scheme, perturbation strength, change in perturbation, end perturbation size
OUTPUT: none
(SIDE)EFFECTS: new solution formed from old solution
COMMENTS: none
*/
long int hold_value[n]; //allocate memory for items to be chosen in move/*
int chosen[pert_size]; //*allocate temporary memory to determine items to be moved */*
long int i;
for(i = 0; i < n; i++)
{
hold_value[i] = p[i]; //initialize hold_value with the same value from local search/*
}
int j = n - 1;
int rand_number;
int rand_no;
for(i = 1; i <= pert_size; i++)
{
rand_number = rand();
rand_no = rand_number % j; //choose random number from 1 to size - 1/
chosen[i - 1] = rand_no + i; //copy the value to chosen[i]
j--;
}
long int temp;
for(i = 0; i < pert_size; i++)
{
temp = chosen[i];
swap(i, temp, hold_value); //swap chosen[i] and hold_value[i]; n first item in hold_value[i] is the index array that will be included in perturbation/
}
long int temp1;
long int temp2;
temp1 = p[hold_value[1]];//
temp2 = p[hold_value[0]];
for(i = 0; i < pert_size - 1; i++)
{
p[hold_value[i + 1]] = temp2;
temp2 = temp1;
temp1 = p[hold_value[i + 2]];
}
p[hold_value[0]] = temp2;
actual_solution_value = compute_evaluation_function(n);
new_eval = actual_solution_value;
current_eval = new_eval;
}
This is the file I read from:
Nug12
12
578
0 1 2 3 1 2 3 4 2 3 4 5
1 0 1 2 2 1 2 3 3 2 3 4
2 1 0 1 3 2 1 2 4 3 2 3
3 2 1 0 4 3 2 1 5 4 3 2
1 2 3 4 0 1 2 3 1 2 3 4
2 1 2 3 1 0 1 2 2 1 2 3
3 2 1 2 2 1 0 1 3 2 1 2
4 3 2 1 3 2 1 0 4 3 2 1
2 3 4 5 1 2 3 4 0 1 2 3
3 2 3 4 2 1 2 3 1 0 1 2
4 3 2 3 3 2 1 2 2 1 0 1
5 4 3 2 4 3 2 1 3 2 1 0
0 5 2 4 1 0 0 6 2 1 1 1
5 0 3 0 2 2 2 0 4 5 0 0
2 3 0 0 0 0 0 5 5 2 2 2
4 0 0 0 5 2 2 10 0 0 5 5
1 2 0 5 0 10 0 0 0 5 1 1
0 2 0 2 10 0 5 1 1 5 4 0
0 2 0 2 0 5 0 10 5 2 3 3
6 0 5 10 0 1 10 0 0 0 5 0
2 4 5 0 0 1 5 0 0 0 10 10
1 5 2 0 5 5 2 0 0 0 5 0
1 0 2 5 1 4 3 5 10 5 0 2
1 0 2 5 1 0 3 0 10 0 2 0

C++ - Building Addition Tables

I am having some troubles with this problem presented from my lab. My goal is to produce an addition table that looks something like this -
(From range(1-5)) :
+ 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10
Mine is looking like this, however :
+ 1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
My code looks like this :
if (choice == ADD) {
cout << "+";
for (int i = 0; i < max; i++) {
cout << "\t";
for (int j = min; j <= max; j++) {
cout << i + j << "\t";
}
}
}
(For reference, int max = maximum number in range, int min = minimum number in range, and choice is the decision for user to do either an addition or multiplication table). How can I change my code to fit the proper format? I can't seem to figure it out. Any hints/help would be greatly appreciated :)
#include <iostream>
using namespace std;
int main(){
int max = 5;
int min = 1;
if (true){
cout << "+\t";//print out the initial +
for(int i = min; i <= max; i++) cout << i << "\t";//print out the entire first row
cout << "\n"; //start the next row
//here is the main loop where you do most of the logic
for(int i = min; i <= max; i++){
cout << i << "\t"; //this prints out the first column of numbers
for(int j = min; j <=max; j++){
cout << j+i << "\t"; //this line fills in the body of your table
}
cout << "\n";//creates the space between each row
}
}
}
This code builds the table as explained:
for (int i = 0; i <= max; i++) {
if (i == 0)
cout << '+';
else
cout << i;
cout << '\t';
for (int j = min; j <= max; j++) {
cout << i + j << '\t';
}
cout << '\n';
}
Tip: when you want to print only a character, it is more efficient to use single quotes like '+' or '\t'. Double quotes are more expensive because they represent a const char*.

Enter values into a 2d array and print

I am trying to write a C++ program that asks for inputs for a 3 by 3 array and then prints them. I am trying to print it but am not sure why it is just giving me 0 for each val
#include <iostream>
using namespace std;
const int row = 3;
const int col = 3;
void printMatrix(int array[row][col])
{
int i, j;
cout << endl << "Matrix " << endl;
for(i = 0; i < row; i++)
{
cout << endl;
for(j = 0; j < col; j++)
{
cout << array[row][col] << "\t";
}
}
cout << "\n";
}
int main()
{
int i, j, array[row][col];
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
cout << "Enter a value for Row " << i + 1 << " Col " << j + 1 << ": "
cin >> array[i][j];
}
}
printMatrix(array);
}
Console Output:
Enter a value for Row 1 Col 1: 1
Enter a value for Row 1 Col 2: 2
Enter a value for Row 1 Col 3: 3
Enter a value for Row 2 Col 1: 1
Enter a value for Row 2 Col 2: 2
Enter a value for Row 2 Col 3: 3
Enter a value for Row 3 Col 1: 1
Enter a value for Row 3 Col 2: 2
Enter a value for Row 3 Col 3: 3
Output Matrix:
0 0 0
0 0 0
0 0 0
the problem is here
cout << array[row][col] << "\t";
try
cout << array[i][j] << "\t";
You just need to change the printMatrix() function.
Change this function as below:
cout << array[i][j] << "\t";
And put semicolon (;) at the end of the cout statement in the main function.

How to implement multiple pointers in C++ to a pointer array

I've been working on a class project program using pointers and arrays with pointer offset notation (I think).
Below is the diagram used for the project
Prompt for project
*The shaded boxes represent pointers. The unshaded boxes represent a dynamically allocated two dimensional int array. Your program cannot create named identifiers for any of the unnamed items. The number of rows and the number of columns will be user input. The ellipses in the diagram represent that the sizes of the rows and columns are variable. The vertical array is an array of pointers. Each of the vertical array elements points to a one dimensional array of integers.
In your program only refer to the integer array through the pointers represented by s, t, u, and v. When passing to functions you must always pass these pointers. You cannot dereference actual parameters in a function call. Nor can you dereference pointers and assign them to variables just in order to work with them.*
#include <iostream>
using namespace std;
void fillArray(int **pArray, int rows, int columns)
{
for(int row = 0; row < rows; row++)
{
*(pArray + row) = new int;
cout << "Enter " << " row " << row + 1<< endl;
for(int col = 0; col < columns; col++)
{
cin >> *(*(pArray + row) + col);
}
}
}
void printArray(int **pArray, int rows, int columns)
{
for(int row = 0; row < rows; row++)
{
cout << "Row " << row + 1 << endl;
for(int col = 0; col < columns; col++)
{
int num = *(*(pArray + row) + col);
cout << num << " ";
}
cout << endl;
}
}
void reverseArray(int **pArray, int rows, int columns)
{
for(int row = 0; row < rows; row++)
{
cout << "Reversed Row " << row + 1 << endl;
for(int col = columns - 1; col >= 0; col--)
{
int num = *(*(pArray + row) + col);
cout << num << " ";
}
cout << endl;
}
}
int main()
{
int rows;
int columns;
cout << ("Input number of rows: ");
cin >> rows;
cout << ("Input number of columns: ");
cin >> columns;
int **pArray; //Initialize array
int ****s; //Initialize pointers s, t, u, v
**s = pArray;
int ****t;
*t = *s;
int ****u;
**u = pArray;
int ****v;
*v = *u;
pArray = new int*[rows]; //create pointer to rows. 1st level of indirection
*pArray = new int[columns]; //create pointer to columns. 2nd level of indirection
fillArray(pArray, rows, columns);
printArray(pArray, rows, columns);
reverseArray(pArray, rows, columns);
//Loop to terminate program
while (true)
{
cout << "\nEnter letter \'q\' to terminate program\n";
char c;
cin >> c;
if(c == 'q')
break;
}
}
Sorry for the poor code formatting. I couldn't understand how to copy and paste in code block.
So my question is, how do I implement the diagram with my program. I started with the basics of creating the array using pointer offset and labeling it with it's own name.
I believe I have to change all of my references to work with the pointer variables 's, t, u, v.' Any help is appreciated.
In an effort to be at least somewhat helpful, note that the question is not crystal-clear on whether the arrays are fixed or variant You're given two sizes; a column count (the width) and a row count (the height). Also note that this statement:
"In your program only refer to the integer array..."
is nonsense. there are 'cols' integer arrays, not one.
int rows, cols; // acquire rows and cols from wherever.
// create the row vector that will hold the column pointers.
// this constitutes the first grey box above the main array.
int **rowsp = new int*[rows];
// for each slot we just allocated, set the value an allocation
// of 'int' values. the list will be `cols` wide
for (int i=0;i<rows;)
rowsp[i++] = new int[cols];
// thats both white boxes and the first grey box. rowsp points
// to the allocated pointer array holding 'rows' pointers, each
// pointing to an int array hold 'cols' ints.
// now we make the two pointers that point at rowsp
int ***p1 = &rowsp;
int ***p2 = p1;
// and finally, setup s,t,u,v
int ****s = &p1;
int ****t = s;
int ****u = &p2;
int ****v = u;
Terrific, but reading the context of your question, this isn't right at all. According to the question, you have only four pointers to work with: {s,t,u,v} The must be declared of a type appropriate to allow us to build all the way to the end int arrays. Looking at the above (which is throw-way code at this point) we know we need four levels of indirection. So this time, we start with s,t,u, and v, working our way down.
int ****s = new int***; // p1 in the prior code
int ****t = s; // points to the same p1
int ****u = new int***; // p2 in the prior code
int ****v = u; // points to the same p2
Now we need to get our p1 and p2 pointing to a common pointer as well. This pointer will be the rowsp pointer in out code from above (which means when we're done s,t,u, and v all should have a path to it eventually)
*s = new int**; // s-->p1-->rowsp, t-->p1-->rows
*u = *s; // u-->p2-->rowsp, v-->p2-->rowsp
Now we need to allocate the actual rows vector, which will contain pointers to ints:
**s = new int*[rows]; // the integer pointer array.
And finally, populate this array with 'cols'-length int-arrays.
for (int i=0;i<cols;++i)
{
*(**s+i) = new int[ cols ]();
for (int j=0;j<cols;++j)
*(*(**s+i)+j) = i+j; // <== beautiful dereference chain =P
}
If we did this right, the following should all point to the same thing:
cout << "**s = " << **s << endl;
cout << "**t = " << **t << endl;
cout << "**u = " << **u << endl;
cout << "**v = " << **v << endl;
I leave moving this madness to functions on your own dime. The entire code base from above appears below in one contiguous listing. You can also see it live on ideone.com:
#include <iostream>
using namespace std;
int main()
{
static const int rows = 8; // yours would come from user input
static const int cols = 9;
int ****s = new int***; // s --> p1
int ****t = s; // t --> p1
int ****u = new int***; // u --> p2
int ****v = u; // v --> p2
*s = new int**; // s-->p1-->rowsp, t-->p1-->rows
*u = *s; // u-->p2-->rowsp, v-->p2-->rowsp
**s = new int*[rows]; // s --> p1 --> rowsp --> int*[rows]
for (int i=0;i<rows;++i)
{
*(**s+i) = new int[ cols ];
for (int j=0;j<cols;++j)
*(*(**s+i)+j) = (i+j) % cols; // <== beautiful dereference chain =P
}
cout << "**s = " << **s << endl;
cout << "**t = " << **t << endl;
cout << "**u = " << **u << endl;
cout << "**v = " << **v << endl << endl;;
cout << "======= S =======" << endl;
for (int i=0;i<rows;++i)
{
for (int j=0;j<cols;++j)
cout << *(*(**s+i)+j) << ' ';
cout << endl;
}
cout << endl;
cout << "======= T =======" << endl;
for (int i=0;i<rows;++i)
{
for (int j=0;j<cols;++j)
cout << *(*(**t+i)+j) << ' ';
cout << endl;
}
cout << endl;
cout << "======= U =======" << endl;
for (int i=0;i<rows;++i)
{
for (int j=0;j<cols;++j)
cout << *(*(**u+i)+j) << ' ';
cout << endl;
}
cout << endl;
cout << "======= V =======" << endl;
for (int i=0;i<rows;++i)
{
for (int j=0;j<cols;++j)
cout << *(*(**v+i)+j) << ' ';
cout << endl;
}
cout << endl;
// delete rows.
for (int i=0;i<rows;++i)
delete [] *(**s+i);
// delete row pointer array
delete [] **s;
// delete rowsp pointer
delete *s;
// and finally, delete s and u (or t and v)
delete s;
delete u;
return 0;
}
Output (pointers will vary)
**s = 0x100103b60
**t = 0x100103b60
**u = 0x100103b60
**v = 0x100103b60
======= S =======
0 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 0
2 3 4 5 6 7 8 0 1
3 4 5 6 7 8 0 1 2
4 5 6 7 8 0 1 2 3
5 6 7 8 0 1 2 3 4
6 7 8 0 1 2 3 4 5
7 8 0 1 2 3 4 5 6
======= T =======
0 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 0
2 3 4 5 6 7 8 0 1
3 4 5 6 7 8 0 1 2
4 5 6 7 8 0 1 2 3
5 6 7 8 0 1 2 3 4
6 7 8 0 1 2 3 4 5
7 8 0 1 2 3 4 5 6
======= U =======
0 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 0
2 3 4 5 6 7 8 0 1
3 4 5 6 7 8 0 1 2
4 5 6 7 8 0 1 2 3
5 6 7 8 0 1 2 3 4
6 7 8 0 1 2 3 4 5
7 8 0 1 2 3 4 5 6
======= V =======
0 1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 0
2 3 4 5 6 7 8 0 1
3 4 5 6 7 8 0 1 2
4 5 6 7 8 0 1 2 3
5 6 7 8 0 1 2 3 4
6 7 8 0 1 2 3 4 5
7 8 0 1 2 3 4 5 6