Dear group,
The below given link which I want to implement in C. http://online-judge.uva.es/p/v1/100.html
The code which I wrote does not even reaches the near to the
given problem.(I am just learning)
#include<stdio.h>
#include<stdlib.h>
unsigned evn,odd;
void odd_evn(unsigned int number)
{
if((number % 2) == 0)
{
evn = 1;
odd = 0;
}
evn = 0;
odd = 1;
}
int main(int argv, char *argc[])
{
unsigned long n;
unsigned int i,j;
int counter=0;
if(argv == 0 && argv >=3)
{
printf("eror\n");
exit(EXIT_FAILURE);
}
i=atoi(argc[1]);
j=atoi(argc[2]);
for(n=i; n<j;n++)
{
odd_evn(n);
if(odd == 1 && evn == 0)
n = (3 * n) + 1;
if(odd == 0 && evn == 1)
n = n / 2;
counter++;
printf("%lu and %d\n",n, counter);
}
return 0;
}
Once the n gets the even number it just divides it by two
and, at the end n reaches with even number and terminates.
Can any one give me some clue or hint for correct implementation.
of the above? 15 3748
"sathyashrayan" writes: The below given link which I want to implement in C.
http://online-judge.uva.es/p/v1/100.html
The code which I wrote does not even reaches the near to the given problem.(I am just learning)
#include<stdio.h> #include<stdlib.h>
unsigned evn,odd;
void odd_evn(unsigned int number) { if((number % 2) == 0) { evn = 1; odd = 0; } evn = 0; odd = 1; }
The function above does nothing except use up some time. change it to:
int odd(unsigned int number)
and then return 1 to signify odd. A name such as odd/even is simply
annoying. Which is it? odd? Even? int main(int argv, char *argc[]) { unsigned long n; unsigned int i,j; int counter=0; if(argv == 0 && argv >=3) { printf("eror\n"); exit(EXIT_FAILURE); }
i=atoi(argc[1]); j=atoi(argc[2]); for(n=i; n<j;n++) { odd_evn(n); if(odd == 1 && evn == 0)
Don't do this belt and suspenders thing. Write
if(odd( ...) )
n = (3 * n) + 1; if(odd == 0 && evn == 1) n = n / 2; counter++; printf("%lu and %d\n",n, counter); } return 0; }
Once the n gets the even number it just divides it by two and, at the end n reaches with even number and terminates. Can any one give me some clue or hint for correct implementation. of the above?
I don't know if that is all you need to make it work. Doing things wrong is
a huge part of the learning process so being helpful is not, in case like
this, very helpful.
"osmium" writes: The below given link which I want to implement in C.
http://online-judge.uva.es/p/v1/100.html
The code which I wrote does not even reaches the near to the given problem.(I am just learning)
#include<stdio.h> #include<stdlib.h>
unsigned evn,odd;
void odd_evn(unsigned int number) { if((number % 2) == 0) { evn = 1; odd = 0; } evn = 0; odd = 1; }
The function above does nothing except use up some time. change it to:
int odd(unsigned int number)
and then return 1 to signify odd. A name such as odd/even is simply annoying. Which is it? odd? Even?
int main(int argv, char *argc[]) { unsigned long n; unsigned int i,j; int counter=0; if(argv == 0 && argv >=3) { printf("eror\n"); exit(EXIT_FAILURE); }
i=atoi(argc[1]); j=atoi(argc[2]); for(n=i; n<j;n++) { odd_evn(n); if(odd == 1 && evn == 0)
Don't do this belt and suspenders thing. Write
if(odd( ...) )
n = (3 * n) + 1; if(odd == 0 && evn == 1) n = n / 2; counter++; printf("%lu and %d\n",n, counter); } return 0; }
Once the n gets the even number it just divides it by two and, at the end n reaches with even number and terminates. Can any one give me some clue or hint for correct implementation. of the above?
I don't know if that is all you need to make it work. Doing things wrong is a huge part of the learning process so being helpful is not, in case like this, very helpful.
That post is utter nonsense. I didn't see the global variables. Ignore
everything I said except the part about a bad name.
"sathyashrayan" wrote: The below given link which I want to implement in C.
http://online-judge.uva.es/p/v1/100.html
The code which I wrote does not even reaches the near to the given problem.(I am just learning)
#include<stdio.h> #include<stdlib.h>
unsigned evn,odd;
void odd_evn(unsigned int number) { if((number % 2) == 0) { evn = 1; odd = 0; } evn = 0; odd = 1; }
int main(int argv, char *argc[]) { unsigned long n; unsigned int i,j; int counter=0; if(argv == 0 && argv >=3)
How can any number be equal to 0 and greater than 3?
<snip>
sathyashrayan schrieb: Dear group,
The below given link which I want to implement in C.
http://online-judge.uva.es/p/v1/100.html
The code which I wrote does not even reaches the near to the given problem.(I am just learning)
#include<stdio.h> #include<stdlib.h>
unsigned evn,odd;
void odd_evn(unsigned int number) { if((number % 2) == 0) { evn = 1; odd = 0; } evn = 0; odd = 1;
This code always executes the last two statements.
Either put this part into an else branch or directly compute
evn and odd via
odd = ((number % 2) != 0);
evn = 1 - odd;
or similar.
}
int main(int argv, char *argc[])
You changed the usually used main parameter identifiers.
It's
int main (int argc, char **argv)
This is perfectly legal but may irritate or confuse people
reading your code.
{ unsigned long n; unsigned int i,j;
unsigned int has a minimal maximum value of 65535 -- this
is not enough for the task at hand. Use unsigned long values.
int counter=0; if(argv == 0 && argv >=3)
This is never true.
Make it
if (argv != 3) { printf("eror\n"); exit(EXIT_FAILURE); }
i=atoi(argc[1]); j=atoi(argc[2]);
atoi() is not exactly the safest input function; in addition,
it does not guaranteedly cover the range discussed. Read up on
the use of strtoul().
for(n=i; n<j;n++) {
Within this loop, you are supposed to determine the cycle length
for every number from i to j, including i and j.
odd_evn(n); if(odd == 1 && evn == 0) n = (3 * n) + 1; if(odd == 0 && evn == 1) n = n / 2;
What a mess.
You obviously do not need evn at all as evn always is 1-odd;
this is the second, unnecessary, check you are performing for
every if.
In addition, the two if conditions are mutually exclusive.
if (odd)
n = (3 * n) + 1;
else
n = n/2;
counter++; printf("%lu and %d\n",n, counter);
You misunderstood the task.
} return 0; }
Once the n gets the even number it just divides it by two and, at the end n reaches with even number and terminates. Can any one give me some clue or hint for correct implementation. of the above?
unsigned long max = 0;
unsigned long index;
unsigned long cycle_length;
for (index = i; index <= j; index++) {
cycle_length = determine_cycle_length(index);
if (cycle_length > max) {
max = cycle_length;
}
}
printf("%lu %lu %lu\n", i, j, max);
matches the requirements from the link. Now just implement
determine_cycle_length along the given algorithm.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
On Sun, 30 Apr 2006 17:04:15 UTC, "sathyashrayan"
<sa***********@REMOVEgmail.com> wrote: Dear group,
The below given link which I want to implement in C.
http://online-judge.uva.es/p/v1/100.html
The code which I wrote does not even reaches the near to the given problem.(I am just learning)
#include<stdio.h> #include<stdlib.h>
unsigned evn,odd;
void odd_evn(unsigned int number) { if((number % 2) == 0) { evn = 1; odd = 0; }
Why does you destroy the set you've made when the if is true? When you
remove the whole if block there would be noch change on the result in
any case.
Hint: for what does else exist?
evn = 0; odd = 1; }
int main(int argv, char *argc[]) { unsigned long n; unsigned int i,j; int counter=0; if(argv == 0 && argv >=3) { printf("eror\n"); exit(EXIT_FAILURE); }
i=atoi(argc[1]); j=atoi(argc[2]); for(n=i; n<j;n++) { odd_evn(n); if(odd == 1 && evn == 0) n = (3 * n) + 1; if(odd == 0 && evn == 1) n = n / 2; counter++; printf("%lu and %d\n",n, counter); } return 0; }
Once the n gets the even number it just divides it by two and, at the end n reaches with even number and terminates. Can any one give me some clue or hint for correct implementation. of the above?
--
Tschau/Bye
Herbert
Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
sathyashrayan wrote: Dear group,
The below given link which I want to implement in C.
http://online-judge.uva.es/p/v1/100.html
The code which I wrote does not even reaches the near to the given problem.(I am just learning)
[..] Once the n gets the even number it just divides it by two and, at the end n reaches with even number and terminates. Can any one give me some clue or hint for correct implementation. of the above?
this seems to work..
#include <stdio.h>
int is_odd (int n)
{
return n%2;
}
int main (void)
{
int number;
scanf("%d", &number);
if (number > 0) {
while (1) {
printf("%d ", number);
if (number == 1) {
break;
}
if (is_odd(number)) {
number = 3 * number + 1;
} else {
number = number / 2;
}
}
}
exit(0);
}
-- www.iuz-lab.info
In article <44***********************@reader2.news.tin.it>,
iuz <us**@host.network> wrote: this seems to work.. #include <stdio.h>
while (1) { printf("%d ", number);
You never output a \n . Your output is not certain to appear at
all, and if it does appear it might get erased by the next command
prompt.
--
All is vanity. -- Ecclesiastes
sathyashrayan wrote: Dear group,
The below given link which I want to implement in C.
http://online-judge.uva.es/p/v1/100.html
The code which I wrote does not even reaches the near to the given problem.(I am just learning)
<snip> Can any one give me some clue or hint for correct implementation. of the above?
Hmmm....
"The input will consist of a series of pairs of integers i and j, one
pair
of integers per line. All integers will be less than 1,000,000 and
greater than 0. ...
"You can assume that no opperation overflows a 32-bit integer. "
You know what they say about _ass_u_me_ don't you... ;-)
% type 3n1.c
#include <stdio.h>
#include <stdlib.h>
#if 0
#define PRINT printf
#else
#define PRINT while (0) printf
#endif
int main(int argc, char **argv)
{
unsigned long i, j, input, n;
unsigned long c, mc = 0;
if (argc != 3) { return 0; }
i = strtoul(argv[1], 0, 10);
j = strtoul(argv[2], 0, 10);
if (i == 0) { return EXIT_FAILURE; }
for (input = i; input <= j; input++)
{
n = input;
PRINT("%lu -\n", n);
c = 1;
while (n != 1)
{
if ((n % 2) == 0) /* even? */
n /= 2;
else if (n <= (-1UL - 1) / 3) /* 'safe' odd? */
n = n * 3 + 1;
else /* overflow! */
{
printf("\noverflow");
printf(": input = %lu", input);
printf(", cycle = %lu", c);
printf(", n = %lu\n", n);
exit(EXIT_FAILURE);
}
PRINT(" %lu\n", n);
c++;
}
PRINT(" cycle(s):%lu\n", c);
if (c > mc) mc = c;
}
printf("%lu %lu %lu\n", i, j, mc);
return 0;
}
% acc 3n1.c -o 3n1.exe
% 3n1.exe 1 10
1 10 20
% 3n1.exe 100 200
100 200 125
% 3n1.exe 201 210
201 210 89
% 3n1.exe 900 1000
900 1000 174
% 3n1.exe 1 999999
overflow: input = 159487, cycle = 60, n = 1699000271
%
Peter Nilsson said: sathyashrayan wrote: Dear group,
The below given link which I want to implement in C.
http://online-judge.uva.es/p/v1/100.html
The code which I wrote does not even reaches the near to the given problem.(I am just learning) <snip> Can any one give me some clue or hint for correct implementation. of the above?
Hmmm....
"The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0. ...
"You can assume that no opperation overflows a 32-bit integer. "
You know what they say about _ass_u_me_ don't you... ;-)
<snip> % 3n1.exe 1 999999
overflow: input = 159487, cycle = 60, n = 1699000271
In this case, it would appear that you (yes, you, Peter!) assumed that the
ACM will provide arbitrary input. Have you not considered the possibility
that they might have thought of that already, and filtered out such inputs
from their online judge's test data stream?
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Walter Roberson wrote: In article <44***********************@reader2.news.tin.it>, iuz <us**@host.network> wrote:
this seems to work.. #include <stdio.h>
while (1) { printf("%d ", number);
You never output a \n . Your output is not certain to appear at all, and if it does appear it might get erased by the next command prompt.
really? i didn't know..
can you tell me in which cases omitting the new line can give this kind of
problem?
-- www.iuz-lab.info
iuz schrieb: Walter Roberson wrote:In article <44***********************@reader2.news.tin.it>, iuz <us**@host.network> wrote:
this seems to work.. #include <stdio.h>
while (1) { printf("%d ", number);
You never output a \n . Your output is not certain to appear at all, and if it does appear it might get erased by the next command prompt.
really? i didn't know.. can you tell me in which cases omitting the new line can give this kind of problem?
The _last_ output of your programme should be a newline character.
In the following, I assume that you work with a console:
Most of the time, you only will see something like
lastOuputNextPrompt >
instead of
lastOutput
NextPrompt >
but it is also possible that you only see
NextPrompt >
If "lastOutput" is the only information given by your programme and
output after three days of non-stop computation, the latter case is
rather annoying...
If you do not work with a run-of-the-mill console or not a console
at all, then your IO may be line based and input or output happens
only for '\n'-terminated character sequences.
So, you are on the safe side if you append an '\n'. No disadvantages.
Note: You can force output using fflush().
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Thanks for all the help that this group provides.
"sathyashrayan" <sa***********@REMOVEgmail.com> wrote in message
news:44***********************@news.sunsite.dk... Dear group,
The below given link which I want to implement in C.
http://online-judge.uva.es/p/v1/100.html
The code which I wrote does not even reaches the near to the given problem.(I am just learning)
#include<stdio.h> #include<stdlib.h>
unsigned evn,odd;
void odd_evn(unsigned int number) { if((number % 2) == 0) { evn = 1; odd = 0; } evn = 0; odd = 1; }
int main(int argv, char *argc[]) { unsigned long n; unsigned int i,j; int counter=0; if(argv == 0 && argv >=3) { printf("eror\n"); exit(EXIT_FAILURE); }
i=atoi(argc[1]); j=atoi(argc[2]); for(n=i; n<j;n++) { odd_evn(n); if(odd == 1 && evn == 0) n = (3 * n) + 1; if(odd == 0 && evn == 1) n = n / 2; counter++; printf("%lu and %d\n",n, counter); } return 0; }
Once the n gets the even number it just divides it by two and, at the end n reaches with even number and terminates. Can any one give me some clue or hint for correct implementation. of the above?
Michael Mair wrote:
[..] If you do not work with a run-of-the-mill console or not a console at all, then your IO may be line based and input or output happens only for '\n'-terminated character sequences. So, you are on the safe side if you append an '\n'. No disadvantages.
Note: You can force output using fflush().
[..]
thanks..
-- www.iuz-lab.info
On 2006-05-01, iuz <us**@host.network> wrote: Walter Roberson wrote:
In article <44***********************@reader2.news.tin.it>, iuz <us**@host.network> wrote:
this seems to work.. #include <stdio.h>
while (1) { printf("%d ", number);
You never output a \n . Your output is not certain to appear at all, and if it does appear it might get erased by the next command prompt.
really? i didn't know.. can you tell me in which cases omitting the new line can give this kind of problem?
On my system, if there is no newline at the end of the last line of
output, the cursor will remain at the end of the line. My shell clears
the line and returns the cursor to the left edge before printing its
prompt.
Richard Heathfield wrote: Peter Nilsson said: sathyashrayan wrote: http://online-judge.uva.es/p/v1/100.html Hmmm....
"The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0. ...
"You can assume that no opperation overflows a 32-bit integer."
You know what they say about _ass_u_me_ don't you... ;-) <snip> % 3n1.exe 1 999999
overflow: input = 159487, cycle = 60, n = 1699000271
In this case, it would appear that you (yes, you, Peter!) assumed that the ACM will provide arbitrary input.
No, I tested that my reading of ACM's assertion is correct
for the specified input range. I found it not to be the case.
In any case, the notion that a C program can make wholesale
assumptions about the input it receives is dangerous.
Have you not considered the possibility that they might have thought of that already, and filtered out such inputs from their online judge's test data stream?
If ACM were to specify that no input line is longer than say
60 characters, would that mean that it's okay to use gets()?
Just because someone tells you that the input they supply
will not cause problems is no reason to take that input or
their assertion for granted.
--
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rusty Shackleford |
last post by:
Hi -
I'm studying algorithms and I want to write a python program that
calculates the actual runtimes.
I want to increment up some global variable called n in my program so
that I can see the...
|
by: c_beginner |
last post by:
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....
|
by: dd |
last post by:
testtestest
|
by: Markus Svilans |
last post by:
Hi,
There seems to be some functionality missing from the STL.
I am iterating through a linked list (std::list) using a reverse
iterator and attempting to erase certain items from the list. It...
|
by: Nobody |
last post by:
I've been looking for a job for a while now, and have run into this
interview question twice now... and have stupidly kind of blown it twice...
(although I've gotten better)... time to finally...
|
by: Gabriel Genellina |
last post by:
En Wed, 16 Apr 2008 07:37:55 -0300, Good Z <goodz158@yahoo.comescribió:
If the Java class implements Base64 as defined in RFC 3548, it should be compatible with the Python base64 module.
Try...
|
by: xahlee |
last post by:
I'd like to introduce a blog post by Stephen Wolfram, on the design
process of Mathematica. In particular, he touches on the importance of
naming of functions.
• Ten Thousand Hours of Design...
|
by: John Salerno |
last post by:
Is it possible to write a list comprehension for this so as to produce a
list of two-item tuples?
base_scores = range(8, 19)
score_costs =
print zip(base_scores, score_costs)
I can't think...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |