Reputation: 33
I want to have two arrays A
and B
where A=[a0, a1, a2, …, aN-1]
, B=[b0, b1, b2, …, bN-1]
where "N" would be an input from the user. I wanna fill both arrays with random numbers between 0 and 1. Then I wanna have the product of aN
and bN
in an array C[n]
, also I want the summation of the elements in array C
. I am not sure what I'm doing wrong but the application runs fine until I enter a number like 100,000 as N
, if I enter some big number like this the application will crash.
Here is my code in C++:`
int main(int argc, char **argv)
{
long long int n;
cout << "Hi, what do you want n to be?\n";
cin >> n;
long long int c = n - 1;
double A[c], B[c], C[c];
srand(time(NULL));
for (long long int i = 0; i <= c; i++) {
A[i] = ((double) rand() / (double) (RAND_MAX));
B[i] = ((double) rand() / (double) (RAND_MAX));
C[i] = (double) A[i] * B[i];
printf("%d %.15f\n",i, C[i]);
}
double sum = 0;
for (long long int i = 0; i <= c; i++){
sum += C[i];
}
printf("%d", sum);
return 0;
}
Upvotes: 1
Views: 460
Reputation: 695
Besides that you are running out of array bounds, due to "i<=c" you are running out of stack space: The arrays are placed on stack and the size on stack is limited. Exceeding the stack size your program will crash. Default stack size depends on OS and OS settings. On Linux you can change it using "ulimit -s".
Upvotes: 1
Reputation: 35454
One issue is this:
double A[c], B[c], C[c];
This is not valid C++, as arrays must have constant expressions to denote the number of entries. You're using an extension offered by your compiler, namely Variable Length Arrays (VLA). The issue is more than likely you're blowing out the stack space due to the arrays being too large.
Instead of this, use something that is valid C++, namely std::vector:
long long int n;
cin >> n;
long long int c = n - 1;
std::vector<double> A(c), B(c), C(c);
However, please note that a std::vector
is limited to std::vector::max_size()
elements, thus it is possible that the value you enter for n
may be too large to store the elements. Reconsider whether you want to go beyond the max_size()
value.
In addition, you are accessing elements out of bounds. This is another area where using std::vector
is advantageous over arrays.
for (long long int i = 0; i <= c; i++) {
A.at(i) = ((double) rand() / (double) (RAND_MAX));
B[i] = ((double) rand() / (double) (RAND_MAX));
C[i] = (double) A[i] * B[i];
printf("%d %.15f\n",i, C[i]);
}
You will see that when i == c
, the call to A.at()
is guaranteed to throw an std::out_of_range
exception, denoting that you have gone out of bounds with the index i
. An array cannot report errors like this with any consistency, as going out-of-bounds of an array is undefined behavior (the code may "work", may crash, etc.).
There is another issue, and it's this:
printf("%d", sum);
Since sum
is a double
, giving printf
a variable type that does not match the format specifier is undefined behavior. More than likely, you will see wild numbers being printed. The format specifier, %d
, requires an int
type, not a double
. So the correction would be this:
printf("%lf", sum);
But since you're using C++, you should simply use std::cout
, as it is typesafe and will not have you get into this type of trouble:
std::cout << sum;
When you remove all of the errors and make the corrections describe, here is a live example showing the output.
In addition, here is an alternative that uses the STL algorithm functions that is much shorter than the code you have now, and does not need to declare 3 vectors (only one vector needs to be declared):
Upvotes: 4
Reputation: 1133
When you have an array of size c, you can access elements 0 through c-1, not c!
So your loop should be
for (long long int i = 0; i < c; ++i)
instead of
for (long long int i = 0; i <= c; ++i)
Also, for a large c consider using heap memory instead of stack memory, e.g. use
A = new double[c];
instead of
double A[c];
Further, get familiar with smart pointers such as std::unique_ptr and STL containers such as std::vector.
Upvotes: 3