LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2011, 12:24 PM   #1
smag
LQ Newbie
 
Registered: May 2011
Location: Linux land
Distribution: Arch linux
Posts: 26

Rep: Reputation: 11
Smile Help in C with the collatz problem.


I have wrote the code in Python and it works, but in C I have some problems.
Can someone help me, please?
This is my code:
Quote:
#include <stdio.h>

int main(void){
int N=0;
for(N=3;N<5;N++){
printf("N: %d",N);
int End=0;
while (End==0){

if(N%2==0){
N=N/2;
}
else if(N==1){
End=1;
}
else{
N=(N*3)+1;
}
}
}
printf("End\n");
return 0;
}
 
Old 09-04-2011, 12:36 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
N will never be 1; thus the app will never exit the while-loop.
 
2 members found this post helpful.
Old 09-04-2011, 01:06 PM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Also, you should be using [code] tags, rather than [quote], to preserve indentation.
 
Old 09-04-2011, 01:44 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
N will never be 1; thus the app will never exit the while-loop.
Actually, if you run it you'll see that it does exit the while loop (3-->10-->5-->16-->8-->4-->2-->1) but since the condition that makes it exit from the while loop is N==1, N is never >=5. So the program certainly "works"; however, it's up to interpretation whether or not "do what I meant, not what I wrote" has been fulfilled.
Kevin Barry

PS It could be that N needs to be copied when used in the loop.

Last edited by ta0kira; 09-04-2011 at 02:11 PM.
 
1 members found this post helpful.
Old 09-04-2011, 02:23 PM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by ta0kira View Post
Actually, if you run it ...
Something for me to consider in the future before I submit a post.
 
Old 09-04-2011, 04:12 PM   #6
smag
LQ Newbie
 
Registered: May 2011
Location: Linux land
Distribution: Arch linux
Posts: 26

Original Poster
Rep: Reputation: 11
Thumbs up

Quote:
Originally Posted by ta0kira View Post
PS It could be that N needs to be copied when used in the loop.
Thanks I found the problem.

Quote:
Originally Posted by Nylex View Post
Also, you should be using code tags, rather than quote, to preserve indentation.
Ok, I will do it.

Last edited by smag; 09-04-2011 at 04:15 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Do I have a path problem, an Apache2 problem or a Javascript problem or any other pro rblampain Linux - Networking 0 12-29-2010 03:50 AM
Problem linking f77 and f90 object files Make problem (I think) TheBrick Programming 1 05-22-2006 06:17 AM
Sound Card problem(every time i install linux i have diffirent hardware problem) jacka1l Linux - Newbie 7 08-11-2005 06:10 AM
Lan configuration problem - NFS boot problem - RX&TX packets errors 242VDM242 Linux - Networking 4 11-25-2004 01:35 PM
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration