Process returned -1073741819 (0xC0000005) in vectors - c++

I know that the vector is a dynamic array so you can enter values as you want. But in this code bellow the code crashes before I insert any value in the vector !! I don't know what I am doing wrong and I am new in programming. I tried to give it a size but it is not working.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int H,W;
cin >> H >> W;
vector <vector <int>> A;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
cin >> A[i][j];
return 0;
}

You are reading values to A[i][j] while the vector A has no elements.
You have to allocate elements to read something there.
To allocate beforehand, you can use constructors.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int H,W;
cin >> H >> W;
// allocate H W-element vectors
vector <vector <int>> A(H, vector <int>(W));
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
cin >> A[i][j];
return 0;
}
Alternatively, you can insert elements via push_back().
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int H,W;
cin >> H >> W;
vector <vector <int>> A;
for (int i = 0; i < H; i++)
{
vector <int> row;
for (int j = 0; j < W; j++)
{
int value;
cin >> value;
row.push_back(value);
}
A.push_back(row);
}
return 0;
}

Related

C++ Segmentation fault while trying to create a vector of vectors

I want to create a vector of vectors in C++ with dimensions nx2 where n(rows) is given by user. I am trying to insert values in this vector using a for loop but As soon as give the value of n(rows), it gives a Segmentation fault error
What to do?
#include <iostream>
#include <vector>
#include <cstdlib>
#define col 2
using namespace std;
int main()
{
int row;
cin >> row;
vector<vector<int>> vec;
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
cin >> vec[i][j];
}
return 0;
}
You need to resize a vector before inserting elements. Or use push_back to insert incrementally.
vector<vector<int>> vec;
vec.resize(row);
for (int i = 0; i < row; ++i)
{
vec[i].resize(col);
for (int j = 0; j < col; ++j)
{
cin >> vec[i][j];
}
}
OR:
vector<vector<int>> vec;
vec.resize(row);
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
int value;
cin >> value;
vec[i].push_back(value);
}
}

c++) i have error when putting 1D array in 2D array

whats the prob
#include <iostream>
using namespace std;
int main() {
int M; //height
int N; //length
cin >> M;
cin >> N;
int list[M][N], smaller[N];
string smaller_str;
for (int i = 0;i < M; ++i){
getline(cin, smaller_str);
for (int j = 0; j < N; i = i++) {
cin >> smaller_str[j];
}
list[i] = smaller;
}
}
i want to put 1D array "smaller" in to 2D array list
however i do get errors in the "list[i] = smaller;" part
i need help guys
Unless you want to check for errors, you won't need to use getline in this case. Just read each elements via >> operator.
Also note that Variable-Length Arrays like int list[M][N]; is not in the standard C++. You should use std::vector instead of that.
Another point is the the i = i++ in the inner loop is wrong. It should be ++j.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int M; //height
int N; //length
cin >> M;
cin >> N;
//int list[M][N];
std::vector<std::vector<int> > list(M, std::vector<int>(N));
for (int i = 0;i < M; ++i){
for (int j = 0; j < N; ++j) {
cin >> list[i][j];
}
}
}
You cannot assign another arrays to an array, what you can do is copy/ move its content. For this, use std::copy,
...
int list[M][N], smaller[N];
string smaller_str;
for (int i = 0; i < M; ++i) {
getline(cin, smaller_str);
for (int j = 0; j < N; i = i++) {
cin >> smaller_str[j];
}
std::copy(smaller, smaller + N, reinterpret_cast<int*>(&list[i]));
}
Note: If you would like to move the content rather than copying, swap the std::copy with std::move.

core dumped error while inputting value in 2D vector

c++ code
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int N;
vector<vector<int>> arr;
cin >> N;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < 2; j++)
{
int a;
cin >> a;
arr[i].push_back(a);
}
}
return 0;
}
When I input value in 2d vector, I get error(core dumped).
ex)
compilier(input)
3
1 3
segmentation error(dore dumped)
How Can I fixed it?
[Recommended] Simply do this : Ref. Range-based for loop
#include <iostream>
#include <vector>
int main() {
int N;
std::cin >> N;
std::vector<std::vector<int>> arr(N, std::vector<int>(2));
for (auto &&row : arr)
for (auto &&ele : row)
std::cin >> i;
}
The problem with your code is, you haven't told compiler how many elements are there in the vector.
Corrected code :
// ...
int main() {
int N;
cin >> N;
vector<vector<int>> arr(N);
for (int i = 0; i < N; i++) {
// ...
Another way to fix your code :
Add
arr.push_back(vector<int>());
just before inner loop :
for (int j = 0; j < 2; j++) {
One more way :
Add
arr.resize(N); // https://en.cppreference.com/w/cpp/container/vector/resize
just before outer loop :
for (int i = 0; i < N; i++) {
Bonus Tip :
If your data is limited to two columns only then you may consider using std::pair instead :
#include <iostream>
#include <utility>
#include <vector>
int main() {
int N;
std::cin >> N;
std::vector<std::pair<int, int>> arr(N);
for (auto &&i : arr)
std::cin >> i.first >> i.second;
}
Because you are assessing out of bounds in arr, Have a look at resize here or here
#include<iostream>
#include<vector>
using namespace std; // don't do this
int main ()
{
int N;
vector < vector < int > > arr;
cin >> N;
arr.resize(N); // resize, without this arr have size 0
for (int i = 0; i < N; i++)
{
for (int j = 0; j < 2; j++)
{
int a;
cin >> a;
arr[i].push_back (a);
}
}
for(const auto &vec : arr)
{
for(auto i : vec)
{
std::cout<<i;
}
}
return 0;
}
and if you are not comfort with range loop see here.
Better Code:
#include<iostream>
#include<vector>
int main ()
{
int N;
std::cin >> N;
std::vector<std::vector<int>> arr(N, std::vector<int>(2)); //allocate once
for( auto &vec: arr)
{
for( auto &v: vec )
{
std::cin >> v;
}
}
for ( const auto & vec:arr )
{
for ( auto i:vec )
{
std::cout << i;
}
}
return 0;
}
See init vector of vector here

c++ how to pass 2D array to function with variables that the user enters?

When I run the following code I get an error at the function.
using namespace std;
void function(int a[rows][columns]) {}
int main() {
int rows, columns;
cin >> rows >> columns;
int matrix[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cin >> matrix[i][j];
}
}
function(matrix);
return 0;
}
How do I pass the 2D array to the function with variables that the user enters?
You can use vector<vector<int>> like this:
#include <iostream>
#include <vector>
using namespace std;
void function(const vector<vector<int>>& a) {}
int main() {
int rows, columns;
cin >> rows >> columns;
int initial_value = 0;
std::vector<std::vector<int>> matrix;
matrix.resize(rows, std::vector<int>(columns, initial_value));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cin >> matrix[i][j];
}
}
function(matrix);
return 0;
}

Passing a 2D array to a function in C++ gives me the following error

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
\\Errors:
\\'n' was not declared in this scope
\\ expected ')' before ',' token
\\ expected unqualified-id before 'int'
void abst_diff(int arr[][n], int n){
int sum_1=0;
int sum_2=0;
for(int a_i = 0; a_i < n; a_i++)
sum_1 = sum_1 + a[a_i][a_i];
for(int a_i = 0, a_j = n-1; a_i < n; a_i++, a_j--)
sum_2 = sum_2 + a[a_i][a_j];
sum_2=abs(sum_2-sum_1);
cout << sum_2;
}
int main(){
int n;
cin >> n;
int arr[n][n];
for(int a_i = 0;a_i < n;a_i++){
for(int a_j = 0;a_j < n;a_j++){
cin >> arr[a_i][a_j];
}
}
abst_diff(arr,n);
return 0;
}
Can someone please help me identify the error here in this code. its a fairly simple logic. the errors that i'm getting are for the line:
void abst_diff(int arr[][n], int n){
You can't assign a variable to define the size of an array.May Be you could use a macro.
#define SIZE 50
Or you could use dynamic variable.
Ex:
#include<iostream>
using namespace std;
int main()
{
int n ,*a;
cout<<"Enter size of array= ";
cin>>n;
a=new int[n]; //allocate memory
for(int i=0;i<n;i++)
*(a+i)=i;
for(int i=0;i<n;i++)
cout<<*(a+i)<<endl;
return 0;
}
And
void abst_diff(int arr[][**n**], int n)
You can't give the size here too.
#include <vector>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
void abst_diff(int arr[50][50], int n){
int sum_1=0;
int sum_2=0;
for(int a_i = 0; a_i < n; a_i++)
sum_1 = sum_1 + arr[a_i][a_i];
for(int a_i = 0, a_j = n-1; a_i < n; a_i++, a_j--)
sum_2 = sum_2 + arr[a_i][a_j];
sum_2=abs(sum_2-sum_1);
cout << sum_2;
}
int main(){
int n;
cin >> n;
int arr[50][50];
for(int a_i = 0;a_i < n;a_i++){
for(int a_j = 0;a_j < n;a_j++){
cin >> arr[a_i][a_j];
}
}
abst_diff(arr,n);
return 0;
}
Please comment using '/' not '\'.
i was actually looking to pass a dynamic variable (which is the array), not a defined variable of fixed size.
I took your first advise and managed to come up with a solution of my own:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int **createArr(int **array, int n){
array = new int*[n];
for (int row=0; row<n; row++) {
array[row] = new int[n];
}
return array;
}
void abst_diff(int **arr,int n){
int sum_1=0;
int sum_2=0;
for(int a_i = 0; a_i < n; a_i++)
sum_1 = sum_1 + *(*(arr+a_i)+a_i);//arr[a_i][a_i];
for(int a_i = 0, a_j = n-1; a_i < n; a_i++, a_j--)
sum_2 = sum_2 + arr[a_i][a_j];
sum_2=abs(sum_2-sum_1);
cout << sum_2;
}
int main(){
int n;
cin >> n;
int **arr=createArr(arr,n);
for(int a_i = 0;a_i < n;a_i++){
for(int a_j = 0;a_j < n;a_j++){
cin >> arr[a_i][a_j];
}
}
abst_diff(arr,n);
return 0;
}
This program calculates the abs. difference between the sum of the diagonals. My issue was just passing a dynamic variable to a function.
Thanks for your help! :)
PS. '\' was an honest mistake.