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;
}
Related
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";
}
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.
So I have this C++ program that contains .h file and main.cpp. In .h I have this class:
class Plaza
{
public:
int length;
double x;
double y;
Plaza();
~Plaza();
};
In main.cpp, I am trying to enter the data using for loop, and I manage to store data for int i = 0 state, but when i is increased, no data that has been entered is being stored into array. For the inside loop, I tried to put j < n, j < n-1 and j < n+1, but it's not working. How can I store all the data and print it out?
#include <iostream>
#include "Plaza.h"
using namespace std;
int main() {
int n;
Plaza *obj1;
cout << "Enter limit number (N): ";
cin >> n;
obj1 = new Plaza[n];
for (int i = 0; i < n; i++) {
cout << "Enter length, x and y for " << i + 1 << ". plaza: " << endl;
for (int j = 0; j < 1; j++) {
cin >> obj1[j].length;
cin >> obj1[j].x >> obj1[j].y;
}
}
for (int i = 0; i < n; i++) {
cout << i + 1 << ". " << obj1[i].x << " " << obj1[i].y << " Length=" << obj1[i].length;
}
delete[] obj1;
system("pause");
return 0;
}
This is the print I get:
for (int i = 0; i < n; i++) {
cout << "Enter length, x and y for " << i + 1 << ". plaza: " << endl;
for (int j = 0; j < 1; j++) {
cin >> obj1[j].length;
cin >> obj1[j].x >> obj1[j].y;
}
}
Here's your culprit. Get rid of the inner for-loop (not the cin-statements, just the for... line and its closing bracket) and replace obj[j] with obj[i]. You are currently repeatedly writing to obj[0].
for (int i = 0; i < n; i++) {
cout << "Enter length, x and y for " << i + 1 << ". plaza: " << endl;
for (int j = 0; j < 1; j++) {
cin >> obj1[j].length;
cin >> obj1[j].x >> obj1[j].y;
}
}
Why there is a need of second for loop., if you check the j value, it is always 0 so only one value is inserted.,
try this
for (int i = 0; i < n; i++) {
cout << "Enter length, x and y for " << i + 1 << ". plaza: " << endl;
cin >> obj1[i].length;
cin >> obj1[i].x >> obj1[i].y;
}
}
I am taking an input from user for the size of an array, and then the elements of it.
In the below code, cin>>A[i] in the first for loop is giving me an error.
From other questions similar to this one, it's a simple operator error, and the script is similar to working three dimensional ones I've seen. Is new creating a 3 dimensional array by default, meaning I'd need to define columns too? If not, where would I be missing the operator?
int** A;
int s;
cin >> s;
A = new int*[s];
for(int i=0;i<s;i++)
{
A[i]=new int[s];
cout<< "Enter value: ";
cin>>A[i];
}
cout<< "Array:\n";
for(int j=0;j<s;j++)
{
cout << A[j] << " ";
}
A[] is an int* pointer, not an int value.
There is no operator>> that can read an int value into an int* pointer. Since you want to read an int value, you have to read into an int variable, so change A[i] in your 1st loop to *A[i] instead:
cin >> *A[i];
You need to do the same with A[j] in the 2nd loop:
cout << *A[j] << " ";
This is because there is no operator<< to write an int value from an int* pointer, but there is an operator<< that can write the value of a memory address held by a void* pointer, and int* is implicitly convertible to void*.
Don't forget to delete[] your arrays when you are done with them:
int s;
cin >> s;
int** A = new int*[s];
for(int i = 0; i < s; ++i)
A[i] = new int[s];
for(int i = 0; i < s; ++i)
{
cout << "Enter value: ";
cin >> *A[i];
}
cout << "Array:\n";
for(int j = 0; j < s; ++j)
cout << *A[j] << " ";
for(int j = 0; j < s; ++j)
delete[] A[j];
delete[] A;
That being said, you are wasting memory for the second dimension when s > 1, since you are filling in and using only the 1st column and ignoring additional columns. The code you showed only really needs a 1-dimensional array instead:
int s;
cin >> s;
int* A = new int[s];
for(int i = 0; i < s; ++i)
{
cout << "Enter value: ";
cin >> A[i];
}
cout << "Array:\n";
for(int j = 0; j < s; ++j)
cout << A[j] << " ";
delete[] A;
If you really want a 2-dimensional array, try something more like this instead:
int rows, columns;
cin >> rows;
cin >> columns;
int** A = new int*[rows];
for(int i = 0; i < rows; ++i)
A[i] = new int[columns];
for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < columns; ++j)
{
cout << "Enter value for (" << i << "," << j << "): ";
cin >> A[i][j];
}
}
cout << "Array:\n";
for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < columns; ++j)
cout << A[i][j] << " ";
cout << endl;
}
for(int i = 0; i < rows; ++i)
delete A[i];
delete[] A;
That being said, you really should be using std::vector instead of new[] directly.
What array do you want to create? Two-dimensional SxS, or just S-sized?
Because you are creating an array of arrays, while trying to access it as a single-dimensional.
Changing int** A to int* A, A = new int*[s] to A = new int[s], and getting rid of A[i]=new int[s] in the first loop makes the code correct.
Here is my full code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <string.h>
using namespace std;
int main(){
int ***my3DArray;
int length, width, height;
bool doAgain = true;
string answer("");
string yes("y");
do{
cout << "Enter length: ";
cin >> length;
cout << "\nEnter width: ";
cin >> width;
cout << "\nEnter height: ";
cin >> height;
srand((unsigned)time(NULL));
my3DArray = new int**[length];
for (int i= 0; i < length; ++i){
my3DArray[i] = new int*[width];
for(int j = 0; j< width; ++j){
my3DArray[i][j] = new int[height];
for(int k = 0; k < height; ++k){
my3DArray[i][j][k] = rand()%100;
cout << my3DArray[i][j][k] << " ";
do{
for (int i = 0; i < length; ++i){
for (int j = 0; j < width; ++j){
for (int k=0; k< height; k++){
cout << "\n\nEnter coodinates: ";
cin >> i;
cin >> j;
cin >> k;
cout << "Element is " << my3DArray[i][j][k];
}
cout << endl;
}
cout << endl;
}
cout << "Find another element? (Y/n)";
cin >> answer;
if(answer.compare(yes) == 0)
doAgain = true;
else doAgain = false;
}
while(doAgain == true);
}
cout << endl;
}
cout << endl;
}
for(int i = 0; i < length; i++){
for(int j = 0; j < width; j++){
delete[]my3DArray[i][j];
}
delete[]my3DArray[i];
}
delete[]my3DArray;
my3DArray = NULL;
cout << "Again? (y, n)";
cin >> answer;
if(answer.compare(yes) == 0)
doAgain = true;
else doAgain = false;
}
while(doAgain == true);
}
This prints a 3D array of integers, I need to write a function called tick that returns the element of user specified coordinates i j k. That part of the code is this
do{
for (int i = 0; i < length; ++i){
for (int j = 0; j < width; ++j){
for (int k=0; k< height; k++){
cout << "\n\nEnter coodinates: ";
cin >> i;
cin >> j;
cin >> k;
cout << "Element is " << my3DArray[i][j][k];
}
cout << endl;
}
cout << endl;
}
cout << "Find another element? (Y/n)";
cin >> answer;
if(answer.compare(yes) == 0)
doAgain = true;
else doAgain = false;
}
while(doAgain == true);
when I have this in my code, it doesnt print out the 3D array and I get a segmentation fault when it's supposed to print the element. Any Ideas? Thanks in advance
instead of int ***my3DArray use int my3DArray[256][256] when declaring an array is just a pointer to the first item in the list. you then run a for loop to go through that list.