+ 3

How can I make an array if I don't know its size or what it contains?

I'm working on a program that calculates all the prime numbers from 1 to n so i cant't know the size of the array. What should I do?

11th Mar 2017, 6:19 PM
Giovanni Del Gallo
Giovanni Del Gallo - avatar
2 Antworten
+ 15
4 calculating prime no there is no need of array but u can use dynamic array
11th Mar 2017, 6:22 PM
Mansi Dagla
Mansi Dagla - avatar
+ 2
#include<iostream> using namespace std; int main() { int n,i,j,flag; cin>>n; cout<<"2\n"; for(i=3;i<=n;i++) { for(j=2;j<=i/2;j++) { flag = 1; if(i%j==0) { flag = 0; break; } } if(flag == 1) { cout<<i<<"\n"; } } return 0; } *** Try this. This will give you the results. ***
11th Mar 2017, 7:08 PM
Shubham Agrawal
Shubham Agrawal - avatar