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 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;
}
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...