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 Rate Thread
  #11  
Old 29-Oct-2004, 14:58
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by jenmaz
Hi there,

I fixed the i variable it was suppose to be n (to my knowledge). I just forgot to change it. I am still weak at looking over these minor mistakes.

I then placed your code

if ( num % n == 0)
{
IsPrim=0;
printf("%d is a divisor of %d\n", n, num);

In the script so that we could see the output.

So when I ran the program and it read these numbers from my input.txt file
6 7 28 18 496 31 24 17 36 3 19 -999

It gave me this out put which shows me that the script is reading the numbers (12 all together and stopping at -999) however it is not out puting properly because n is now = 2 not = to the input number. Then it displays n = 2 and its division and it is the same output for each 12 data imput from the text file.

Here is the output
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
Press any key to continue

I understand now what is going wrong but not sure how to fix it.

Thanks in advance.
jen


So: show us what your Prime() looks like.

General Debugging Hint: before calling a function, put in a printf() to show what the value of the arguments are:

so, in main() you would have something like:

CPP / C++ / C Code:
  printf("Calling Prime() with num = %d\n", num);
  Prime(num);


Then, in Prime(), you would have something like

CPP / C++ / C Code:
  void Prime(int n)
{
  int num, IsPrime;

  printf("In IsPrime: n = %d\n", n);

Get the idea? That makes sure that the function is working on what you think it should be working on.

Next time: don't just describe the problem, please: Show us your code.


Don't say, "I placed your code..." Say, "Here is the function, showing the code that I am using."

Regards,

Dave
  #12  
Old 29-Oct-2004, 15:08
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road

My Prime Function


Thanks Dave,

Here is my function for Prime:

/* For prime */
int Prime(int num)
{
int n, IsPrim;

IsPrim = 1;

for(n = 2; n < num; n++);
if ( num % n == 0)
{
IsPrim=0;
printf("Divisor of prime\n");

}
if(IsPrim)
printf("It is Prim");

return n;
}

Quote:
Originally Posted by davekw7x
So: show us what your Prime() looks like.

General Debugging Hint: before calling a function, put in a printf() to show what the value of the arguments are:

so, in main() you would have something like:

CPP / C++ / C Code:
  printf("Calling Prime() with num = %d\n", num);
  Prime(num);


Then, in Prime(), you would have something like

CPP / C++ / C Code:
  void Prime(int n)
{
  int num, IsPrime;

  printf("In IsPrime: n = %d\n", n);

Get the idea? That makes sure that the function is working on what you think it should be working on.

Next time: don't just describe the problem, please: Show us your code.


Don't say, "I placed your code..." Say, "Here is the function, showing the code that I am using."

Regards,

Dave
  #13  
Old 29-Oct-2004, 15:35
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by jenmaz
Thanks Dave,

Here is my function for Prime:

/* For prime */
int Prime(int num)
{
int n, IsPrim;

IsPrim = 1;

for(n = 2; n < num; n++);
if ( num % n == 0)
{
IsPrim=0;
printf("Divisor of prime\n");

}
if(IsPrim)
printf("It is Prim");

return n;
}


Well, did you follow my suggestion and put the printf() in the main() program before calling Prime() and another printf() inside the Prime() function when it is entered? The idea is that you can debug your own code much faster than waiting for someone else to tell you. (I really don't mind trying to help, but I think you are better served by helping yourself.)

(Please use code tags when posting code: put [c] before the first line of code, and put [/c] after the last line.)

Look here for an explanation: http://www.gidforums.com/t-689.html

Regards,

Dave
  #14  
Old 29-Oct-2004, 15:41
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road
Thanks Dave,

I did place your code in both places and I have been testing and trying to debug my code. Again, from the test I noticed that the loop or the prime function that I have created is missing something because it does not detect the prime. I have also been surfing the net for 2 hours trying to look at prime detection formulas and can't seem to get it. I apologize if you are frustrated and thank you for your time. I will figure out some how. I am new to this so I guess I will keep tackling it.

jennifer
Quote:
Originally Posted by davekw7x
Well, did you follow my suggestion and put the printf() in the main() program before calling Prime() and another printf() inside the Prime() function when it is entered? The idea is that you can debug your own code much faster than waiting for someone else to tell you. (I really don't mind trying to help, but I think you are better served by helping yourself.)

(Please use code tags when posting code: put [c] before the first line of code, and put [/c] after the last line.)

Look here for an explanation: http://www.gidforums.com/t-689.html

Regards,

Dave
  #15  
Old 29-Oct-2004, 15:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by jenmaz
Thanks Dave,

I did place your code in both places and I have been testing and trying to debug my code. Again, from the test I noticed that the loop or the prime function that I have created is missing something because it does not detect the prime. I have also been surfing the net for 2 hours trying to look at prime detection formulas and can't seem to get it. I apologize if you are frustrated and thank you for your time. I will figure out some how. I am new to this so I guess I will keep tackling it.

jennifer

My point was that I don't see anything wrong with your Prime() function, but that doesn't have the printf() statement. Show me the code that you are running (including main() and its call to Prime). There is nothing fundamentally wrong with your Prime() detection function. There is some small detail that I can't possibly help you with unless I see what you are actually running.

Regards,

Dave
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

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
Two virtual hosts, cgi script behaves differently on each blimbo Apache Web Server Forum 0 04-Aug-2004 10:35
Where can I find web statistics script / software? rhino1616 Web Design Forum 2 02-Jan-2004 21:31
Redirect multiple URLs to one script, masking true URL Maccaday Apache Web Server Forum 1 10-Dec-2003 09:34
JavaScript Tutorial Part 1 pcxgamer Web Design Forum 2 01-Dec-2003 10:16
VALIDATING file types on an PHP upload script? JdS MySQL / PHP Forum 0 02-Jan-2003 08:58

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

All times are GMT -6. The time now is 20:24.


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