Temperatures puzzle discussion

#475
``````You are given a number N and a string (TEMPS) that looks like "1 2 3 -7 5526 -273", here N = 6;
there are 6 temperatures to evaluate.

You need to convert the string into an array ["1","2","3","-7","5526","-273"] (split)
Then [1,2,3,-7,5526,-273] (parseInt)

And then use a for-loop (0->N) to  find the closest to 0, here 1.``````

#476

Hi, I am attempting to convert the char array to ints using C and strtol. I’ve gotten close but I am running into issues since the array contains a \n (10) character. It cannot seem to figure out how to tell it to end before the \n.

Conversion code:

``````char *end;
arr[0] = strtol(temps, &end, 10);
for(int i = 1; i < n - 1; i++){
arr[i] = strtol(end, &end, 10);
}
arr[n] = strtol(end, NULL, 10);
``````

Output:

``````> 49 32 45 50 32 45 56 32 52 32 53 10 // ascii values
> 1 -2 -8 4 5 // character array
> 1 -2 -8 4 1 // int array
``````

Could anyone offer some tips? Thank you.

#477

Should be : `arr[n-1] = ...`

#478

But i have trouble with temperatures puzzle.In short.

My loop for checking all temperatures is:

char tempx = 5527;

for (int i = 0; i < 257; i++) {
if (temps[i] < tempx && temps[i] != 0)
tempx = temps[i];

`````` fprintf(stderr, "%d", temps[i]);
``````

}

But my output of that is:
"“45, 49, 50, 32, 45, 53, 32, 45, 49, 51, 55, 10, 0, 127, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 61, 87, -34, -9, -1, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -120, -58, -2, -9, -1, 127, 0, 0, 1, 0, 0, 0, -1, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1, 127, 0, 0, -88, -31, -1, -9, -1, 127, 0, 0, -64, 59, -115, -9, -1, 127, 0, 0, 64, 52, 108, -9, -1, 127, 0, 0, -64, 59, -115, -9, -1, 127, 0, 0, 64, -80, -35, -9, -1, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -27, -1, -9, -1, 127, 0, 0, -16, -23, -1, -1, -1, 127, 0, 0, -32, -23, -1, -1, -1, 127, 0, 0, 46, 78, 61, -10, 0, 0, 0, 0, -107, 4, 64, 0, 0, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -88, 97, -18, -10, -1, 127, 0, 0, 0, -64, -2, -9, -1, 127, 0, 0, -7, -19, -1, -1, -1, 127, 0, 0, 96, -64, 39, -9, -1, 127, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -99, 9, 64, 0, 0, 0, 0, 0, 96, -64, 39, -9, -1, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 80, 9, 64, 0, 0, 0, 0, 0, -16, 6, 64, 0, 0, 0, 0, 0, 48, -120”…

But 2 test case “only negatives” wants -5 for output but there is not any -5, so how i can receive it as a result ? Or maybe im fail in stderr check ?

#479

Now I understand the logic behind the solution. What about the string composed by 6 numbers? Is it just an example? I see that the different test cases seem to have a different number of temperatures in the input, so how can I make a genera loop?
Forgive me if I sound too dumb for solving it on my own, I just began a short time ago the “You don’t know Javascript” series after covering HTML and CSS.

#480

It tells you how many numbers are in the string… that’s what the first number you read in (N) means.

#481

I read the solution on a github repository, searched the logic behind each line on internet and rewrote it by my own with a lot of comments so that I could explain why I was coding this or that. Please take a look.

[removed the source code screenshot]

There is one thing left though: could you explain me

`for(i = n - 1; n >= 0; i--){}`?

I mean why did the author write `i = n - 1`?

#482

Because he begins from the end?

#483

When we learn loops, we usually do “standard” loops starting from 0 until n. You can actually do whatever you want. You just need an initialised index (i= n-1), a loop condition (n>=0) and a increment (i–).

#484

Hi, guys! Could you please advice on that test case (it’s in JavaScript), why the answer is ‘-’, it seems to me that it should be -5 (5) as it is the closest value to 0.

Standard Output Stream:
temps:-12 -5 -137

Failure
Found: 5
Expected: -

#485

You are correct, it should be -5. Your output was 5, so the failure message complained about finding a 5 where it was looking for a - .

#486

Thanks, I’ve got it!

#487

My code is in C#.
Why do I receive an unassigned local variable with the code inthese?

``````    **int lowestTemp = 5526;**
int lowestIndex = 0;
int[] currentTemp;
``````
``````    for(int i = 0; i < n; i++){
**currentTemp[i] = Int32.Parse(temp[i]);**
int abs = Math.Abs(currentTemp[i]);``````

#488

I don’t know about lowestTemp but for

It’s because `int[] currentTemp;` is not assigned. You need to call `currentTemt = new int[x];` somewhere (where x is the number of elements you want in your array)

#489

Thankyou. That helped a lot.

#490

I keep getting this error when i try the test for all negative temperatures. Any Help?

Standard Output Stream:
-12
Failure
Found: -1
Expected: -5

``````    String temperatures []=temps.split(" ");
int lowestTemp=5526;

for(int i=0; i<n; i++)
{
int tempC=Integer.parseInt(temperatures[i]);

if( Math.abs(tempC) < lowestTemp){

lowestTemp=tempC;
}

}

// Write an action using System.out.println()
// To debug: System.err.println("Debug messages...");

System.out.println(""+ lowestTemp );
}``````

#491

#492

I dont particularly understand what you mean by my if statement being problematic can you please explain sorry. What I am attempting to do is take the absolute value of any temperature regardless of it being negative. By this statement, I am taking the temperature closest to zero as if it was a number line. Im going by pure/absolute distance between the temperatures.

#493

With Lowest = 5526, tempC1 = -12, tempC2 = 1;

Math.abs(tempC1) = 12, 12 < 5526, lowestTemp=tempC1; (-12)

Math.abs(tempC2) = 1, 1 < -12, This is problematic !

#494

ohh yes go it tyty!