if n is for example 1.000.000 compiler stops working. Why? Long int dont help.
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
ifstream fin;
fin.open("share.in");
fout.open("share.out");
int n;
fin>>n;
int array1[n];
for(int i=0; i<n; i++)
{
fin >> array1[i];
}
int suma=0, sumb=0, sumc=0, a=1, b=1, c=n-2, a1=0, b1=0, c1=0, apotelesma=1000000000, mikroterhdiafora=1000000000;
while(c>0)
{
while(a1<a)
{
suma+=array1[a1];
a1++;
}
while(b1<b)
{
sumb+=array1[(a1+b1)];
b1++;
}
while(c1<c)
{
sumc+=array1[(a1+b1+c1)];
c1++;
}
if(max(abs(suma-sumb),max(abs(sumb-sumc),abs(sumc-suma)))<=mikroterhdiafora)
{
mikroterhdiafora=min(mikroterhdiafora, max(abs(suma-sumb),max(abs(sumb-sumc),abs(sumc-suma))));
apotelesma=min(apotelesma, max(suma,max(sumb,sumc)));
}
suma=0;
sumb=0;
sumc=0;
a1=0;
b1=0;
c1=0;
c--;
b++;
if(c==0)
{
++a;
b=1;
c=n-a-1;
}
}
fout<<apotelesma;
fin.close();
fout.close();
return 0;
}
1,000,000 is a bit too much for a static array. You should create it dynamically:
int* array1 = new int[1000000];
// Do operations on array...
delete[] array1;
The cause of this behaviour is that when you create static array (int array1[1000000]), compiler puts it on the stack, which has limited capacity and when you exceed that limit, you have unfamous stack overflow, which leads to your problem. Dynamic memory on the other hand is stored on the heap and is limited only by your hardware, but unlike in the stack, you have to remember to free it, when it's not longer needed.
Related
this code works well for all samples, but after all samples are finished, a problem occurs. I don’t know what happens and the program crashes. Is there a problem with this code?
i have this problem when i use strings arrays usualy can it be the problem?
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
long long t,n;
int a[1000];
string str[1000];
int main()
{
cin>>t;
for(int r=1;r<=t;r++){
cin>>n;
int maxi=0;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]>maxi)maxi=a[i];
};
//input first value
maxi=maxi+3;
for(int r1=0;r1<maxi;r1++){
str[1][r1]=(rand()%26)+'a';
}
for(int i=0;i<maxi;i++){
cout<<str[1][i];
}
cout<<endl;
//
for(int k=2;k<=(n+1);k++){
int w;
for(w=0 ; w<=a[k-1];w++){
str[k][w]=str[k-1][w];
};
for(int l=w-1;l<maxi;l++){
str[k][l]=(rand()%26)+'a';
};
for(int i=0;i<maxi;i++){
cout<<str[k][i];
}
cout<<endl;
}
}
return 0;
}
You are using elements of strings without allocating them.
Allocate elements by inserting
for(int i=1;i<=(n+1);i++){
str[i].resize(maxi);
}
just after
maxi=maxi+3;
It is code to reverse the values as they entered.When I am running the following code. It is taking 8 inputs only. After that it is not printing anything.
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int *p = new int(sizeof(int)*n);
int q = n;
for(int i=0;i<n;i++)
{
cin>>*p;
p++;
}
for(int j=0;j<n;j++)
{
cout<<*p<<" ";
p--;
}
return 0;
}
You can also try the following answer.
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int *p = (int *)malloc(sizeof(int)*n);
int q = n;
for(int i=0;i<n;i++)
{
cin>>*p;
p++;
}
for(int j=0;j<n;j++)
{
p--;
cout<<*p<<" ";
}
free(p);
return 0;
}
#include <iostream>
using namespace std;
(Not related to the title, but using namespace std is bad practice that can lead to breakage when switching compilers, for example. Better write the std:: prefix when needed, such as std::cin >>.
int main() {
int n;
cin>>n;
int *p = new int(sizeof(int)*n);
The above is allocating a single int object, whose value is sizeof(int)*n, and p points to that integer. You probably mean:
int *p = (int*)malloc(sizeof(int)*n); // bad style
... at the end:
free(p);
But using malloc in C++ is a bad idea, unless you want to go closer to the operating system for educational purposes.
Slightly better is to use new, which besides allocating the objects also calls their constructors (but nothing is constructed for basic types such as int).
int *p = new int[n]; // so-so style
... at the end:
delete [] p;
The above is not the best practice because it requires manual memory management. Instead, it is recommended to use smart pointers or containers whenever possible:
std::vector<int> p(n);
// continue with the code, just like with the pointers
Or allocate the individual elements only when needed.
std::vector<int> p;
p.reserve(n); // this is a minor optimization in this case
// ...
if (int value; std::cin >> value)
// This is how to add elements:
p.push_back(value);
else
std::cin.clear();
This looks ok:
int q = n;
for(int i=0;i<n;i++)
{
cin>>*p;
p++;
}
But this is broken. When the loop starts, p points after the last element. The following *p dereferences a pointer which goes past the last element:
for(int j=0;j<n;j++)
{
cout<<*p<<" ";
p--;
}
Replacing the order of pointer decrement and dereference avoids the crash:
for(int j=0;j<n;j++)
{
p--;
std::cout << *p << " ";
}
Ok, there many issues here:
int *p = new int(sizeof(int)*n);
This memory allocation is wrong. It allocates n times sizeof(int) bytes, so if int is 4 bytes long it will allocates n * 4 integers.
int q = n;
q variable is never used.
for(int i=0;i<n;i++)
{
cin>>*p;
p++;
}
There is no need for pointer arithmetic here. It would be better to access the array in simple p[i] way.
for(int j=0;j<n;j++)
{
cout<<*p<<" ";
p--;
}
Same here...
return 0;
}
You allocated memory, but never deallocate. This will cause memory leaks.
A better, correct version of the program could be:
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int * p = new int[n];
for (int i = 0; i < n; ++i)
{
cin >> p[i];
}
for (int i = (n - 1); i >= 0; --i)
{
cout << p[i] << ' ';
}
delete [] p;
return 0;
}
I have a C++ program, in which I have to create a recursive function that writes all the digits of a given positive integer in an array - in this case a vector.
However, when I compile the program and enter a number, it stops working. I want to ask why this happens?
#include <iostream>
#include <vector>
using namespace std;
vector <int> arr;
int temp;
int fill_Array(int num)
{
if(num>=1 && num<=9)
{
arr.push_back(num);
}
temp = fill_Array(num)%10;
arr.push_back(temp);
num/=10;
}
int main()
{
int n;
cin>>n;
fill_Array(n);
for(int i=0; i<arr.size(); i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
In the given code, recursion function does not returning any value, so return type for that function have no use.
calling the function for recursion is in the wrong place. Correct code given below:
#include <iostream>
#include <vector>
using namespace std;
vector <int> arr;
int temp;
void fill_Array(int num)
{
if(num>=1 && num<=9)
{
arr.push_back(num);
}
else{
temp = num%10;
arr.push_back(temp);
fill_Array(num/=10);
}
}
int main()
{
int n;
cin>>n;
fill_Array(n);
for(int i=0; i<arr.size(); i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
A couple of reasons I can see:
There is no conditional to stop the recusion so it will keep going until it runs out of stack or memory. I presume you want to stop when num is zero
fill_Array has no return value so will assign temp with some random value which will be pushed into the array
Also why use recursion for this when iterative would be easier and more obvious what it is doing
I want to enter n times values for c and e arrays. The following program doesn't allow me to even enter the value of 'n'. Could you tell me where is the mistake?
#include <iostream>
using namespace std;
int main()
{
int n,c[n],e[n];
cin>>n;
for(int i=0;i<n;i++){
cin>>c[i]>>e[i];
}
return 0;
}
"n" should be defined before using it to fix array size. Also, const int or constant should be used to declare array size not plain int.
In order to use plain datatype, you can initialize array dynamically like
vector<int> a(n); or
int a = new int[n]
int n,c[n],e[n];
This declaration creates arrays c and e on stack with random size, because n as an automatic variable is initialized with random value. Instead you need to dynamically create arrays on heap or use std::vector.
Example:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// your code goes here
int n;
vector<int> v;
std::cin >> n;
v.resize( n);
for( int i = 0; i < n; ++i) {
cin >> v[i];
}
for( int i = 0; i < n; ++i) {
cout << v[i];
}
return 0;
}
http://ideone.com/QhgfNv
In the line of
int n,c[n],e[n];
Computer don't know the exact value of 'n', so it can't alloc memory of array.
The simplest solution is create array with fixed number, and check n after you know the value of n as follows:
int n, c[1024], e[1024];
cin >> n;
if (n > 1024) { /* error */ }
The other way is malloc memory after u know the value of n:
int n;
cin >> n;
int *c = new int[n];
int *e = new int[n];
xxxx
delete [] c;
delete [] e;
You can try something like this:
#include <iostream>
using namespace std;
int main()
{
int temp = 100; /*Random value*/
int c[temp];
int e[temp];
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>c[i]>>e[i];
}
return 0;
}
Now, I chose temp as 100, but you can do big as your int can store. Now, if n is lower than temp, your for cycle will let you save your values without troubles.
In C++, using the vector header, how do I find the number of elements?
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int primer(int max);
int main()
{
system("pause");
return 0;
primer(1000);
}
int primer(int max){
vector<int> a;
a[1]=2;
for (int i=2;i<=max;i++){
bool prime=true;
for (int ii=1;ii<=#a;ii++) {
if i/a[ii]==math.floor(i/a[ii]) {
prime=false;
}
}
if prime==true {
a[#a+1]=i;
}
}
for (i=1;i<=#a;i++) {
cout << a[i]);
}
}
}
I originally wrote the code
for lua, and this is my attempt to translate it to C++. I would appreciate specifics, for example, a specific replacement for a bad line. I tried to replace #a with a.size, but it didn't work.
Revised:
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int primer(int max);
int main()
{
primer(5);
system("pause");
return 0;
}
int primer(int max){
vector<int> a;
a[1]=2;
for (int i=2;i<=max;i++){
bool prime=true;
for (int ii=0;ii<a.size();ii++) {
if (i/a[ii]==floor(i/a[ii])) {
prime=false;
}
}
if (prime==true) {
a.push_back(i);
}
}
for (int iii=0;iii<=a.size();iii++) {
cout << a[iii] << endl;
}
}
It crashes without running. For what reason is this?
a.size().
I would recommend using some sort of reference material, e.g. http://cplusplus.com/reference/stl/vector/.
To answer your immediate question:
a.size(); // use size as a function
But there are several other things wrong with your code:
vector<int> a;
a[1]=2;
Ordinarily you need to set the size of a beforehand, since C++ must allocate space for it. You can use push_back() though, which will incrementally add space as needed.
Also, C++ arrays start counting at 0:
for (int ii=1;ii<=#a;ii++) {
This should be
ii = 0
And since arrays start at 0, they end at size() - 1, not size().
for( int ii = 0; ii < a.size(); ++ii )
C and C++ array indexes start at zero and end at size-1, so you need to compare less-than, not less-than-or-equal-to. vector follows the same rule.
Another obvious problem that needs pointing out:
int main()
{
system("pause");
return 0;
primer(1000);
}
Your function is never going to be called. Your app will exit when main returns.
a[#a+1]=i;
changed to use size() becomes:
a[ a.size() + 1 ] = i;
This is syntactically correct but guaranteed wrong. It should be:
a.push_back(i);
Read the API referenced by Oli.