• 0

Function (not a specific language)


Question

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."

 

  Quote

 

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

 

 

Link to comment
https://www.neowin.net/forum/topic/1182067-function-not-a-specific-language/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

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!

 

  Quote

 

 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

  • 0

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.

  • 0

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.

  • 0
  On 15/10/2013 at 02:21, Asik said:

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!

  • 0
  On 14/10/2013 at 19:03, Phouchg said:

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!

This topic is now closed to further replies.
  • Posts

    • Yeah!!!! I was damn well shocked to see the tab with previous file open when I clicked on the new file. It had private info and I was opening another file to show another person. That was weird
    • The article says the whole setup is about the size of a pencil case which sounds pretty portable to me.
    • I thought router has to have 6 Ghz band to be called wifi-7, guess I was wrong...
    • I have the Pixel 9 Pro XL...Unless this thing is "leaps and bounds" faster than the 9, I'll pass. And by leaps and bounds, I don't mean on benchmarks. "Real world" faster. Most people don't even come close to topping out the performance of their phones. Tensor G5 is Google's most powerful chip to date, boasting a staggering 36 percent performance leap over G4.
    • MIT's stunning 'bubble wrap' device squeezes water out from thin air even in deserts by Sayan Sen Image by Matteo Roman via Pexels Massachusetts Institute of Technology (MIT) engineers have built a new kind of device that can pull clean drinking water straight out of the air—no electricity needed. It’s designed for areas where water is scarce and traditional sources like rivers or lakes aren’t reliable. Right now, more than 2.2 billion people globally don’t have access to safe drinking water. In the United States alone, 46 million face water insecurity, with either no running water or water that’s not safe to drink. This new device, called an Atmospheric Water Harvesting Window (AWHW), uses a unique hydrogel panel that looks like black bubble wrap. These dome-shaped bubbles soak up water vapor from the air, especially at night when humidity is higher. During the day, sunlight makes the vapor inside evaporate. That vapor then condenses on a glass surface and drips down through a tube, turning into drinkable water. The AWHW doesn’t rely on power sources like batteries or solar panels. It’s completely passive, meaning it works on its own. The team tested a meter-sized panel in Death Valley, California, one of the driest places in North America, and got between 57.0 and 161.5 milliliters of water per day even with humidity as low as 21 percent. That’s more than what other similar passive devices have managed. “We have built a meter-scale device that we hope to deploy in resource-limited regions, where even a solar cell is not very accessible,” said Xuanhe Zhao, a professor at MIT. “It’s a test of feasibility in scaling up this water harvesting technology. Now people can build it even larger, or make it into parallel panels, to supply drinking water to people and achieve real impact.” Another cool part of the design is how they kept the water safe to drink. Usually, these kinds of hydrogels use salts like lithium chloride to absorb more vapor but that can lead to salt leaking into the water, which isn’t ideal. To solve this, MIT’s team mixed in glycerol, a compound that helps keep salt locked inside the gel. In testing, the lithium ion concentration in the harvested water stayed below 0.06 ppm (parts per million), which is way below the safe limit. The hydrogel domes also give the material more surface area, letting it collect more vapor. The outer glass panel is coated with a special polymer film that helps cool the glass, making it easier for vapor to condense. “This is just a proof-of-concept design, and there are a lot of things we can optimize,” said lead author Chang Liu, now a professor at the National University of Singapore. “For instance, we could have a multipanel design. And we’re working on a next generation of the material to further improve its intrinsic properties.” Published in Nature Water, the study says the AWHW could last at least a year and shows promise for making safe, sustainable water in places with harsh climates. The researchers believe an array of vertical panels could one day supply water to individual households, especially in remote or off-grid locations. Source: MIT News, Nature This article was generated with some help from AI and reviewed by an editor. Under Section 107 of the Copyright Act 1976, this material is used for the purpose of news reporting. Fair use is a use permitted by copyright statute that might otherwise be infringing.
  • Recent Achievements

    • One Month Later
      Ricky Chan earned a badge
      One Month Later
    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
    • One Month Later
      Ian_ earned a badge
      One Month Later
    • Dedicated
      MacDaddyAz earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      506
    2. 2
      ATLien_0
      209
    3. 3
      Michael Scrip
      204
    4. 4
      Xenon
      143
    5. 5
      +FloatingFatMan
      121
  • Tell a friend

    Love Neowin? Tell a friend!