Thanks, that all makes sense. When I however try and implement it into my complete code I still can not get it to work. I might be introducing a typo which I cannot see. Or could it be the online compiler that I am using, GDB online Debugger | Compiler - Code, Compile, Run, Debug online C, C++
Code:
#include <stdio.h>
#include <stdlib.h>
int getMinutes (float n)
{
return trunc (n);
}
int getSeconds (float n)
{
float integer;
int num;
num = round (modf (n, &integer) * 100);
return num;
}
int * getTimings(int n)
{
int *lghts = malloc(3 * sizeof(lghts));
lghts[0] = n;
if (n < 60)
{
lghts[1] = n + 10;
lghts[2] = n + 20;
}
else
{
lghts[1] = n + 60;
lghts[2] = n + 120;
}
return lghts;
}
int
main ()
{
float number;
int min, sec, totalseconds, x;
printf ("Enter a real number: ");
scanf ("%f", &number);
min = getMinutes (number);
sec = getSeconds (number);
// printf() displays the formatted output
printf ("You entered: %.2f", number);
printf ("\nMinutes: %d", min);
printf ("\nSeconds: %d", sec);
totalseconds = min * 60 + sec;
int *lights = getTimings(totalseconds);
printf ("\nTotal seconds: %d", totalseconds, "\n");
printf("\n",lights[0]);
for (x = 0; x < 3; x += 1)
{
printf ("\nLight %d", x + 1, ": ", lights[x]);
}
free(lights);
return 0;
}
Output is
Enter a real number: 1
You entered: 1.00
Minutes: 1
Seconds: 0
Total seconds: 60
Light 1
Light 2
Light 3