How to work with 3d arrays through pointers in C++ - c++

I'm facing some difficulties with pointers for 3D arrays in C++. I've a array of Q[3][N][N] which I want to pass to a function to print the values at [0][i][j] location (and also for [1][i][j] and [2][i][j]). How can I achieve this? Will it be more convenient to use Q[i][j][0] etc?
for 2D, the following piece of code works just fine when I give &Q[0][0] to the *var:
template <typename T>
void print2d(T *var, int I, int J){
cout << endl;
for (int j = 0; j < J; j++){
for (int i = 0; i < I; i++){
cout << setprecision(3) << setw(12) << *(var + (N*i + j));
}
cout << endl;
}
cout << endl;
}
I'm using the same approach to write a similar function for 3D which does not write out the correct values:
Can anybody let me know the correct way to point to the correct address of Q[i][j][1]. In input argument, I'm giving the address of Q[0][0][0]. Should I use different addresses (such as Q[i][j][1]) if I want to write out for that particular value of k?
template <typename T>
void print3d(T *var, int I, int J, int K){
cout << endl;
for (int j = 0; j < J; j++){
for (int i = 0; i < I; i++){
cout << setprecision(3) << setw(12) << *(var + I*J*i + I*j + K);
}
cout << endl;
}
cout << endl;
}

Sample example, N must be const.
But consider using vector.
#include <stdio.h>
#include <iostream>
using namespace std;
const int N = 5;
void func2(int* tab, int A, int B, int C)
{
printf("%d\n", tab[N*N*A + N*B + C] );
}
void func(int tab[3][N][N])
{
for (int i = 0; i < 3; ++i)
for (int j = 0; j < N; ++j, printf("\n"))
for (int k = 0; k < N; ++k)
{
printf("%d ", tab[i][j][k]);
}
}
int main()
{
int tab[3][N][N];
int p = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < N; ++j)
for (int k = 0; k < N; ++k)
{
tab[i][j][k] = p++;
}
func(tab);
printf("\n");
func2((int*)tab, 1, 1, 3);
}

I agree with Adam's answer but cant comment, need more points there.
1) Arrays are not pointers. Multidimensional arrays cannot be passed via a pointer, needs to have all but one dimension constant. Here is an answer to help understand.
Cannot cast array to pointer
How do I pass a reference to a two-dimensional array to a function?
http://www.cplusplus.com/forum/articles/17108/
You might want to try vectors from STL instead.
2) Templates are compiled only when there is an instance of it used, that is why its declaration and usage is generally put in one file.
Template Compilation

Related

Storing a number of integers into array and printing them out

How do I make a function with for loop to store a given range of numbers into an array, then call that function in main program and print out the stored elements inside of the array?
int main ()
{
testing(array, 20);
}
int testing(int array[], int k)
{
for (int i = 0; k < 20; i++)
{
array[k] = i;
k++;
}
for (int j = 0; j < 20; j++)
{
cout << array[j] << endl;
}
}
I get the error of, "testing has to return value", which I understand that I should of have return var; for example. However, I don't know how to return an array of given elements to print out all the elements with a for loop.
Your function and loop constructions are not carefully designed. You can use pointers instead of having to return an array.
I think this is what you are trying to do:
#include <iostream>
void testing(int* a, int k)
{
for (int i = 0; i < k; i++)
a[i] = i;
}
int main()
{
int a[20];
testing(a, 20);
// you can see that a's elements have changed outside the main (in testing)
for (int j = 0; j < 20; j++)
std::cout << a[j] << "\n";
return 0;
}
You don’t seem to ever execute the first loop. Your stop condition for your first loop is k < 20, but k already is 20.
Then on the second loop, the array just prints out the garbage that was previously saved at those memory locations.
I think what you meant to do was:
for (int i= 0; i < k; i++) {
array[i] = i;
}
for (int j = 0; j < k; ++j) {
cout << array[j] << endl;
}
If you wanted to print the array in main, you need to change the function a bit, by passing in a pointer to the array rather than the array itself.
Also, your main function never initialised an array to pass into your function.
int main (void) {
int a[20];
testing (a, 20);
return 0;
}
Finally, set the return type of the testing function to void.
void testing (int array[], int k);
All in all, it should instead look like this:
#include <iostream>
using namespace std; // I don't recommend doing this
void testing (int * array, const int k);
int main (void) {
const int size = 20;
int a[size] = {0};
testing (a, size);
for (int i = 0; i < size; ++i) {
cout << a[i] << endl;
}
return 0;
}
void testing (int * array, const int k) {
for (int i = 0; i < k; ++i) {
array[i] = i;
}
}

C++ why is there a segmentation fault in my pointer selection sort?

Below is my c++ code. I am trying to implement a selection sort using pointers (start and end). The code compiles, but I am getting a segmentation fault before it will sort the random generated list (currently only prints the random numbers).
Any help as to why this is and how to fix it would be greatly appreciated.
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
void selectionSort(int *start, int *stop) {
for (int i = *start; i < *stop - 1; ++i) {
int min = i;
for (int j = i + 1; j < *stop; ++j) {
if ((&start[0])[j] < (&start[0])[min])
min = j;
}
swap((&start[0])[i], (&start[0])[min]);
}
}
int main()
{
int size = 10;
int* data = new int[size];
for (int i = 0; i < size; ++i)
{
data[i] = rand() % size;
}
for (int k = 0; k < size; k++)
{
cout << data[k] << " ";
}
cout << endl;
selectionSort(data, data+size);
for (int j = 0; j < size; j++)
{
cout << data[j+1] << " ";
}
return 0;
}
The general logic in your function is in the right direction. However, you seem to be confused between values of the elements of the array and the indexing used to access the elements of the array.
The line
for (int i = *start; i < *stop - 1; ++i)
shows the first signs of the confusion.
You are initializing i with the value of the first element of the array and incrementing the value in the subsequent iterations of the loop. That is not correct. Incrementing the value of the first element of the array does not make logical sense.
*stop causes undefined behavior since stop points to a place one past the last valid element.
You need to use int* i, int* j, and int* min to properly sort the elements. That also means updating almost the entire function accordingly. Here's an updated function that works for me.
void selectionSort(int *start, int *stop) {
for (int* i = start; i < (stop - 1); ++i) {
int* min = i;
for (int* j = i + 1; j < stop; ++j) {
if (*j < *min)
{
min = j;
}
}
swap(*i, *min);
}
}
Also, the following lines in main are not correct. You end up accessing the array using an out of bounds index.
for (int j = 0; j < size; j++)
{
cout << data[j+1] << " ";
}
Replace them by
for (int k = 0; k < size; k++)
{
cout << data[k] << " ";
}

c++ Find average of negatives with function

This code to find average of the negative elements works fine when I put everything in main. The problem is when I try to split it to functions. How can I connect the elements from cinarray and negative_average function?
#include <iostream>
using namespace std;
int main()
{
cinarray();
negative_average();
}
int cinarray()
{
int A[3][3];
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
cout << "\n A[" << i + 1 << "][" << j + 1 << "]=";
cin >> A[i][j];
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
cout << A[i][j] << "\t";
cout << "\n";
}
// compute average of only negative values
int negative_average()
{
int negCount = 0;
int average = 0;
for (int x = 0; x < 3; ++x) {
for (int y = 0; y < 3; ++y) {
if (A[x][y] < 0) {
++negCount;
average += A[x][y];
}
}
}
if (negCount > 0) {
average /= negCount;
cout << "Average of only negative values \n" << average;
}
}
}
and one more thing why the error list show that i need ";"
int negative_average()
{ //here
int negCount = 0;
int average = 0;
First of all, you cannot define a function inside another function's body, that's why the "; needed here" error. Move it to the global scope. In this case, you can create int A[3][3]; in main, and declare your functions accordingly:
void cinarray(int A[3][3]); // why int return type?
void negative_average(const int A[3][3]);
Then pass A to both.
As an option define array in main of the and pass reference to cinarray() and negative_average().
Do something like this:
int main()
{
int A[3][3];
cinarray(A);
negative_average(A);
return 0;
}
Where:
int cinarray(int (&A)[3][3])
int negative_average(const int (&A)[3][3])
Your array A isn't visible to both functions. You need to declare it in your main(), then pass it as an argument to your other functions.

C++ 2D Array - Error invalid types ‘int[int]’ for array subscript

I am trying to create MxN matrix using 2D-arrays in C++.
The createMatrix() function asks for user input for matrix items and the printMatrix() function has to print the matrix.
But the printing task is not working (I can't access the array created, I don't understand why)
I receive the error :
matrix.cpp:35:20: error: invalid types ‘int[int]’ for array subscript
cout << matrix[i][j];
The code I'm working with is:
#include "iostream"
using namespace std;
// user input matrix
int createMatrix(int m, int n){
int arr[m][n];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
cout << "A[" << i << "][" << j << "] : ";
cin >> arr[i][j];
}
cout << endl;
}
return arr[m][n];
}
/*
void printMatrix(int matrix[][2], int m, int n){
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
cout << matrix[i][j];
}
}
}
*/
int main(){
int m = 2, n = 2; // m = rows, n = columns
int matrix = createMatrix(m,n);
// printMatrix(matrix, m, n); // not working as sub-routine too, main target to make it work with sub-routine
// to print matrix
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
cout << matrix[i][j];
}
}
return 0;
}
matrix is an int not an int[][]. Since it is an int there is no subscript operator and that is why you are getting the error you are getting. You are also using veriable length arrays which is not standard C++. I would suggest you change your code to use a std::vector like
std::vector<std::vector<int>> createMatrix(int m, int n)
{
std::vector<std::vector<int>> arr(m, std::vector<int>(n));
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
cout << "A[" << i << "][" << j << "] : ";
cin >> arr[i][j];
}
cout << endl;
}
return arr;
}
And then main() would be:
int main(){
int m = 2, n = 2; // m = rows, n = columns
std::vector<std::vector<int>> matrix = createMatrix(m,n);
// printMatrix(matrix, m, n); // not working as sub-routine too, main target to make it work with sub-routine
// to print matrix
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
cout << matrix[i][j];
}
}
return 0;
}
Your matrix is not array. it is int.
You need to work with the pointers.
Yes, createMatrix works but you won't be able to do anything with what it created. Because:
arr[n][m] is local (and out of boundaries by the way). It is not a matrix as you probably thought but an item of arr at position [n][m].
It is not well defined to declare array of fixed sizes with vary sizes that depend on function input.
You need to pass to createMatrix array from the main() as pointer (like you did in printMatrix) and createMatrix should work with it and not something local.
Now regarding your original question:
But the printing task is not working (I can't access the array
created, I don't understand why)
matrix was defined as int, not as array.
int matrix = createMatrix(m,n);

multidimensional array function outputting garbage?

I have this function meant to initialize a multidimensional 2d (6x6) array to zero. I call the function in main using cout to test it and it outputs garbage. Please help. Thanks!
int** initializeArray(void)
{
typedef int* rollArray; //this line is actually outside of the function in my
//program
int i, j;
rollArray *m = new rollArray[6];
for (i = 0; i < 6; i++)
m[i] = new int[6];
for (i = 0; i < 6; i++)
for (j = 0; j < 6; j++)
m[i][j] = 0;
return m;
}
If the value 6 is known at compile-time, I would suggest using std::array in a nested fashion. For example:
#include <array>
#include <iostream>
int main()
{
std::array<std::array<int,6>,6> a = {0};
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 6; ++j)
{
std::cout << a[i][j] << std::endl; // Prints 0.
}
}
return 0;
}
In fact, you won't even need to create a function to initialize your array. Declare your nested array and you are good to go. (If you don't know the dimension at compile-time, you could use std::vector in a similar fashion.)
The problem is with your test.
How can you mess up such a simple test? Just use:
int ** a = initializeArray();
int i,j;
for (i = 0; i < 6; i++) {
for (j = 0; j < 6; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}