GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rating: Thread Rating: 5 votes, 5.00 average.
  #1  
Old 02-May-2004, 18:37
pigninja pigninja is offline
New Member
 
Join Date: Apr 2004
Posts: 10
pigninja is on a distinguished road

so i got this book


i got this book on C programming, the whole teach yourself thing, i've been doing fine all the way up till the for loop concept, i thought i understood it, until i had to create a program involving the for loop. i'm looking for a brief but oxymoronically well explained. the program code that i can't quite grasp goes like this.
CPP / C++ / C Code:
/* Prime number tester. */
#include <stdio.h>

int main(void)
{
  int num, i, is_prime;

  printf("Enter the number to test: ");
  scanf("%d", &num);

  /* now test for factors */
  is_prime = 1;
  for(i=2; i<=num/2; i=i+1)
    if((num%i)==0) is_prime = 0;

  if(is_prime==1) printf("The number is prime.");
  else printf("The number is not prime.");

  return 0;
}
initially when the for loop concept was explained i grasped how
CPP / C++ / C Code:
for(x=1; x<101; x++);
would create integers from 1-100, or so i think that's how it works. then it got a little more complicated and i think i figured those out, but now this i simply can't put the concept together...
  #2  
Old 02-May-2004, 20:17
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Quote:
Originally Posted by pigninja
i got this book on C programming, the whole teach yourself thing, i've been doing fine all the way up till the for loop concept, i thought i understood it, until i had to create a program involving the for loop. i'm looking for a brief but oxymoronically well explained. the program code that i can't quite grasp goes like this.
CPP / C++ / C Code:
/* Prime number tester. */
#include <stdio.h>

int main(void)
{
  int num, i, is_prime;

  printf("Enter the number to test: ");
  scanf("%d", &num);

  /* now test for factors */
  is_prime = 1;
  for(i=2; i<=num/2; i=i+1)
    if((num%i)==0) is_prime = 0;

  if(is_prime==1) printf("The number is prime.");
  else printf("The number is not prime.");

  return 0;
}
initially when the for loop concept was explained i grasped how
CPP / C++ / C Code:
for(x=1; x<101; x++);
would create integers from 1-100, or so i think that's how it works. then it got a little more complicated and i think i figured those out, but now this i simply can't put the concept together...

yes the loop would run from when x=1 to when x became 101, each time incrementing x by 1, but it would not run the 101st time.

this same thing could be expressed as this: for(int x=1;x<=100;x++)

means x starts with value 1, and then increments with each run till it becomes 100. The middle expression means that while x<100 or x=100
keep doing this loop. so once x becomes 100 and passes thru and then is incremented to 101, the loop will cease running. x<101 or x<=100 is essentially the same in this case since x is an integer and is being incremented by 1.

in the above posted code, i starts with the value of 2, and runs till i reaches num/2 and increments each time. i = i+1 is same as i++.
since num is an integer num/2 will always yield an integer result. for example if num was 5, then 5/2 would yield 2, instead of 2.5 because when you perform division on two integer operands, the resultant outcome is an integer since the system takes off any decimal points after the first digit. to yield a decimal result the resultant variable needs to be stored in a floating point number space, e.g. float or double and also atleast one of the two operands need to be floating point e.g. 5/2.0 or 5.0/2 or 5.0/2.0

basically the for loop works this way:
for(variable=value;while loop exit condition not met;perform operation on variable)

in the above case you've seen the variable i being incremented by 1. However, it's not necessary to always increment it by 1. you can say i=i+2 or i=i+3, this way it would add 2 or 3 each run to i. you can also say i-- or i=i-2 or i =i-3. or i = i/3 or i=i*2. just as long as you know that at some point the loop exit condition will be met, you'll be fine.

also, i should point out shorthand notation, since it's quicker. below are short hand notations for a few kinds of operations:
i = i+1 : i++
i = i+4 : i+=4
i = i-1 :i--
i = i-4 :i-=4
i = i*(-1) :i*=(-1)
i = i*4 :i*=4
i = i/4 :i/=4
and so on

you could also run the loop with real numbers:

for(double i=0;i<1;i+=0.01) //runs the loop a hundred times
{}
cout<<i; //i = 1

p.s. someone plz let me know if im absolutely right about this, the for loop still messes me up sometimes as soon as it comes to counting as to how many times it executed
Last edited by machinated : 02-May-2004 at 21:29.
  #3  
Old 02-May-2004, 21:21
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
something peculiar happened while i was testing the above posted for loop repeatedly:

for(double i=0;i<1;i+=0.01) //runs the loop a hundred times
{}
cout<<i; //i = 1

below, the 2 loops produce similar results:

CPP / C++ / C Code:
/*this loop works as expected*/
int x=0;
	for(double i=0;i<1;i+=0.01)
	{	cout<<i<<" "<<x<<" ";
		x++;
	}
	cout<<endl<<i<<" "<<x;  //i displays 1, x displays 100

/*this loop doesnt work as expected, it falls short one execution*/

int x=0;
	for(double i=0;i<=1;i+=0.01)
	{	cout<<i<<" "<<x<<" ";
		x++;
	}
	cout<<endl<<i<<" "<<x; //i still displays 1, x still displays 100
my advice would be to try not to compare decimal numbers for equality unless absolutely necessary, since it may not always work out. In this case, when i reaches 1 or 1.0 and then compares it with 1, the system doesnt recognize them as being equals. It probably has to do with the real numbers being stored differently in the system. 1.0 could be stored as 1.0000000, we consider it as the same but the system probably doesnt, hence when i reaches 1, it doesnt execute the loop once more as it's supposed to, because it thinks i > 1.
  #4  
Old 03-May-2004, 09:30
pigninja pigninja is offline
New Member
 
Join Date: Apr 2004
Posts: 10
pigninja is on a distinguished road
well my problem now is understanding what happens after the loop goes true or false, if it's true according to the book it repeats, and if so what for, then when it's false program execution picks up with the next line of code... the next line of code being what? the if statement? and after all is said and done i still don't understand how the code determines prime numbers...
  #5  
Old 03-May-2004, 09:52
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Then let's clean up the code to make it less ambiguous:
CPP / C++ / C Code:
/* now test for factors */
is_prime = 1;
for (i=2; i<=(num/2); i=i+1)  // loop from 2 up to half of num
{
    if ((num%i) == 0)          // if the remainder of num/i is equal to 0...
    { 
        is_prime = 0;          // set the flag that num is not prime
    }
}
Also, change the i=i+1 term to simply i++. They both work but i++ is the more accepted form.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #6  
Old 03-May-2004, 10:15
pigninja pigninja is offline
New Member
 
Join Date: Apr 2004
Posts: 10
pigninja is on a distinguished road
Quote:
Originally Posted by WaltP
Then let's clean up the code to make it less ambiguous:
CPP / C++ / C Code:
/* now test for factors */
is_prime = 1;
for (i=2; i<=(num/2); i=i+1)  // loop from 2 up to half of num
{
    if ((num%i) == 0)          // if the remainder of num/i is equal to 0...
    { 
        is_prime = 0;          // set the flag that num is not prime
    }
}
Also, change the i=i+1 term to simply i++. They both work but i++ is the more accepted form.

i've been starring at this for over a day, and i just figured it out right before i read this post, but what if the value is prime, i'm still a little blurry and fuzzy on that. why is is_prime = 1, declared at the top of everything, that's the last thing i can't figure out, and i know about i++ i'm just doing things straight from the book, so i didn't bother to change the code..thanks though i'll change it in the future. also when i divide by i say my value for num is 8 am i dividing by 2,3,and 4?
  #7  
Old 03-May-2004, 10:52
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Quote:
Originally Posted by pigninja
i've been starring at this for over a day, and i just figured it out right before i read this post, but what if the value is prime, i'm still a little blurry and fuzzy on that. why is is_prime = 1, declared at the top of everything, that's the last thing i can't figure out, and i know about i++ i'm just doing things straight from the book, so i didn't bother to change the code..thanks though i'll change it in the future. also when i divide by i say my value for num is 8 am i dividing by 2,3,and 4?

even though is_prime is of type int, it is actually being used as a boolean value(true or false, 1 or 0). I think what you have there is a pure c code, which is fine. In c++ there is a boolean type bool which only allows variable of its type to be true or false or 0 or 1. so is_prime is declared true before the loops starts. Sometimes when you declare boolean variables you have to declare them based on what situation demands. Anyway after you enter the loop, yes num is being modularly divided by 2,3,4 and so on each time, and if the remainder is 0,meaning that number goes evenly into num, then is_prime is changed to false. But if no value of i goes into num till i reaches num/2 according to the loop, then is_prime remains 1 as it was declared before the loop.
  #8  
Old 03-May-2004, 11:25
pigninja pigninja is offline
New Member
 
Join Date: Apr 2004
Posts: 10
pigninja is on a distinguished road
Quote:
Originally Posted by machinated
even though is_prime is of type int, it is actually being used as a boolean value(true or false, 1 or 0). I think what you have there is a pure c code, which is fine. In c++ there is a boolean type bool which only allows variable of its type to be true or false or 0 or 1. so is_prime is declared true before the loops starts. Sometimes when you declare boolean variables you have to declare them based on what situation demands. Anyway after you enter the loop, yes num is being modularly divided by 2,3,4 and so on each time, and if the remainder is 0,meaning that number goes evenly into num, then is_prime is changed to false. But if no value of i goes into num till i reaches num/2 according to the loop, then is_prime remains 1 as it was declared before the loop.

so what about the number 8 when 3 goes into it twice with the remainder of 2, what happens to the code?
  #9  
Old 03-May-2004, 11:30
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by pigninja
. why is is_prime = 1, declared at the top of everything, that's the last thing i can't figure out,

Like Machinated said, is_prime is an integer that is being used as a BOOLEAN variable. In this case, the program is assuming that the number is prime before it enters the for loop by setting the value to 1 (true). Then if the number is evenly divisable by any of the test numbers, the boolean is set to 0 (false) and the number is not a prime.
  #10  
Old 03-May-2004, 11:31
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Quote:
Originally Posted by pigninja
so what about the number 8 when 3 goes into it twice with the remainder of 2, what happens to the code?

it doesnt stop after 3 goes into 8, it will actually stop after 4 goes into 8 (since 8/2 = 4 which is stop conditionf or loop) twice with remainder being 0, and at that point, is_prime will evaluate to false or 0

i know its very confusing and a lot to digest, but keep curious and ask any questions you might have, and maintain an interest and don't give up, pretty soon after a few months, it'll all clear up and you'll be very good at it
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
The Traps of Linux...&open source software michael Open Discussion Forum 6 22-May-2004 02:05
need help with passing 3 arrays into a function tommy69 C Programming Language 14 07-Apr-2004 00:22
C++ books for newbie mak90thug C++ Forum 4 04-Feb-2004 14:58
Where do I go now? - Questions about PHP, and etc. PerfectMercy MySQL / PHP Forum 7 11-Sep-2003 05:03
Mfc gmn C++ Forum 4 03-Sep-2003 09:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:17.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.