You are on page 1of 21

OUR PRESENTATION ON CHAPTER 3 OF C

GROUP MEMBERS: OCKOURI BARNES


MALIK ALLWOOD
MALAKAI ALLWOOD
PICCARD HARRIS
SUBJECT: COMPUTER SCIENCE
POOL 4
MR. K POWELL
MODULE 3 PROGRAMMING
THEME: C PROGRAMMING
*/Build a number guessing game that uses input validation
(isdigit() function) to verify that the user has entered a digit
and not a non-digit (letter). Store a random number between 1 and
10 into a variable each time the program is run. Prompt the user
to guess a number between 1 and 10 and alert the user if he was
correct or not.

*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

int main()
{
int iGuessNum;
int iRandomNum;
const int iUPPER = 10;
const int iLOWER = 1;

srand(time(NULL));

printf("Enter a number between 1 and 10: ");

if (scanf("%d", &iGuessNum) == 1)
{
iRandomNum = (rand() % (iUPPER - iLOWER + iLOWER)) +
iLOWER;
if (iGuessNum == iRandomNum)
{
printf("You guessed correctly!\n");
}
else
{
printf("You guessed wrong!\n");
}
}
else
{
printf("Please enter a number.\n");
}

return 0;
}
/*
Create a dice game that uses two six-sided dice. Each time the
program runs, use random numbers to assign values to each die
variable. Output a “player wins” message to the user if the sum
of the two dice is 7 or 11. Otherwise output the sum of the two
dice and thank the user for playing.
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int iDie1;
int iDie2;
const int iUPPER = 6;
const int iLOWER = 1;

srand(time(NULL));
iDie1 = (rand() % (iUPPER - iLOWER + iLOWER)) + iLOWER;
iDie2 = (rand() % (iUPPER - iLOWER + iLOWER)) + iLOWER;

if (iDie1 + iDie2 == 7 || iDie1 + iDie2 == 11)


{
printf("You won!!!");
}
else
{
printf("Sum of dice: %d\n", iDie1 + iDie2);
printf("Thank you for playing!");
}
return 0;
}
/* Fig. 3.6: fig03_06.c
2 Class average program with */
#include <stdio.h>

/* function main begins program execution */


int main(void)
{
int counter;
int grade; /* grade value */
int total; /* sum of grades input by user */
int average; /* average of grades */

/* initialization phase */
total = 0; /* initialize total */
counter = 1;

/* processing phase */
while (counter <= 5)
{ /* loop 5 times */
printf("Enter grade: "); /* prompt for input */
scanf("%d", &grade); /* read grade from
user */
total = total + grade; /* add grade to total
*/
counter = counter + 1;
} /* end while */

/* termination phase */
average = total / 10; /*
integer division */
printf("Class average is %d\n", average); /*
display result */
return 0; /*
indicate program ended successfully */
} /* end function main */
/* Fig. 3.6: fig03_06.c
2 Class average program with */
#include <stdio.h>

/* function main begins program execution */


int main(void)
{
int counter;
int grade; /* grade value */
int total; /* sum of grades input by user */
int average; /* average of grades */

/* initialization phase */
total = 0; /* initialize total */
counter = 1;

/* processing phase */
while (counter <= 5)
{ /* loop 5 times */
printf("Enter grade: "); /* prompt for input */
scanf("%d", &grade); /* read grade from
user */
total = total + grade; /* add grade to total
*/
counter = counter + 1;
} /* end while */

/* termination phase */
average = total / 10; /*
integer division */
printf("Class average is %d\n", average); /*
display result */
return 0; /*
indicate program ended successfully */
} /* end function main */
/* Fig. 3.8: fig03_08.c
Class average program with sentinel-controlled repetition */
#include <stdio.h>

/* function main begins program execution */


int main(void)
{
int counter; /* number of grades entered */
int grade; /* grade value */
int total; /* sum of grades */

float average; /* number with decimal point for average */

/* initialization phase */
total = 0; /* initialize total */
counter = 0; /* initialize loop counter */

/* processing phase */
/* get first grade from user */
printf("Enter grade, -1 to end: "); /* prompt for input */
scanf("%d", &grade); /* read grade from user */

while (counter < 4) /* loops five times */


{
total = total + grade; /* add grade to total */
counter = counter + 1; /* increment counter */

/* get next grade from user */


printf("Enter grade, -1 to end: ");
scanf("%d", &grade);
} /* end while */

/* termination phase */
/* if user entered at least one grade */
if (counter != 0)
{
/* calculate average of all grades entered */
average = (float)total / counter; /* avoid truncation */

/* display average with two digits of precision */


printf("Class average is %.2f\n", average);
} /* end if */
else
{ /* if no grades were entered, output message */
printf("No grades were entered\n");
} /* end else */

return 0; /* indicate program ended successfully */


} /* end function main */
#include <stdio.h>

void main()
{

int iResponse = 0;

printf("\n\tAC Control Unit\n");


printf("\n1\tTurn the AC on\n");
printf("2\tTurn the AC off\n");
printf("3\tTurn the Timer on\n");
printf("4\tTurn the Timer off\n");
printf("\nEnter your selection: ");
scanf("%d", &iResponse);

if (iResponse == 1)
printf("\nAC is now on\n");
if (iResponse == 2)
printf("\nAC is now off\n");
if (iResponse == 3)
printf("\nTimer is now on");
if (iResponse == 4)
printf("\nTimer is now off");
}
#include <stdio.h>
void main()
{

int iSelection = 0;
float fTransAmount = 0.0;
float fBalance = 100.25;

printf("\n\tATM\n");
printf("\n1\tDeposit Funds\n");
printf("2\tWithdraw Funds\n");
printf("3\tCheck Balance\n");
printf("4\tTransfer Funds\n");
printf("\nEnter your selection: ");
scanf("%d", &iSelection);

if (iSelection == 1)
{
printf("\nEnter fund amount to deposit: ");
scanf("%f", &fTransAmount);
printf("\nYour new balance is: $%.2f\n", fBalance +
fTransAmount);
} //end if
if (iSelection == 2)
{
printf("\nEnter fund amount to withdraw: ");
scanf("%f", &fTransAmount);
if (fTransAmount > fBalance)
printf("\nInsufficient funds\n");
else
printf("\nYour new balance is $%.2f\n", fBalance -
fTransAmount);
} //end if
if (iSelection == 3)
{
printf("\nYour balance is: $%.2f\n", fBalance);
} //end if
if (iSelection == 4)
{
printf("\nYour Transfer Fund is: $%.2f\n", fTransAmount);
} //end if

} //end main function


#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void main()
{
int iRandomNum = 0;
srand(time(NULL));
iRandomNum = (rand() % 4) + 1;
printf("\nFortune Cookie - Chapter 3\n");
switch (iRandomNum)
{
case 1:
printf("\nYou will meet a new friend
today.\n");
break;
case 2:
printf("\nYou will enjoy a long and happy
life.\n");
break;
case 3:
printf("\nOpportunity knocks softly. Can you
hear it?\n");
break;
case 4:
printf("\nYou'll be financially rewarded for
your good deeds.\n");
break;
default:
printf("\nYou've gained zero fortune");
} //end switch
printf("\nLucky lotto numbers: ");
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d\n", (rand() % 49) + 1);
} //end main function
/* Fig. 3.8: fig03_08.c
Class average program with sentinel-controlled repetition */
#include <stdio.h>

/* function main begins program execution */


int main(void)
{
int counter; /* number of grades entered */
int grade; /* grade value */
int total; /* sum of grades */

float average; /* number with decimal point for average */

/* initialization phase */
total = 0; /* initialize total */
counter = 0; /* initialize loop counter */

/* processing phase */
/* get first grade from user */
printf("Enter grade, -1 to end: "); /* prompt for input */
scanf("%d", &grade); /* read grade from user
*/

do
{
total = total + grade; /* add grade to total */
counter = counter + 1; /* increment counter */

/* get next grade from user */


printf("Enter grade, -1 to end: ");
scanf("%d", &grade);
} while (counter < 4); /* loops five times */

/* termination phase */
/* if user entered at least one grade */
if (counter != 0)
{
/* calculate average of all grades entered */
average = (float)total / counter; /* avoid truncation */

/* display average with two digits of precision */


printf("Class average is %.2f\n", average);
} /* end if */
else
{ /* if no grades were entered, output message */
printf("No grades were entered\n");
} /* end else */

return 0; /* indicate program ended successfully */


} /* end function main */
C Codes With Output from C Programming for Absolute beginners
Figure 3.5
C Code: Output:
Figure 3.6

C Code:

Output:
Figure 3.8
C Code:

Output:
Question 1:
C Code:

Output:
Guessed correctly

Guessed incorrectly

Validation test
Question 3:
C Code:

Output:
C Codes With Output from C How to Program
Figure 3.5
Modified to execute for only five students
C Code:

Output:
Is repetition in Figure 3.5 bounded or unbounded? It is bounded
While loop converted to a Do While Loop:
C Code:

Output:
Figure 3.8
Modified to execute for only five students
C Code:

Output:
Is repetition bounded or unbounded? - It is unbounded

C Code:

Output:

You might also like