7 Replies - 328 Views - Last Post: 07 November 2017 - 08:34 AM Rate Topic: -----

#1 KING iMm0rt4lxM  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 08-November 15

Display Prime Numbers with a Number Class

Posted 06 November 2017 - 08:22 PM

I have to display the prime numbers between 2 and the number entered with a Number class. Compiler says I need an argument on display_Prime_Numbers. Therefore program is not running.

Any help would be appreciated it.

 // Number.h
#ifndef NUMBER_H
#define NUMBER_H

#include <iostream>
using namespace std;

class Number
{
    public:
        Number();
        //Number(int);
        //bool isPrime();
        //bool isDivisibleBy(int);
        bool display_Prime_Numbers(int);
        int get_Number();
    private:
        //int value;
        int number_entered;
};

#endif // NUMBER_H



 // Number2.cpp
#include <iostream>
#include "Number.h"

using namespace std;

Number::Number()
{
    // Constructor
}

bool Number::display_Prime_Numbers(int i)
{
    bool isPrime = true;
    
    for (int i = 2; i < get_Number(); i++)
    {
        for (int j = 2; j * j <= i; j++)
        {
        // Checks if number is even
            if (i % j == 0)
            {
                isPrime = false;
                break;
            }
        }
    }
    
    // *** OUTPUT ***
    if (isPrime) 
    {
        cout << i << " ";
    }
    
    return isPrime;
}

int Number::get_Number()
{
    cout << "Enter a positive integer number greater than 1: \n";
    cin >> number_entered;
    
    return number_entered;
}



#include <iostream>
#include "Number.h"
#include "Number2.cpp"

using namespace std;

int main()
{
    Number output;
    
    output.get_Number();
    
    output.display_Prime_Numbers();

    
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Display Prime Numbers with a Number Class

#2 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: Display Prime Numbers with a Number Class

Posted 06 November 2017 - 08:31 PM

You have made

display_Prime_Numbers()


take an int as an argument ie

display_Prime_Numbers(int i)


I would remove the reference to int i .....
Was This Post Helpful? 0
  • +
  • -

#3 KING iMm0rt4lxM  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 08-November 15

Re: Display Prime Numbers with a Number Class

Posted 07 November 2017 - 12:58 AM

I managed to make the code run, but I don't understand why the number variable is not been included onto the display_Prime_Numbers function. Any ideas?

My output is basically "2 3 0", doesn't matter what number I write in.

#include <iostream>
#include "Number.h"

using namespace std;

Number::Number()
{
    // Constructor
}

bool Number::display_Prime_Numbers()
{
    bool isPrime = true;
    
    for (int i = 2; i < number; i++)
    {
        for (int j = 2; j * j <= i; j++)
        {
        // Checks if number is even
            if (i % j == 0)
            {
                isPrime = false;
                break;
            }
        }
        
    // *** OUTPUT ***
        if (isPrime) 
        {
            cout << i << " ";
        }
    }
    return isPrime;
}

int Number::set_Number()
{
    cout << "Enter a positive integer number greater than 1: \n";
    cin >> number_entered;
    
    return number_entered;
}

void Number::get_Number()
{
    number = number_entered;
}



#include <iostream>
#include "Number.h"
#include "Number2.cpp"

using namespace std;

int main()
{
    Number output;
    
    output.set_Number();
    output.get_Number();
    
    cout << output.display_Prime_Numbers();

    
    return 0;
}


#ifndef NUMBER_H
#define NUMBER_H

#include <iostream>
using namespace std;

class Number
{
    public:
        Number();
        //Number(int);
        //bool isPrime();
        //bool isDivisibleBy(int);
        bool display_Prime_Numbers();
        int set_Number();
        void get_Number();
    private:
        //int value;
        int number;
        int number_entered;
};

#endif // NUMBER_H

Was This Post Helpful? 0
  • +
  • -

#4 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 590
  • View blog
  • Posts: 2,484
  • Joined: 13-June 14

Re: Display Prime Numbers with a Number Class

Posted 07 November 2017 - 01:12 AM

When do you ever initialise the member variable number? Hint: what does your constructor do?
Was This Post Helpful? 1
  • +
  • -

#5 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,318
  • Joined: 20-March 10

Re: Display Prime Numbers with a Number Class

Posted 07 November 2017 - 01:20 AM

Wilson's general theorem for prime numbers is given as

Posted Image

for all n.

taken from

https://en.wikipedia...mula_for_primes

your theorem is not valid.

A prime is a natural positive number that is only divisible by 1 and itself.

taken from here

https://en.wikipedia...ki/Prime_number

have a think on your method.
Was This Post Helpful? 0
  • +
  • -

#6 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 69
  • Joined: 26-September 16

Re: Display Prime Numbers with a Number Class

Posted 07 November 2017 - 01:53 AM

Your isPrime flag seems to be initialized in the wrong scope. Try to fix that. Also, your code can be improved/simplified. I would even venture to say that using a class in your example is a bit of an overkill (unless you're required to do so).

By the way, the 0 is the boolean output of the display_Prime_Numbers member function. I'm not sure you even want that.

(I'm referring to the second version of your code.)

This post has been edited by GazinAtCode: 07 November 2017 - 02:26 AM

Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,472
  • Joined: 08-August 08

Re: Display Prime Numbers with a Number Class

Posted 07 November 2017 - 05:39 AM

View PostKING iMm0rt4lxM, on 06 November 2017 - 10:22 PM, said:

Compiler says I need an argument on display_Prime_Numbers.

Read up on scopes.
Was This Post Helpful? 0
  • +
  • -

#8 KING iMm0rt4lxM  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 08-November 15

Re: Display Prime Numbers with a Number Class

Posted 07 November 2017 - 08:34 AM

Thank you! It seems my prime function was not correct and also my constructor was not initialized. After correcting those 2 things the program ran smooth.

And yes, I'm require to use a class for this particular assignment
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1