Thread: How to print prime numbers from 2 to n?

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    17

    Question How to print prime numbers from 2 to n?

    Hello!

    I have been given the task to write a program that will print prime numbers from 2 to n. The program will allow the user to enter the upper bound n for the range of numbers. It needs to be done using a function call.

    I have tried to break the task down into smaller parts I can work with more easily. My idea was to see if I can make a program that lists integers from 1 to n. Then see if I can make a program that passes in values into a function and get a return value. Then see if I can make a program that will do a primality test.

    Disclaimer:
    To be honest, a certain Indian guy donated the primality test to me. But I fully understand it, and for me, that's the important thing, that's what matters to me.

    Should the teacher accuses me of plagiarism... well... I will tell him to go suck a lemon! Maybe he should start teaching in person or start answering my questions more quickly. It's a distance learning course and I am absolutely not getting anything from the course itself, it's all me working with a dumb 300 page book. I'm a complete beginner.

    I don't mean to trash talk or whine, but he expects too much of me... this is a beginner's course after all. I made my own binary search implementation in the last assignment. The way I see it, it's like being thrown out of an airplane without parachute and be expected to learn to fly on the way down. Fly or die! Code or be executed! Same thing!
    This is what I got so far.

    Listing integers 1 to n:
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        int lowerBound = 1;
        int upperBound;
    
        cout << "Enter upper bound: " << endl;
    
        cin >> upperBound;
    
        int arr[upperBound];
    
        // Setting array values
        for (int i=0; i < upperBound; i++)
        {
            arr[i] = i+1;
        }
    
        // Getting array values
        for (int i = 0; i < upperBound; i++)
        {
            cout << arr[i] << endl;
        }
    
        return 0;
    }
    Primality test:
    Code:
    #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
        int n,i;
        int c=1;
        cout<<"Enter a number: ";
        cin>>n;
        for(i=2;i<n-1;i++)
        {
            if(n%i!=0)
            {
                continue;
            }
            else
            {
                c=0;
            }
        }
        if(c==0)
        {
            cout<<"Number is not prime";
        }
        else
        {
            cout<<"Number is prime";
        }
        return 0;
    }
    Passing values into a function:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int myFunc(int *arr, int arrSize)
    {
        int sum = 0;
    
        for(int i = 0; i < arrSize; i++)
        {
            sum = sum + arr[i];
        }
    
        return sum;
    }
    
    // Enters the program
    int main()
    {
        // Creates an array of 5 integers
        int arr[] = {1, 2, 3, 4, 5};
        int sum = 0;
    
        sum = myFunc(arr, (sizeof(arr)/sizeof(int)));
    
        cout << sum;
    
        // Exits the program
        return 0;
    }
    Before I go any further and start tearing my hair out when I realize I can't combine these, should I take a different approach to this?

    Perhaps I should trash this and start fresh? I just learned that there is something called Sieve of Eratosthenes, and other "sieves" for finding prime numbers. In other words not just testing if a given number is prime, but more directly listing them. I imagine that my original idea would require a lot of rework and nested loops and I would loose myself in code and go insane.

    So what do you think? What should I do? Remember that I'm a beginner, and I can't do all the fancy STL stuff. I'm sure there are toys I could use from that box, but I have to play with the bare basic stuff, on Lego bricks level.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The normal "C" solution to this problem is to write an isPrime function that return a bool value of true if the value passed in the function is prime.

    I would guess that till you learn to write classes that the "C++" solution will be the same as in "C".

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    17
    It's a complete mess! I will probably end up trashing this. But I thought I would just give you an idea of what I was aiming for...

    Code:
    #include<iostream>
    
    using namespace std;
    
    // Testing if the values in array are prime
    int isPrime(int *arr, int arrSize)
    {
        for(int i = 0; i < arrSize; i++)
        {
            // Prime test
            int n,i;
            int c=1;
    //        cout<<"Enter a number: ";
            cin>>n;
            for(i=2;i<n-1;i++)
            {
                if(n%i!=0)
                {
                    continue;
                }
                else
                {
                    c=0;
                }
            }
            if(c==0)
            {
    //            cout<<"Number is not prime";
            }
            else
            {
    //             cout<<"Number is prime";
            }
            return 0;
        }
    }
    
    int main()
    {
        int lowerBound = 2;
        int upperBound;
    
        cout << "Enter upper bound: " << endl;
    
        cin >> upperBound;
    
        int arr[upperBound];
    
        // Setting array values 2 to n
        for (int i=0; i < upperBound; i++)
        {
            arr[i] = i+1;
        }
    
        resultArr[] = isPrime(arr, (sizeof(arr)/sizeof(int)));
    
        // Getting array values 2 to n
        for (int i = 0; i < upperBound; i++)
        {
            cout << resultArr[i] << endl;
        }
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    isPrime normally tests a single input value; trying to test an array is NOT a good idea.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print which numbers that are prime number from 2-100? Logical Error.
    By DecoratorFawn82 in forum C++ Programming
    Replies: 4
    Last Post: 12-01-2015, 03:13 PM
  2. Replies: 3
    Last Post: 12-02-2014, 10:11 AM
  3. Replies: 1
    Last Post: 03-16-2012, 02:07 AM
  4. Replies: 1
    Last Post: 11-04-2011, 01:16 PM
  5. Hi, Can anyone help me? print prime numbers.
    By kelai in forum C Programming
    Replies: 3
    Last Post: 09-29-2010, 01:35 PM

Tags for this Thread