Sign in to follow this  
Followers 0

Function (not a specific language)


8 posts in this topic

Posted

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

Posted

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

Share this post


Link to post
Share on other sites

Posted

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

Posted

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

Posted

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

Posted

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.