Computer Science Canada

how to write those funtions?

Author:  oanh_goose [ Mon Oct 24, 2011 9:05 am ]
Post subject:  how to write those funtions?

Write these functions

min (a,b,c: int) :int
% takes in 3 integers and returns the smallest

maxletter (s: string) :char
% takes in a string and returns the letter with the highest letter of the alphabet

arithmetic_series (a,d,n : int) : int
% takes in the first term(a), the common difference(d) and the number of terms(n) for an arithmetic series and outputs the total


P/S: help me.... im in grade 11 and i hv to write those in turing

Author:  Insectoid [ Mon Oct 24, 2011 9:51 am ]
Post subject:  RE:how to write those funtions?

Exactly what are you having trouble with? Have you tried solving these yourself?

Author:  Beastinonyou [ Mon Oct 24, 2011 2:44 pm ]
Post subject:  Re: how to write those funtions?

Before anybody is going to help you solve these problems, You should post some of the Code you already have, and we can help figure out why it doesn't work, and how you can accomplish a certain task.

You need to put in some effort yourself, before anybody else is going to give any.

oanh_goose wrote:

min (a,b,c: int) :int
% takes in 3 integers and returns the smallest


That should be fairly straight-forward. You Use a repetitive structure to go through and compare the numbers, and keep track of the smallest. Very simple.

oanh_goose wrote:

maxletter (s: string) :char
% takes in a string and returns the letter with the highest letter of the alphabet


What exactly do you wish to happen? "Returns the letter with the highest letter of the alphabet."? The way it's worded is a bit confusing. And what is defined as the "Highest" letter of the Alphabet? Do you mean, from Start to Finish, all 26 Letters of the Alphabet, 'A' is the highest and 'Z' is the lowest, or is it the ASCII Values for the alphabet?

oanh_goose wrote:

arithmetic_series (a,d,n : int) : int
% takes in the first term(a), the common difference(d) and the number of terms(n) for an arithmetic series and outputs the total


What exactly do you want to use this for? and what is the Total defined by? It takes (a), common difference (d) = what exactly?, and what do you do to get the Total?

Author:  tg851 [ Tue Oct 25, 2011 10:36 am ]
Post subject:  Re: how to write those funtions?

here,you can have the code for the first one. this can be easily tweaked to make the second one.
be thankful I'm in a good mood or you would have gotten the code in scrambled form,instead it's just undocumented.figure it out for yourself


Turing:


var a,b,c,d,e,f : int

get a
get b
get c
d:=0
e:=0
f:=0


for : 1..4

    if a>b then
    d:=d+1
    end if

    if a>c then
     d:=d+1
    end if
   
    if b>a then
    e:=e+1
    end if

    if b>c then
    e:=e+1
    end if
   
    if c>a then
    f:=f+1
    end if

    if c>b then
    f:=f+1
    end if
end for

for : 1..5

if d=0 then locate (4,1) put a, " is the lowest"
end if
if e=0 then locate (4,1) put b," is the lowest"
end if
if f=0 then locate (4,1) put  c," is the lowest"
end if
locate (1,60)
put "coded by tomas gaines"
end for

Author:  Insectoid [ Tue Oct 25, 2011 10:40 am ]
Post subject:  RE:how to write those funtions?

tg851, here at compsci.ca we don't like giving full answers for assignments. I'm not a mod, but we'd appreciate it if you'd refrain in future.

Author:  Tony [ Tue Oct 25, 2011 11:00 am ]
Post subject:  RE:how to write those funtions?

Not sure if serious...

Author:  Insectoid [ Tue Oct 25, 2011 11:04 am ]
Post subject:  Re: RE:how to write those funtions?

Tony @ Tue Oct 25, 2011 11:00 am wrote:
Not sure if serious...


My bad; I didn't actually look at the code.

Author:  Nick [ Tue Oct 25, 2011 11:05 am ]
Post subject:  RE:how to write those funtions?

@tg851: either you're a bad programmer or a troll... either way lololol

Author:  QED [ Tue Oct 25, 2011 11:06 am ]
Post subject:  RE:how to write those funtions?

Flag variables are a bad idea. They're confusing, difficult to maintain, and unnecessary. You can almost always avoid them entirely by taking a little bit of time to re-design your procedure.

Secondly, the less-than operator forms a transitive relation. That is,

if min(a,b) = b
and min(b,c) = c
then min(a,c) = c is a certainty

How can you take advantage of this property to succinctly produce a 3-argument min() function? Think about it on paper first, then commit your solution to code.

Author:  Beastinonyou [ Tue Oct 25, 2011 2:29 pm ]
Post subject:  Re: how to write those funtions?

tg851 @ Tue Oct 25, 2011 10:36 am wrote:

be thankful I'm in a good mood or you would have gotten the code in scrambled form,instead it's just undocumented.figure it out for yourself
Turing:

for : 1..5
if d=0 then locate (4,1) put a, " is the lowest"
end if
if e=0 then locate (4,1) put b," is the lowest"
end if
if f=0 then locate (4,1) put  c," is the lowest"
end if
locate (1,60)
put "coded by tomas gaines"
end for


Holy Eggs!... You hear that people? That's code in Non-Scrambled form.... What would that be then? Poached, or Hard-Boiled? because it's definitely not Sunny-side or Over-Easy.

That entire code sequence was like having someone tell me the easiest way to Draw a Square, was to Draw a Diamond with in-equivalent sides, then look at it sideways, then fix those sides.


I'm glad our brain's aren't programmed using that logic. =P

The day one is, someone call George A. Romero, we'll have a real-life zombie at hand.

Author:  crossley7 [ Tue Oct 25, 2011 4:47 pm ]
Post subject:  RE:how to write those funtions?

Ok, that code made me sick. If you are going to post help code, don't code in the ugliest, least efficient way possible. (I don't care about comments as it is something I have never learned how to do properly at the expense of many hours of debugging)

Author:  mirhagk [ Tue Oct 25, 2011 5:11 pm ]
Post subject:  RE:how to write those funtions?

*shudder* I seriously hope that it was a troll, and it must be.

Author:  Tony [ Tue Oct 25, 2011 5:22 pm ]
Post subject:  Re: RE:how to write those funtions?

crossley7 @ Tue Oct 25, 2011 4:47 pm wrote:
least efficient way possible

he could have looped through it a few more times, "just to make sure" Wink

Author:  tg851 [ Tue Oct 25, 2011 6:35 pm ]
Post subject:  Re: how to write those funtions?

its suppose to be inefficient,that way if the tries blatantly ripping it off for a project he doesnt profit from it.It gives an idea of how to do it,but not the BEST way to do it.thats up to him to figure out Wink
and imaen by scrmbld is in no working order whatsoever(its there but every part is out of working order,something my teacher LOVE to do)

Author:  mirhagk [ Tue Oct 25, 2011 7:50 pm ]
Post subject:  RE:how to write those funtions?

Okay good, I'm glad. Actually this gave me an idea for the course in school, give students fuddled code, and tell them to fix it.

Author:  Aange10 [ Tue Oct 25, 2011 8:00 pm ]
Post subject:  Re: RE:how to write those funtions?

mirhagk @ 25/10/2011, 6:50 pm wrote:
Okay good, I'm glad. Actually this gave me an idea for the course in school, give students fuddled code, and tell them to fix it.


That would probably be excellent! Maybe even a glitch or two... (:

Author:  Nick [ Tue Oct 25, 2011 8:16 pm ]
Post subject:  RE:how to write those funtions?

A bit off topic here but @mirhagk, a common occurrence in my programming assignments is a half written piece of code. There may be variables declared, functions written, it may even run and do what it is coded to do right, but we have to add in extra functionality or, in the case it doesn't run, get it to run. Sometimes there are even calls to functions that don't exist... yet.

Author:  tg851 [ Wed Oct 26, 2011 10:12 am ]
Post subject:  Re: how to write those funtions?

what i mean by scrambled is turning this(our latest(easy level)scramble challenge)

Turing:


get terms
third := first + second
first := 1
first := second
end for
var first, second, third, terms : int
for ctr: 1 .. terms
second := third
put "Term #", ctr, " is ", first
put "Please enter the number of terms to display."
second := 1


into this
Turing:


var first, second, third, terms : int
put "Please enter the number of terms to display."
get terms
 first := 1
    second := 1

for ctr: 1 .. terms
   put "Term #", ctr, " is ", first
    third := first + second
    first := second
    second := third
   
   
end for


its a right pain to do,one of the most hated challenges in my class.it often results in us just brute forcing the code into working order on the harder ones. Evil or Very Mad

Author:  QED [ Thu Oct 27, 2011 10:43 am ]
Post subject:  RE:how to write those funtions?

If that's a typical computer programming assignment for you, then you're high school teacher is not doing his job correctly.

There's no educational value in deciphering a programmer's intent by re-ordering his code. None. It's time wasted that you'll never get back.

Author:  Insectoid [ Thu Oct 27, 2011 10:56 am ]
Post subject:  Re: RE:how to write those funtions?

QED @ Thu Oct 27, 2011 10:43 am wrote:
If that's a typical computer programming assignment for you, then you're high school teacher is not doing his job correctly.

There's no educational value in deciphering a programmer's intent by re-ordering his code. None. It's time wasted that you'll never get back.


Agreed. Most of the time, when my prof expects me to complete or fix code, I just comment it all out and re-write from scratch. Not worth my time to fix it. Admittedly, fixing other peoples' code is a useful skill to have, but I simply haven't got enough time in an exam/lab to figure out what my prof is trying to do, and fix it (doesn't help that she goes out of her way to make her implementation the least efficient, obvious, or legible possible).

Author:  tg851 [ Thu Oct 27, 2011 10:58 am ]
Post subject:  Re: how to write those funtions?

QED i know, we've been in this class for 3 months and we've been barley finished FOR LOOPS,for peats sakes i figured that out in 2 days. I've been teaching myself along with asking my teacher for help on something new and thats if hes willing to help me at all.i currently make a working file writer and am working on a minor Turing based OS while most of the rest(2 other people are doing some stuff a few levels below me) of the class blunders along with simple loops.the class has been SUSFU since the start.

Author:  Insectoid [ Thu Oct 27, 2011 11:08 am ]
Post subject:  RE:how to write those funtions?

Quote:
QED i know, we've been in this class for 3 months and we've been barley finished FOR LOOPS,for peats sakes i figured that out in 2 days. I've been teaching myself along with asking my teacher for help on something new and thats if hes willing to help me at all


Get used to this. 90% of what I know is self-taught. In high school, all you'll learn is basic (VERY basic) algorithms and syntax. 90% of your time will be spent on syntax. Syntax is worthless. As long as you know one language's syntax, you can figure out any other languages' on your own very quickly. If you're really interested in programming you'll spend that 90% syntax time on your own learning. It is not a crime to ignore your teacher if he isn't saying something worth hearing.

Author:  mirhagk [ Thu Oct 27, 2011 9:06 pm ]
Post subject:  RE:how to write those funtions?

We really need to split the computer science class into the 2 levels that are allowed. A U course should not be as easy as the computer science class currently is, it should be somewhat difficult.

Author:  Beastinonyou [ Fri Oct 28, 2011 6:46 am ]
Post subject:  Re: how to write those funtions?

If some of you aren't challenged enough in your classes, and are working above and beyond the rest of your classmates, I could offer you an alternative.


Like I'm helping Aange10, I will send you a PDF of the assignment he gave us (I type out the assignment he gives us), and you can do that.

I'm currently on my 5th assignment, and they've all been a blast. They get more challenging as you go =P

Author:  oanh_goose [ Mon Oct 31, 2011 9:43 am ]
Post subject:  Re: how to write those funtions?

this's wat i wrote.... anw thanks 4 those u guys helps

var num1, num2, num3 : int


function minimum (a, b, c : int) : int

if a <= b and a <= c then
result a
elsif b <= c and b <= a then
result b
elsif c <= a and c <= b then
result c
end if

end minimum

put "type in a 3 number"
get num1, num2, num3
put minimum (num1, num2, num3)

Author:  Velocity [ Tue Nov 08, 2011 6:56 pm ]
Post subject:  RE:how to write those funtions?

@ emo
Definetly a troll.
@ tg851
That was very nice of you.

Author:  Tony [ Tue Nov 08, 2011 7:34 pm ]
Post subject:  RE:how to write those funtions?

just to make sure we actually have some good code in here:
code:

function min_2(a,b : int) : int
   if a <= b then
      return a
   else
      return b
   end if
end min_2

function min_3(a,b,c : int) : int
   return min_2(min_2(a,b), c)
end min_3

Author:  Beastinonyou [ Tue Nov 08, 2011 10:34 pm ]
Post subject:  Re: RE:how to write those funtions?

Tony @ Tue Nov 08, 2011 7:34 pm wrote:
just to make sure we actually have some good code in here:
code:

function min_2(a,b : int) : int
   if a <= b then
      return a
   else
      return b
   end if
end min_2

function min_3(a,b,c : int) : int
   return min_2(min_2(a,b), c)
end min_3


Best and Easiest Solution for him So Far! =P


: