Exponential values not taking in large array [duplicate] - c++

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;
}

Related

I get only 00000 [duplicate]

This question already has answers here:
C/C++ the result of the uninitialized array
(2 answers)
Uninitialized variable behaviour in C++
(4 answers)
Closed last month.
i want to create a new array that include negative numbers from array t. My code is running,ı have no error but I get only 00000
#include "iostream"
using namespace std;
int main()
{
int t[3][12]={{-2,7,6,13},{48,-5,1,-7},{16,-9,2,-9}};
int i,j,k;
k=0;
int a[12];
for(i=0; i<=2; i++)
{
for(j=0; j<=3; j++)
{
if(t[i][j]<0)
{
t[i][j]=a[k];
k++;
cout<<t[i][j];
}
}
}
cout<<endl;
}
the error is in t[i][j]=a[k], it should be a[k] = t[i][j]

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.

Variable length in a Simple Matrix c++ [duplicate]

This question already has answers here:
Why can't I create an array of size n? [duplicate]
(5 answers)
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 9 months ago.
I'm using VS 2019. In this code:
using namespace std;
int main()
{
cout << "Inserisci la dimensione della matrice:";
int DIM;
DIM = 2;
cin >> DIM;
int v[DIM][DIM];
return 0;
}
I Don't understand why in:
int v[DIM][DIM];
I've this error:
The expression must have a constant value. It is not possible to use the "DIM" variable value as a constant.
In C++, the size of an array must be a compile time constant. So you cannot write code like:
int n = 10;
int arr[n]; //incorrect
Correct way to write this would be:
const int n = 10;
int arr[n]; //correct
So we cannot use the input given by the user as the size of the array. That is why you are getting the mentioned error.
You can use std::vector in place of built in array to solve your problem.
some compiler/c++ version doesn't support variable length arrays compile time. we can create dynamic array on heap. Below code works for you
using namespace std;
int main()
{
cout << "Inserisci la dimensione della matrice:";
int DIM;
DIM = 2;
cin >> DIM;
int **v = new int*[DIM];
for (int i=0;i<DIM;i++)
v[i] = new int[DIM];
// now you can use it as 2D array i.e
v[0][0] = 12;
cout<<v[0][0];
return 0;
}

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

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];
}

What's the problem here causing "Invalid memory reference (SIGSEGV)" when integers 10^8 and upwards are used as input? [duplicate]

This question already has answers here:
Getting a stack overflow exception when declaring a large array
(8 answers)
Closed 2 years ago.
I've used different values of n below and above 10^8. It works fine below it. But starts showing SIGSEGV once the input to n increases upto 10^8 or more. Why does this happen? My compiler is g++ 7.4.0
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
long long int n, k, c = 0;
cin >> n >> k;
long long int arr[n];
for(long long int i =1; i <=n ; i+=2){
arr[c] = i;
c++;
}
for(long long int j =2; j <=n ; j+=2){
arr[c] = j;
c++;
}
cout << arr[k-1];
return 0;
}
You're allocating a large array on the stack -- when it gets too large, the stack overflows and you get a SIGSEGV.