Question

In: Computer Science

HOW TO PRINT THE CONTENTS OF A TWO-DIMENSIONAL ARRAY USING POINTER ARITHMETIC (USING FUNCTIONS) (C++) Fill...

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;
}

Solutions

Expert Solution

//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


Related Solutions

In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
In C Create a multi-dimensional array and print it out forwards, backwards and then transpose.
In C Create a multi-dimensional array and print it out forwards, backwards and then transpose.
Send an element from 2d array to a function using pointer to pointer. c++ I am...
Send an element from 2d array to a function using pointer to pointer. c++ I am having an issue with being able to send specific elements to the swap function. #include <iostream> using namespace std; void swap(int **a, int **b){    int temp = **a;    **a=**b;    **b=temp; } void selSort(int **arr, int d){    for(int i =0; i<d-1; i++){        int min = *(*arr+i);        int minin = i;        for(int j =i+1; j<d; j++){...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Sum all the elements in the two-dimensional array below. Print the // result to the console....
Sum all the elements in the two-dimensional array below. Print the // result to the console. var arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] javascript
Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with...
Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with random integers between a given min and max value. It must accept for parameters, min and max values for the creation of random integers, and rows and columns for the number of rows and columns of the array. The method should return an array of rows by columns size, filled with random integers between min and max values. The second method is called printRA,...
Below is a class called pointerDataClass that store data in a one-dimensional array using pointer. #include<iostream>...
Below is a class called pointerDataClass that store data in a one-dimensional array using pointer. #include<iostream> using namespace std; class pointerDataClass { int maxSize;//variable to store the maximum size of p int length;//variable to store the number of elements in p int *p;// pointer to an int array public: //Constructor to create an array of the size specified by the parameter size. pointerDataClass(int size); //Destructor to deallocate the memory space occupied by the array p ~pointerDataClass(); //the function insertAt inserts...
in java Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array....
in java Implement a function print2Darray(int[][] array) to print a formatted 4x4 two dimensional integer array. When the array contains {{10, 15, 30, 40},{15, 5, 8, 2}, {20, 2, 4, 2},{1, 4, 5, 0}}, Your output should look like: {10 15 30 40} {15 5 8 2}{ 20 2 4 2}{ 1450} Now, implement another function print2DList(ArrayList<ArrayList<Integer>> list) to print a formatted 2D list.
Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops...
Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops to do the same thing this code does. Be sure that you understand how the code works and how the pointer arithmetic relates to the array expression form. Provide liberal comments to explain what your pointer arithmetic is computing. #include <stdlib.h> #include <stdio.h> int main(int argc, char **argv) { int arg_count = 0; for (arg_count = 0; arg_count < argc; arg_count++) printf("%s\n", argv[arg_count]); }
MUST BE DONE IN C (NOT C++) Using an array and a function, print the values...
MUST BE DONE IN C (NOT C++) Using an array and a function, print the values of an array backwards. Please follow these guidelines: - Setup your array manually (whichever values you want, as many as you want and whichever datatype you prefer). - Call your function. You should send two parameters to such function: the array’s length and the array. - Inside the function, go ahead and print the array backwards. - Your function shouldn’t return anything
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT