Extracting a row from a 2-dimensional C++ array - c++

I have a 2D array called resolvedStress which can take m x n rows and columns. I would like to define a new variable called criticalData which is a 1D array containing just one row of resolvedStress. If I want to assign the 4th row to criticalData, would it be correct to write:
float* criticalData = &resolvedStress[4 * n];
I'm new to C++ so I'm not very confident yet!

Arrays are itself a static pointer, no need to use '&', again as you said you wana point to 4th row so it would be 3 and not 4 as array index starts from 0.
float* criticalData = resolvedStress[3];

Assuming your array is organised as follows:
[ row0 | row1 | row2 | .... ]
That is, a 1D array containing the rows of the matrix in order, then &resolvedStress[4 * n] will be a pointer to row4 - which is actually the 5th row of the array (gotta love zero indexing).

If you want to access fourth row of m X n array, you can access the each row as
resolvedStress[0] will point to first row of the 2D array.
resolvedStress[1] will point to second row of the 2D array.
...
resolvedStress[m-1] will point to mth row of the 2D array.
for your case it would be
float* criticalData = resolvedStress[3];

Related

Find a value in a 2d array and move it to another 2d array

So I'm suppose to search my 2d array and find the locations (row and column number) where the search value occurred and store the row and column number in successive rows of a two dimension array
my instructions
"If the value is found in the data array, store the location (row and columns offset) where found in the data array into the locations array. Each time you find the value in the data array the location will be stored in the next row of the locations array. The row offset will be stored in column 0 and the column offset will be stored in column 1 of the locations array. Search the two dimension data array by rows."
This is where I'm confused
locations[Rsize][Rsize]=data[i][j];
Rsize++;
I don't know how to move the row and column from data array to the new locations array
locations array row 0 col 0 should have the data row
& locations array row 0 col 1 should have the data column
void find_value(short data[][15], short rows, short cols, short locations[] [2], short &Rsize, short searchValue)
{
short i,j;
Rsize=0;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if(data[i][j]==searchValue)
{
locations[Rsize][Rsize]=data[i][j];
Rsize++;
}
}
}
}
You are close. Remember, locations is just an array of arrays. You just want to append an array of size 2 to locations.
You want to change this:
locations[Rsize][Rsize]=data[i][j];
to this:
locations[Rsize][0] = i;
locations[Rsize][1] = j;

converting the index of 1D array into 2D array

How can I convert the index of 1D array into 2D array? I know how to convert a 2D array into 1D (i*the size of row+j). I want the opposite of that.
What you need to know is: How many columns should the 2D array have:
Lets say you have an array of 20 columns and 10 rows (array[20,10]):
int index = 47;
int numberOfColumns = 20;
int column = index % numberOfColumns;
int row = index / numberOfColumns;
// column == 7
// row == 2
You can just do the opposite. if n is the length of the row and x is index in 1D. You can index like
array[x/n][x%n]

What is being referenced for this matrix?

I was given the following code to use as part of an exercise. I am instructed to create a 3x3 matrix and assign specific values to it.
Here is the code:
void minput(int* m, int row, int col) {
/* assign 3X3 matrix to following value
8 1 6
3 5 7
4 9 2
*/
*(m+0) = 8;
}
What I am trying to figure out is what this piece of code *(m+0) = 8; is for. I know that adding a * in front of any variable means to "give me whatever is that the address".
What does the +0 do?
*(m+0) is equivalent to m[0]. So the whole statement is assigning 8 to m[0].
it dereferences m (gives you the value at it) I'm guessing it's + 0 so that you can add in different values and get the resulting part of the matrix
Arrays are contiguous in memory, so if you were to add 1 instead of 0, you would set the next value in the matrix to 8.
*(m+0) = 8 is the same as m[0] = 8, it dereferences the pointer to the first element then assigns to it the value 8. In your code you represent the matrix via a one-dimensional array, so you probably want to index your (i,j) component as
m+i*col + j or, equivalently, m[i*col+j], i.e. the line
*(m+i*col+j) = x // can also write is as m[i*col+j] = x
assigns x to the (i,j) component.

C++ arrays - limit and size

How do you declare an array for a matrix that contains say 4 numbers (2x2)? I assume that int m[4] only allows numbers up to 4. Or does it mean any four numbers? I don't understand the difference.
In a declaration
type array_name[ array_size];
type is the data type that this array stores. The particular value of array under index i, i.e. array_name[i] can be any of the values that type can represent.
In your example int m[4]; declares an array of four integers. The particular value of any of these integers can be any of the values that integer can represent. To know these limits you can print them:
#include <limits>
int imin = std::numeric_limits<int>::min(); // minimum value
int imax = std::numeric_limits<int>::max(); // maximum value
The use of STD should simplify your life in the construction of a matrix :
std::vector<std::vector<int>> M(2, std::vector<int>(2));
But if you want to use arrays :
int x[2][2];
int m [4] would declare an array with 4 uninitialized values of type integer. Remember that these values are zero-indexed,
meaning that to call a value in the array you would call m[0-3]. You may assign any values you like to the array by the following command: m[4] = {Value1, Value2, Value3, Value4} If you prefer, you may also create a loop that will assign values to an array, which can be immensely useful at times.
Keep in mind that arrays are not commonly used in C++, std::vector is far more used, and for good reason.
http://www.cplusplus.com/reference/vector/vector/
int m[4] declares an array of 4 integers. The indexes of the integers will be 0, 1, 2, 3, while the values at those indexes can be any integers. so m[2] = 2003; sets the 3rd value in the array to 2003. As for the 2x2 aspect, you probably want to do something like int m[2][2]; . I think about this as declaring an array of size 2, containing arrays at each spot, instead of ints or floats or whatever. The arrays contained at each spot (there are only two spots so only two arrays in this case) each hold two ints. So if the first value in your matrix is 32, you could set that by doing m[0][0] = 32; or more generally, m[x][y] = value_of_(x,y);
The quickest way to do what you described is probably this, if you know the values ahead of time:
int row0col0 = value at 0th row 0th column;
int row0col1 = value at 0th row 1st column;
int row1col0 = value at 1st row 0th column;
int row1col1 = value at 1st row 1st column;
int m[2][2] = { {row0col0, row0col1}, {row1col0, row1col1} };
or equivalently:
int m[2][2] = {row0col0, row0col1, row1col0, row1col1};
This is referred to as row-major order: elements in a 2d array are sorted first by row, then by column.

2-dimensional array, IF condition checking

I've came across a problem when coding with C++ 2D array.
Just a little question, what does the code below mean?
...
if(array[x][y] >= 9){
...
}
...
Does that mean when the sum of x and y of the array is greater or equal to 9, then only the body of the IF will run? Or ........?
Please explain and provide some simply examples.
the array is two dimensional, it means the element at array[x][y]
unlike 1D arrays, which only require 1 index, 2D arrays require 2 indices in the form of array[x][y] presumably in a nested for loop.
You can iterate through such an array like this
for (int x = 0; x < arrayLength; x++) {
for (int y = 0; y < array[x]Length; y++) {
// do something with array[x][y]
}
}
where arrayLength is the length of array and array[x] length is the length of array[x].
So in reference to the code that you posted, it's testing to see if the member of the 2D array is greater than or equal to 9.
Ok let's start with the basics.
1D Arrays
How can you imagine a normal array? You could say a normal array is like a number line:
|-------------------------------| where every - is one element in your array
The very first '-' on the left side is the element at myArray[0] (the '|' are just symbolizing that it has a start and a end).
2D Arrays
A 2D array can be visualized as checkerboard, bookshelf or a table with columns and rows.
|-------------------------------|
|-------------------------------|
|-------------------------------|
|-------------------------------|
Just like in chess you need 2 values in order to address an element. If you only specify one value the compiler might know the row of your value but not its column (or the other way around). That means you need x and y coordinates (which is a visual analogy for a coordinate system). In order to address a value you have to do it like this:
myArray[x][y] where x could be the row of our checkerboard and y the column.
In your case your 2D array is most likely filled with integers. The 'if' statement checks if the value stored in myArray[x][y] is larger than 9. If myArray[x][y] is larger than 9 this statement returns true and the code inside will get executed.
After executing the code inside of the 'if' statement the program will continue to execute the code after the if statement. 2D arrays can be understood as an array containing arrays.
If you are thinking 3 dimensional arrays are possible you're right. Here you need 3 coordinates in order to describe a point since you have depth, height and length (here I'm talking about the visual length not the length in terms of total amount of elements.).
I don't know whether this helped but this is of course a very visual approach of explaining how multi-dimensional arrays work.
Example
int myArray[3][3] = {{1, 2, 3}, // row 0
{4, 5, 6}, // row 1
{7, 8, 10}}; // row 2
In this case your if statement would only be executed if x = 2 and y = 2 since myArray[2][2] = 10
It means "IF the element at coordinates (x,y) is greater or equal than 9...".
There is no addition operation. The array is probably declared with the minimum dimensions (x+1, y+1).
int array[2][2] = {{12, 6}, {3, -2}};
int x=1, y=0;
if(array[x][y] >= 9){...} // array[1][0] equals 3, condition is false