Sei sulla pagina 1di 2

Programming Lab 7 - Functions

Task 1
Write a C++ function whose prototype is double pow2(double) that returns the square of the argument.
Test the function in a main program section.

Task 2
Write a C++ function whose prototype is double oplus(double, double) that returns the square-root
of the sum of the squares of the arguments. Test the function in a main program section.

Task 3
The following program inputs the radius and height of a cone and outputs its surface area and volume.
Copy the program and complete it by implementing the missing functions getArea() and getVolume().
The arguments of the functions are the cone radius and cone height respectively.
#include <iostream>
#include <cmath>
using namespace std;
double getArea(double r, double h);
double getVolume(double r, double h);
int main() {
double r, h;
cout << "Input the radius and height of the cone: ";
cin >> r >> h;
cout << "The area of the cone is " << getArea(r, h) << endl;
cout << "The volume of the cone is " << getVolume(r, h) << endl;
}
Test your functions by inputting various values and checking the results with your pocket calculator.

Task 4
Write a function that provides a score-to-grade conversion [See Lab 3 Task 1 for reference].
The prototype for the function is given below; its arguments are a students first, second, and final exam
scores in percent respectively.
string getGrade(double, double, double);
Test your function by including it in a main program and inputting various scores.

Task 5
Write a void function whose prototype is void bar(int, string) that outputs n occurrences of a string.
Examples:
bar(8,"#");
results in the output:
########
bar(4,"E");
results in the output:
EEEE
bar(3,"help!");
results in the output:
help!help!help!
Test your function by including it in a main program and executing the above examples.

Programming Lab 7 - Functions


Task 6
Implement the following prototyped function that sets referenced variables N (neutron number) and M (the
atomic mass) for the given atom with atomic number Z. .
void atom( int Z, int &N, double &M );
Hint: complete the following program by including the definition of the function.
int main() {
int Z, N;
double M;
cout << "Input atomic number Z: ";
cin >> Z;
atom( Z, N, M ); // Get values N and M.
cout << "Z=" << Z << " N=" << N << " M=" << M << endl;
}

If data is not available for atom Z then the function should output the message:
Data for atom Z is not available! and the values of N and M should be set to zero.
Your function should include data for some atoms; for this you can use the data provided in the table below.
You find a complete list of elements at http://en.wikipedia.org/wiki/List_of_elements

Download from http://www1.gantep.edu.tr/~eee241/sharepoint/atoms.txt


Z
1
2
3
4
5
6
7
8
9
10
11
12

Symbol
H
He
Li
Be
B
C
N
O
F
Ne
Na
Mg

#Neutrons
0
2
4
5
6
6
7
8
10
10
12
12

Mass (amu)
1.00794
4.002602
6.941
9.012182
10.811
12.0107
14.00674
15.9994
18.998404
20.1797
22.9898
24.305

Classification
Non-metal
Noble Gas
Alkali Metal
Alkaline Earth
Metaloid
Non-Metal
Non-Metal
Non-Metal
Halogen
Noble Gas
Alkali Metal
Alkaline Earth

From http://www.chemicalelements.com/

Also, consider expanding your function to provide more information; for example the name and symbol of the
atom, and its classification.

In your own time


Study the notes and perform the exercises that can be found at
http://www1.gantep.edu.tr/~cpp/tutorialbasic.php?topic=5
http://www1.gantep.edu.tr/~cpp/tutorialbasic.php?topic=6

Potrebbero piacerti anche