Sign in to follow this  
Followers 0
bs819

Function (not a specific language)

8 posts in this topic

Hi all, I have written a program with a function and I want to know if it all looks okay. 

 

"Design a function that accepts an object's falling time (in seconds) as an argument. The function should return the distance in meters that the object falls [ distance = 1/2(gravity)(time^2) ]. Design a program that calls the function in a loop that passes the values 1 through 10 as arguments and displays the return value."

 

 

Constant Real GRAVITY = 9.8

 

Module main( )

   Declare Real distance, time, counter

   For counter = 1 to 10

      Set time = counter

      Set distance = fallingDistance(time)

      Display "Object falls", distance, "meters."

   End For

End Module

 

Function Real fallingDistance(Real seconds)

   Return 0.5 * GRAVITY * seconds^2

End Function

 

 

Share this post


Link to post
Share on other sites

If you have time or care to help, I'd also be curious to know if I am on the right track with a different problem, also using functions. The question tells you the program should prompt the user to enter five test scores, display a letter grade for each score, and then display the average score. The two functions to use are calcAverage (Accept five test scores and return average) and determineGrade (accept test score and return a letter grade as a string).

 

I don't know if I am making it too complicated or if what I have is close to the right answer!

 

 

 Module main( )

Declare Real test1, test2, test3, test4, test5, avg

Declare String grade1, grade2, grade3, grade4, grade5

 

      Display ?Enter test one score: ?

      Input test1

      Set grade1 = determineGrade(test1)

      Display grade1

 

      Display ?Enter test two score: ?

      Input test2

      Set grade2 = determineGrade(test2)

      Display grade2

 

      Display ?Enter test three score: ?

      Input test3

      Set grade3 = determineGrade(test3)

      Display grade3

 

      Display ?Enter test four score: ?

      Input test4

      Set grade4 = determineGrade(test4)

      Display grade4

 

      Display ?Enter test five score: ?

      Input test5

      Set grade5 = determineGrade(test5)

      Display grade5

 

      Set avg = calcAverage(test1, test2, test3, test4, test5)

      Display ?Average is: ?, average

End Module

 

Function Real determineGrade(Real value)

      Declare String grade

      If value < 60 Then

         Set grade = ?F?

      Else If value < 70 Then

         Set grade = ?D?

      Else If value < 80 Then

         Set grade = ?C?

      Else If value < 90 Then

         Set grade = ?B?

      Else If value <= 100 Then

         Set grade = ?A?

      End If

      Return grade

End Function

 

Function Real calcAverage(Real num1, num2, num3, num4, num5)

      Declare Real result

      Set result = (num1 + num2 + num3 + num4 + num5) / 5

      Return result

End Function

Share this post


Link to post
Share on other sites

Both look ok, all in all, except:

 

First code:

1) Variable time is unnecessary.

 

Second code:

1) You've overlooked that determineGrade() should return String type. Currently it incorrectly wants to return Real.

2) You could do with just one grade variable in main(). Although that's premature optimization, strings are expensive memory in just about any implementation down the road.

Share this post


Link to post
Share on other sites

What would you have done if the program required you to input 100 instead of 5 scores? Repeat the same code 100 times with 100 different variables? Create an "average" function that takes 100 arguments as parameters?

 

What would you have done if the program required you to input as many scores as the user wishes? Create a different version of the program for every possible number of inputs?

 

Don't repeat the same code over and over. Make the computer do the repetitive boring jobs for you, not the reverse. Isolate the code that stays the same in functions, and the parts that change in function parameters. Change your variables and ifs to collections and loops.

1 person likes this

Share this post


Link to post
Share on other sites

What would you have done if the program required you to input 100 instead of 5 scores? Repeat the same code 100 times with 100 different variables? Create an "average" function that takes 100 arguments as parameters?

 

What would you have done if the program required you to input as many scores as the user wishes? Create a different version of the program for every possible number of inputs?

 

Don't repeat the same code over and over. Make the computer do the repetitive boring jobs for you, not the reverse. Isolate the code that stays the same in functions, and the parts that change in function parameters. Change your variables and ifs to collections and loops.

Thank you for the help!

Share this post


Link to post
Share on other sites

Both look ok, all in all, except:

 

First code:

1) Variable time is unnecessary.

 

Second code:

1) You've overlooked that determineGrade() should return String type. Currently it incorrectly wants to return Real.

2) You could do with just one grade variable in main(). Although that's premature optimization, strings are expensive memory in just about any implementation down the road.

Thank you for the advice!

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

  • Recently Browsing   0 members

    No registered users viewing this page.