How can i solve my constant error in visual studio? [duplicate] - c++

This question already has answers here:
Variable Length Array (VLA) in C++ compilers
(2 answers)
Closed 2 years ago.
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int m[n][2];
for (int i = 0; i < n; i++) {
cin >> m[i][0];
cin >> m[i][1];
}
}
if you run this codes in visual studio, then i get errors at line 9.
Can anyone tell why i have errors?

It looks like you're trying to initialize m as a 2d array with the first dimension's size as user input, and the second as constant 2. To do that you can use
int n;
cin >> n;
int (*m)[2] = new int[n][2];
for (int i = 0; i < n; i++) {
cin >> m[i][0];
cin >> m[i][1];
}

Related

What's wrong with the space for an array declaration in c++? [duplicate]

This question already has answers here:
C/C++ the result of the uninitialized array
(2 answers)
Uninitialized variable behaviour in C++
(4 answers)
What happens when I print an uninitialized variable in C++? [duplicate]
(4 answers)
(Why) is using an uninitialized variable undefined behavior?
(7 answers)
Closed 4 months ago.
I have long been declaring a lot more space for arrays during competitive programming.
int pr[100010]; //for example
However, when I was trying to declare an array for a smaller space this morning, things went wrong.
#include <iostream>
using namespace std;
int main()
{
int n; cin >> n;
int pr[100]; //seems enough
for(int i = 1; i <= n; i++) {
int x; cin >> x;
pr[i] = pr[i-1] + x;
}
for(int i = 1; i <= n; i++)
cout << pr[i] << endl;
return 0;
}
You see, this program was for calculating the prefix sum of an array. The value for this array comes from input.
The space for this array seems enough.
But when I input these to my program and found that it occurs to a wrong output:
//the input:
5
1 2 3 4 5
//the output:
1875947561
1875947563
1875947566
1875947570
1875947575
The array might just out of bounds. I thought.
However, when I modified the space 100 to 10, the answer was correct this time!
#include <iostream>
using namespace std;
int main()
{
int n; cin >> n;
int pr[10]; //smaller, but correct!
for(int i = 1; i <= n; i++) {
int x; cin >> x;
pr[i] = pr[i-1] + x;
cout << "*" << pr[i] << endl;
}
for(int i = 1; i <= n; i++)
cout << pr[i] << endl;
return 0;
}
5
1 2 3 4 5
1
3
6
10
15
It seems that the problem happend at here:
pr[i] = pr[i-1] + x;
I know that the initial value of the array was 0, but why a smaller space works well and the higher space didn't ?
The most interesting thing is that when I declare the array as a global variable, though small, the answer was correct?
#include <iostream>
using namespace std;
int pr[1]; //such a tiny space but correct!
int main()
{
int n; cin >> n;
for(int i = 1; i <= n; i++) {
int x; cin >> x;
pr[i] = pr[i-1] + x;
}
for(int i = 1; i <= n; i++)
cout << pr[i] << endl;
return 0;
}
The results:
//the input
5
1 2 3 4 5
//the output
1
3
6
10
15
This problem confuses me a whole day, can someone help me? I can't fell asleep as the problem was still there :(
The problem is here:
int pr[100]; //seems enough
for(int i = 1; i <= n; i++) {
int x; cin >> x;
pr[i] = pr[i-1] + x; // uninitialized read of pr[0]
}
Reading an uninitialized variable is undefined behavior. In this case you read some garbage value, but your program could also crash or produce other unexpected results.
As a side note, your program does not need an array at all. You could output each value as soon as you calculate it, and store the current sum in a scalar.
Conclusion
If you declare a global variable, it would be initialized to zero.
For a local variable, you have to initialize it before use.
For that global pr[1], it can work, but with error.

Exponential values not taking in large array [duplicate]

This question already has answers here:
The program doesn't give any output on VS Code if I use 'const int ' and assign it a big value like 1e6 [duplicate]
(1 answer)
Why is this C++ program crashing? [duplicate]
(2 answers)
C++ program is not giving output involving large array
(5 answers)
Closed 9 months ago.
Whenever is give N = 1e6 + 2 like large value to N. VS Code doesn't work and on runtime shows .exe stopped working. Couldn't figure out why happening
#include <iostream>
using namespace std;
int main()
{
int n, arr[n];
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int N = 1e6 + 2;
bool check[N];
for (int i = 0; i < N; i++)
{
check[i] = false;
}
return 0;
}

C++ with #include<string> and getting C2679

Thank you to #crashmstr for triggering the right brain cells to work. My problem is that you can't have an integer written into an array with that format.
cin>>votes[a][0]; was the right answer to the problem.
#include<iostream>
#include<iomanip>
#include<istream>
#include<string>
using namespace std;
const int nameRow = 6, nameCol = 7, voteRow = 6, voteCol = 2;
int main ( )
{
char candName[nameRow][nameCol];
int votes[voteRow][voteCol];
for(int a=0;a< nameRow;a++)
for (int b = 0; b < nameCol;b++)
candName[a][b] = 0;
for (int c = 0; c < voteRow;c++)
for (int d = 0; d < voteCol;d++)
votes[c][d] = 0;
cout << "Enter a candidate's name : ";
for (int a = 0;a < nameRow;a++)
{
cin >> candName[a], nameCol;
cin >> votes[a], voteCol;
}
system ( "pause" );
}
My problem is the last cin of the file. The >> is getting the code. It doesn't matter if I put it in the same line, another loop, same function different loops or different functions all together the >> of that line is tripping C2679.
The most popular fix I have seen is having #include<string.h>and changing that to <string>. I didn't initially have it but I added it when I saw it but it hasn't fixed anything.
Can anyone give me some insight of what I am missing?
Error message:
binary '>>' : no operator found which takes a right-hand operand of type 'int[2]' (or there is no acceptable conversion)
Using #include <string> is part of a solution but you forgot to actually use the string type. Also you use a lot of antique stuff. The code could look like:
#include<iostream>
#include<iomanip>
#include<string>
const int numRows = 6;
int main ( )
{
std::string candName[numRows]; // empty strings
int votes[numRows] {}; // zero-initialized
for (int a = 0;a < numRows;a++)
{
std::cout << "Enter a candidate's name : ";
std::cin >> candName[a] >> votes[a];
if ( ! std::cin )
break; // stop if they type something bad
}
}
Another easy modification to make would be to store the results in a vector of user-defined type containing string and int , instead of having two fixed-size arrays. Then you can accept any number of inputs instead of exactly 6 .
This needs fixing, replace , with >> or something:
for (int a = 0;a < nameRow;a++)
{
cin >> candName[a], nameCol; // << -- here
cin >> votes[a], voteCol; // << -- and here
}
And this is a very bad style:
for (int c = 0; c < voteRow;c++)
for (int d = 0; d < voteCol;d++)
votes[c][d] = 0;
Please, add curly brackets like this:
for (int c = 0; c < voteRow;c++) {
for (int d = 0; d < voteCol;d++) {
votes[c][d] = 0;
}
}

C++ - Reading table of numbers from file into 2D Array (only last row stored) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Starting with an input file that looks like this:
2 3
2 3 4
4 3 2
I am trying to read this data into a 2D array in C++ (the first row specifying number of rows / cols).
My code currently looks like:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
fin.open ("dataset.in");
// a matrix
int a_numrows;
int a_numcols;
int a[a_numrows][a_numcols];
fin >> a_numrows >> a_numcols;
cout << a_numrows << " " << a_numcols << endl;
for (int i = 0; i<a_numrows; i++)
{
for (int j = 0; j<a_numcols; j++)
{
fin >> a[i][j];
}
}
cout << a[0][0] << endl;
fin.close();
return 0;
}
However it seems as though in each row of the 2D array, the last row is being stored. Thus when a[0][0] is outputted, it returns 4. This behavior is not how I think things should work coming from other languages.
You must permute these lines:
int a[a_numrows][a_numcols];
fin >> a_numrows >> a_numcols;
to
fin >> a_numrows >> a_numcols;
int a[a_numrows][a_numcols];
I guess this is a mistake of inattention.
That's said, there are safer/better ways to declare/use 2D arrays. Here is a possible example:
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
std::ifstream fin("dataset.in");
size_t n_rows, n_cols;
fin >> n_rows >> n_cols;
using T = int;
std::vector<T> array(n_rows * n_cols);
array.assign(std::istream_iterator<T>(fin), std::istream_iterator<T>());
fin.close();
//-----
for (size_t i = 0; i < n_rows; i++)
{
for (size_t j = 0; j < n_cols; j++)
{
std::cout << array[i * n_cols + j] << "\t";
}
std::cout << endl;
}
return 0;
}
Output:
g++ reader.cpp; ./a.out
2 3 4
4 3 2
Remember that when doing numerical computations it is generally better to store all the numbers into a contiguous memory chunk (like it is done in std::vector). In this situation it is easier for the compiler to vectorize your code.
To access components use:
[i*n_cols+j]: row-major (C-style) -> the given example,
more efficient to loop in this order: for i { for j ... } }
[j*n_rows+i]: column-major (Fortran-style) -> compatible with Blas & Lapack,
more efficient to loop in this order for j { for i ... } }
To declare an array in C++, the size has to be known at compile time. I.e. you can't pass a_numrows and a_numcols as array-dimensions as these are runtime values. For such an approach I would use a std::vector:
vector<vector<int>> a;
//... read a_numrows and a_numcols
a.resize(a_numrows); //resize creates #a_numrows empty columns
for(int i = 0; i < a_numrows; ++i)
{
for(int j = 0; j < a_numcols; ++j)
{
int value; fin >> value;
a[i].push_back(value); //access the ith row and add a new column with value inside
}
}

Input vector without temporary variable [duplicate]

This question already has answers here:
Getting input directly into a vector in C++
(5 answers)
Closed 6 years ago.
How do I input vector without a temporary variable(like x in this example)?
std::vector<int> a;
int n, x;
std::cin >> n;
for (int i=0;i<n;i++)
{
std::cin >> x;
a.push_back(x);
}
One possible solution:
int n, x;
std::cin >> n;
std::vector<int> a(n);
for (int i=0;i<n;i++){
std::cin >> a[i];
}