printing 1D array with 5 columns - c++

hello stack overflowers!
haha I am relatively new to c++ and I have a small problem figuring out small piece of the code. Basically I need to print out a full array of ints in 5 different columns. The thing that throws me off is that I have no clue how many ints are in the array and how many rows it'd create?(it is a 1d array) 2d is very easy to process. I just dont know how to go about this. Any logical help would be much appreciated. I have no problem coding it i just don't know how to set this up. Ive been thinking about it for some time now.

You need to know the total number of elements in the 1d-array and we need decompose the number of these elements in the 1d array on the two multiplier.
First multiplier will be lines, second - rows.
For example if we have 50 elements in the 1d-array
we can assume that we have 10 lines and 5 columns
and then write something like this:
for(int line = 0; line < 10; line++)
{
for( int col = 0; col < 5; col++)
{
cout << setw(5) << arr[line*5 + col];
}
cout << endl;
}

Related

Creating a 50 x 50 grid

I had a question about C++ if anybody can answer it for me, I will really appreciate it.
Say I create an output file and I want to create a 50 x 50 grid in the file. How can I do this without using multidimensional arrays?
I was thinking of this. However, I think the newline character on each line is not considered to be part of the canvas:
//set up 50x50 canvas
for(int i = 1; i <= 50; i++)
{
outfile.put(' ');
for(int j = 1; j <= 50; j++)
outfile.put('\n');
}
//reset pointer to start of file
outfile.seekp(0, ios::beg);
One of the things I wish to accomplish with this grid is to be able to move the pointer around. Any help will be appreciated
You can represent a 2-dimensional array as a 1-dimensional array with the formula row_index * num_columns + column_index. In your case you the number of columns would be 51 instead of 50 to account for the newline.

Does anyone know how to solve problems on variable length arrays?

Input Format
The first line contains two space-separated integers denoting the respective values of (the number of variable-length arrays) and (the number of queries).
Each line of the subsequent lines contains a space-separated sequence in the format k a[i]0 a[i]1 … a[i]k-1 describing the -element array located at.
Each of the subsequent lines contains two space-separated integers describing the respective values of (an index in the array ) and (an index in the array referenced by ) for a query.
Output Format-
For each pair of and values (i.e., for each query), print a single integer denoting the element located at an index of the array referenced by. There should be a total of lines of output.
Sample Input
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
Sample Output
5
9
Somebody has solved this problem by -
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,q; //n number of variable lenght arrays
// q no of queries asked
cin >>n >>q;
int ** Vectors = new int *[n];//no of length of var. arrays
int j;
for (int i=0;i<n;i++)
{
cin>>j;
Vectors[i] = new int [j];
for (int y=0;y<j;y++)
cin>>Vectors[i][y];
}
int q1,q2;
for (int i=0;i<q;i++)
{
cin >>q1 >> q2;
cout<<Vectors[q1][q2]<<endl;
}
return 0;
}
Can somebody explain me this code? Or if anyone has a better approach to solve this problem. Then please explain it in detail.
This shouldn't be hard to understand, that code is basically initializing dynamic 2D array at run time then inserting values to the 2D array and then accessing it by giving index:
int ** Vectors = new int *[n];//no of length of var. arrays
int j;
for (int i=0;i<n;i++)
{
cin>>j;
Vectors[i] = new int [j]; // initialzing inner array.. consider it as 2D array with n rows and j columns
for (int y=0;y<j;y++)
cin>>Vectors[i][y]; // insert element at specified index
}
cout<<Vectors[q1][q2]<<endl; // access element from 2D array
What you might want to use is a Matrix class.
Using
vector<vector<int>>
should do it.
Alternatively the snipet code should be refactored into a Matrix class with a constructor and a destructor.
The example you give present a memory leak since the allocated memory is not freed.

2-D Array C++ Triangle

I am trying to create a program in c++ that takes in triangle pattern of numbers into a 2-D array.
Example:
1
3 4
5 9 2
9 4 6 1
The top row is one number(integer) and each row of the triangle has one more number than the row above it.
Once the triangle has been entered and/or examined via for loops, the program needs to traverse the triangle from top to bottom and record all possible sums of each path;
The path taken down the triangle must always be adjacent to the number in the row above it.
While traversing down the triangle each "path" should be stored in a new array so the path can be displayed.
After recording the sum for each path down the triangle the program should compare them and display the path with the smallest sum.
With the changes i made so far thanks to #Beta i have this so far:
int main()
{
int row = 0;
int col = 0;
int A[4][4] = {{2},{8,9},{3,4,5},{6,2,9,1}};
for (row = 0; row < 4; row++)
{
for (col = 0; col <= row; col++)
{
cout << A[row][col] << " ";
}
cout << endl;
My Output is so far is:
2
8 9
3 4 5
6 2 9 1
I think the trick you're looking for is this:
for (col = 0; col <= row; col ++)
I couldn't parse the last part of your question ("After that...").
EDIT:
The problem of making the triangle look symmetrical is a problem of printing spaces at the beginning of each line. With the trick above, you should be able to figure than one out.
As for considering all paths and displaying the one with the smallest sum, what have you tried? If you are not familiar with Breadth-First Search and Depth-First Search, copying arrays and arrays of pointers, you're probably not ready for this exercise.

How can I randomize an array?

Here's the part of the program I'm having problems with:
// precondition: board is initialized
// postcondition: board is shuffled by randomly swapping 20 values
void shuffle(int board[][NCOLS]) {
int num = rand();
num = num %6 + 1;
for (int i = 0; i < 20; i++) {
}
}
Pretty sure I have it wrong already, I think I may need the rand function but I'm not sure how the for loop would work.
Basically there are 6 pictures and they're in 4 columns, it's a memory game and as of the moment they stay in the same place. I need to make it so that they are random and flipped on the side where you can't see them but I can't figure it out.
I have no idea how to randomize columns especially when they're under the name of simply board and NCOLS.
I can see why this is hard - random_shuffle prefers 1D arrays, and you have a 2D array. Luckily, since arrays are contiguous, that means a 2D array can also be accessed as a 1D array - it's just NCOLS x NROWS elements in memory:
auto begin = &(board[0][0]);
auto end = begin + NCOLS*NROWS;

How to find best matching for all columns of a 2D array?

Let's say that I have a 2D array that looks like:
________________
|10|15|14|20|30|
|14|10|73|71|55|
|73|30|42|84|74|
|14|74|XX|15|10|
----------------
As I showed, the columns don't need to be same size.
Now I need to find the best matching for each column (the one that has most exactly the same items and lowest different). Of course, I could do that in n^2 but it's too slow for me. How can I do it?
I thought about a k-dimension tree and finding the closest neighbor for every one, but I don't know if it's good and it will work as I want (probably not).
Result for example:
First column is most likely third (only three different - 10, 14, 42)
Second column -> fifth (only two different - 15 and 55)
and so on and so on... :)
If you know that all the numbers in the table are 2-digit numbers (i.e. 10 =< x <100), for each column create an array of booleans where you will mark the existing numbers:
bool array[5][100];
std::fill( &array[0][0], &array[0][0] + sizeof(array) , false ); // init to false
for (int i = 0; i < 5; i++)
{
for (int j = 0; j <5; j++)
{
array[i][table[i][j]] = true;
}
}
Should be easy from there.