About Array Element Division Problem in Loop

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

About Array Element Division Problem in Loop

Aziz YÜCELEN
Hi,

I m newbee and i have a problem about array element division in loop.Can you help me please? Here my code

Get_N_value = input('Enter N value: ');
Get_alpha_count =(2*Get_N_value)-4;
Get_beta_count =(Get_N_value-1);
b_count =(Get_N_value-2);
s_count =b_count;

for gac=1:Get_alpha_count
  sprintf('Enter alpha  %d th value:', gac)
  alpha_value(1,gac) = input('',gac);
end

for gbc=1:Get_beta_count
  sprintf('Enter beta %d th value:', gbc)
  beta_value(1,gbc) = input('',gbc);
end

for i_index=1:b_count
m=(beta_value(i_index)-beta_value(i_index+1))/beta_value(i_index)
end

and result:
Enter N value: 5
ans = Enter alpha  1 th value:
2
ans = Enter alpha  2 th value:
0
ans = Enter alpha  3 th value:
2
ans = Enter alpha  4 th value:
2
ans = Enter alpha  5 th value:
1
ans = Enter alpha  6 th value:
3
ans = Enter beta 1 th value:
2
ans = Enter beta 2 th value:
2
ans = Enter beta 3 th value:
4
ans = Enter beta 4 th value:
4
m = 0
m = -0.040000 <---- this is actually -2/2 = -1 but octave find -0.04000
m = 0


Reply | Threaded
Open this post in threaded view
|

Re: About Array Element Division Problem in Loop

Doug Stewart-4


On Tue, Jul 9, 2019 at 10:46 AM Aziz YÜCELEN <[hidden email]> wrote:
Hi,

I m newbee and i have a problem about array element division in loop.Can you help me please? Here my code

Get_N_value = input('Enter N value: ');
Get_alpha_count =(2*Get_N_value)-4;
Get_beta_count =(Get_N_value-1);
b_count =(Get_N_value-2);
s_count =b_count;

for gac=1:Get_alpha_count
  sprintf('Enter alpha  %d th value:', gac)
  alpha_value(1,gac) = input('',gac);

try alpha_value(1,gac) = input('')
what your are getting is chars not doubles.
 
end

for gbc=1:Get_beta_count
  sprintf('Enter beta %d th value:', gbc)
  beta_value(1,gbc) = input('',gbc);
end

for i_index=1:b_count
m=(beta_value(i_index)-beta_value(i_index+1))/beta_value(i_index)
end

and result:
Enter N value: 5
ans = Enter alpha  1 th value:
2
ans = Enter alpha  2 th value:
0
ans = Enter alpha  3 th value:
2
ans = Enter alpha  4 th value:
2
ans = Enter alpha  5 th value:
1
ans = Enter alpha  6 th value:
3
ans = Enter beta 1 th value:
2
ans = Enter beta 2 th value:
2
ans = Enter beta 3 th value:
4
ans = Enter beta 4 th value:
4
m = 0
m = -0.040000 <---- this is actually -2/2 = -1 but octave find -0.04000
m = 0



--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Ynt: About Array Element Division Problem in Loop

Aziz YÜCELEN
Thanks doug, you are right, This is solve my problem


Gönderen: Doug Stewart <[hidden email]>
Gönderildi: 9 Temmuz 2019 Salı 18:28
Kime: Aziz YÜCELEN
Bilgi: [hidden email]
Konu: Re: About Array Element Division Problem in Loop
 


On Tue, Jul 9, 2019 at 10:46 AM Aziz YÜCELEN <[hidden email]> wrote:
Hi,

I m newbee and i have a problem about array element division in loop.Can you help me please? Here my code

Get_N_value = input('Enter N value: ');
Get_alpha_count =(2*Get_N_value)-4;
Get_beta_count =(Get_N_value-1);
b_count =(Get_N_value-2);
s_count =b_count;

for gac=1:Get_alpha_count
  sprintf('Enter alpha  %d th value:', gac)
  alpha_value(1,gac) = input('',gac);

try alpha_value(1,gac) = input('')
what your are getting is chars not doubles.
 
end

for gbc=1:Get_beta_count
  sprintf('Enter beta %d th value:', gbc)
  beta_value(1,gbc) = input('',gbc);
end

for i_index=1:b_count
m=(beta_value(i_index)-beta_value(i_index+1))/beta_value(i_index)
end

and result:
Enter N value: 5
ans = Enter alpha  1 th value:
2
ans = Enter alpha  2 th value:
0
ans = Enter alpha  3 th value:
2
ans = Enter alpha  4 th value:
2
ans = Enter alpha  5 th value:
1
ans = Enter alpha  6 th value:
3
ans = Enter beta 1 th value:
2
ans = Enter beta 2 th value:
2
ans = Enter beta 3 th value:
4
ans = Enter beta 4 th value:
4
m = 0
m = -0.040000 <---- this is actually -2/2 = -1 but octave find -0.04000
m = 0



--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: About Array Element Division Problem in Loop

nrjank
In reply to this post by Aziz YÜCELEN

m = 0
m = -0.040000 <---- this is actually -2/2 = -1 but octave find -0.04000
m = 0


the operation you're performing at that step is  (50-52)/50 = -0.04

you're inputting your beta_value as a character string.  so the value of ascii '2' is numerical 50, and '4' is 52.  

you should get familiar debugging with setting breakpoints in your script using the GUI or inserting the 'keyboard' command so that you can inspect variable values in the middle of execution.  Also, look at the help for sprintf.  


Reply | Threaded
Open this post in threaded view
|

Ynt: About Array Element Division Problem in Loop

Aziz YÜCELEN
Thank you, I understand why now


Gönderen: Nicholas Jankowski <[hidden email]>
Gönderildi: 9 Temmuz 2019 Salı 18:33
Kime: Aziz YÜCELEN
Bilgi: [hidden email]
Konu: Re: About Array Element Division Problem in Loop
 

m = 0
m = -0.040000 <---- this is actually -2/2 = -1 but octave find -0.04000
m = 0


the operation you're performing at that step is  (50-52)/50 = -0.04

you're inputting your beta_value as a character string.  so the value of ascii '2' is numerical 50, and '4' is 52.  

you should get familiar debugging with setting breakpoints in your script using the GUI or inserting the 'keyboard' command so that you can inspect variable values in the middle of execution.  Also, look at the help for sprintf.