Hi everyone this is my first post on these forums and I hope that someone can clear up a simple question for me...Right now I am reading Ivor Horton's Beginning Visual c++ 2008 and in chapter four he gives an example program for finding prime numbers using pointers and a simple algorithm. I understand the program and I even rewrote the part that I didn't understand and it still works now. But I have a question regarding 3 lines of code in his version, I don't fully understand what they are doing. I hope someone can clear this up. Here goes...

#include "stdafx.h"
#include <iostream>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::setw;


int _tmain(int argc, _TCHAR* argv[])
{	

	const int MAX  = 100;									             
	long primes[MAX] = {2,3,5};								                  
	long trial = 5;											                  
	int count = 3;											                
	int found = 0;											                 

	do 
	{
		trial +=2;											
		found = 0;											
		for(int i = 0; i < count; i++)						
		{
			found = (trial % *(primes + i)) == 0;			
			if(found)										
				break;									

			
			/* Heres how I rewrote it
			found = (trial % *(primes + i));			
			
			if(found == 0)
			{
				found =1;
				
			break;
			}
			else 
				found = 0;
			*/
		}
		if (found == 0)									
			*(primes + count++) = trial;				
	}while(count < MAX);
	for(int i = 0; i < MAX; i++)
	{
		if(i % 5 == 0)									
			cout << endl;
		cout << setw(10) << *(primes + i);

	}
	cout << endl;
	return 0;
}

Barf, I sat here for 20 min trying to get the "code" and "text" tags to display correctly, no such luck. Never had this problem on any other website :( Ohh well. Maybe its just a display issue with my browser. Any ways if you click the plain text option it seems to display correctly.

I accidently hit the submit button I was about to give up. I didn't even get across what I wanted to ask about in post.

Anyways so if someone takes to the time to read this the problem I wanted to ask about was the three lines of code that are as follows

found = (trial % *(primes + i)) == 0;
         if(found)
            break;

not really sure what the above code means I think it translates to this

found = (trial % *(primes + i);
if(found == 0)
	{
		found =1;
				
		break;
	}
	else 
		found = 0;

You've got the general idea.

The % sign is the modulo (or remainder) operator. So what it is saying is:

"If the number I am looking at (trial) divides evenly by the indexed prime (which should have been written as primes[ [B][/B]i[B][/B] ] to be less confusing), then we have found a composite number."

Hope this helps.

I guess what i really didn't understand was the
== 0
on the end of the first line and the following line
If(found)

I guess what is confusing me is the assignment and test for equality all on the same line and then the following line
If(found)
I don't understand what that line is doing.

I guess what i really didn't understand was the
== 0
on the end of the first line and the following line
If(found)

I guess what is confusing me is the assignment and test for equality all on the same line and then the following line
If(found)
I don't understand what that line is doing.

Typically conditions (in C++) can be measured as true (1) or false (0) in a boolean expression. For example...

while(1){} //infinite while loop, 1 equals true.

if(found){} //if found equals zero, then this statement wont run.

I try to avoid these kind of conditions when I code. Whenever I want to make a statement like the above I'll usually do something like..

if(found != 0){} //much more readable

Consider the following example--

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    if(1) //evaluates to true so it will run
    {
         cout << "true!" << endl;
    }
    
    if(0) //evaluates to false, so it wont run
    {
         cout << "false" << endl;
    }
    
    if(-1) //it's not zero - most likely will run
    {
          cout << "?" << endl;
    }
    
    cin.get();
    return 0;
}

--if I'm missing something please let me know.

(Shifty eyes at Narue >_>)

I agree. If it isn't a boolean value, I (almost) always state the condition explicitly.

C, and consequently C++, lend themselves to a lot of "shortcuts" in style. After a while you will get used to looking at the funny jumble of punctuation and understand it easily enough.

Some people, however, like to take it waaay past readability. Ever hear of the IOCCC? It's enough to make anyone's head swim.

Sure is fun though. :)

I agree. If it isn't a boolean value, I (almost) always state the condition explicitly.

C, and consequently C++, lend themselves to a lot of "shortcuts" in style. After a while you will get used to looking at the funny jumble of punctuation and understand it easily enough.

Some people, however, like to take it waaay past readability. Ever hear of the IOCCC? It's enough to make anyone's head swim.

Sure is fun though. :)

I noticed that programming consists of many challenges...

-Besides writing code, you must be able to use the code with practical applications.
-Besides writing code, you must relate the system you're using/building to something tangible.
-Making code precise, readable, modifiable (OOP), etc...
-Being able to understand how to implement things on a business level, technical level and realistic level...

the list goes on... but I am not discouraged. Making code readable is definitely a challenge in my opinion since everyone has their own style and way of seeing things.

One could say "sure my code may not be as readable as yours, but if someone isn't experienced enough to understand my code then they really shouldn't be reading it!"

Of course I disagree. I think code should first be made readable. If code can be shortened later and you're not working on the project with others then compress it to your delight.

This is an opinion coming from a rookie programmer, still working towards my CS degree.

Awesome! Thanks guys, I appreciate the help.

Ok Here is what I have learned...

found = (trial % *(primes + i)) ==  0;
if(found)
break;

This above code essentially does this
1. Tests to see if the variable trial divides evenly into the next prime number in the array
2. It next checks to see if found is equal to zero via. the == 0 statement.
3. It next stores the result in the variable found which that result can either be a 1 or 0 for true and false respectively.
4.Next the statement if(found) causes the loop to break because it evaluated to true or 1 and this means the number can't be prime.

So my final question is
Does if(found) evaluate to 1 because there is no test inside the parentheses and a statement like this defaults to true rather than false? So this if(found) is like saying if(1) because found "is" I think a better question would be, can the statement if(found) ever evaluate to 0?

I'm really sorry if these questions seem stupid. But Your guys help is truly appreciated.

You only made one error:
1. Tests to see if the variable trial is evenly divisible by the next prime number in the array
2. yep
3. yep
4. yep

C++ has a native boolean type (bool, of course) but in C-related languages any non-zero value is considered true. Hence, saying: if (foo) is exactly the same as saying: if (foo != 0) (or in C++: if (foo != false) ).

Hope this helps.

Awesome! Thx so much guys. I learned a lot from your quick replies and thx for taking the time to explain the nuances of the language to me. The book I'm reading never explained that if(foo) is the same as saying if (foo !=0) or if (foo != false). I thought that's what it meant but I only sort of figured it out through process of elimination. Again, thx a bunch Alex and Duoas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.