I'm trying to reverse each row of a two dimensional array and store that in a new two dimensional array. I understand I need to use a temp pointer, but I seem to be a bit lost on how to actually do this all. I've tried researching the problem but couldn't find anything specific to mine.
using namespace std;
void compute(int *p1, int *p2, int ROWS, int COLUMNS);
int main() {
const int COLUMNS = 3;
const int ROWS = 3;
int A[ROWS][COLUMNS] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int B[ROWS][COLUMNS] ;
int *p1, *p2;
p1 = &A[0][0];
p2 = &B[0][0];
compute(p1, p2, ROWS, COLUMNS);
return 0;
}
void compute(int *p1, int *p2, int ROWS, int COLUMNS) {
int *savePtrA, *savePtrB, *temp;
savePtrA = p1;
savePtrB = p2;
temp = p1+COLUMNS;
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLUMNS; j++) {
temp = *p1;
*p2 = *p1 + 1;
p1--;
p2++;
}
}
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLUMNS; ++j) {
cout << setw(5) << *savePtrA;
savePtrA++;
}
cout << setw(10) << " ";
for (int k = 0; k < COLUMNS; ++k) {
cout << setw(5) << *savePtrB;
savePtrB++;
}
cout << setw(10) << " ";
cout << endl;
}
}
You could simply call std::reverse_copy in a loop:
#include <algorithm>
int main()
{
const int COLUMNS = 3;
const int ROWS = 3;
int A[ROWS][COLUMNS] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int B[ROWS][COLUMNS] ;
for (int i = 0; i < ROWS; ++i)
std::reverse_copy(&A[i][0], &A[i][COLUMNS], &B[i][0]);
}
Live Example
The source array is given in the first two arguments -- a pointer to the first element in the source row, and a pointer to one past the last element in the source row. The third parameter is the destination row, and that is merely a pointer to the first element in the B row.
I do not know if it is what you are looking for but change your code so that it does what you ask, although it can be done in an easier way
#include <iostream>
#include <iomanip>
using namespace std;
void compute(int *p1, int *p2, int COLUMNS);
int main() {
const int COLUMNS = 3;
const int ROWS = 3;
int A[ROWS][COLUMNS] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int B[ROWS][COLUMNS] ;
int *p1, *p2, *savePtrA, *savePtrB;
savePtrA = p1;
savePtrB = p2;
for (int i = 0; i < ROWS; ++i) {
p1 = &A[i][0];
p2 = &B[i][0];
compute(p1, p2, COLUMNS);
}
for (int i = 0; i < ROWS; ++i) {
savePtrA = &A[i][0];;
savePtrB = &B[i][0];
for (int j = 0; j < COLUMNS; ++j) {
cout << setw(5) << *savePtrA;
savePtrA++;
}
cout << setw(10) << " ";
for (int k = 0; k < COLUMNS; ++k) {
cout << setw(5) << *savePtrB;
savePtrB++;
}
cout << setw(10) << " ";
cout << endl;
}
return 0;
}
void compute(int *p1, int *p2, int COLUMNS) {
for (int j = COLUMNS-1; j >= 0; j--) {
*p2 = *(p1 + j);
p2++;
}
}
Related
I have a question as below C++ code.
this is the code(No function) that can successful execute.
#include <iostream>
using namespace std;
int main()
{
int size_3 = 3;
//arr1 is a static array
int arr1[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
//arr2 is a dynamic array
int** arr2 = new int* [size_3];
for (int i = 0; i < size_3; i++) {
arr2[i] = new int[size_3];
}
int val = 9;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
arr2[i][j] = val--;
}
}
cout << "Elements in matrix 1: " << endl;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
cout << arr1[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << "Elements in matrix 2: " << endl;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
cout << arr2[i][j] << " ";
}
cout << endl;
}
cout << endl;
int** prod_arr = new int* [size_3];
for (int i = 0; i < size_3; i++) {
prod_arr[i] = new int[size_3];
}
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
prod_arr[i][j] = 0;
for (int k = 0; k < size_3; k++) {
prod_arr[i][j] += arr1[i][k] * arr2[k][j];
}
}
}
cout << "Elements in the product of 2 matrices: " << endl;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
cout << prod_arr[i][j] << " ";
}
cout << endl;
}
}
However, I am gonna to use the function for the multiplication of the 2 matrices.
how could I move it to function, since the first array is static array, the second array is dynamic array.
I have try different way to use pointer , pass-by reference in the function, but still can't work.
My invalid code as below.
#include <iostream>
using namespace std;
int** function(int farr1, int farr2, int size3) {
int** prod_arr = new int* [size3];
for (int i = 0; i < size3; i++) {
prod_arr[i] = new int[size3];
}
for (int i = 0; i < size3; i++) {
for (int j = 0; j < size3; j++) {
prod_arr[i][j] = 0;
for (int k = 0; k < size3; k++) {
prod_arr[i][j] += farr1[i][k] * farr2[k][j];
}
return prod_arr[i][j];
}
}
}
int main()
{
int size_3 = 3;
//arr1 is a static array
int arr1[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
//arr2 is a dynamic array
int** arr2 = new int* [size_3];
for (int i = 0; i < size_3; i++) {
arr2[i] = new int[size_3];
}
int val = 9;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
arr2[i][j] = val--;
}
}
cout << "Elements in matrix 1: " << endl;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
cout << arr1[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << "Elements in matrix 2: " << endl;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
cout << arr2[i][j] << " ";
}
cout << endl;
}
cout << endl;
int** prod_arr = function(farr1, farr2, size_q3);
cout << "Elements in the product of 2 matrices: " << endl;
for (int i = 0; i < size_3; i++) {
for (int j = 0; j < size_3; j++) {
cout << prod_arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
I want to use the function to execute the code.
I got 2 array, one is static and another is dynamic,
How to use pointer and pass to function with this different arrays.
Thanks a lot for your help.
Some C++ array examples to get you started :
A 2D dynamically array can be modeled as std::vector<std::vector<int>>
#include <array> // static array
#include <vector> // dynamic array, can resize at runtime
#include <iostream>
// https://en.cppreference.com/w/cpp/container/array
// accept a modifiable static array (content can be changed
// the & means by reference, the array will not get copied
// https://isocpp.org/wiki/faq/references
void func1(std::array<int, 4>& values)
{
values[2] = 3;
}
// array cannot be modified in body of func2 only used : const
void func2(const std::array<int, 4>& values)
{
std::cout << values[2] << "\n";
}
// https://en.cppreference.com/w/cpp/container/vector
// return a dynamically allocated array of integers
// it will have a size of 5 when returned
std::vector<int> get_values()
{
return std::vector<int>{1, 2, 3, 4, 5};
}
int main()
{
auto values = get_values();
values.push_back(6); // add another value
// https://en.cppreference.com/w/cpp/language/range-for
// prefer to use those if you don't need indices
// (which is not as often as you think)
for (const int value : values)
{
std::cout << value << " ";
}
return 0;
}
i'm trying to make it so if i enter the number 3 , the matrix would change to {1,2,4,5,6,7,8,9,10,11,12,0}, i'm not sure if the method of switching the element between two arrays is the best way to do it and i'm trying to do it using pointers
using namespace std;
#include <iomanip>
int main() {
int arr[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, i, j,c;
int* ptr = &arr[0][0];
int zero[3][4] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
int* zptr = &zero [0][0];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
cout << setw(8) << arr[i][j] << ' ';
}
cout << endl;
cout << ' ' << endl;
}
for (i = 0; i < 3; i++) {
if (arr[i][0] = i)
{
break;
}
for (int j = 0; j < 4; j++)
cin >> i;
cout << ptr;
}
return 0;
}
you could use easier approach to achieve this using vector in c++ and by finding the index of the input element, consider this example :
int getIndex(vector<int> v, int K) {
auto it = find(v.begin(), v.end(), K);
// calculating the index of K
// note that here i'm supposed the input is valid
int index = it - v.begin();
return index;
}
and you can use the index you get to 1) append in A the element B[index], 2) assign B[index] to A[index] and 3) erase the element A[index].
hope this can be helpful.
So I need to find sum of all positive even numbers in every row (which I've already done actually). And then I have to sort these rows depending on their sums
As I think, it must be done in one function (otherwise there will be no connections as I cannot use global variables). In my case it's void print:
void print(const int** arr, int rows, int columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << setw(7) << arr[i][j];
}
cout << endl;
}
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (arr[i][j] > 0 && arr[i][j] % 2 == 0)
{
//cout << setw(7) << arr[i][j];
sum = sum + arr[i][j];
}
}
cout << endl << "Sum of positive even elements in the row " << i + 1 << " = " << sum;
sum = 0;
}
}
Full code:
#include <iostream>
#include <iomanip>
#include <time.h>
#include <conio.h>
#include <algorithm>
using namespace std;
int** createMalloc(int, int);
int** createCalloc(int rows, int columns);
int** createNew(int rows, int columns);
void deleteNew(int** arr, int rows);
void init(int**, int, int);
void freeMemory(int**, int);
void print(const int**, const int, const int);
void initPrint(int** arr, int rows, int columns);
void main() {
int rowCount, colCount;
cout << "Enter number of rows: "; cin >> rowCount;
cout << "Enter number of columns: "; cin >> colCount;
cout << " Array creation algorithm\n";
start:
cout << "Input number : \n1 for malloc\n2 for calloc\n3 for new\n";
int k;
cin >> k;
switch (k) {
case 1: {
int** a = createMalloc(rowCount, colCount);
initPrint(a, rowCount, colCount);
freeMemory(a, rowCount);
break;
}
case 2: {
int** a = createCalloc(rowCount, colCount);
initPrint(a, rowCount, colCount);
freeMemory(a, rowCount);
break;
}
case 3: {
int** a = createNew(rowCount, colCount);
initPrint(a, rowCount, colCount);
deleteNew(a, rowCount);
break;
}
default:cout << "Input 1, 2 or 3, please.";
cout << endl << endl;
goto start;
}
cout << endl << endl;
}
int** createMalloc(int rows, int columns) {
int** arr = (int**)malloc(rows * sizeof(int*));
for (int i = 0; i < rows; i++) {
arr[i] = (int*)malloc(columns * sizeof(int));
}
return arr;
}
int** createCalloc(int rows, int columns) {
int** arr = (int**)calloc(rows, sizeof(int*));
for (int i = 0; i < rows; i++) {
arr[i] = (int*)calloc(columns, sizeof(int));
}
return arr;
}
int** createNew(int rows, int columns) {
int** arr = new int* [rows];
for (int i = 0; i < rows; i++) {
arr[i] = new int[columns];
}
return arr;
}
void initPrint(int** arr, int rows, int columns) {
init(arr, rows, columns);
print((const int**)arr, rows, columns);
}
void init(int** arr, int rows, int columns) {
const int Low = -2, High = 4;
srand((unsigned)time(NULL));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
arr[i][j] = Low + rand() % (High - Low + 1);
}
}
}
void freeMemory(int** arr, int rows) {
for (int i = 0; i < rows; i++) {
free(arr[i]);
}
free(arr);
}
void deleteNew(int** arr, int rows) {
for (int i = 0; i < rows; i++) {
delete[] arr[i];
}
delete[] arr;
}
void print(const int** arr, int rows, int columns) {
// виведення масива на екран
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << setw(7) << arr[i][j];
}
cout << endl;
}
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (arr[i][j] > 0 && arr[i][j] % 2 == 0)
{
//cout << setw(7) << arr[i][j];
sum = sum + arr[i][j];
}
}
cout << endl << "Sum of positive even elements in the row " << i + 1 << " = " << sum;
sum = 0;
}
}
P.S. Two-dimensional arrays must be necessarily dynamic ones. Also, cannot do this task using vector. Only malloc, calloc and new
So I have this code:
main.cpp
#include "matrix.h"
int main(int argc, char *argv[])
{
matrix m;
regularMatrix rMatrix;
rMatrix.setDetails(3, "1 2 3 4 5 6 7 8 9");
//rMatrix.displayMatrix();
//int i, j, r = 0, c = 0, a;
//cout << "Enter number of Rows and Columns: " << endl;
//cin >> r ;
system("PAUSE");
return EXIT_SUCCESS;
}
matrix.cpp
#include "matrix.h"
int rows = 3, columns = 3;
int **a;
void matrix::displayMatrix(int **arr)
{
cout << "Values Of 2D Array [Matrix] Are : ";
for (int i = 0; i < rows; i++ )
{
cout << " \n ";
for (int j = 0; j < columns; j++ )
{
cout << arr[i][j] << " ";
}
}
}
void matrix::setDetails(int dimension, string y)
{
int f = dimension;
rows = dimension;
columns = rows;
string input = y;
istringstream is(input);
int n;
a = new int *[rows];
for(int i = 0; i <rows; i++)
{
a[i] = new int[columns];
}
for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < columns; j++ )
{
while ( is >> n)
{
a[i][j] = n;
//cout << a[i][j] << endl;
}
}
}
matrix::displayMatrix(a);
//cout << f << endl << g << endl;
}
matrix.h
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class matrix
{
public:
virtual void displayMatrix(int** arr);
virtual void setDetails(int dimension, string y);
//virtual void setMatrix(int m[]);
//virtual void printArray(int** i);
};
class regularMatrix : public virtual matrix
{
public:
};
It runs without error but the problem is, I'm getting different value when I'm displaying the matrix? I think I'm getting the address of the array.How will I get the value out of it? I think I'm right about passing my array.
for ( i = 0; i < rows; i++ )
{
for ( j = 0; j < columns; j++ )
{
while ( is >> n)
{
a[i][j] = n;
//cout << a[i][j] << endl;
}
}
}
This is actually pretty wrong. Look what you're doing here.
At beggining, you have i = 0 and j = 0
Then you got into the while loop.
And here until you input ints from stringstream you assign a[0][0] to new value.
See it? You never go to a[0][1] etc. Only first element will be valid and the rest will stay uninitialized, because after first execution of your while loop
there is no characters left in the istringstream object.
So to correct it:
for ( i = 0; i < rows; i++ )
{
for ( j = 0; j < columns; j++ )
{
if ( is >> n )
a[i][j] = n;
}
}
I was wondering if it was possible to move the starting point of the Origin to the bottom-left-corner of my grid.
This is the code I'm working with:
#include <iostream>
using namespace std;
char **createBoard(int n, int m); // Býr til tvívítt kvikt fylki og skilar því til baka
void initiaizeBoard(char **p, int n, int m); // Upphafsstillum allt með '.'
void printBoard(int n, int m, char **p); // Prentum út leikborðið
int main()
{
int rows, columns;
int xhnit;
int yhnit;
cin >> rows >> columns >> xhnit >> yhnit;
char **board = createBoard(rows, columns);
initiaizeBoard(board, rows, columns);
board[xhnit][yhnit] = player;
printBoard(rows, columns, board);
return 0;
}
char **createBoard(int n, int m)
{
char **p = new char*[n];
for (int i = 0; i < n; i++)
{
p[i] = new char[m];
}
return p;
}
void initiaizeBoard(char **p, int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
p[i][j] = '.';
}
}
}
void printBoard(int n, int m, char** p)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << p[i][j];
}
cout << endl;
}
}
For the input "10 10 5 6" my output is as follows:
..........
..........
..........
..........
..........
......X...
..........
..........
..........
..........
The Origins is now set in the top left corner as you can see from the output. I've been searching on this site and the internet in general and I can't seem to figure this out.
In a way, top-left and bottom-left are just arbitrary distinctions before you print the array. The array elements don't really have a physical location before you assign them one. So, in order to move the origin you can just print the rows from first to last.
void printBoard(int n, int m, char** p)
{
for (int i = n-1; i >= 0; i--)
{
for (int j = 0; j < m; j++)
{
cout << p[i][j];
}
cout << endl;
}
}
You can treat everything the same and simply print the rows in reverse order:
void printBoard(int n, int m, char** p) {
for (int i = n-1; i > -1; i--) { // Print in reverse order!
for (int j = 0; j < m; j++) {
cout << p[i][j];
}
cout << endl;
}
}
Here is a live example: http://ideone.com/GJVw8M
I am probably misunderstanding the question but is it just the output display that needs to change?
void printBoard(int n, int m, char** p)
{
for (int i = (n-1); i >=0; i--)
{
for (int j = 0; j < m; j++)
{
cout << p[i][j];
}
cout << endl;
}
}