When I try to run my code, it compiles fine, but during runtime it gives an out of range vector error. Can anyone help me out?
I have written my code in Xcode:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int numOfRows = 0;
cout << "Enter number of rows: ";
cin >> numOfRows;
vector<vector<int>> vec;
int sizeOfAnotherArray = 0;
int value = 0;
for (int i = 0; i < numOfRows; i++) {
cout << "Enter size of another array: ";
cin >> sizeOfAnotherArray;
vec.resize(numOfRows,vector<int>(sizeOfAnotherArray));
for (int j = 0; j < sizeOfAnotherArray; j++) {
cout << "Store Value: ";
cin >> value;
vec.at(i).at(j) = value;
}
}
for (int i = 0; i < numOfRows; i++) {
for (int j = 0; j < sizeOfAnotherArray; j++) {
cout << vec.at(i).at(j) << " ";
}
cout << "\n";
}
return 0;
}
The odd thing about your code is that you enter sizeOfAnotherArray multiple times and therefore resize your whole array multiple times. But note that you only change the number of rows. Each row you add will have the latest size but earlier rows will keep the size they originally had.
This means that if one of the later values for sizeOfAnotherArray is larger than one of the earlier values then you are going to get an out of range error, because the earlier row will still have the smaller size.
I'm guessing that the code you meant to write is this. It creates a ragged array, which is an array where the number of columns varies depending on which row you are on.
cout << "Enter number of rows: ";
cin >> numOfRows;
vector<vector<int>> vec(numRows); // create array with N rows
for (int i = 0; i < numOfRows; i++) {
cout << "Enter size of another array: ";
cin >> sizeOfAnotherArray;
vec.at(i).resize(sizeOfAnotherArray); // resize this row only
for (int j = 0; j < sizeOfAnotherArray; j++) {
...
}
for (int i = 0; i < vec.size(); i++) {
for (int j = 0; j < vec.at(i).size(); j++) { // loop on the size of this row
cout << vec.at(i).at(j) << " ";
}
cout << "\n";
}
Related
This question already has answers here:
What is error in code for adding 3*3 matrix? [duplicate]
(2 answers)
Closed 1 year ago.
I'm trying to input a 2D array in c++ but my code doesn't even get to calculation lines:
int dim1, dim2;
cout << "Enter first dimension: ";
cin >> dim1;
cout << endl << "Enter second dimension: ";
cin >> dim2;
int arr[dim1][dim2];
for (int i = 1; i <= dim1; i++) {
for (int j = 1; j <= dim2; j++) {
cin >> arr[i][j];
}
}
for (int i = 1; i <= dim1; i++) {
for (int j = 1; j <= dim2; j++) {
cout << arr[i][j] << ' ';
}
cout << endl;
}
It's simple, you should start inputing an array in c++ from 0, so your loop should begin with i = 0 and should be stopped when i == n.
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
Your code has two problems.
First, VLA arrays is not valid C++ code. You have to use std::vector.
Second. C++ array indexes start at 0 and end at size - 1. But you are looping from 1 to n. This creates out-of-bounds.
Your code should look like this:
int dim1, dim2;
std::cout << "Enter first dimension: ";
std::cin >> dim1;
std::cout << endl << "Enter second dimension: ";
std::cin >> dim2;
std::vector<std::vector<int>> arr;
arr.resize(dim1, std::vector<int>(dim2));
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
std::cin >> arr[i][j];
}
}
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
std::cout << arr[i][j] << ' ';
}
std::cout << endl;
}
First in C++, the size of an array must be a compile-time constant.So, take for example the following code snippets:
int n = 10;
int arr[n]; //INCORRECT because n is not a constant expression
The correct way to write the above would be:
const int n = 10;
int arr[n]; //CORRECT
Similarly, the following(which you did in your code example) is incorrect:
int dim1, dim2;
cout << "Enter first dimension: ";
cin >> dim1;
cout << endl << "Enter second dimension: ";
cin >> dim2;
int arr[dim1][dim2];//INCORRECT
You should instead use std::vector<> as shown below:
#include <iostream>
#include <vector>
int main()
{
int dim1, dim2;
std::cout << "Enter first dimension: ";
std::cin >> dim1;
std::cout << std::endl << "Enter second dimension: ";
std::cin >> dim2;
std::vector<std::vector<int>> arr(dim1, std::vector<int>(dim2));
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
std::cin >> arr[i][j];
}
}
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
std::cout << arr[i][j] << ' ';
}
std::cout << std::endl;
}
return 0;
}
The number of rows and columns of this array are given by the user however the number of rows are not the same (the array is uneven) and also the user will fill the array by entering the elements.
This is the code that I wrote but when I try to take input from user, the code crashes after taking some inputs. Please could you help me out and correct my code and point my flaws. Thank you.
#include <iostream>
//2d array
using namespace std;
int main()
{
int row;
int col_x;
cout << "Enter the row number:" << endl;
cin >> row;
//cout<<"Enter the column number:"<<endl;
//cin>>col;
int **a = new int *[row];
for (int r = 0; r < row; r++)
{
cout << "Enter the column no.of array " << r << endl;
cin >> col_x;
a[r] = new int[col_x];
cout << "Enter the elements in the array:" << endl;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col_x; j++)
{
cin >> a[i][j];
}
}
cout << "The elements in the array:" << endl;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col_x; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
delete[] a;
a = NULL;
return 0;
}
There was an extra for loop. Also, you have to store the size of each row.
And make the proper deallocation of the 2D array.
#include <iostream>
//2d array
using namespace std;
int main()
{
int row;
cout<<"Enter the row number:"<<endl;
cin>>row;
int **a=new int *[row];
int *col_x = new int [row];
for(int r=0;r<row;r++){
cout<<"Enter the column no.of array "<<r<<endl;
cin>>col_x[r];
a[r]=new int[col_x[r]];
cout<<"Enter the elements in the array:"<<endl;
for(int j=0;j<col_x[r];j++){
cin>>a[r][j];
}
}
cout<<"The elements in the array:"<<endl;
for(int i=0;i<row;i++){
for(int j=0;j<col_x[i];j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
for (int i=0; i<row; ++i)
delete[] a[i];
delete []a;
delete []col_x;
return 0;
}
The way you are getting the input from the user is very vague, and is prone to accessing invalid memory. You are getting the same row many times in the inner loop.
Try something like this:
#include <iostream>
//2d array
using namespace std;
int main() {
int row;
int col_x;
cout << "Enter the row number:" << endl;
cin >> row;
//cout<<"Enter the column number:"<<endl;
//cin>>col;
int ** a = new int * [row];
for (int r = 0; r < row; r++) {
cout << "Enter the column no.of array " << r << endl;
cin >> col_x;
a[r] = new int[col_x];
cout << "Enter the elements in the array:" << endl;
for (int j = 0; j < col_x; j++) {
cin >> a[r][j];
}
}
cout << "The elements in the array:" << endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col_x; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
delete[] a;
a = NULL;
return 0;
}
Also, note that col_x will hold only the size of the last row. So, it's not working for the printing at the end of the code.
Not sure what you are trying to achieve, but the main issue with above code is, that you are accessing non-existing elements of your array. Maybe your loop over r should end in line 18? Even then, you would have to store the number of columns per row in some external variable. I would suggest to use a std::vector as the container instead of fixed arrays, in your case a std::vector< std::vector<int> >. The vector class has a method size() which stores its actual size.
Since you are using C++, you should take advantage of the containers it provides for you to store your data, in this case a vector of vectors would be appropriate:
Live Sample
#include <iostream>
#include <vector>
using namespace std; //<-- for test, souldn't be used
int main() {
int rows, cols, temp;
vector<vector<int>> matrix;
cout << "Enter the row number:" << endl;
cin >> rows;
for (int i = 0; i < rows; i++){
vector<int> v;
cout << "Enter the column no.of array " << i << endl;
cin >> cols;
cout << "The elements in the array:" << endl;
for (int j = 0; j < cols; j++){
cin >> temp;
v.push_back(temp);
}
matrix.push_back(v);
}
cout << endl;
for( auto i: matrix){ //print results
for(int j: i)
cout << j << " ";
cout << endl;
}
}
Im trying to create a program that creates two 2d dynamic arrays and multiply them and give an output and calculate the time taken for the multiplication process based on the input size n. This code works when it comes to outputs lesser than 7 rows and 7 cols but gives an error when the number goes above 8.
using namespace std;
int m1c , m1r , m2c , m2r , i , j , k , l;
int** arr1 = new int*[m1c];
int** arr2 = new int*[m2c];
int** multArr = new int*[m1c];
int main(){
cout << "Enter the Number of rows for matrix 1 :";
cin >> m1c;
cout << "Enter the Number of columns for matrix 1 :";
cin >> m1r;
cout << "Enter the Number of rows for matrix 2 :";
cin >> m2c;
cout << "Enter the Number of columns for matrix 2 :";
cin >> m2r;
for (i = 0; i < m1r; i++) {
arr1[i] = new int[m1c];
multArr[i] = new int[m1c];
}
for (i = 0; i < m2r; i++) {
arr2[i] = new int[m2c];
}
if (m1r != m2c) {
cout << "Number of rows in the first matrix must be equal to the numbr of columns in the second matrix ";
return -1;
}
for (i = 0; i < m1r; i++) {
for (j = 0; j < m2c; j++) {
arr1[i][j] = rand() % 100;
}
}
for (i = 0; i < m2r; i++) {
for (j = 0; j < m2c; j++) {
arr2[i][j] = rand() % 100;
}
}
//Displaying the two arrays
for (i = 0; i < m1r; i++) {
for (j = 0; j < m1c; j++) {
cout << arr1[i][j] << " ";
}
cout << endl;
}
cout << endl;
for (i = 0; i < m2r; i++) {
for (j = 0; j < m2c; j++) {
cout << arr2[i][j] << " ";
}
cout << endl;
}
delete[] arr1;
delete[] arr2;
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Looks like an error initializing arr1. Your using m2c as the column count. You probably meant m1c.
I'm new to programming and am struggling with the idea of setting up a two dimensional array that is 5x5 and allows for the input of the numbers 0-10. I've scratched down a few things but need some advice or guidance. The book and youtube videos can't help me any further.
#include <iostream>
using namespace std;
int main()
{
int numbers[5][5];
for (int i = 0; i < 5; i++)
{
cout << "Please enter the five scores for Contestant #1: " << i << endl;
cin >> numbers[i]
}
int *array;
cout << "Please enter the contestants 5 scores: ";
int n;
cin >> n;
array = new int[n];
for (int i = 0; i < n; i++)
try this:
include
using namespace std;
int main()
{
int numbers[5][5];
//Input Data
for (int i = 0; i < 5; i++)
{
for (int j = 0; j<5; j++)
{
cout << "Please enter Contestant # " << i <<"score for Test # "<<j<<" :"<<endl;
cin>>numbers[i][j];
}
}
//Print Data
for (int i = 0; i < 5; i++)
{
for (int j = 0; j<5; j++)
{
cout << "Contestant # " << i <<"score for Test # "<<j<<" :"<< numbers[i][j]<<endl;
}
}
return 0;
}
I need to write a program that receives 2 arrays and checks how many times 1 is included in the other...
But I cant find what is wrong with my program! tx!!
#include <iostream>
using namespace std;
int main()
{
int vector1[500];
int vector2[100];
int a = 0, b = 0, count = 0, k = 0;
cout << "enter size of first array:" << endl;
cin >> a;
cout << " enter first array values:" << endl;
for (int i = 0; i < a; i++)
cin >> vector1[i];
cout << "enter size of second array:" << endl;
cin >> b;
cout << "enter secound array values:" << endl;
for (int i = 0; i < b; i++)
cin >> vector2[i];
for (int i = 0; i < b; i++)
for (int j = 0; j < a; j++)
if (vector2[i + k] == vector1[j])
{
count++;
k++;
}
else
k = 0;
cout << count << endl;
system("pause");
return 0;
}
Why at all do you need k? The problem is about all inclusions of all elements right? If O(n^2) complexity is fine, then...
for (int i = 0; i < b; i++)
for (int j = 0; j < a; j++)
if (vector2[i] == vector1[j])
count++;
One obvious disadvantage of the code above is that you'll get the total sum of all occurences of elements from vector1 in vector2. The key idea remains the same in case you need to know, which elements exactly appeared in another array and how many times, you'll just have to use map or other vector.