In: Computer Science
HOW TO PRINT THE CONTENTS OF A TWO-DIMENSIONAL ARRAY USING POINTER ARITHMETIC (USING FUNCTIONS)
(C++)
Fill out the function definition for "void print_2darray_pointer(double *twoDD, int row, int col)".
Should match the output from the function void print_2darray_subscript(double twoDD[][ARRAY_SIZE], int row, int col)
# include
using namespace std;
const int ARRAY_SIZE = 5;
const int DYNAMIC_SIZE = 15;
const int TIC_TAC_TOE_SIZE = 3;
// function definitions:
void print_2darray_subscript(double
twoDD[][ARRAY_SIZE], int row, int col)
// printing array using
subscripts
{
for (int i = 0; i < row;
i++)
{
for (int j = 0;
j < col; j++)
{
cout << twoDD[i][j] << " ";
}
cout <<
endl;
}
cout << endl;
}
void print_2darray_pointer(double *twoDD, int row,
int col)
// print array using pointer
arithmetic
{
for (int i = 0; i < row;
i++)
{
for (int j = 0;
j < col; j++)
{
// I tried doing this, but it didn't work : cout
<< *(*(twoDD + i) + j);
// our 2d array is layed out linearly in memory
as contiguous rows, one after another, there are #row rows
// each row has #col columns
// to compute the offset using pointer
math
// offset from twoDD: #row (i) * #col + #col
(j), result: pointer to array element
// ...
}
cout <<
endl;
}
cout << endl;
}
//------------------------------------------------------------------------------
int main()
{
// complete the following function
implementations
// Q#3 - pointer arithmetic, indexing multidimensional
arrays
double twoDDoubles[ARRAY_SIZE][ARRAY_SIZE] = {
{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}
};
cout << endl << "print 2d array of doubles" << endl << endl;
// print 2ddoubles via subscript operator
print_2darray_subscript(twoDDoubles, ARRAY_SIZE,
ARRAY_SIZE);
// print 2ddoubles via pointer arithmetic
print_2darray_pointer((double*)twoDDoubles,
ARRAY_SIZE, ARRAY_SIZE);
cout << endl << endl;
system("pause");
return 0;
}
//Use this code
#include<iostream>
using namespace std;
const int ARRAY_SIZE = 5;
const int DYNAMIC_SIZE = 15;
const int TIC_TAC_TOE_SIZE = 3;
// function definitions:
void print_2darray_subscript(double twoDD[][ARRAY_SIZE], int
row, int col)
// printing array using subscripts
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col;
j++)
{
cout <<
twoDD[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void print_2darray_pointer(double* twoDD, int row, int
col)
// print array using pointer arithmetic
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col;
j++)
{
cout <<
*((twoDD + i*col)+j) << " ";
}
cout << endl;
}
cout << endl;
}
//------------------------------------------------------------------------------
int main()
{
// complete the following function
implementations
// Q#3 - pointer arithmetic, indexing multidimensional
arrays
double twoDDoubles[ARRAY_SIZE][ARRAY_SIZE] = {
{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}
};
cout << endl << "print 2d array of doubles" << endl << endl;
// print 2ddoubles via subscript operator
print_2darray_subscript(twoDDoubles, ARRAY_SIZE,
ARRAY_SIZE);
// print 2ddoubles via pointer arithmetic
print_2darray_pointer((double*)twoDDoubles,
ARRAY_SIZE, ARRAY_SIZE);
cout << endl << endl;
system("pause");
return 0;
}
//Output
//If you need any help regarding this solution ......... please leave a comment ...... thanks