Programming, Octave problem.

Steinar Valsson

New member
Aug 28, 2010
135
0
0
I'm making a basic program to solve the sides of a right-angled triange. I us if statements as demonstrated below and I keep getting this error message:
(It's in Icelandic, but I didn't want to translate the code to be sure everything I see, you see)

Hvada hlid viltu finna? skammhlid
hlid = skammhlid
error: hlidar: mx_el_eq: nonconformant arguments (op1 is 1x9, op2 is 1x8)
error: called from:
error: C;\aefingar\hlidar.m line 10, column 1

Translation: Hvada hlid viltu finna? skammhlid = What side do you want to solve for? shortside
hlid = side, skammhlid = shortside
error: hlidar (hlidar is the file name, means: sides)

Here is the code:


%---------------------------
% (Names)
% Lausn hliða í
% rétthyrndum þríhyrningi
%----------------------------
clc

hlid=input('Hvada hlid viltu finna? ', 's')

if hlid =='langhlid';
b=input('Fyrri skammhlid: ');
a=input('Seinni skammhlid: ');
c=sqrt(a^2+b^2);
printf('Langhlidin er: %f \n', c)
elseif hlid =='skammhlid';
b=input('Hin skammhlidin: ');
c=input('Langhlidin er: ');
a=sqrt(c^2-b^2)
printf('Skammhlidin er: %f \n', a)
endif

Fyrri skammhlid = first short side
Seinni skammhlid = second short side
Langhlidin er = the long side is
Hin skammhlidin = the other short side
Skammhlidin er = the short side is

It works perfectly if I type: langhlid to solve for the long side, but if I type skammhlid to solve for the short sides, the error pops up.

I solved this problem using the switch and {case}, but I'm trying to figure out why if does not work.
Note: I tried writing skammhlid, skamhlid to make the letters 8, instead of 9, didn't work...

Help!
 

MidnightCat

New member
Jul 21, 2009
125
0
0
First off, next time you post code you'll want to use the
Code:
code
tags, it makes the code much easier to read.

As for your problem - this may just be an error you made when translating the code while preparing to post it, but I noticed that you're missing a semi-colon after a=sqrt(c^2-b^2) in the ifelse section of your if statement. Does this help?
 

MidnightCat

New member
Jul 21, 2009
125
0
0
Steinar Valsson said:
The semi-colon is only tells the program not to print this calculation.
Ah, I see now. I think your problem may lie in comparing strings. Quoted from the Octave documtentation:

To determine if two strings are identical it is necessary to use the strcmp function. It compares complete strings and is case sensitive. strncmp compares only the first N characters (with N given as a parameter). strcmpi and strncmpi are the corresponding functions for case-insensitive comparison.

? Built-in Function: strcmp (s1, s2)

Return 1 if the character strings s1 and s2 are the same, and 0 otherwise.
I tried applying the strcmp() function, and it worked for me. The modified code is below:

Code:
%---------------------------
% (Names)
% Lausn hliða í
% rétthyrndum þríhyrningi
%----------------------------
clc

hlid=input('Hvada hlid viltu finna? ', 's')

if (strcmp(hlid, 'langhlid') == 1);
	b=input('Fyrri skammhlid: ');
	a=input('Seinni skammhlid: ');
	c=sqrt(a^2+b^2);
	printf('Langhlidin er: %f \n', c)
elseif (strcmp(hlid, 'skammhlid') == 1);
	b=input('Hin skammhlidin: ');
	c=input('Langhlidin er: ');
	a=sqrt(c^2-b^2)
	printf('Skammhlidin er: %f \n', a)
endif
Hope this was more helpful ;)
 

Steinar Valsson

New member
Aug 28, 2010
135
0
0
MidnightCat said:
Ah, I see now. I think your problem may lie in comparing strings. Quoted from the Octave documtentation:

To determine if two strings are identical it is necessary to use the strcmp function. It compares complete strings and is case sensitive. strncmp compares only the first N characters (with N given as a parameter). strcmpi and strncmpi are the corresponding functions for case-insensitive comparison.

? Built-in Function: strcmp (s1, s2)

Return 1 if the character strings s1 and s2 are the same, and 0 otherwise.
I tried applying the strcmp() function, and it worked for me. The modified code is below:

Code:
%---------------------------
% (Names)
% Lausn hliða í
% rétthyrndum þríhyrningi
%----------------------------
clc

hlid=input('Hvada hlid viltu finna? ', 's')

if (strcmp(hlid, 'langhlid') == 1);
	b=input('Fyrri skammhlid: ');
	a=input('Seinni skammhlid: ');
	c=sqrt(a^2+b^2);
	printf('Langhlidin er: %f \n', c)
elseif (strcmp(hlid, 'skammhlid') == 1);
	b=input('Hin skammhlidin: ');
	c=input('Langhlidin er: ');
	a=sqrt(c^2-b^2)
	printf('Skammhlidin er: %f \n', a)
endif
Hope this was more helpful ;)
Yep, it worked.
Thank you.