I am practicing about primeSieve with C++ language in VS Code 1.57.1.
But the attached code doesn't show output in VS Code while it shows output in online c++ compiler like
https://www.onlinegdb.com/online_c++_compiler
Can anyone help me to resolve this issue.
#include<iostream>
using namespace std;
void primeSieve(){//Genearate array containing prime number
int number;
cin>>number;
int p[1000] = {0}; //1=Prime;0=Non-Prime
//Mark All Odd number prime
for(int i=3;i<=1000;i+=2){
p[i]=1;
}
//Sieve
for(int i=3;i<=1000;i+=2){//jumping over odd numbers
//if current number is not marked(it is prime)
if(p[i]==1){
//Mark Multiples as 0
for(int j=2*i;j<=1000;j=j+i){
p[j]=0;
}
}
}
//Special Case
p[2]=1;
p[1]=p[0]=0;
for(int x=0;x<=number;x++){
if(p[x]!=0){
cout<<x<<" ";
}
}
}
int main(){
primeSieve();
return 0;
}
Size of p[] is 1000. Your loop goes up to 1000. Try removing the = in the loops and just keep i < 1000.
Related
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cout<<"enter the no till which prime nos to be found :"<<endl;
cin>>n;
vector<int> prime(n+1,0);// we want n index too
for(int i=2;i<=n;i++) {
if(prime[i]==0) {
for(int j=i*i;j<=n;j=j+i) {
prime[j]=1;
}
}
}
cout<<"prime numbers are: "<<endl;
for(int i=2;i<=n;i++) {
if(prime[i]==0) {
cout<<i<<endl;
}
}
return 0;
}
above code should be working for n>=100000 since n(log(log(n))) will equal to 400000.
Your i*i overflows a 32-bit int when n is more than about 46000. This causes undefined behavior. On my system, it wraps around and initializes j with a negative value, causing a segmentation fault when prime[j] is accessed.
This has nothing to do with time complexity.
This is a syntax of a program that I made of hackerrank to reverse an array. The location of the question is Practice < Data Structures < Arrays < Arrays - DS
the program seems to work fine on an online compiler but is showing error over hackerrank.
can anyone guide me where I went wrong?
#include<iostream>
using namespace std;
int main(){
int n,i;
int arr[n];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=n-1;i>=0;i--)
{
cout<<arr[i];
}
}
You are creating an array with n elements before you read n, i.e n is having garbage value. Here is correct working code.
#include <iostream>
using namespace std;
int main(){
int n,i;
cout<<"Enter number of elements"<<endl; // Comment it if you don't want debug console outut
cin>>n;
int arr[n];
cout<<"Enter "<<n<<" elements"<<endl; // Comment it if you don't want debug console outut
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=n-1;i>=0;i--)
{
cout<<arr[i];
}
}
I changed your solution and it work fine now , i will tell you some notes for your future:
1 - You have to read size of array first before declare array as mentioned in comments by #HolyBlackCat , otherwise it will give garbage value.
2 - this command int arr[n]; won't work on all Compilers and may give you an Error. Is there a solution to declare array without specific size ? Yes , you can use Vector like that vector<int> arr(n);but you have to include library of vector first #include<vector>
3 - In C and C++ programs the main function is of type int and therefore it should return an integer value. The return value of the main function is considered the "Exit Status" of the application. On most operating systems returning 0 is a success status like saying "The program worked fine". So you have to write return 0; in the end of your program.
#include<iostream>
#include<vector> // must include to use Vectors
using namespace std;
int main(){
int n;
cin >> n; // Must read size of array first , otherwise it will give it garbude value
vector<int> arr(n); // instead of int arr[n]
for(int i = 0 ; i < n ; i++)
cin >> arr[i];
for(int i = n-1 ; i >= 0 ; i--)
cout << arr[i] << " ";
cout << endl; // to print new line as mentioned in the problem statement
return 0; // Prefer to use it always
}
Hope that helps you and Good Luck.
The exercise of Hacker Rank needs that you reverse an array of integers, might you have a problem with the variable size array, cause some compilers support this characteristic but others not.
And Hacker Rank do not support some properties in other languages like JS , Hacker Rank do not support prompt() for take an input.
In any case for fix this error you can use vector class.
#include<iostream>
#include<vector>//Include library vector for dynamic size of array
using namespace std;
int main(){
int n;
cin>>n;
vector<int> arr(n);//Declare the vector with a max size n
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i = n-1 ;i>=0;i--){
cout<<arr[i]<<" ";
}
return 0;
}
This code has passed al test cases of hackerRank
All the test on hackerRank had passed
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int bins(int* p,int lo,int hi,int t)
{//BinarySearch
int mid=0;
int c=0;
if(lo<hi)
{
mid=(lo+hi)/2;
if(p[mid]==t) c=1;
else if(t>p[mid])
bins(p,mid+1,hi,t);
else if(t<p[mid])
bins(p,0,mid,t);
}
return c;
}
int main()
{
clrscr();
int target;
int k;
int count=0;
cout<<"Enter the number of elements:";
cin>>k;
int* numbers=new int(k);
cout<<"Enter the target element"<<endl;
cin>>target;
cout<<"Enter the elements:"<<endl;
for(int i=0;i<k;i++)
cin>>numbers[i];
int* nu=new int(k);
memset(nu,0,sizeof(int));
/*for(i=0;i<k;i++){
cout<<numbers[i]<<endl;
}*/
for(i=0;i<k;i++)
{
int bool=bins(nu,0,k,numbers[i]);
//printf("%d\n",bool);
if(bool)
{
count++;
}
else
{
int tg=target-numbers[i];
cout<<"targetval:"<<tg<<endl;
nu[i]=tg;
}
//bool?count++:(nu[i]=target-numbers[i]);
}
cout<<"Count is:"<<count<<endl;
getch();
return 0;
}
this is the code i have written for TwoSum problem in leetcode,instead of using HashMap whose time complexity is O(n) i used binary search whose complexity is O(logn)
The problem i'm facing is the input i give to the array numbers[i] takes only 3 values and from then it stores junk values even after assigining proper inputs from console
this program has been compiled succesfully on Turbocompiler on system
int* numbers=new int[k];////allocates an array of k adjacent integers. (undefined values)
You are allocating for an array. Check the notations.
Also you are using a keyword as a variable.(bool).
What have you done?
int *numbers=new int(k);////allocates an integer, set to k. (same syntax as constructors)
Leetcode doesn't give exact code. It will have syntax errors or compilation erros. You have to fix them to make it work. In this case, following are the issues,
int* numbers=new int[k]; //not int* numbers=new int(k);
Another thing is that bool is a keyword, that is used to give true or false for variables. You have to use another variable.
I took it upon myself to learn C++ a few days ago. I have just written a program to find prime numbers, up to a user inputted value, and write these values to a file. The program works fine with numbers up to the order of 100,000 - 500,000. But, if I try to go to 1,000,000 the program freezes. Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
long pp, z,counter,lim,indyCounter;
bool isPrime=false;
ofstream myfile;
myfile.open("E:\\Program Files (x86)\\C++ Programs\\PrimeFinder\\Primes.txt");
cout<<"up to what number would you like to calculate primes? ";
cin>>lim;
cout<<endl;
long ps[lim]; //real-time array of primes
pp=3; //prospective prime
ps[0]=2; //initializing prime array with first prime number
counter=1;
indyCounter=1;
for(int y=1; y<=lim;y++)
{
ps[y]=1;
}
for(int z=0; z<=lim; z++)
{
for(int x=0;x<counter;x++)
{
if(pp%ps[x]!=0)
{
isPrime = true;
}
if(pp%ps[x]==0 && ps[x]!=1)
{
isPrime=false;
break;
}
}
if(isPrime)
{
ps[indyCounter]=pp;
indyCounter++;
}
counter++;
pp++;
}
for(int y=0; y<=lim-1;y++)
{
if(ps[y]!=1)
{
myfile<<ps[y]<<endl;
}
}
myfile.close();
return 0;
}
please excuse my beginners code, and all advice is much appreciated!
Thanks,
Steve
The default stack size on Windows is only 8MB, and an array of 1000000 longs requires 8MB, so you're overflowing the stack. You need to allocate your array on the heap instead.
The following prime generator code shows an error when i want prime numbers greater than 1000000.Why??
at first it seemed to occur b'cuz of int so i changed it to long but the error is still there....
technically speaking it nit an error the program after running displays the message "primegen.exe has stopped working"
#include <iostream>
using namespace std;
int main()
{
long int a,c,k,d;
k=0;
cin>>a;
cin>>d;
long int b[a];
b[a-1]=0;
for(long int i=2;i<=a;i++)
{
for(long int j=2;j<=(i/2);j++)
{
c=1;
if ( i%j!=0 )
{
continue;
}
else
{
c=0;
break;
}
}
if (c!=0)
{
b[k]=i;
//++k;
}
else b[k]=0;
++k;
}
for(long int i=d;i<a;i++)
{
if (b[i]!=0)
{
cout<<b[i]<<"\t";
}
}
cin.ignore();
cin.get();
return 0;
}
There is no error in this code.
The code is too slow though, it is almost quadratic. The projected time to reach 1 mln on ideone: 290 seconds.
After fixing it, by changing the inner for loop condition from for(...;j<=(i/2);...) to for(...;j<=(i/j);...), it runs at ~ n^1.45, empirically, and reaches 1 mln in 1.27 seconds, on Ideone.
Defining a static array won't help in this case. Because you're not allowed to declare such a long static array in C++;
You might want to try
int *b = new int [a];
declaring the array size dynamically (i.e the array size will be decided at run time). The code should work until variable 'a' exceeds the range of int (–2,147,483,648 to 2,147,483,647).