can anyone tell me how val[i*c+j] is working? - c++

#include<iostream>
using namespace std;
int main()
{
int* val;
int r,c;
val = new int[r*c];
cout<<"Enter Rows:";
cin>>r;
cout<<"Enter cols :";
cin>>c;
for(int i= 0 ;i<r ;i++ )
{
for(int j=0 ; j<c; j++)
{
cin>>val[i*c+j];
}
}
for(int i=0; i<r ;i++)
{
for(int j =0 ;j<c;j++)
{
cout<<val[i*c+j]<<"\t";
}
cout<<endl;
}
delete []val;
return 0;
}
i am not getting how val[i*c+j] is working in this. Can anyone explain this to me.
i want to know how 2d array works dynamically and takes input from user and display it

Related

Int and Float arrays give wrong results after adding numbers using loops

Doing some simple exercies with C++ and I'm stuck...
When my sum[t] array is declared as integer or float it sometimes outputs at the end some crazy values like for example 4239023 or -3.17802e+30 (despite the fact that I add only small numbers from range <-100; 300>). When I change it for double it works correctly. Why int and float don't work here?
#include <iostream>
using namespace std;
int main()
{
int t=0;
int n=0;
int x=0;
cout<<"Amount of sums: ";
cin>>t;
int sum[t];
for (int i=0; i<t; i++)
{
cout<<"Sum no. "<<i+1<<". How many numbers you wish to add: ";
cin>>n;
for (int j=0; j<n; j++)
{
cout<<"Insert number "<<j+1<<" : ";
cin>>x;
sum[i]+=x;
}
}
for (int i=0; i<t; i++)
{
cout<<sum[i]<<endl;
}
return 0;
}
You should initialize your array of sums to zeroes after you receive the value of t. You could do it like this:
for (int i=0; i<t; i++)
{
sum[i] = 0;
}

Adding values of 2 columns from a matrice and putting the sum in a vector

My first question about CPP in Stackoverflow. I hope i get an answer.
#include <fstream>
using namespace std;
ifstream fin("Matr.dat");
ofstream fout("out.dat");
int a[1000][1000];
int b[10000];
int n, m;
void read()
{
fin>>n>>m;
for(int i=1; i<n; i++)
{
for(int j=1; j<m; j++)
fin>>a[i][j];
}
}
int vect()
{
int c=1;
for(int i=2; i<=n; i++)
{
for(int j=2; j<=m; j++)
{
b[c]=a[j]+a[j+1];
c++;
}
}
return c;
}
int main()
{
read();
int c=vect();
for(int i=0; i<c; i++)
fout<<b[i]<<' ';
return 0;
}
I and a colleague are trying to add the values of 2 columns into a vector.
For the line `b[c]=a[j]+a[j+1]; i receive an error saying that i use 2 incompatible types together. It is not working...
Can someone please help me add the values of 2 columns into a vector?

CodeChef Error in submission(Not asking the solution)

I completed my solution for SUMTRIAN i.e. https://www.codechef.com/problems/SUMTRIAN
When I submit, it gives me wrong answer. I am definite that I have formatting problems and I am not sure how do I print my answer for that.
here is the code -
#include<bits/stdc++.h>
using namespace std;
int main()
{int w;
cin>>w;
for(int x=0;x<w;x++)
{
int a[105][105],n;
cin>>n;
int i,j;
for( i=0;i<n;i++)
for( j=0;j<=i;j++)
cin>>a[i][j];
i = n-1;
j = n-1;
int sumarr[n],sum=0;
for(int z=0;z<n;z++)
{
i=n-1;
j=n-1-z;
sum=0;
do{
sum+=a[i][j];
// cout<<a[i][j]<<" ";
i--;j--;
if(j<0)
j++;
}while(i>=0 && j>=0);
sumarr[z]=sum;
// cout<<endl;
}
// cout<<sumarr[1];
cout<<*max_element(sumarr,sumarr+n)<<"\n";
}
}
Any help would be great! :D
Thanks in advance !!:)

Deleting array of structures, changing structure adress

I create dynamically two arrays of structures (player1 and player1temp). Than i want to make some calculation using data from player1 and save it to player1temp. Then i want to copy data from array player1temp to player1.
Is it a good solution?
struct team
{
int value1;
int value2;
};
int main()
{
srand(time(NULL));
team *player1=new team[5];
for(int i=0; i<5; i++)
{
player1[i].value1=rand()%20;
player1[i].value2=rand()%20;
cout<<"Player1 index: "<<i<<" "<<player1[i].value1<<" "<<player1[i].value2<<"\n";
}
team *player1temp=new team[5];
cout<<"\n\n";
for(int i=0; i<5; i++)
{
player1temp[i].value1=rand()%20;
player1temp[i].value2=rand()%20;
cout<<"Player1temp index: "<<i<<" "<<player1temp[i].value1<<" "<<player1temp[i].value2<<"\n";
}
delete player1;
player1=player1temp;
cout<<"\n\n";
for(int i=0; i<5; i++)
{
cout<<"Player1 index: "<<i<<" "<<player1[i].value1<<" "<<player1[i].value2<<"\n";
}
return 0;
}
Two errors:
delete should be delete[] if used for arrays.
The remaining array should be deleted too before the program ends.
A vector would be easier:
struct team
{
int value1;
int value2;
};
int main()
{
srand(time(NULL));
std::vector<team> player1(5);
for(int i=0; i<5; i++)
{
player1[i].value1=rand()%20;
player1[i].value2=rand()%20;
cout<<"Player1 index: "<<i<<" "<<player1[i].value1<<" "<<player1[i].value2<<"\n";
}
std::vector<team> player1temp = player1;
cout<<"\n\n";
for(int i=0; i<5; i++)
{
player1temp[i].value1=rand()%20;
player1temp[i].value2=rand()%20;
cout<<"Player1temp index: "<<i<<" "<<player1temp[i].value1<<" "<<player1temp[i].value2<<"\n";
}
player1 = std::move(player1temp);
//donĀ“t use player1temp anymore now
cout<<"\n\n";
for(int i=0; i<5; i++)
{
cout<<"Player1 index: "<<i<<" "<<player1[i].value1<<" "<<player1[i].value2<<"\n";
}
return 0;
}

Passing matrix to a function

I am getting an error when calling the 'printMat' function. My requirement is to create a matrix after accepting the number of rows and columns, and then take input into the matrix, then call the printMat by sending the matrix and print the elements. The error is as follows:
error: parameter 'a' includes pointer to array of unknown bo
und 'int []'
#include<iostream>
using namespace std;
int row,col;
void printMat(int* a[])
{
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cout<<a[i][j]<<" ";
}
}
}
int main()
{
cin>>row;
cin>>col;
int mat[row][col];
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cin>>mat[i][j];
}
}
printMat(mat);
return 0;
}
int* a[]
is an array of pointer, but you are passing a pointer to an array:
int (*a)[]
The reason it doesn't work is that arrays are just a nightmare. In C++, we use vectors instead.
#include<iostream>
#include<vector>
using namespace std;
void printMat(vector<vector<int>> mat)
{
for(vector<int> one_row : mat)
{
for(int one_cell_in_this_row : one_row)
{
cout << one_cell_in_this_row << ' ';
}
cout << endl;
}
}
int main()
{
int row,col;
cin>>row;
cin>>col;
vector< vector<int> > mat( row , vector<int>(col,0) );
// ^ ^
// initialize the vector ~~~~~/ |
// with 'row' items, each |
// of which is a vector |
// of 'col' integers. ~~~~~~~~~~~~~~~~~~~~~~~~~/
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
int current_entry;
cin>>current_entry;
mat.at(i).at(j) = current_entry;
}
}
printMat(mat);
return 0;
}
You can solve using pointer arithmetic. See following code.
#include<iostream>
using namespace std;
int row,col;
void printMat(int *a)
{
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cout<< *((a+i*col) + j)<<" ";
}
}
}
int main()
{
cin>>row;
cin>>col;
int mat[row][col];
for(int i=0; i<row; ++i)
{
for(int j=0; j<col; ++j)
{
cin>>mat[i][j];
}
}
printMat((int *)mat);
return 0;
}
Other possible solutions are explained in this link