Functions

1.One foot equals 12 inches. Design a function named
feetTolnches that accepts a number of feet as an argument, and returns the number of inches in that many feet. Use the function in a program that prompts the user to enter a number of feet and then displays the number of inches in that many feet.
2.Rainfall Statistics
Design a program that lets the user enter the total rainfall for each of 12 months into an array. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts

Questions by pokhara

Showing Answers 1 - 6 of 6 Answers

George

  • Jul 29th, 2014
 

In Haskell (please excuse syntax highlighting; they dont have it for this language):

Code
  1. -- feetToInches.hs

  2. module Main where

  3.  

  4. feetToInches :: Num a => a -> a

  5. feetToInches = (*12)

  6.  

  7. main :: IO ()

  8. main = do

  9.   feet <- putStr "Enter number of feet to convert: ""Enter rainfall for " ++ (snd . (!!n)) months ++ ": ") >> getLine) [0..11]

  10.   let values = map (read :: String -> Float) inputs

  11.   let f g = snd . g $ zip values [0..]

  12.  

  13.   mapM_ putStrLn ["Total rainfall: ""Mean rainfall: ""Month with highest rainfall: " ++ snd (months !! (f maximum)),

  14.                   "Month with lowest rainfall: " ++ snd (months !! (f minimum))]

  15.   where months = zip [0..] ["January", "February", "March", "April", "May", "June", "July",

  16.                             "August", "September", "October", "November", "December"]  

  Was this answer useful?  Yes

Sunil shenoy

  • May 16th, 2016
 

Code
  1. #include<iostream>

  2. using namespace std;

  3. float ft_to_inch(float ft)

  4. {

  5.     return ft*12;

  6. }

  7. main()

  8. {

  9.     float f,i;

  10.     cout<<"Enter a Feet value:"<<endl;

  11.     cin>>f;

  12.     i=ft_to_inch(f);

  13.     cout<<"result in Inches: "<<i<<endl;

  14. }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions