printing an array as a table in c++ - c++

So, I am trying to print an array Fi[rows][columns] in a table format of 11 rows and 11 columns. The code I am using is:
for (int i = 0; i < rows ; i++){
for (int j=0; j< columns; j++)
std::cout << Fi[i][j]<<"\t";
std::cout << "\n";
}
And my problem is, that the element printed on Fi[0][10] is not the one that it's supposed to be. Actually, if I simply print out
std::cout<< Fi[0][10];
I get the correct value.
Can someone help me to figure out what I am doing wrong?

Are you looking for something like this? I changed the part std::cout << "\n"; to std::cout << std::endl;
#include <array>
int main() {
int rows = 4;
int columns = 5;
int my_array[rows][columns] = {{0,1,2,3,4},{5,6,7,8,9},{10,11,12,13,13},{14,15,16,17,18}};
for (int i = 0; i < rows ; i++){
for (int j=0; j< columns; j++)
std::cout << my_array[i][j]<<"\t";
std::cout << std::endl;
}
return 0;
}
With the output:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 13
14 15 16 17 18

using namespace std;
#include<iostream>
int main(){
int num[3][3],i=0,sum=0;
for(i=0;i<3;i++){
for(int j=0;j<3;j++){
cin>>num[i][j];
sum+=num[i][j];
}
}
for(i=0;i<3;i++){
for(int j=0;j<3;j++){
cout<<num[i][j]<<"\t";
}
cout<<endl;
}
cout<<sum<<endl;
return 0;
}

Related

Making a matrix out of an array using std::vector

I want to make a program that converts an array into matrix. I made something that sort of works but I'm not quite satisfied with the result. I am getting output that actually makes sense but I don't know what I should change to get the one I want.
The size of matrix doesn't have to be 4x4 it can be whatever in fact I want to make one that can make me a 4x4 out of an array[15].
#include<iostream>
#include<vector>
#include<ctime>
#include<cstdlib>
typedef unsigned int uint;
void print(const std::vector<int>& array)
{
for(uint i=0; i< array.size(); i++)
std::cout << array[i] << " ";
std::cout << "\n" << std::endl;
}
void random(std::vector<int>& array, int size = 4)
{
srand(time(0));
std::vector<std::vector<int>> mat;
for(uint i=0; i<4; i++)
{
for(uint j=0; j<4; j++)
{
array.push_back(rand()%10);
}
mat.push_back(array);
}
print(array);
for(uint i=0; i<4; i++)
{
for(uint j=0; j<4; j++)
{
std::cout << mat[i][j] << " ";
}
std::cout << std::endl;
}
}
main()
{
std::vector<int> A;
random(A);
}
Example of an output since it's random
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
The one i would like to get would look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
After you've added array to mat with mat.push_back(array), you need to clear array, otherways array will always grow.
std::vector<std::vector<int>> mat;
for (uint i = 0; i < 4; i++)
{
for (uint j = 0; j < 4; j++)
{
array.push_back(rand() % 10);
}
mat.push_back(array);
array.clear(); // <<<< add this
}
Change the code that prints mat to this:
for (uint i = 0; i < mat.size(); i++)
{
for (uint j = 0; j < mat[i].size(); j++)
{
std::cout << mat[i][j] << " ";
}
std::cout << std::endl;
}
and you'll see what happens if you don't call array.clear() as shown above.

C++ "Flipping" the rows in an array

I am attempting to print out an array in a specific order, where it is formatted with columns and rows in ascending order, but with the bottom row containing the lowest values. The array is created via a for loop and a pointer.
here is my code so far:
#include <iostream>
#include <iomanip>
int main()
{
// Creation of the array
int* array = new int[24];
for (int i = 0; i < 24; i++)
{
array[i] = i;
}
// Displaying in grid format with 3 rows and 8 columns
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 8; j++)
{
std::cout << std::setw(2) << *array << ' ';
array++;
}
std::cout << '\n';
}
}
The output of my code is:
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
The desired output is:
16 17 18 19 20 21 22 23
8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7
How would I go about "reversing" the rows to reach the desired output?
SOLVED WITH THE FOLLOWING
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int* table = new int[40];
for (int i = 0; i < 40; ++i)
{
table[i] = i;
}
for (int i = 4; i >= 0; --i)
{
for (int j = 0; j < 8; ++j)
{
cout << setw(2) << table[j + (8 * i)] << ' ';
}
cout << "\n";
}
}
Thanks for the help.

Matrix column comparison

I have to compare matrix columns.
I tried many variations, but as far as I got, is when I compare "next to each other" columns.
// N rows
// M columns
#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
int N, M, ok = 1;
const int maxn = 1000;
const int maxm = 1000;
short H[maxn][maxm];
cin >> N >> M;
for (int i=0; i<N; i++){
for (int j=0; j<M; j++){
cin >> H[i][j];
}
}
for (int j = 1; j < M; ++j)
{
ok = 1;
for (int i = 0; i < N; ++i)
{
if (H[i][j-1] >= H[i][j])
{
ok = 0;
}
}
if (ok)
{
cout << j+1 << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
I have to give back the index of the first column where is true, that the column's every element is greater than ANY OTHER column's elements. (Not summarized.)
For example:
10 10 12 15 10
11 11 11 13 20
12 16 16 16 20
It returns with 4, because the 4th column's every element is greater, than the 1st column's elements.
If there are none, it has to return with -1.
You need another inner loop to go through every other column:
#include <ios>
#include <iostream>
int main()
{
using namespace std;
ios_base::sync_with_stdio(false);
int N, M, ok = 1;
const int maxn = 1000;
const int maxm = 1000;
short H[maxn][maxm];
cin >> N >> M;
for (int i=0; i<N; i++){
for (int j=0; j<M; j++){
cin >> H[i][j];
}
}
for (int j = 0; j < M; ++j)
{
for (int j2 = 0; j2 < M; ++j2)
{
if (j == j2) continue;
ok = 1;
for (int i = 0; i < N; ++i)
{
if (H[i][j] <= H[i][j2])
{
ok = 0;
break;
}
}
if (ok)
{
cout << j + 1 << endl;
return 0;
}
}
}
cout << -1 << endl;
return 0;
}
Given the input:
3 5
10 10 10 15 10
11 11 11 13 20
12 16 16 14 20
This prints:
4
See it on Rextester

How to calculate sum of each row in 2D array and place in vector?

I am trying to calculate the sum of each row in the array and place it in a vector,
You can find my attempt below,
For that, it prints the same values for the first 4 and a different for the last 1, 215 215 215 215 316
What I want is for example
x1 2 4 4 6 7 Sumx1=??
x2 1 2 3 4 5 etc
x3 1 2 3 4 5
x4 1 2 4 5 6
and place the value Sumx1 in a vector.
Here's my attempt
#include <time.h>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string name;
srand(time(NULL));
int pay[5][4];
vector<string> names;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
pay[i][j] = rand() % 51 + 50;
cout << pay[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
vector<int> totals;
for (int i = 0; i < 5; i++) {
for (int c = 0; c < 4; c++) {
totals.push_back((pay[i][0] + pay[i][1] + pay[i][2] + pay[i][3]));
}
cout << totals[i] << " ";
}
return 0;
}
for(int i=0; i<5; i++){
for(int c=0; c<4; c++){
totals.push_back((pay[i][0]+pay[i][1]+pay[i][2]+pay[i][3]));
}
cout<<totals[i]<<" ";
}
Try removing the inner loop (the one with variable c). Should work.
You add the sum pay[i][0]+pay[i][1]+pay[i][2]+pay[i][3] to the vector four times due to your inner loop c.
What you really want is the following,
for(int i=0; i<5; i++){
int sum = 0;
for(int c=0; c<4; c++)
sum += pay[i][c];
totals.push_back(sum);
cout<<totals[i]<<" ";
}

Block Matrix Transpose

I wanted to implement transposition of a matrix by dividing the input matrix into blocks and then transposing them. I referred to the corresponding post A Cache Efficient Matrix Transpose Program? and wrote my code like this:
#include<iostream>
#include<stdlib.h>
#define m 4
#include<sys/time.h>
#include<time.h>
#include<malloc.h>
using namespace std;
int **a, **b, **c;
int count = 0;
clock_t t1, t2;
int blocksize = 2;
int main(){
a = (int **)malloc(m*sizeof(int *));
for(int i = 0;i<m;i++){
a[i] = (int *)malloc(m*sizeof(int));
}
b = (int **)malloc(m*sizeof(int *));
for(int i = 0;i<m;i++){
b[i] = (int *)malloc(m*sizeof(int));
}
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
a[i][j]=(2*i)+(3*j);
}
}
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
cout << a[i][j] << "\t";
}
cout << "\n";
}
cout << "\n";
t1 = clock();
// MAIN BLOCK TRANSPOSE CODE
for (int i = 0; i < m; i += blocksize) {
for (int j = 0; j < m; j += blocksize) {
for (int k = i; k < i + blocksize; ++k) {
for (int l = j; l < j + blocksize; ++l) {
b[k + l*m] = a[l + k*m];
}
}
}
}
t2 = clock();
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
cout << b[i][j] << "\t";
}
cout << "\n";
}
free(a);
free(b);
cout << "\n";
cout << (double)(t2-t1)/CLOCKS_PER_SEC << "\n";
return 0;
}
However, the code is not working as expected. I implemented the code that is said to be working in the corresponding post. Please help if possible.
Input Array:
0 3 6 9
2 5 8 11
4 7 10 13
6 9 12 15
Expected Output Array:
0 2 4 6
3 5 7 9
6 8 10 12
9 11 13 15
Obtained Result:
0 3 6 9
Segmentation fault
I think your matrix is supposed to be encoded in a single array, not in an array of arrays (See the Edit 2 of the linked question).
You might want to try that instead:
int *a, *b, *c;
a = (int *)malloc(m*m*sizeof(int));
b = (int *)malloc(m*m*sizeof(int));
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
a[i*m+j]=(2*i)+(3*j);
}
}
for(int i=0; i<m; i++){
for(int j =0; j<m; j++){
cout << a[i*m+j] << "\t";
}
cout << "\n";
}
cout << "\n";