Please provide a working solution in C++. Write a function and prototype stateme
ID: 667056 • Letter: P
Question
Please provide a working solution in C++.
Write a function and prototype statement that accepts three parameters. The first two parameters are pointers to 1-D arrays of integers. You may assume that the two arrays have already been declared with initial values and are of equal size but you must show the declaration of the pointer variables. The third parameter is just an empty 1-D array of integers. Your function must contain statements that select all integer values that are equal between the arrays pointed to by the first two pointer parameters and assign those common values to the third array. Hint: Use nested loops. Please provide a working solution in C++.Explanation / Answer
Try this :
#include <stdio.h>
void print(int *arr, int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", *((arr+i*n) + j));
}
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3, n = 3;
print((int *)arr, m, n);
return 0;
}
and this :
#include<stdio.h>
#define ROWSIZE 2
#define COLSIZE 10
char *copy(char (*ptr)[COLSIZE], char (*ptr2)[COLSIZE]);
int main(void)
{
char array1[ROWSIZE][COLSIZE]={{'a','b','c','d','e','f','g','h','i','j'},
{'1','2','3','4','5','6','7','8','9',''}};
char array2[ROWSIZE][COLSIZE];
char (*ptr1)[COLSIZE];
char (*ptr2)[COLSIZE];
ptr1=array1;
ptr2=array2;
copy(ptr2, ptr1);
printf("%s", *ptr2);
return 0;
}
char *copy(char (*copyto)[COLSIZE], char (*copyfrom)[COLSIZE])
{
int row, col;
for(row=0; row<ROWSIZE; ++row){
for(col=0; col<COLSIZE; ++col)
*(*(copyto+row)+col)=*(*(copyfrom+row)+col);
}
return copyto;
}
#include <stdio.h>
void print(int *arr, int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", *((arr+i*n) + j));
}
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3, n = 3;
print((int *)arr, m, n);
return 0;
}
and this :
#include<stdio.h>
#define ROWSIZE 2
#define COLSIZE 10
char *copy(char (*ptr)[COLSIZE], char (*ptr2)[COLSIZE]);
int main(void)
{
char array1[ROWSIZE][COLSIZE]={{'a','b','c','d','e','f','g','h','i','j'},
{'1','2','3','4','5','6','7','8','9',''}};
char array2[ROWSIZE][COLSIZE];
char (*ptr1)[COLSIZE];
char (*ptr2)[COLSIZE];
ptr1=array1;
ptr2=array2;
copy(ptr2, ptr1);
printf("%s", *ptr2);
return 0;
}
char *copy(char (*copyto)[COLSIZE], char (*copyfrom)[COLSIZE])
{
int row, col;
for(row=0; row<ROWSIZE; ++row){
for(col=0; col<COLSIZE; ++col)
*(*(copyto+row)+col)=*(*(copyfrom+row)+col);
}
return copyto;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.