Temperatures puzzle discussion

and what’s wrong there? looks right

Why it doesn’t work?
for (( i=1; i<=n; i++ )); do actual=echo $temps|awk '{print $i}'; echo $actual; done
1 -2 -8 4 5
1 -2 -8 4 5
1 -2 -8 4 5
1 -2 -8 4 5
1 -2 -8 4 5

There’s an error with your code. You need to use double quotes and add a protected dollar sign:

actual=`echo $temps|awk "{print \\$$i}"`

or (still ugly, but slightly better):

actual=$(echo $temps|awk "{print \$$i}")

It works, thanks a lot :slight_smile:

Bonjour,
Je code le programme en C.
Je me demandais s’il existe des fonctions permettant de convertir une chaines de caractères en tableau d’int.
Merci d’avance pour votre aide.
Bien cordialement.

Hello ,I wrote my solution on C++ lang. but i have a problem ,and i cant resolve it :frowning:
Please help ,how can I fix that?
all needed header included.

int main()
{
    int n;
    cin >> n; cin.ignore();
    string temps;
    getline(cin, temps);
int t[1000];
int i=0,max=5526;
char * pch;
  pch = strtok (temps," ,.-"); //Here is my problem :(
  while (pch != NULL)
  {
      t[i]=(int)*pch;
    pch = strtok (NULL, " ,.-");
    i++;
  }
  for(int j=0;j<n;j++) //test ,witch number is the minimal
  {
      if (abs(t[j])<max)
      {max=abs(t[j]);
      }
  }
    cout << max << endl;
}

debug info:

/tmp/Answer.cpp:22:29: error: cannot convert ‘std::string {aka std::basic_string}’ to ‘char*’ for argument ‘1’ to ‘char* strtok(char*, const char*)’
pch = strtok (temps," ,.-");

It’s exactly what it said, you’re trying to use strtok which takes a char* and you give it temps which is a string.

Short answer: temps.c_str() is what you’re looking for. Next time try searching on google “string to char *” and you’d have found it (first link on stackoverflow).

Slightly longer answer: try not to do C inside C++ unless absolutely necessary, you’ll stumble around countless problem like this and be further away from doing regular OOP. It might be a long shot to convert your habits, but it’s well worth it.

Bonjour,
Je code le programme en C.
Je me demandais s’il existe des fonctions permettant de convertir une chaines de caractères en tableau d’int.
Merci d’avance pour votre aide.
Bien cordialement.

English please,

There is no such things as automatic char* to int*, althought there is strsep, strtok and other functions like this that can transform a char * into a char **. Then you have to use int atoi(char *) on each member of the array to transform into int.

You can also code those functions by yourself, to improve your skill on the matter. Also note that while C is a great tool for learning, it lacks a lot of useful methods like the one you’re looking for. C++ is better but not perfect on some regards.

If you’re looking for a less restrictive language, you could try python or ruby which are made for this kind of parsing.

Thank you for your help, I understand better now.
Yes I realize this point with C programming, I’m going to create my own function as you told me. I tried to do this but I have one problem with the function “atoi”. I can apply it on a character which is a number between 0-9. However when I try to convert ‘25’, I cannot use atoi on it.
Consequently this code doesn’t work :

int main(int argc, char const *argv[])
{
char c = ‘25’;
int cInt = atoi(&c);
printf("%d\n",cInt );
return 0;
}

As a matter of fact, how could I handle bit temperatures ?

Thank for giving me a bit of your time, it helps me a lot.

there are several ways to parse a string into array of data, in a way idiomatic for C++.
It gets much easier if you know the number of items to read beforehand:

vector<int> itemps(n);
for (auto &i : itemps) cin >> i;

if you don’t, this might be a good way:

vector<int> itemps;
for (int t; cin >> t; itemps.push_back(t));
1 Like

I’m surprised it compiles.
char takes values ranging from -127 to 128, when you say something like char c = ‘X’ you actually say char c = 88. That means that char can contain ONE character, and only one.

char c = ‘25’ is impossible since simple quote is used to refer to one character only, and since char is also one character only, it doesn’t fit. (maybe it buffer overflow to fit in, or trunk the 5, i need to test to be sure)

If you read the man of atoi, you’ll find that it is described as such:

int atoi(const char *nptr);

It means that you need to provide it a char *, not a char. (const is here to assure you that the char * you pass it won’t be modified)

If you don’t know how to handle pointers, I suggest to learn a little on openclassroom or similar to help you about it, cause it will take a lot more time to explain here.

what you need is this:

char *str = "25";
int cInt = atoi(str);

“25” by itself is a const char*, so by doing char* X = const char *, you just add an alias to “25”. That also means that atoi(“25”); works and will give you the integer 25.

That’s it. By the way “This code doesn’t work” isn’t verbose enough, tell us what is its output, what were you expecting, which error pops up, etc… Here i had to guess, but in some cases it can be difficult to know what you’re trying to do.

Good luck on the puzzle and keep trying.

P.S: You can also try to recode atoi by yourself (around 5-15 lines of codes), it can be a good exercice and help you understand pointers better.

Greetings,

I can see just 6 test cases, but when i submit it seems to be 8…
Anyone can help me?

Same here. Got an 82% because of it… I’m assuming it’s a bug.

To avoid hardcoding tests are differents between ide and submit.

1 Like

Après plusieurs tentatives, je n’arrive pas a voir 100% du test. Je comprend bien qu’il y a une petite différence entre les tests des validateurs et les tests de IDE mais après de nombreux essaies je ne comprend pas le test “-273 alone” ou “5526 alone”.
J’ai réalise pas mal de vérifications (genre si le caractère était bien dans la plage des chiffres ou bien si c’était un signe).

Est il possible de comprendre un peu plus le test?

Alone c’est qu’il n’y a qu’un seul chiffre. Ton code ne doit pas marcher dans ce cas la.

I’ve a problem with this puzzle (C++) : I have splitted temps into a string array (vector), transformed it into int array(vector),I’ve sorted it from the smaller to the bigger but I can’t find how to compare numbers to have the closer from 0.
Does someone has a hint?

problem solved

How can i get an Array of Numbers out of the temps(string) variable?

(C#)

var array = temps.Split(’ ').Select(int.Parse);