Thread: Jumping Into C++ Sample Code Help

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    4

    Jumping Into C++ Sample Code Help

    Not really understanding the code from Chapter 7, the program lists all prime numbers from 1-100.

    Code:
    #include <iostream>
    
    
    bool isDivisible (int number, int divisor);
    bool isPrime (int number);
    
    
    using namespace std;
    
    
    int main ()
    {
        for ( int i = 0; i < 100; i++ )
        {
            if ( isPrime( i ) )
            {
                cout << i << endl;
            }
        } 
    }
    
    
    bool isPrime (int number)
    {
        for ( int i = 2; i < number; i++)
        {
            if ( isDivisible( number, i  ) )
            {
                return false;
            }
        }
        return true;
    }
    
    
    bool isDivisible (int number, int divisor)
    {
        return number % divisor == 0;
    }
    I don't understand how it works, if someone could briefly describe what each function does, that would help considerably.

    Thanks in advance,
    -lamb
    Last edited by lamb; 08-28-2014 at 07:26 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,036
    Read this to understand the isDivisible function.
    Modulus Operator in C and C++ - Programming Tutorials - Cprogramming.com

    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by lamb View Post
    I don't understand how it works, if someone could briefly describe what each function does, that would help considerably.
    I dunno what to say. These functions are as easy as they come. What's the problem?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,658
    Add some print statements all over the place, eg
    Code:
    int main ()
    {
        for ( int i = 0; i < 100; i++ )
        {
            cout << "for loop i = " << i << endl;
            if ( isPrime( i ) )
            {
                cout << i << endl;
            }
        }
    }
    Run the code, watch your print statements.

    Or run the code in a debugger (consider this an excellent opportunity to learn about this essential tool). Use the debugger to single step the code so you can follow the program flow line by line. Examine the values of variables as you go.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    The algorithm is wrong as it prints out numbers 0 and 1 which are not prime.

  6. #6
    Registered User
    Join Date
    Aug 2014
    Posts
    4
    Quote Originally Posted by Elysia View Post
    I dunno what to say. These functions are as easy as they come. What's the problem?
    Code:
    #include <iostream>
    
    
    bool isDivisible (int number, int divisor);
    bool isPrime (int number);
    
    
    using namespace std;
    
    
    int main ()
    {
    	for ( int i = 0; i < 100; i++ ) //Cycles through all values from 1-100.
    	{
    		if ( isPrime( i ) ) //If it's prime...
    		{
    			cout << i << endl;//Print it!
    		}
    	}
    }
    
    
    bool isPrime (int number)
    {
    	for ( int i = 2; i < number; i++)
    	{
    		if ( isDivisible( number, i  ) ) //If the two numbers are divisible...
    		{
    			return false; //I don't care about them!
    		}
    	}
    	return true; //If they're not, tell me!
    }
    
    
    bool isDivisible (int number, int divisor)
    {
    	return number % divisor == 0; //Only give values that have no remainder(are divisible).
    }
    So I added comments to show you all my interpretation of the code, what I don't understand is, by the looks of it, the variable 'i'(in main) only gets checked for whether or not it's prime while it is greater than the variable 'i'(in isPrime) and as long 'i'(in main) becomes greater than 'i'(in isPrime), the 'i'(in isPrime) increases by one.

    So my question is, how is the program checking the 'i'(in main) by every possible number from 2-100 when the 'i'(in isPrime) is constantly increasing directly with the 'i'(in main).

    For instance when 'i'(in main) = 5, won't 'i'(in isPrime) be 5, since 'i'(in isPrime) will increment up to the 'i'(in main)'s value in order to fulfill the loop condition it? Making 5 % 5 have no remainder making the function isPrime return a false value? Also the 'i'(in main) value would never get checked by the values of 'i'(in isPrime) that are below it's current value. For instance wouldn't 5 never get checked by 4 since the loop is constantly increasing?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Keep in mind that in main and isPrime, i is a local variable. Therefore, the i from main and the i from isPrime are not related. They are different variables that happen to have the same name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Aug 2014
    Posts
    4
    Code:
    bool isPrime (int number)
    {
        for ( int i = 2; i < number; i++)
        {
            if ( isDivisible( number, i  ) ) //If the two numbers are divisible...
            {
                return false; //I don't care about them!
            }
        }
        return true; //If they're not, tell me!
    }
    Wait so does 'i' start at 2 every time the function is given a new 'number' value?

    So like after 'i'(in main aka 'number' in isPrime) is tested @ 5 and moves to 6, will the 'i'(in isPrime) start back at 2 in order to test all values below 5, like this?

    TESTING 5 (RESTART)
    5/2 5/3 5/4 = PRIME
    TESTING 6 (RESTART)
    6/2 = COMPOSITE
    TESTING 7 (RESTART)
    7/2 7/3 7/4 7/5 7/6 = PRIME
    Last edited by lamb; 08-29-2014 at 02:41 PM. Reason: Clarification

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    "i" starts at 2 every time the function is called. Keep in mind that it's IsPrime's "i", and not any other "i"s (say, main's). Locals variables may have the same names as variables in other functions, but they are in no way related.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Aug 2014
    Posts
    4
    Quote Originally Posted by Elysia View Post
    "i" starts at 2 every time the function is called. Keep in mind that it's IsPrime's "i", and not any other "i"s (say, main's). Locals variables may have the same names as variables in other functions, but they are in no way related.
    Ah thank-you, I'm aware of the difference between variables of the same name in different functions, just wasn't sure on the starting at 2 each time a new input is entered.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with sample code 36 from Jumping into C++
    By Alexi Vuckovich in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2014, 12:35 PM
  2. Sample .cpp & .h code
    By wriimage in forum Windows Programming
    Replies: 7
    Last Post: 02-23-2013, 03:19 PM
  3. How to get the sample code?
    By krishnampkkm in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2009, 04:41 AM
  4. Jumping in the code...
    By AdampqmabA in forum C++ Programming
    Replies: 24
    Last Post: 10-06-2008, 05:34 PM
  5. SMS sample code
    By hagenuk in forum C Programming
    Replies: 1
    Last Post: 05-30-2008, 11:47 AM