|
|
View previous topic :: View next topic |
Author |
Message |
mmc01
Joined: 16 Jun 2010 Posts: 31
|
Calculate data in Array error. It's not a valid value. |
Posted: Thu Jul 18, 2013 10:59 am |
|
|
I want to calculate next time in 24Hr from Array. First, I sent hour and min to set in array inside next_time() and convert hour to min. Second, I want to calculate by find minimum value which more than current time . If have no minimum value more than current time then find minimum value of Array. such as, current time = 20.00 then next time should be minimum of 20.01-23.59 but if have no value in 20.01-23.59 next time should be minimum of 00.00-19.59. I try calculate time from array but it not show valid value. This is result of my code. In line min a=448, n=1404, ck=0 a should same all[2] that is 1020 . But why it show a=448 please help me.
Quote: | pos=0 a=540, n=1404, hr=9, mn=0
pos=1 a=960, n=1404, hr=16, mn=0
pos=2 a=1020, n=1404, hr=17, mn=0
min a=448, n=1404, ck=0 |
Code: | int16 next_time(int16 hr, int16 min, int pos){ //pos is position of array
int16 minimVal1,minimVal2;
int i=0,check;
int16 now_all;
int16 all[5];
int16 hrk[5],mnk[5];
hrk[pos] = hr;
mnk[pos] = min;
all[pos] =(hrk[pos]*60 + mnk[pos]) ; //change hr to min
minimVal2 = (hrk[0]*60) + mnk[0];
now_all = getNow(); // current time
printf("\r\npos=%d a=%ld, n=%ld, hr=%ld, mn=%ld",pos ,all[pos],now_all,hrk[pos],mnk[pos]);
for(i=1;i<pos+1;i++){
if(all[i]>now_all){ // If have a value more than current time then check =1
check=1;
minimVal1 = all[i];
break;
}
if(all[i]<minimVal2){ // If have no value more than current time then find minimum value of array
minimVal2 = all[i];
printf("\r\nmin a=%ld, n=%ld, ck=%d",all[i],now_all,check); //It not show valid value
}
}
if(check==1){ // If have a value more than current time then find minimum value which more than current time.
for(i=1;i<pos+1;i++){
if(all[i]>now_all){
if(all[i]<minimVal1){
minimVal1 = all[i];
}
}
}
return minimVal1;
}
else{
return minimVal2;
}
} |
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Jul 19, 2013 6:47 am |
|
|
First of all, your code is difficult to read because the indentation goes all directions. When you take little time to make your code look good, then I take little time to study your code and give you answers.
I don't exactly understand what you want to do, but a major problem is that your arrays inside the function are not initialised, that is the arrays 'all', 'hrk' and 'mnk' are containing random values. Perhaps the memory is containing data from the previous call, but here you are just lucky.
If you want the values in the array to be remembered till the next function call then you have to change the arrays into global variables, or make them static.
A few small other problems:
- A test for 'pos' being out of range is missing. To prevent errors when the calling function uses a 'pos' value that is too large will make your life as a programmer a lot easier.
- Code: | for(i=1;i<pos+1;i++){ | Arrays in C start a position 0, not at 1 as you have coded now. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|