简体   繁体   中英

strange errors while trying to create a program about Collatz conjecture. How to solve them?

I was writing this program: "program takes a number from the command prompt, and it returns the length of the hailstone sequence". the hailstone sequence is defined as follows:

  • if the number is even, then halve the number (n / 2).
  • if the number is odd, then triple the number, and add one (3 * n + 1).

if the sequence reaches 1, then the algorithm is stopped.

I have written the code this way (note that at the end of each number there must be a comma and after the comma a space, therefore I've printed them like that):

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
    if (argc != 2) {
        return -1; 
    }
    if (argv[1] <= 0) {
        return 0; 
    }
    int n = strtod(argv[1], NULL);
    int length = 0; 
    while (n == 1) {
        if (n % 2 == 0) {
            n /= 2; 
            printf("%d%s", n, ","); 
            ++length; 
            printf("%s", " ");
        }
        else {
            n = (3 * n) + 1; 
            printf("%d%s", n, ","); 
            ++length; 
            printf("%s", " ");
        }
    }
    return length; 
}

I've two errors:

  • while using strtod function, the debugger warned me "conversion from double to int: possible loss of data". why? argv doesn't contain double, but it's a string. And strtod converts between string to decimal; am I wrong?
  • I typed in the terminal "hailstone 3". but the terminal doesn't recognise the command. So I've typed only 3, but the terminal returns 3, without any calculations.

EDIT: I've edited my code like that: int main(int argc, char* argv[]) {

int n = strtol(argv[1], NULL, 10);
    if (n <= 0) {
        return 0;
    }

so, basically, I've used the strtol function and after that I've put the if statement because with that I work with a variable (n) instead of working with string (and I think it's way easier). But nothing changed. (I still have the command line problem, but I no longer have the warning).

strtod is used to convert decimal values, the warning tells you that converting a value to double , and assigning it to an int will result in a loss of the fractional part of the assigned value, in this case nothing would result of it, but you should use strtol instead, anyway.

In your code, since argv[1] is a string and you are comparing it to an int value 0 , the comparison is not correct, use the aforementioned strtol , or compare it to a string, eg const char* str = "0" with something like strcmp (argv[1], str) , though I would prefer the first method.

As for the program launch, try ./hailstone 3 , assuming you give your executable to your program when you compile the source code.

For example, say your compiler is gcc, you'd have to use something like gcc -o hailstone source.c where source.c would be the name of the source code file.

Using Visual Studio IDE, the build result will not be in the project root, you need to navigate to the folder where the file is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM