im getting a Segmentation fault error 11 C++

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



im getting a Segmentation fault error 11 C++



So I'm getting a segmentation fault error and im not sure what im doing wrong. Any help would be appreciated. My task was to to write a C++ program, quitegood, that determines numbers of a specified maximum badness that are less than a specified value. The limiting value and maximum badness are specified as command-line arguments when the program is executed.



And we had 4 levels to complete



Level 1: Small perfect numbers
Begin by writing a program that prints perfect numbers (badness 0) up to values less than 10000, separated by a single space. For example quitegood 100 should print 6 28.



Level 2: Small near-perfect numbers
Extend the program so that the badness limit can be specified as a second command-line parameter. For example quitegood 100 3 will print 2 3 4 6 8 10 16 18 20 28 32 64.



Level 3: Large numbers
Modify the program so that it will work efficiently for values up to at least 1000000. For example quitegood 1000000 1 should print 2 4 6 8 16 28 32 64 128 256 496 512 1024 2048 4096 8128 8192 16384 32768 65536 131072 262144 524288



Level 4: Displaying a count
Modify the program so that a negative badness value causes the program to output a count of the "quite good" numbers, rather than the numbers themselves. For example quitegood 1000000 -1 should print 23 because there are 23 numbers less than 1000000 with badness no more than 1.



My current code is


#include <vector>
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;

std::vector<int> prime_numbers_till( int n )

++n ;
std::vector<int> sieve( n, true ) ;
for( int i = 2 ; i < n ; ++i ) if( sieve[i] )
for( auto j = i*i ; j < n ; j += i )
sieve[j] = false ;

std::vector<int> primes ;
for( int i = 2 ; i < n ; ++i ) if( sieve[i] )
primes.push_back(i);
return primes ;


int multiplicity( int number, int factor )

int n = 0 ;
for( ; number%factor == 0 ; ++n )
number /= factor ;
return n ;


int ipow( int n, int e )

if( e == 0 ) return 1 ;
else return n * ipow( n, e-1 ) ;


int term( int prime_factor, int multiplicity )

return ( ipow( prime_factor, multiplicity+1 ) - 1 ) /
(prime_factor-1) ;


int sum_proper_divisors( int number, const std::vector<int>&
primes )

int sum = 1 ;
for( int prime_no : primes )

if( prime_no > (number/2) ) break ;
const auto mult = multiplicity( number, prime_no ) ;
if(mult) sum *= term( prime_no, mult ) ;

return sum - number ;



bool quite_good( int number, int max_badness, const std::vector<int>& primes )

return std::abs( number - sum_proper_divisors( number, primes ) ) <= max_badness ;


int main(int argc, char *argv)

int badness= argc ;
int N = atoi(argv[1]);
cout<<"N="<<N<<endl;
cout<<"Badness= "<<badness<<endl;
const auto primes = prime_numbers_till( std::ceil( std::sqrt(N) ) + 2 ) ;

int cnt = 0 ;
for( int number = 1 ; number <= N ; ++number )

if( quite_good( number, badness, primes ) )

std::cout << std::setw(8) << number << ' ' ;
if( ++cnt%10 == 0 ) std::cout << 'n' ;


std::cout << 'n' ;



Sorry about the really long question and code but wanted to provide as much information as possible





Please fix your intendation. Why do you use a vector<int> to store bools?
– user463035818
Aug 10 at 12:52


vector<int>


bool





Turn on C++11 and try compiling again
– NathanOliver
Aug 10 at 12:52





How do i turn on c++11 for mac terminal?
– R_ Bowsy
Aug 10 at 12:53





How are you invoking your code? argv[1] might access out-of-bounds memory, and you don't check against argc, before accessing it, to make sure that the index is valid.
– Algirdas Preidžius
Aug 10 at 12:54


argv[1]


argc





Please note that argc stands for argument count and is the number of arguments your program was passed from the command line. Judging by your question statement, that's not how you want to read the badness.
– Qubit
Aug 10 at 12:56


argc









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard