Content of the lesson:
To let our programs solve more difficult problems you have to use more functions like powers and roots. We are going to show these function inside the programming language C.
Note: To be able to use these functions you have to add the header file (also called library) math.h. A header file is a file which contains declaration of additional functions (for example mathematical) which can be used after inserting the library, otherwise our program would not be able to use these functions and it would report a problem.
#include "stdafx.h";
#include <math.h>
int _tmain(int argc, _TCHAR* argv[])
{
...
return 0;
}
To compute powers you can use the pow function (shortcut from power). You can see the following text in the reference manual of C language:
double pow (double base, double exponent );
long double pow ( long double base, long double exponent );
float pow (float base, float exponent );
double pow (double base, int exponent );
long double pow (long double base, int exponent );
Raise to power.
Returns base raised to the power exponent.
The definition tells that the function can be used by the pow command which has 2 parameters:
The reference manual offers more variants of notation - these variants show which data type can be used for the input values and the data type of the result.
In case you want your program to compute and write the result of the expression a5 you have to write printf("%f", pow(a,5));
To compute the square root function you can use the sqrt function (shortcut for square root). Using the reference manual you can see this:
double sqrt (double x );
float sqrt (float x );
long double sqrt (long double x );
Compute square root.
Returns the square root of x.
The power and sqrt function use the real numbers so you have to use another types of variables when working with these functions. These data types are float, double and long double. The list of these variables was mentioned in the previous lesson but you can see it once more for revision:
Data type |
Allowed shortcut |
Description |
Range |
Integer data types with sign |
|||
signed int |
int |
Integer number with sign |
from - 2 147 483 648 |
signed short int |
short int or short |
Short integer number with sign |
from - 32 768 |
signed long int |
long int or long |
Long integer number with sign |
from - 2 147 483 648 |
signed char |
char |
Char with sign |
from - 128 |
Integer data types without sign |
|||
signed int |
|
Integer number without sign |
from 0 |
signed short int |
signed short |
Short integer number without sign |
from 0 |
signed long int |
signed long |
Long integer number without sign |
from 0 |
signed char |
|
Char without sign |
from 0 |
Real data types |
|||
float |
|
Real number with single accuracy |
from 3.4×10-38 |
double |
|
Real number with double accuracy |
from 1.7×10-308 |
long double |
|
Real number with extended accuracy |
from 3.4×10-38 |
#include "stdafx.h"
#include <math.h>
int _tmain(int argc, _TCHAR* argv[])
{
float a, b, e;
printf("Zadejte prosim zaklad mocniny: ");
scanf("%f",&a);
printf("Zadejte prosim exponent mocniny: ");
scanf("%f",&e);
b = pow(a,e);
printf("Vyraz %f^%f = %f\n", a, e, b);
getchar();
return 0;
}
If you launch the program you can see that all results are displayed with 6 decimal places which can be unpractical so we have to set the number of decimal places. Adjust the program in the following way:
#include "stdafx.h"
#include <math.h>
int _tmain(int argc, _TCHAR* argv[])
{
float a, b, e;
printf("Zadejte prosim zaklad mocniny: ");
scanf("%f",&a);
printf("Zadejte prosim exponent mocniny: ");
scanf("%f",&e);
b = pow(a,e);
printf("Vyraz %5.2f^%5.2f = %5.2f\n", a, e, b);
fflush(stdin);
getchar();
return 0;
}
If you do not want to specify the minimal number of characters but only the number of decimal places, ou can use the notation %5.2f with syntax %.2f.
In case you need the value of PI in C language, you can define it as a constant. To define a constant you have to use the directive #define which has to be written at the beginning of the program similarly as the directives #include (which were used in the previous part to insert a header file).
To define a constant use the following syntax:
#define JMENO HODNOTA
You can see two things in the previous syntax: as for the directive #include the line is not ended by a semicolon and the data type is not written (the syntax is written in the form "#define space name of constant space value).
The definition of the pi constant is shown in the following example (note: the value can be changed according to required accuracy):
#define PI 3.14159
Write a program to compute the capacity of any cylinder and any sphere (user should be able to set values). Find required formulas in the Internet.
Write a program to compute this formula: ax+by. User should be able to set all values.