Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need help with drawing an array. I basically want to draw the array as it looks in the array instead of on 1 line
Here's my array:
const int MAP_WD = 5;
const int MAP_HT = 5;
int map[MAP_WD * MAP_HT] = {
1, 1, 1, 1, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 1, 1, 1, 1
};
Here's where I'm trying to draw the array:
int y, x;
for (y = 0; y < MAP_HT; ++y)
{
for (x = 0; x < MAP_WD; ++x)
{
}
}
Try printing a new line at the end of each inner loop.
In the inner loop, print the actual elements.
To get the relevant element, simply use y*MAP_WD + x.
Related
This question already has answers here:
Initialization of all elements of an array to one default value in C++?
(12 answers)
Closed 3 years ago.
I have an array, ex:
int M[10] = { 1 };
I expected M[0] ~ M[9] will all be filled with 1, but when I printed them out, it doesn't, only M[0] is 1, others are 0.
This is a common misconception. Often you see people write things like this
int m[10] = { 0 };
To "initialise all elements to 0"; however, really that just initialises the first element to 0 and the rest are default initialised (to 0, which is why it works). Really they could've just written int m[10] = {};
You have the same problem: you initialise only the first element to 1 then the rest default to 0. You can either specify all the parameters, or use std::fill:
int m[10] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int n[10];
std::fill(std::begin(n), std::end(n), 1);
Sorry for the vague title, I'm not sure how to word it.
Say I have a vector:
vector<int> vec{{
1, 2, 3, 4, 5, 7, 8, 9, 10, 12, 13, 14
}};
and a corresponding positions vector which occurs after a number is missed (like 5 to 7).
vector<int> positions{{
1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1
}};
How would I return a 2D vector running from each 1, up to but not including the next one: e.g.
1 2 3 4 5
7 8 9 10
12 13 14
Thanks in advance, I'd post my attempts, but they all led down blind avenues. I know it's an odd structure...
There are a few ways to solve this, but I believe this should work:
std::vector<std::vector<int>> vec2D;
int index = -1;
// Identify the indices of the position vector and use that to identify the
// correct indices of the vec.
for (int i = 0; i != position.size(); ++i)
{
// if the value at i of the position vector is 0,
// push_back the value at i of the vec vector into
// the correct vector of vector.
if (0 == position[i])
{
vec2D[index].push_back(vec[i])
}
else if (1 == position[i])
{
++index; //increment to the next vector
std::vector<int> temp;
vec2D.push_back(temp);
vec2D[index].push_back(vec[i])
}
}
I've got a C-style array called board that contains some char's. I'm trying to create a std::array or std::vector (either would be fine, although std::array would be preferable) to store all the indices of board that are a certain value (in my case, 0).
This code I wrote is functional and works well:
std::vector<int> zeroes;
zeroes.reserve(16);
//board has 16 elements, so zeroes.size() will never be larger than 16.
//I used this reserve for speedup - the compiler doesn't require it.
for (int i = 0; i < 16; ++i)
{
if (board[i] == 0)
{
zeroes.push_back(i);
}
}
However, from past experience, whenever a std function exists that could replace part of my code, it is terser and hence stylistically preferred and also faster. My function seems like a fairly basic operation - I know there is a standard function* to access the index of an array that contains a value when that value only occurs once** in the array. So, is there a standard function to create an array of the indices that contain a value, assuming that more than one such index exists?
* Technically, two nested function calls: int x = std::distance(board, std::find(board, board + 16, 0));. See the accepted answer here.
** Well, it still works if more than one index with the desired value is present, but it returns only the first such index, which isn't very useful in my context.
Edit:
As one of the answers misunderstood the question, I'll clarify what I'm seeking. Let's say we have:
char board[16] = {0, 2, 0, 4,
2, 4, 8, 2,
0, 0, 8, 4,
2, 0, 0, 2};
Now, the indices which I'm looking for are {0, 2, 8, 9, 13, 14} because board[0] = 0, board[2] = 0, board[8] = 0, etc. and these are the only numbers which satisfy that property.
Here's a solution using std::iota and std::remove_if:
#include <algorithm>
#include <iostream>
int main () {
const std::size_t board_size = 16;
char board [board_size] = {
0, 2, 0, 4,
2, 4, 8, 2,
0, 0, 8, 4,
2, 0, 0, 2
};
// Initialize a zero-filled vector of the appropriate size.
std::vector<int> zeroes(board_size);
// Fill the vector with index values (0 through board_size - 1).
std::iota(zeroes.begin(), zeroes.end(), 0);
// Remove the index values that do not correspond to zero elements in the board.
zeroes.erase(std::remove_if(zeroes.begin(), zeroes.end(), [&board] (int i) {
return board[i] != 0;
}), zeroes.end());
// Output the resulting contents of the vector.
for (int i : zeroes) {
std::cout << i << std::endl;
}
}
Output of the program (demo):
0
2
8
9
13
14
This question already has answers here:
Sorting zipped (locked) containers in C++ using boost or the STL
(5 answers)
Closed 8 years ago.
Consider the case where “rowPtr”, “colInd” and “values” in a struct are dynamically allocated with same number of elements. In this scenario, what is the fastest way (without copying if possible!!) to sort elements of colInd so that rowPtr and value elements are swapped or change positions based on how elements of colInd change positions.
struct csr
{
int rows;
int cols;
int nzmax;
int *rowPtr;
int *colInd;
double *values;
};
// A simple example without a struct. Just based on arrays
double values[10] = {0.2135, 0.8648, 7, 0.3446, 0.1429, 6, 0.02311, 0.3599, 0.0866, 8 };
int rowPtr[10] = { 0, 3, 6, 10, 2 -1, 24, -4, 1, 11 };
int colInd[10] = { 0, 2, 4, 1, 2, 3, 0, 1, 2, 4 };
// sort colInd and simultaneously change positions in rowPtr and values
//After sorting
Values = {0.214, 0.023, 0.345, 0.360, 0.865, 0.143, 0.087, 6.0};
rowPtr = {0, 24, 10, -4, 3, 2, 1, -1};
colInd = {0, 0, 1, 1, 2, 2, 2, 3};
I suggest putting the three arrays into an array of struct and sorting the array of struct.
struct csr_data
{
int rowPtr;
int colInd;
double value;
};
and
struct csr
{
int rows;
int cols;
int nzmax;
csr_data* data_array;
};
You can sort an array of csr_data using any of the three member variables. When they are sorted, all elements of csr_data will be rearranged regardless of which member you use to sort the data by.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So assume I have a file as shown below:
1 2 6 2 3 7
3 7 1 2 3 7
In C++, how can I store the values in two arrays like the ones below?
[1, 2, 6, 2, 3, 7]
[3, 7, 1, 2, 3, 7]
Use two std::vector<int>s and a std::stringstream:
std::vector<int> a, b;
std::string str1, str2;
if (std::getline(file, str1) && std::getline(file, str2))
{
std::stringstream iss(str1);
for (int n; iss >> n; )
a.push_back(n);
iss.clear();
iss.str(str2);
for (int n; iss >> n; )
b.push_back(n);
}
Take a look boost::tokenizer and, like a comment said, use std::vector.