Arrays, Memory Window, Math, Random Numbers

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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值