#1
Write a program to simulate an experiment rolling two six-sided “fair” dice. Allow the user to enter the number of rolls of the dice to simulate. What percentage of the time does the sum of the dots on the dice equal 8 in the simulation?
ANSWER:
about 8% which includes combinations (2,6), (3,5), (4,4), (5,3), (6,2) out of 72 combinations
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
/* Declare variables and function prototype. */
unsigned int seed;
int a=1, b=6, sum, dice1, dice2, k;
int rand_int(int a,int b);
/* Get seed value and interval limits. */
printf("Enter a positive integer seed value: \n");
scanf("%u",&seed);
srand(seed);
//printf("Enter integer limits a and b (a<b): \n");
//scanf("%i %i",&a,&b);
printf("Enter the amount of times to roll the dice: \n");
scanf("%i", &k);
/* Generate and print ten random numbers. */
for (int i=1; i<=k; i++){
dice1 = rand_int(a,b);
dice2 = rand_int(a,b);
printf("Random dice number 1: \n");
printf("%i ",dice1);
printf("\n");
printf("Random dice number 2: \n");
printf("%i ",dice2);
printf("\n");
sum = dice1 + dice2;
printf("the sum of the dice is: %i \n",sum);}
/* Exit program. */
return 0;
}
/* This function generates a random integer */
/* between specified limits a and b (a<b). */
int rand_int(int a,int b)
{
return rand()%(b-a+1) + a;
}
A convenient approximation for computing the factorial n! for large values of n is given by the Stirling formula: Write an integer function for computing this approximation to a factorial. Assume that the corresponding prototype is int n_fact(int n);
/* This program compares a recursive function and */
/* a nonrecursive function for computing factorials. */
#include <stdio.h>
#include <math.h>
#define PI 3.1415
#define e 2.7182
int main(void)
{
/* Declare variables and function prototypes. */
int n;
double factorial(int k);
double factorial_r(int k);
int n_fact(int n);
/* Get user input. */
printf("Enter positive integer: \n");
scanf("%i",&n);
/* Compute and print factorials. */
printf("sterling formula for the factorial: %i! = %i",n,n_fact(n));
//printf("Nonrecursive: %i! = %f \n",n,factorial(n));
//printf("Recursive: %i! = %f \n",n,factorial_r(n));
/* Exit program. */
return 0;
}
int n_fact(int n){
n = ceil((sqrt(2*PI*n))*(pow((n/e),n)));
return n;
}
Arduino speaker and servo/PIR sensor
#1
Combine your servo and your PIR sensor. Tape a large cardboard arrow on top of the servo. When
someone walks into the room, have the servo point a sign that reads "occupied". When no person is
detected, have the servo point to a sign that says "empty" If you wish to be more creative and use
something other than an arrow and have other signs that is great!
I developed 2 programs one where the sensor is triggered once and the servo moves from unoccupied
to occupied and back when the sensor is triggered only once; the other program is triggered once and
moves from unoccupied to occupied and needs to be triggered again to go back to unoccupied.
the first one
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int PIR=2;
Servo myServo;
int val = 0; //for storing the reading from the POT
void setup()
{
pinMode (PIR, INPUT);
myServo.attach(SERVO);
Serial.begin(9600);
}
void loop()
{
if (digitalRead(PIR) == HIGH)
{
myServo.write(80);
Serial.println("occupied");
delay(500);
}
else if (digitalRead(PIR) == LOW)
{
myServo.write(0);
Serial.println("unoccupied");
delay(500);
}
}
the second one
#include <Servo.h>
const int SERVO=9; //Servo on Pin 9
const int PIR=2;
Servo myServo;
int val = 1;
void setup()
{
pinMode (PIR, INPUT);
myServo.attach(SERVO);
Serial.begin(9600);
}
void loop()
{
while (val == 1)
{
if (digitalRead(PIR) == HIGH)
{
myServo.write(80);
Serial.println("triggered");
delay(2000);
val++;
Serial.println(val);
while (val == 2)
{
Serial.println("occupied");
if (digitalRead(PIR) == HIGH)
{
myServo.write(0);
delay(500);
val--;
Serial.println(val);
}
}
}
val--;
}
if (digitalRead(PIR) == LOW)
{
myServo.write(0);
Serial.println("unoccupied");
delay(500);
val++;
}
Serial.println(val);
}
#2
See if you can modify this program to play a simple song.
I made when you wish upon a star
const int kSpkr = 9;
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_F45 370
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_Bb4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_D5 587
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_G5 784
#define NOTE_C6 1047
void setup() {
pinMode(kSpkr, OUTPUT);
}
void loop() {
tone(kSpkr, NOTE_C4, 500);
delay(500);
tone(kSpkr, NOTE_C5, 500);
delay(500);
tone(kSpkr, NOTE_Bb4, 500);
delay(500);
tone(kSpkr, NOTE_A4, 500);
delay(500);
tone(kSpkr, NOTE_F45, 500);
delay(500);
tone(kSpkr, NOTE_G4, 500);
delay(500);
tone(kSpkr, NOTE_D5, 1000);
delay(500);
noTone(kSpkr);
delay(50);
tone(kSpkr, NOTE_E4, 500);
delay(500);
tone(kSpkr, NOTE_E5, 500);
delay(500);
tone(kSpkr, NOTE_D5, 500);
delay(500);
tone(kSpkr, NOTE_C5, 500);
delay(500);
tone(kSpkr, NOTE_B4, 500);
delay(500);
tone(kSpkr, NOTE_C5, 500);
delay(500);
tone(kSpkr, NOTE_F5, 1000);
delay(500);
noTone(kSpkr);
delay(50);
tone(kSpkr, NOTE_G5, 500);
delay(500);
tone(kSpkr, NOTE_F5, 500);
delay(550);
tone(kSpkr, NOTE_E5, 500);
delay(550);
tone(kSpkr, NOTE_D5, 500);
delay(550);
tone(kSpkr, NOTE_C5, 500);
delay(550);
tone(kSpkr, NOTE_Bb4, 550);
delay(600);
tone(kSpkr, NOTE_A4, 600);
delay(650);
tone(kSpkr, NOTE_G4, 650);
delay(700);
tone(kSpkr, NOTE_D5, 700);
delay(750);
tone(kSpkr, NOTE_G5, 700);
delay(750);
tone(kSpkr, NOTE_C6, 1500);
delay(2000);
}


No comments:
Post a Comment