Arrays
Declaration
int array1[5]; (number is how much components in the array)
this array includes the components: array1[0], array1[1], array1[2], array1[3], array1[4]
This process can be also obviated in watch window.
Notice: the index of components start from “0“
Declaration can also combine with assignment
int array1[5] = {3,6,5,7,2};
And compiler can fill in the dimension
int array1[] = {3,6,5,7,2}; // compiler will know the size of array1 according to elements.
Memory Window
(view the RAM of the computer while debugging)
Pre-built mathematical functions(math function)
powf (x, y)
Where x is the base value.
And y is the power value.
Use assignment to save the result into a variable.
a = powf (2, 3);
Example:
Requirements (Powers Printer)
Design
Declare variables => printf(“enter”) => scanf() => calculation (power = base * power) => printf * 8
Implementation
Test
Errors Encountered
Debugging
Use the wrong base, should be the variable power*= base
sqrtf (x)
Where x is the number to square root.
Use assignment to save the result into a variable.
a = sqrtf (16.0f);
expf (x)
Where x is nth power to raise the number e to.
Use assignment to save the result into a variable.
a = expf (1);
logf (x)
Calculate the natural log (ln) of x.
Use assignment to save the result into a variable.
a = logf (2.7f);
log10f(x)
Calculate the base-10 log of x.
Use assignment to save the result into a variable.
a = log10f(1000);
fabsf (x)
Which calculates the absolute value of x.
Use assignment to save the result into a variable.
a = fabsf (-5);
floorf (x)
Calculates the floor value of x… rounds x downwards.
Use assignment to save the result into a variable.
a = floorf (3.75f);
ceilf(x)
Calculates the ceiling value of x… rounds x upwards.
Use assignment to save the result into a variable.
a = ceilf (3.75f);
sinf(a)
Compute the sine of a, in radians.
cosf(a)
Compute the cosine of a, in radians.
tanf(a)
Compute the tangent of a, in radians.
Generating Random Numbers
Call srand () first… once at the start of your program.
To get access to this function: #include <stdlib.h>
Pass in the result from time () function…
To get access to this function: #include <time.h>
Then to get a random number…
Call the rand (); function.
Rand returns an integer between 0and value RAND_MAX.
RAND_MAX is worth: 32767
A dice with 32767 faces… this is too big for a dice roll…
So we use modulus to get a number that is in a more reasonable result…
time (0) is get time from system, and the result will be used in srand () to create a seed for random number. And then the rand () evaluate a random number.
Requirements (Five Dice)
Design
get a random number1 -5 => print out
Implementation
Test