C++ vector values keep changing? - c++

This is a real simple problem. I'm writing a sliding block puzzle game for an exercise.
1, 1, 1, 1, 1,
1, 0, 3, 4, 1,
1, 0, 2, 2, 1,
1, 1, 1, 1, 1,
It receives input as in the form above, with '0' representing empty spaces, '1' representing walls, and all other numbers representing blocks.
Here is the class definition and constructor for the game state:
class GameState {
public:
GameState(int hght, int wdth);
GameState(const GameState &obj);
~GameState();
int getHeight();
int getWidth();
int getElem(int i, int j);
void setElem(int i, int j, int val);
void display();
void readFile(char* filename);
bool checkSolved();
map<int, vector<int*> > blockLocations;
vector<int> blockList;
void getBlockLocations();
void findBlock(int n);
private:
int **grid;
int height, width;
void allocate() {
grid = new int*[height];
for(int i = 0; i < height; i++)
{
grid[i] = new int[width];
}
}
};
GameState::GameState(int hght, int wdth) {
height = hght;
width = wdth;
allocate();
for(int i = 0; i < hght; i++) {
for (int j = 0; j < wdth; j++) {
grid[i][j] = 0;
}
}
};
Essentially, the grid is represented by a two-dimensional pointer array of integers. height and width are self-explanatory; blockLocations is a map that maps a block number to its point-wise coordinates of the form (y, x). For the time being, if a block occupies multiple spaces only the lowest rightmost space is listed. The matrix initializes as being nothing but zeros; the actual values are read in from a csv.
All of these methods are defined, but the two methods of concern are getBlockLocations() and findBlock(int n).
void GameState::getBlockLocations() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
blockList.push_back(grid[i][j]);
int pos[2] = {i, j};
vector<int*> v;
v.push_back(pos);
blockLocations[grid[i][j]] = v;
}
}
}
void GameState::findBlock(int n) {
vector<int>::iterator it;
it = find(blockList.begin(), blockList.end(), n);
if (it != blockList.end()) {
vector<int*> * posList = &blockLocations[n];
for (int itr = 0; itr < posList->size(); itr++) {
vector<int*> curPos = *posList;
cout << curPos[itr][0] << ", " << curPos[itr][1] << endl;
}
}
}
The problem comes up when I actually run this. As a case example, when I run getBlockLocations(), it correctly stores the coordinate for '2' as (2, 3). However, when I ask the program to display the location of that block with findBlock(2), the resulting output is something along the lines of (16515320, 0). It's different every time but never correct. I don't see the pointer mistake I'm making to get incorrect values like this.

That is bad:
for (int j = 0; j < width; j++) {
blockList.push_back(grid[i][j]);
int pos[2] = {i, j};
vector<int*> v;
v.push_back(pos);
blockLocations[grid[i][j]] = v;
}
You create a pos variable locally and store its reference. When you go out of scope of the for loop it is invalid / data can be replaced by something else.
(actually as Barmar pointed out, since the pos address is always the same within the loop, the values change at each iteration)
You could use a std::pair<int,int> to store your values instead.
When you insert the pair in the vector, the data is copied, not only the pointer: it is safe.
typedef std::pair<int,int> IntIntPair;
IntIntPair pos(i,j);
std::vector<IntIntPair> v;

Related

how to return 2d array using pointers [duplicate]

Hi I am a newbie to C++
I am trying to return a 2d array from a function.
It is something like this
int **MakeGridOfCounts(int Grid[][6])
{
int cGrid[6][6] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }};
return cGrid;
}
This code returns a 2d array.
#include <cstdio>
// Returns a pointer to a newly created 2d array the array2D has size [height x width]
int** create2DArray(unsigned height, unsigned width)
{
int** array2D = 0;
array2D = new int*[height];
for (int h = 0; h < height; h++)
{
array2D[h] = new int[width];
for (int w = 0; w < width; w++)
{
// fill in some initial values
// (filling in zeros would be more logic, but this is just for the example)
array2D[h][w] = w + width * h;
}
}
return array2D;
}
int main()
{
printf("Creating a 2D array2D\n");
printf("\n");
int height = 15;
int width = 10;
int** my2DArray = create2DArray(height, width);
printf("Array sized [%i,%i] created.\n\n", height, width);
// print contents of the array2D
printf("Array contents: \n");
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
printf("%i,", my2DArray[h][w]);
}
printf("\n");
}
// important: clean up memory
printf("\n");
printf("Cleaning up memory...\n");
for (int h = 0; h < height; h++) // loop variable wasn't declared
{
delete [] my2DArray[h];
}
delete [] my2DArray;
my2DArray = 0;
printf("Ready.\n");
return 0;
}
A better alternative to using pointers to pointers is to use std::vector. That takes care of the details of memory allocation and deallocation.
std::vector<std::vector<int>> create2DArray(unsigned height, unsigned width)
{
return std::vector<std::vector<int>>(height, std::vector<int>(width, 0));
}
That code isn't going to work, and it's not going to help you learn proper C++ if we fix it. It's better if you do something different. Raw arrays (especially multi-dimensional arrays) are difficult to pass correctly to and from functions. I think you'll be much better off starting with an object that represents an array but can be safely copied. Look up the documentation for std::vector.
In your code, you could use vector<vector<int> > or you could simulate a 2-D array with a 36-element vector<int>.
What you are (trying to do)/doing in your snippet is to return a local variable from the function, which is not at all recommended - nor is it allowed according to the standard.
If you'd like to create a int[6][6] from your function you'll either have to allocate memory for it on the free-store (ie. using new T/malloc or similar function), or pass in an already allocated piece of memory to MakeGridOfCounts.
The function returns a static 2D array
const int N = 6;
int (*(MakeGridOfCounts)())[N] {
static int cGrid[N][N] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }};
return cGrid;
}
int main() {
int (*arr)[N];
arr = MakeGridOfCounts();
}
You need to make the array static since it will be having a block scope, when the function call ends, the array will be created and destroyed. Static scope variables last till the end of program.
#include <iostream>
using namespace std ;
typedef int (*Type)[3][3] ;
Type Demo_function( Type ); //prototype
int main (){
cout << "\t\t!!!!!Passing and returning 2D array from function!!!!!\n"
int array[3][3] ;
Type recieve , ptr = &array;
recieve = Demo_function( ptr ) ;
for ( int i = 0 ; i < 3 ; i ++ ){
for ( int j = 0 ; j < 3 ; j ++ ){
cout << (*recieve)[i][j] << " " ;
}
cout << endl ;
}
return 0 ;
}
Type Demo_function( Type array ){/*function definition */
cout << "Enter values : \n" ;
for (int i =0 ; i < 3 ; i ++)
for ( int j = 0 ; j < 3 ; j ++ )
cin >> (*array)[i][j] ;
return array ;
}
Whatever changes you would make in function will persist.So there is no need to return anything.You can pass 2d array and change it whenever you will like.
void MakeGridOfCounts(int Grid[][6])
{
cGrid[6][6] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }};
}
or
void MakeGridOfCounts(int Grid[][6],int answerArray[][6])
{
....//do the changes in the array as you like they will reflect in main...
}
int** create2DArray(unsigned height, unsigned width)
{
int** array2D = 0;
array2D = new int*[height];
for (int h = 0; h < height; h++)
{
array2D[h] = new int[width];
for (int w = 0; w < width; w++)
{
// fill in some initial values
// (filling in zeros would be more logic, but this is just for the example)
array2D[h][w] = w + width * h;
}
}
return array2D;
}
int main ()
{
printf("Creating a 2D array2D\n");
printf("\n");
int height = 15;
int width = 10;
int** my2DArray = create2DArray(height, width);
printf("Array sized [%i,%i] created.\n\n", height, width);
// print contents of the array2D
printf("Array contents: \n");
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
printf("%i,", my2DArray[h][w]);
}
printf("\n");
}
return 0;
}
returning an array of pointers pointing to starting elements of all rows is the only decent way of returning 2d array.
I would suggest you Matrix library as an open source tool for c++, its usage is like arrays in c++. Here you can see documention.
Matrix funcionName(){
Matrix<int> arr(2, 2);
arr[0][0] = 5;
arr[0][1] = 10;
arr[1][0] = 0;
arr[1][1] = 44;
return arr;
}

C++ parameterized constructor makes code to stop working when large input's are passed

void initialize(int arr[], int size[], int n)
{
int i;
for(i = 1; i <= n; i++) {
arr[i] = i;
size[i] = 1;
}
}
class hell
{
public:
int edges;
int vertices;
pair<int , pair<int,int>> p[100000];
int disjoint_set[10000];
int cc_size[10000]; // size of connected components
hell(int e, int v)
{
edges = e;
vertices = v;
initialize(disjoint_set, cc_size, vertices);
}
};
In the following class when I create an object using vertices=100000 and edges=100000, the code stops working. But when we remove the initialize(disjoint_set, cc_size, vertices) it starts working. I don't have any clue to such behavior. Please guide me.
Arrays in C++ are zero indexed, which means that valid index is in [0..n[ range. Your code does it wrong:
for(i = 1; i <= n; i++) {
arr[i] = i;
size[i] = 1;
}
it should be:
for(i = 0; i < n; i++) {
arr[i] = i + 1;
size[i] = 1 + 1;
}
or better use algo std::iota() and std::fill():
std::iota( arr, arr + n, 1 );
std::fill( size, size + n, 1 );
and you better use std::vector, which will adjust its size properly, rather than have huge array.

Multidimension vector concept C++

I have a huge 3d vector where I store double values. In order to increase the performance I wanted to reserve the number of elements in advance as I know them before processing;however, I couldn't really figure it out how reserve&clear&erase work in this case.
I have implemented a small program which has 2d vector in this case, pls see the code snippet below:`
for(int counter = 0; counter < 2; counter++){
cout << "Counter-> " << counter << endl;
vector<vector<double> > vec2D;
vec2D.reserve(2);
// assign values to the vec
for(int i = 0; i < 2; i++){
vec2D[i].reserve(5);
for(int j = 0; j < 5; j++){
vec2D[i][j] = j;
}
}
// print the vector content
for(int i = 0; i < 2; i++){
for(int j = 0; j < 5; j++){
cout << vec2D[i][j] << "\t";
}
vec2D[i].clear();
cout << endl;
}
vec2D.clear();
}
When I run this code snippet it iterates thorugh the for loop just for once where it should do it twice; however, when I declare the vector outside of the for loop it does iterate twice. The output of the above snippet is:
Counter-> 0
0 1 2 3 4
0 1 2 3 4
Counter-> 1
Could you please make it clear how it actually should be in this case and how it works.
Thanks in advance.
You have a big problem in your code: reserve() does not change the size of the vector, only its storage capacity (to avoid system calls in the future).
What is needed in your case instead of reserve() is assign() or resize().
If you want any semblance of performance don't use vectors of vectors: this is terribly inefficient. This is probably the most efficient memory utilization, and faster access than vectors, although there are faster solution with some memory trade-offs:
class GridWithSmallDimensions
{
double * values;
int sizeX, sizeY, sizeZ;
public:
GridWithSmallDimensions(int x, int y, int z)
: sizeX(x), sizeY(y), sizeZ(z)
{
values = new double [x*y*z];
}
~ GridWithSmallDimensions()
{
delete [] values;
}
double get(int x, int y, int z)
{
return value[x+sizeX*(y+sizeY*z)];
}
void set(int x, int y, int z, double v)
{
value[x+sizeX*(y+sizeY)] = v;
}
};
Here's a slightly more involved implementation, aimed at reduction of index calculations:
class GridWithRowPointers
{
int sizeX, sizeY, sizeZ; // Dimensions
double * values; // Data
double ** rows; // Pointers to "rows" of data indexed by x
inline int getIndex(y, z) const // Get index of data within a row
{
return sizeZ * y + z;
}
public:
GridWithRowPointers(int x, int y, int z)
: sizeX(x), sizeY(y), sizeZ(z)
{
values = new double [x*y*z]; // Allocate data
rows = new (double*) [x]; // Allocate row pointers
for( int n = 0; n < sizeX; n++ )
{
rows[n] = values + sizeY * sizeZ;
}
}
~ GridWithRowPointers()
{
delete [] values;
delete [] rows;
}
inline double get(int x, int y, int z) const
{
// Access value via row pointer
return rows[ x ][ getIndex(y,z) ];
}
inline void set(int x, int y, int z, double v)
{
// Access value via row pointer
rows[ x ][ getIndex(y,z) ] = v;
}
};

Return a 2d array from a function

Hi I am a newbie to C++
I am trying to return a 2d array from a function.
It is something like this
int **MakeGridOfCounts(int Grid[][6])
{
int cGrid[6][6] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }};
return cGrid;
}
This code returns a 2d array.
#include <cstdio>
// Returns a pointer to a newly created 2d array the array2D has size [height x width]
int** create2DArray(unsigned height, unsigned width)
{
int** array2D = 0;
array2D = new int*[height];
for (int h = 0; h < height; h++)
{
array2D[h] = new int[width];
for (int w = 0; w < width; w++)
{
// fill in some initial values
// (filling in zeros would be more logic, but this is just for the example)
array2D[h][w] = w + width * h;
}
}
return array2D;
}
int main()
{
printf("Creating a 2D array2D\n");
printf("\n");
int height = 15;
int width = 10;
int** my2DArray = create2DArray(height, width);
printf("Array sized [%i,%i] created.\n\n", height, width);
// print contents of the array2D
printf("Array contents: \n");
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
printf("%i,", my2DArray[h][w]);
}
printf("\n");
}
// important: clean up memory
printf("\n");
printf("Cleaning up memory...\n");
for (int h = 0; h < height; h++) // loop variable wasn't declared
{
delete [] my2DArray[h];
}
delete [] my2DArray;
my2DArray = 0;
printf("Ready.\n");
return 0;
}
A better alternative to using pointers to pointers is to use std::vector. That takes care of the details of memory allocation and deallocation.
std::vector<std::vector<int>> create2DArray(unsigned height, unsigned width)
{
return std::vector<std::vector<int>>(height, std::vector<int>(width, 0));
}
That code isn't going to work, and it's not going to help you learn proper C++ if we fix it. It's better if you do something different. Raw arrays (especially multi-dimensional arrays) are difficult to pass correctly to and from functions. I think you'll be much better off starting with an object that represents an array but can be safely copied. Look up the documentation for std::vector.
In your code, you could use vector<vector<int> > or you could simulate a 2-D array with a 36-element vector<int>.
What you are (trying to do)/doing in your snippet is to return a local variable from the function, which is not at all recommended - nor is it allowed according to the standard.
If you'd like to create a int[6][6] from your function you'll either have to allocate memory for it on the free-store (ie. using new T/malloc or similar function), or pass in an already allocated piece of memory to MakeGridOfCounts.
The function returns a static 2D array
const int N = 6;
int (*(MakeGridOfCounts)())[N] {
static int cGrid[N][N] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }};
return cGrid;
}
int main() {
int (*arr)[N];
arr = MakeGridOfCounts();
}
You need to make the array static since it will be having a block scope, when the function call ends, the array will be created and destroyed. Static scope variables last till the end of program.
#include <iostream>
using namespace std ;
typedef int (*Type)[3][3] ;
Type Demo_function( Type ); //prototype
int main (){
cout << "\t\t!!!!!Passing and returning 2D array from function!!!!!\n"
int array[3][3] ;
Type recieve , ptr = &array;
recieve = Demo_function( ptr ) ;
for ( int i = 0 ; i < 3 ; i ++ ){
for ( int j = 0 ; j < 3 ; j ++ ){
cout << (*recieve)[i][j] << " " ;
}
cout << endl ;
}
return 0 ;
}
Type Demo_function( Type array ){/*function definition */
cout << "Enter values : \n" ;
for (int i =0 ; i < 3 ; i ++)
for ( int j = 0 ; j < 3 ; j ++ )
cin >> (*array)[i][j] ;
return array ;
}
Whatever changes you would make in function will persist.So there is no need to return anything.You can pass 2d array and change it whenever you will like.
void MakeGridOfCounts(int Grid[][6])
{
cGrid[6][6] = {{0, }, {0, }, {0, }, {0, }, {0, }, {0, }};
}
or
void MakeGridOfCounts(int Grid[][6],int answerArray[][6])
{
....//do the changes in the array as you like they will reflect in main...
}
int** create2DArray(unsigned height, unsigned width)
{
int** array2D = 0;
array2D = new int*[height];
for (int h = 0; h < height; h++)
{
array2D[h] = new int[width];
for (int w = 0; w < width; w++)
{
// fill in some initial values
// (filling in zeros would be more logic, but this is just for the example)
array2D[h][w] = w + width * h;
}
}
return array2D;
}
int main ()
{
printf("Creating a 2D array2D\n");
printf("\n");
int height = 15;
int width = 10;
int** my2DArray = create2DArray(height, width);
printf("Array sized [%i,%i] created.\n\n", height, width);
// print contents of the array2D
printf("Array contents: \n");
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
printf("%i,", my2DArray[h][w]);
}
printf("\n");
}
return 0;
}
returning an array of pointers pointing to starting elements of all rows is the only decent way of returning 2d array.
I would suggest you Matrix library as an open source tool for c++, its usage is like arrays in c++. Here you can see documention.
Matrix funcionName(){
Matrix<int> arr(2, 2);
arr[0][0] = 5;
arr[0][1] = 10;
arr[1][0] = 0;
arr[1][1] = 44;
return arr;
}

How does one rank an array (sort) by value? *With a twist*

I would like to sort an array in ascending order using C/C++. The outcome is an array containing element indexes. Each index is corespondent to the element location in the sorted array.
Example
Input: 1, 3, 4, 9, 6
Output: 1, 2, 3, 5, 4
Edit: I am using shell sort procedure. The duplicate value indexes are arbitrarily chosen based on which duplicate values are first in the original array.
Update:
Despite my best efforts, I haven't been able to implement a sorting algorithm for an array of pointers. The current example won't compile.
Could someone please tell me what's wrong?
I'd very much appreciate some help!
void SortArray(int ** pArray, int ArrayLength)
{
int i, j, flag = 1; // set flag to 1 to begin initial pass
int * temp; // holding variable orig with no *
for (i = 1; (i <= ArrayLength) && flag; i++)
{
flag = 0;
for (j = 0; j < (ArrayLength - 1); j++)
{
if (*pArray[j + 1] > *pArray[j]) // ascending order simply changes to <
{
&temp = &pArray[j]; // swap elements
&pArray[j] = &pArray[j + 1]; //the problem lies somewhere in here
&pArray[j + 1] = &temp;
flag = 1; // indicates that a swap occurred.
}
}
}
};
Since you're using C++, I would do it something like this. The SortIntPointers function can be any sort algorithm, the important part is that it sorts the array of pointers based on the int that they are pointing to. Once that is done, you can go through the array of pointers and assign their sorted index which will end up in the original position in the original array.
int* intArray; // set somewhere else
int arrayLen; // set somewhere else
int** pintArray = new int*[arrayLen];
for(int i = 0; i < arrayLen; ++i)
{
pintArray[i] = &intArray[i];
}
// This function sorts the pointers according to the values they
// point to. In effect, it sorts intArray without losing the positional
// information.
SortIntPointers(pintArray, arrayLen);
// Dereference the pointers and assign their sorted position.
for(int i = 0; i < arrayLen; ++i)
{
*pintArray[i] = i;
}
Hopefully that's clear enough.
Ok, here is my atempt in C++
#include <iostream>
#include <algorithm>
struct mycomparison
{
bool operator() (int* lhs, int* rhs) {return (*lhs) < (*rhs);}
};
int main(int argc, char* argv[])
{
int myarray[] = {1, 3, 6, 2, 4, 9, 5, 12, 10};
const size_t size = sizeof(myarray) / sizeof(myarray[0]);
int *arrayofpointers[size];
for(int i = 0; i < size; ++i)
{
arrayofpointers[i] = myarray + i;
}
std::sort(arrayofpointers, arrayofpointers + size, mycomparison());
for(int i = 0; i < size; ++i)
{
*arrayofpointers[i] = i + 1;
}
for(int i = 0; i < size; ++i)
{
std::cout << myarray[i] << " ";
}
std::cout << std::endl;
return 0;
}
create a new array with increasing values from 0 to n-1 (where n is the length of the array you want to sort). Then sort the new array based on the values in the old array indexed by the values in the new array.
For example, if you use bubble sort (easy to explain), then instead of comparing the values in the new array, you compare the values in the old array at the position indexed by a value in the new array:
function bubbleRank(A){
var B = new Array();
for(var i=0; i<A.length; i++){
B[i] = i;
}
do{
swapped = false;
for(var i=0; i<A.length; i++){
if(A[B[i]] > A[B[i+1]]){
var temp = B[i];
B[i] = B[i+1];
B[i+1] = temp;
swapped = true;
}
}
}while(swapped);
return B;
}
create a new Array and use bubble sort to rank the elements
int arr[n];
int rank[n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(arr[i]>arr[j])
rank[i]++;
The rank of each element will be rank[i]+1 to be in the order of 1,2,....n
Well, there's a trival n^2 solution.
In python:
newArray = sorted(oldArray)
blankArray = [0] * len(oldArray)
for i in xrange(len(newArray)):
dex = oldArray.index(newArray[i])
blankArray[dex] = i
Depending on how large your list is, this may work. If your list is very long, you'll need to do some strange parallel array sorting, which doesn't look like much fun and is a quick way to introduce extra bugs in your code.
Also note that the above code assumes unique values in oldArray. If that's not the case, you'll need to do some post processing to solve tied values.
Parallel sorting of vector using boost::lambda...
std::vector<int> intVector;
std::vector<int> rank;
// set up values according to your example...
intVector.push_back( 1 );
intVector.push_back( 3 );
intVector.push_back( 4 );
intVector.push_back( 9 );
intVector.push_back( 6 );
for( int i = 0; i < intVector.size(); ++i )
{
rank.push_back( i );
}
using namespace boost::lambda;
std::sort(
rank.begin(), rank.end(),
var( intVector )[ _1 ] < var( intVector )[ _2 ]
);
//... and because you wanted to replace the values of the original with
// their rank
intVector = rank;
Note: I used vectorS instead of arrays because it is clearer/easier, also, I used C-style indexing which starts counting from 0, not 1.
This is a solution in c language
#include <stdio.h>
void swap(int *xp, int *yp) {
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n - 1; i++)
// Last i elements are already in place
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(&arr[j], &arr[j + 1]);
}
/* Function to print an array */
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 98};
int arr_original[] = {64, 34, 25, 12, 22, 11, 98};
int rank[7];
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
//PLACE RANK
//look for location of number in original array
//place the location in rank array
int counter = 1;
for (int k = 0; k < n; k++){
for (int i = 0; i < n; i++){
printf("Checking..%d\n", i);
if (arr_original[i] == arr[k]){
rank[i] = counter;
counter++;
printf("Found..%d\n", i);
}
}
}
printf("Original array: \n");
printArray(arr_original, n);
printf("Rank array: \n");
printArray(rank, n);
return 0;
}