Finding prime numbers: segmentation fault - c++

I have a program that finds and prints prime numbers from a predefined scope:
#include <iostream>
#include <vector>
int main()
{
int scope = 90;
std::vector<int> arr;
int n = 2;
for (int i = n; i < scope; i++)
{
arr.push_back(i);
}
int index = 0;
while ((n * n) <= scope)
{
int size = (int)arr.size();
for (int i = index + 1; i < size; i++)
{
if (arr[i] % n == 0)
{
arr.erase(arr.begin() + i);
}
}
index += 1;
n = arr[index];
}
int final_size = (int)arr.size();
for (int i = 0; i < final_size; i++)
{
std::cout << arr[i] << std::endl;
}
}
it works fine but only when the scope is <= 90. When it is larger than 90 I get an error:
zsh: segmentation fault
I am a beginner and can't figure out reason for this. What's wrong with my code?

Related

print all the prime numbers upto an upperlimit

#include<iostream>
using namespace std;
int main(){
int n=5;
int i = 2;
for (i; i <= n; i++)
// for all num to n
{
int j = 2;
bool divide = false;
for (j; j <= n - 1; j++)
// for checking each num
{
if (i % j == 0)
{
divide = true;
break;
}
}
if (divide == false)
{
cout << i << " ";
}
}
return 0;
}
my Q is that
//please tell me why it is not working
//it is expected to give ans 2,3,5 which it is not giving why???
maybe I found the issue.
I think that the problem here is:
for (j; j <= n - 1; j++)
Here you did j<=n-1;
So to fix this just do:
for(j; j < i; j++){
//this should fix
So everything should look like this:
#include<iostream>
using namespace std;
int main() {
int n = 5;
int i = 2;
//check prime numbers starting from i and max n using for loop
for (i = 2; i <= n; i++) {
bool divide = false;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
divide = true;
break;
}
}
if (!divide) {
//!divide is equal to divide=false
cout << i << " ";
}
}
}

prime seive algorithm giving a runtime error

I am trying to implement the Sieve of Eratosthenes algorithm but it giving a runtime error.
didn't get any output though. after providing the input,
#include<iostream>
using namespace std;
//Sieve Approach - Generate an array containing prime Numbers
void prime_sieve(int *p) {
//first mark all odd number's prime
for (int i = 3; i <= 10000; i += 2) {
p[i] = 1;
}
// Sieve
for (long long int i = 3; i <= 10000; i += 2) {
//if the current number is not marked (it is prime)
if (p[i] == 1) {
//mark all the multiples of i as not prime
for (long long int j = i * i; j <= 10000; j = j + i ) {
p[j] = 0;
}
}
}
//special case
p[2] = 1;
p[1] = p[0] = 0;
}
int main() {
int n;
cin >> n;
int p[10000] = {0};
prime_sieve(p);
//lets print primes upto range n
for (int i = 0; i <= n; i++) {
if (p[i] == 1) {
cout << i << " ";
}
}
return 0;
}
compiler didn't throwing any error also it is not providing the output also
program freezes for some seconds and then terminates
As mentioned in the comments, you are going out of bound.
There is also some confusion about the meaning of p[].
In addition, you are not using the value of n in the function, which leads to unnecessary calculations.
Here is a tested programme (up to n = 10000):
#include <iostream>
#include <vector>
#include <cmath>
//Sieve Approach - Generate an array containing prime Numbers less than n
void prime_sieve(std::vector<int> &p, long long int n) {
//first mark all odd number's prime
for (long long int i = 4; i <= n; i += 2) {
p[i] = 0;
}
// Sieve
for (long long int i = 3; i <= sqrt(n); i += 2) {
//if the current number is not marked (it is prime)
if (p[i] == 1) {
//mark all the multiples of i as not prime
for (long long int j = i * i; j <= n; j = j + i ) {
p[j] = 0;
}
}
}
//special cases
p[1] = p[0] = 0;
}
int main() {
long long int n;
std::cout << "Enter n: ";
std::cin >> n;
std::vector<int> p (n+1, 1);
prime_sieve(p, n);
//lets print primes upto range n
for (long long int i = 0; i <= n; i++) {
if (p[i] == 1) {
std::cout << i << " ";
}
}
return 0;
}

Segmentation fault (core dumped) run time bug with MaxPairwiseProduct program

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int MaxPairwiseProduct(vector<int>& numbers);
//declaration
vector<int> *x;
x->push_back(1);
x->push_back(2);
int answer = MaxPairwiseProduct(*x);
cout << answer;
}
int MaxPairwiseProduct(vector<int>& numbers) {
int index1 = 1;
int index2;
//vector<int> numbers = number;
int n = numbers.size();
for(int i = 2;i < n;++i){
if(numbers[i]>numbers[index1]){
index1 = i;
}
}
if(index1 == 1){
index2 = 2;
} else {
index2 = 1;
}
for(int i = 1;i < n;++i){
if(numbers[i] != numbers[index1] && numbers[i]>numbers[index2]){
index2 = i;
}
}
numbers[index1] * numbers[index2];
return numbers[index1] * numbers[index2];
}
I am trying to implement a seemingly advance algorithm to find the max pair wise product. I continue to get Segmentation fault (core dumped) errors and I know it has something to do with my pointers and scope of my functions perhaps. Any advice or tips?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int MaxPairwiseProduct(vector<int>& numbers);
//declaration
vector<int> *x = new vector<int>();
x->push_back(7);
x->push_back(4);
x->push_back(5);
x->push_back(6);
int answer = MaxPairwiseProduct(*x);
cout << answer;delete x;
}
int MaxPairwiseProduct(vector<int>& numbers) {
int index1 = 1;
int index2;
//vector<int> numbers = number;
int n = numbers.size();
for(int i = 0;i < n;++i){
if(numbers[i]>numbers[index1]){
index1 = i;
}
}
if(index1 == 1){
index2 = 2;
} else {
index2 = 1;
}
for(int i = 0;i < n;++i){
if(numbers[i] != numbers[index1] && numbers[i]>numbers[index2]){
index2 = i;
}
}
return numbers[index1] * numbers[index2];
}
I got it to work there was an odd late night brain fart.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
long MaxPairwiseProductFast(vector<long>& numbers);
long N, input;
vector<long> *V = new vector<long>();
cin >> N;
do {
V->push_back(input);}
while (V->size() <= N && cin >> input);
long answer = MaxPairwiseProductFast(*V);
cout << answer;
delete V;
}
long MaxPairwiseProductFast(vector<long>& numbers) {
int index1 = 1;
int index2 = 1;
int n = numbers.size();
for(int i = 2;i < n;++i){
if(numbers[i]>numbers[index1]){
index1 = i;
}
}
if(index1 == 1){
index2 = 2;
} else {
index2 = 1;
}
for(int i = 2;i < n;++i){
if(i != index1 && numbers[i]>numbers[index2]){
index2 = i;
}
}
return (long)(numbers[index1] * numbers[index2]);
}
long MaxPairwiseProduct(vector<long>& A) {
int index = 1;
int n = A.size();
for(int i = 2; i < n; ++i){
if (A[i] > A[index]){
index = i;
}
}
swap(A[index], A[n]);
index = 1;
for (int i = 2; i < n-1; ++i){
if(A[i] > A[index]){
index = i;
}
}
swap(A[index], A[n - 1]);
return A[n-1] * A[n];
}
Modified version with user defined size of vector and the ability to define user input values.

Decimal to binary converter c++

I'm creating this program just for fun but i'm havin a problem
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int j = 1;
std::vector<int> n_bin(j);
int n=0;
int a;
cin>>n;
do
{
n_bin[j] = n%2;
n= n/2;
a++;
j++;
}while(n>0);
for(j=a; j>0; j--)
{
cout<<n_bin[j];
}
return 0;
}
the only problem is the output, infact before the real binary number i get a lot of more number for example:
if i assign 104 to n i get: 246978862916874543020108190880108137760108190884026797651687454302000108137760108191524026797651687454302183600828414839627280108137760146978862716874543020001321208800108136960-10108137760108364961101000 where only the last 7 are useful.
n_bin is initialized to have exactly one element, at n_bin[0]. n_bin[j] exhibits undefined behavior for any value of j other than 0, by way of accessing an index out of bounds.
void decToBinary(int n)
{
int binaryNum[1000];
int i = 0;
while(n > 0)
{
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for(int j = i - 1; j >= 0; j--)
{
cout << binaryNum[j];
}
}

Finding evens in an array

I'm writing a program that is made of a function that takes in a 2d array, c0unts the evens in the array and returns the amount of evens in that array. The function isn't returning the value that i intend it to, which is 6. Sometimes I get 0, sometimes I get a number like 2147483646.
#include <iostream>
#include <array>
using namespace std;
const int MaxNumOfRows = 3;
const int MaxNumOfColumns = 2;
int Even(int A[MaxNumOfRows][MaxNumOfColumns], int length, int width)
{
int NumnberOfEvens = 0;
int i;
int j;
for (i = 0; i < length; length++)
{
for (j = 0; j < width; width++)
{
if (A[i][j] % 2 == 0)
{
NumnberOfEvens++;
}
}
}
return NumnberOfEvens;
}
int main()
{
//int output = 0;
int A[MaxNumOfRows][MaxNumOfColumns] =
{
{2,2},{2,4},{2,2}
};
Even(A, MaxNumOfRows, MaxNumOfColumns);
//output = Even(A, MaxNumOfRows, MaxNumOfColumns);
cout << Even(A, MaxNumOfRows, MaxNumOfColumns) << endl;
system("pause");
return 0;
}
Check those for loops, I imagine you want to be incrementing the variables ++i and ++j rather than width++ and length++.
With an example this trivial, I imagine stepping through the executing code and finding the problem in a debugger would be pretty straightforward...are you writing this using an IDE with a debugger?
You are not applying increment on loop variables ('i' and 'j') here. 'length' and 'width' are increasing (due to length++, width++) whereas 'i' and 'j' are not. So, loop won't stop and thus the garbage values.
for (i = 0; i < length; length++)
{
for (j = 0; j < width; width++)
{
if (A[i][j] % 2 == 0)
{
NumnberOfEvens++;
}
}
}
This must work.
for (i = 0; i < length; i++)
{
for (j = 0; j < width; j++)
{
if (A[i][j] % 2 == 0)
{
NumnberOfEvens++;
}
}
}