Help me understand.

This is a discussion on Help me understand. within the C++ Programming forums, part of the General Programming Boards category; Code: // This is the main project file for VC++ application project // generated using an Application Wizard. #include "stdafx.h" ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    41

    Help me understand.

    Code:
    // This is the main project file for VC++ application project
    // generated using an Application Wizard.
    
    #include "stdafx.h"
    #include <iostream>
    
    #using <mscorlib.dll>
    
    using namespace System;
    using namespace std;
    
    bool checkprime(int, int);
    
    int k=3;
    
    
    int _tmain()
    {
           int n;
    	  
           cout << "Input Number: "; cin >> n;
           
           
    
    	   if(checkprime(n,k))
    		   cout << "Yes"<<endl;
    	   else 
    		   cout << "no" <<endl;
    	
    
    		  
    
    return 0;
    
    }
    
    
    bool checkprime(int nv, int kv) {
    	
    	
    	if (nv%2 ==0)
    		return false;
    	
    	if (nv %k ==0){
    		return false;
    		checkprime(nv, k+1);
    	}
    	else
    		return true;
    }
    I wrote this program using recursion. I know what I did for the most part, but I am not clear on when it returns true. The program runs alright and does what its supposed to do. But I didnt set any boundary on k so I am wondering when it breaks out and returns true. If k didnt break then the program wouldnt display the correct answer. Does it break when k =nv? If so, why? Thanks for the help.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The program is broken. A sure sign to find that out is that there is a statement just after "return false" without a block ending.
    Your compiler should have even warned you about unreachable code.

    The program is also very inefficient.

    And you created a Managed C++ Console Project. Don't do that. Create a Win32 Console Application, and in the application settings, check "Empty Project".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I am not clear on when it returns true.
    The program will return true, if it fails the first two tests for being not evenly divisible by two and not evenly divisible by three.




    Also, you might want to re-check this part of the code. Just by running the program and everything seems fine, you might be lead into believing that recursive calls are being made, but this is not the case... because the call to return will break out of the function before the next function call will take place:
    Code:
    if (nv %k ==0){
    		return false;    //The function will stop at this point
    		checkprime(nv, k+1);    // This function will never be called.
    }
    Last edited by The Brain; 05-05-2005 at 12:14 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Apart from the points made above, you should also double check your programs logic. If someone enters a prime number bigger than 2 (eg. 3) it will return false. In fact, this function (without the errors pointed out already) will return false for every number entered, because eventually k will become the same value as nv, and nv % k == nv % nv == 0, so you need to make sure that you stop looping/recursing when hit the number that is one below nv.

    As a side note, you don't need to check for nv % 2 every time you call the function, this is one of the inefficiencies that I'm sure CornedBee was referring to.

    A small suggestion, instead of starting from k = 3 and go upwards, why not start at k = n - 1 and stop when you get to 2?

    And as a big suggestion, do some reading up on the Sieve of Eratosthenes, it might just help you find a faster solution

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    so you need to make sure that you stop looping/recursing when hit the number that is one below nv.
    Better yet, stop when you reach the square root of nv, as nothing above that is of any interest.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 10-14-2006, 04:38 AM
  2. Need software to help to understand C source code
    By Kincider in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2006, 09:44 PM
  3. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  4. Need help to understand x["AC"] in the following code
    By jcourcel in forum C Programming
    Replies: 11
    Last Post: 06-06-2006, 01:13 AM
  5. I don't understand K&R example
    By rllovera in forum C Programming
    Replies: 8
    Last Post: 10-25-2004, 10:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21