Sign in to follow this  
Followers 0
Hello Me You

Algorithm - I want it to be very completed!

10 posts in this topic

#1 ·  Posted (edited)

At school we are learning algorithm, which I know what is. He gave us (the teacher) some examples of algorithms and I bet with him that if I make a program with a specific algorithm (from the examples), I will get A!

The algorithm calculates the area and the perimeter of a (2D) square/box and the algorithm is:

1) Read A (first side)

2) Read B (second side)

3) Calculate A*B

4) Calculate 2A+2B

5) Print area & perimeter

Here is what I tried:

$a = InputBox("Side A", "Length of side A (cm)")
If @error Then MsgBox(16, "Error!", "Invalid value")
$b = InputBox("Side B", "Length of side B (cm)")
If @error Then MsgBox(16, "Error!", "Invalid value")
MsgBox(64, "Area and Perimeter", "Area: " & $a*$b & "cm²" & @CRLF & "Perimeter: " & (2*$a)+(2*$B) & "cm")

But it is not complete. That it misses is:

1) If @error then show msgbox BUT go to the first step again. (ask for side A lenght)

2) The same problem with the up one but if it happens to the second inputbox, which asks for side B lenght.

3) If the entered value isl letters (a, b, c, etc...) instead of numbers show msgbox and ask for side lenght again

4) And the last I can think is if the value is >0 (I can do this)

Can you help me finish it?

Did I forgot something?

Are these very noob questions?

Edited by Hello Me You

Random

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Some suggestions

- You want to check the value entered is numeric and also that its a number small enough to make a calculation ( StringRegExp )...... will you handle floating numbers ? If not StringRegExp then a check involving Length and isNumber()

- Declare variables

- Make the cancel button in your msgbox do something

- put you inputbox statement in a Do - Until loop ..... once algo is happy it go's to next stage

- It may work that math exression can be done in a msgbox but I think its just not good practice, Maybe others have views on this

- Comment your code.....just to impress your teacher :whistle:

My 2 cents :P

Edited by Hasher

Firefox's secret is the same as Jessica Simpson's: its effortless, glamorous style is the result of — shhh — extensions!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I still can't figure it out. Here is my source:

$a = InputBox("Side A", "Length of side A (cm)")
If $a<0 Or @error Then MsgBox(16, "Error!", "Invalid value")
$b = InputBox("Side B", "Length of side B (cm)")
If $b<0 Or @error Then MsgBox(16, "Error!", "Invalid value")
MsgBox(64, "Area and Perimeter", "Area: " & $a*$b & " cm²" & @CRLF & "Perimeter: " & (2*$a)+(2*$B) & " cm")

EDIT:

1) I can't use isNumber(). I tried but I can't do it.

2) I maked a do-until loop but loops and even when I type a number..

3) Any help?

Edited by Hello Me You

Random

Share this post


Link to post
Share on other sites

I still can't figure it out. Here is my source:

$a = InputBox("Side A", "Length of side A (cm)")
If $a<0 Or @error Then MsgBox(16, "Error!", "Invalid value")
$b = InputBox("Side B", "Length of side B (cm)")
If $b<0 Or @error Then MsgBox(16, "Error!", "Invalid value")
MsgBox(64, "Area and Perimeter", "Area: " & $a*$b & " cm²" & @CRLF & "Perimeter: " & (2*$a)+(2*$B) & " cm")oÝ÷ Ø@ÈORj}ý¶ëÃn·«"ÚâyÖî´mv­ØçZv§¶)e¢[ºÙh¢×¯z|!zr-Êéz½À(^êº^1¬zëiÚ.Ø¥Zê뢶®¶­s`¦Fð¢b33c¶ÒçWD&÷gV÷Cµ6FRgV÷C²ÂgV÷C´ÆVæwFöb6FR6ÒgV÷C²§VçFÂb33c¶fwC³÷"äõBW'&÷

code not tested


All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Share this post


Link to post
Share on other sites

My only problem is that I can not an error when value contains letters..

$a = InputBox("Side A", "Length of side A (cm)")
If $a<0 Or @error Then MsgBox(16, "Error!", "Invalid value")
$b = InputBox("Side B", "Length of side B (cm)")
If $b<0 Or @error Then MsgBox(16, "Error!", "Invalid value")
MsgBox(64, "Area and Perimeter", "Area: " & $a*$b & " cm²" & @CRLF & "Perimeter: " & (2*$a)+(2*$B) & " cm")

Random

Share this post


Link to post
Share on other sites

Except of the preview post I want to say that if there is an error (e.g. at the first side [A]) the script will continue to the side B without asking again the side. Here is when we need Go To command... What can I do for that?


Random

Share this post


Link to post
Share on other sites

From the help file

InputBox

--------------------------------------------------------------------------------

Displays an input box to ask the user to enter a string.

Therefor your input is always going to be a string


Get Beta versions Here Get latest SciTE editor Here AutoIt 1-2-3 by Valuater - A great starting point.

Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Therefor your input is always going to be a string

I want to check if only numbers are entered. Edited by Hello Me You

Random

Share this post


Link to post
Share on other sites

Try

do
$a = InputBox("Side A", "Length of side A (cm)")
until not StringIsAlpha($a)
Do
$b = InputBox("Side B", "Length of side B (cm)")
until not StringIsAlpha($B)
MsgBox(64, "Area and Perimeter", "Area: " & $a*$b & " cm²" & @CRLF & "Perimeter: " & (2*$a)+(2*$B) & " cm")


Get Beta versions Here Get latest SciTE editor Here AutoIt 1-2-3 by Valuater - A great starting point.

Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Thanks BigDod, finally I am done. What do you think, is it complete (for algorithm)? Here it is:

Do
    $a = InputBox("Side A", "Length of side A (cm)", "Numbers Only")
    If @error = 1 Then Exit
    If StringIsAlpha($a) Then MsgBox(16, "Numbers Only!", "Please enter numbers only.")
    If $a < 0 Or @error Then MsgBox(16, "Error!", "Invalid value.")
Until $a > 0 And Not @error

Do
    $b = InputBox("Side B", "Length of side B (cm)", "Numbers Only")
    If @error = 1 Then Exit
    If StringIsAlpha($B) Then MsgBox(16, "Numbers Only!", "Please enter numbers only.")
    If $b < 0 Or @error Then MsgBox(16, "Error!", "Invalid value.")
Until $b > 0 And Not @error

SoundPlay(@WindowsDir & "\media\tada.wav")
MsgBox(64, "Area and Perimeter", "Area: " & $a * $b & " cm²" & @CRLF & "Perimeter: " & (2 * $a) + (2 * $B) & " cm")
Edited by Hello Me You

Random

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0