C
c_beginner
As a mean to improve my C skill for a more of program oriented
I started the acm's problem set.
In the following code the stdin gets the two inputs but the program
does not proceeds further. Please help.
The problem link follows:
http://acm.uva.es/p/v1/100.html
The program:
#include<stdio.h>
#include<limits.h>
#include<stdlib.h>
unsigned int is_odd(unsigned int num)
{
if((num % 2) == 0)
return 0; /* the num is even*/
else
return 1; /* else odd*/
}
int main(void)
{
unsigned int i,j; /*The two numbers*/
unsigned int i_counter=0,j_counter=0; /*calcuting the counters*/
unsigned int cycle_length; /* total cycle lenght*/
fscanf(stdin,"%d%d",&i,&j);
if ( i == (UINT_MAX + 1) || j == (UINT_MAX + 1))
{
exit(1); /*exit on error*/
}
while( i++ && j++ )
{
if(is_odd(i) && is_odd(j))
{
i = (3 * i )+1;
j = (3 * j )+1;
}
else
{
i = i / 2 ;
j = j / 2;
}
i_counter++;
j_counter++;
}
cycle_length = i_counter + j_counter;
printf("%d\n",cycle_length);
return 0;
}
Please guide me to the proper logics.
--
Nothing is stable, even the universe.
By
Hindu God
Mail to :sathyashrayan at gmail dot com
I started the acm's problem set.
In the following code the stdin gets the two inputs but the program
does not proceeds further. Please help.
The problem link follows:
http://acm.uva.es/p/v1/100.html
The program:
#include<stdio.h>
#include<limits.h>
#include<stdlib.h>
unsigned int is_odd(unsigned int num)
{
if((num % 2) == 0)
return 0; /* the num is even*/
else
return 1; /* else odd*/
}
int main(void)
{
unsigned int i,j; /*The two numbers*/
unsigned int i_counter=0,j_counter=0; /*calcuting the counters*/
unsigned int cycle_length; /* total cycle lenght*/
fscanf(stdin,"%d%d",&i,&j);
if ( i == (UINT_MAX + 1) || j == (UINT_MAX + 1))
{
exit(1); /*exit on error*/
}
while( i++ && j++ )
{
if(is_odd(i) && is_odd(j))
{
i = (3 * i )+1;
j = (3 * j )+1;
}
else
{
i = i / 2 ;
j = j / 2;
}
i_counter++;
j_counter++;
}
cycle_length = i_counter + j_counter;
printf("%d\n",cycle_length);
return 0;
}
Please guide me to the proper logics.
--
Nothing is stable, even the universe.
By
Hindu God
Mail to :sathyashrayan at gmail dot com