Question

Hello

these are midterm study guide questions for my C++ class. I need some clarification on how to understand and write these functions.

Can someone write out examples and use definitions on how to go about the stuff listed below?

again this is for an introductory C++ computer science class

Functions Predefined Functions Know basic predefined functions covered from <cmath> Know how to use the rand) function from <

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Functions are like black boxes, it hides the implementation from the client, client don't need to get involved into how the function has been implemented.

Client only need to know the function prototype that is what arguments it will take what the function is suppose to do and what is the return value of the function.

Implementation of the function is hidden here.

All inbuilt data types that is int , float, double short uses pass by value which means when you call a method with an argument, the value of the argument is copied and then assigned to the function parameter

Here is the remaining code with details comment.

===============================================================================

#include<iostream>

#include<cmath> // needed to included the header file before using the pow() and sqrt() function

#include<stdlib.h> // needed to include the header file before using rand() function

// this is the function prototype

// this function takes two numbers = number and power

// returns the number raised to the power

// eg 2^10 = 1024

// here number is 2, 10 is the power and the function returns 1024

int raiseToThePower(int number, int power);

// function prototype, returns a double which is the square root of the number passed to this function

double findSquareRoot(double number);

// calculates the temperature in celsius and returns the value

// function prototype

// takes in temeprature in fahrenheit and returns value in celsius

double toCelsius(double temperature);

// function prototype

// takes in two values min and max and returns a random number

// between these two numbers

int getRandomNumber(int min, int max);

// function implementation

int raiseToThePower(int number, int power){

                // using cmath function to find the value

                // and then returning the value from this function

                return pow(number,power);

}

// function implementation

double findSquareRoot(double number){

                // using cmath function to find the value

                // and then returning the value from this function

                return sqrt(number);

}

// function implementation

double toCelsius(double temperature){

                return (temperature-32)*5/9;   

}

// function implementation

int getRandomNumber(int min, int max){

                               

                int randomNumber = rand()/(max-min);

                return randomNumber + min+1;

}

int main(){

               

                using namespace std;

                               

                int number = raiseToThePower(2,10); // invoking function which returns the result

                cout<<"2 raised to the power of 10 is "<<number<<endl;

               

                double squareRoot = findSquareRoot(25); // invoking function which returns the result

                cout<<"Square root of 25 is "<<squareRoot<<endl;

                double celsius = toCelsius(212); // invoking function which returns the result

                cout<<"Temperature 212F in celsius is "<<celsius<<endl;

                int randomNumber = getRandomNumber(50,100); // invoking function which returns the result

                cout<<"Random number between 50 and 100 is "<<randomNumber<<endl;

               

}

Add a comment
Know the answer?
Add Answer to:
Hello these are midterm study guide questions for my C++ class. I need some clarification on how to understand and write these functions. Can someone write out examples and use definitions on how to g...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT