Anda di halaman 1dari 5

//tutorial 4 qns 8

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void guessgame(void);
int main(void){
srand(time(NULL));
guessgame();
}
void guessgame(void)
{
int x;
int play;
char guess;
do{
x = 65 + rand() % 25;
printf("Please enter a letter from A to Z\n");
scanf("\n%c", &guess);
while (guess != x){
if (guess < x)
printf("Too low! Please try again:\n");
else
printf("Too high! Please try again:\n");
scanf(" %c", &guess);
}
printf("Great! Your guess is right\n");
printf("Would you like to play again?\n");
printf("Please Enter 1 for Yes and 2 For No:\n");
scanf("%d", &play);
} while (play == 1);
}
//Tutorial 5 qns 4
#include <stdio.h>
#define size 9
void printcomission(double c[], int w);
int main(void)
{
double b, f; // sales amount
double a[30] = { 0 }; // to store sales amount
size_t i;
int m; // number of employee
printf("Please enter the number of employees to be calculated, maximum 3
0 employee:\n");
scanf("%d", &m);
printf("Please enter the sales amount of the first employee:\n");
scanf("%lf", &b);
a[0] = b;
for(i = 1; i < m; ++i){

printf(" Please enter the next employee sales amount.\n");


scanf("%lf", &f);
a[i] = f;
}
printcomission(a, m);
}
void printcomission(double c[], int w)
{
int n[size] = {0};//comission array
size_t g;//counter
for (g = 0; g < w; ++g){
c[g] *= 0.06;
}
for (g = 0; g < w; ++g){
if (c[g]>0 && c[g] <= 120)
++n[0];
else if (c[g]>121 && c[g] <= 240)
++n[1];
else if (c[g]>241 && c[g] <= 360)
++n[2];
else if (c[g]>361 && c[g] <= 480)
++n[3];
else if (c[g]>481 && c[g] <= 600)
++n[4];
else if (c[g]>601 && c[g] <= 720)
++n[5];
else if (c[g]>721 && c[g] <= 840)
++n[6];
else if (c[g]>841 && c[g] <= 960)
++n[7];
else
++n[8];
}
printf("Comission amount\tno\n");
for (g =0 ; g < 9; ++g){
printf("%d-%d%5d\n", 120*g+120+1,240+g*120, n[g]);
}
}
// tutorial 5 qns 6
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 13
int main(void){
int dice1 = 0;
int dice2 = 0;
int sum = 0;
size_t i;
int count[MAX] = { 0 };
srand(time(NULL));
for (i = 0; i < 36000; ++i){
dice1 = 1 + (rand() % 6);
dice2 = 1 + (rand() % 6);
sum = dice1 + dice2;

++count[sum];
}
printf("Roll sum\tNumber of times\n");
for (i = 0; i <= 12; ++i){
printf("%4d%17d\n", i, count[i]);
}
}
//tutorial 5 qns 7
#include <stdio.h>
#define MAX 40
int main(void){
int a[40] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2,
7,
6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
int b[10] = { 0 };
size_t i;
size_t pass;
int hold;
size_t e;
int k = 0;
for (pass = 1; pass < 40; ++pass){
for (i = 0; i < 39; ++i){
if (a[i]>a[i + 1]){
hold = a[i];
a[i] = a[i + 1];
a[i + 1] = hold;
}
}
}
printf("The newly sort array is:\n");
for (i = 0; i < 40; ++i){
printf("%d", a[i]);
}
puts("");
for (i = 0; i < 40; ++i){
switch (a[i]){
case 1:
++b[0];
break;
case 2:
++b[1];
break;
case 3:
++b[2];
break;
case 4:
++b[3];
break;
case 5:
++b[4];
break;
case 6:
++b[5];
break;
case 7:

++b[6];
break;
case 8:
++b[7];
break;
case 9:
++b[8];
break;
case 10:
++b[9];
break;
}
}
printf("Response\tVaue\tHistogram\n");
for (e = 1; e <= 10; ++e){
printf("%d\t\t%d\t", e, b[e - 1]);
for (i = 0; i < b[k];++i){
printf("*");
}
// Tutorial 5 qns 8
#include <stdio.h>
void printheader(void);
int main(void){
int total=0;
int a[4][5] = { 0 };
size_t i;
size_t j;
printf("Please enter the number of product sold by the salesperson,start
ing with p0 to p4.\n");
printf("Press enter after each entry.\n");
for (i = 0; i <= 3; ++i){
for (j = 0; j <= 4; ++j){
scanf("%d", &a[i][j]);
}
if (i == 3){
break;
}
printf("Please enter the sales detail of the next salesperson.\n
");
}
printheader();
for (j = 0; j <= 3; ++j){
printf("%d\t\t", j);
for (i = 0; i <= 4; ++i){
printf("%d\t", a[j][i]);
total += a[j][i];
}
printf("%d\n", total);
total = 0;
}
total = 0;
printf("\t");
for (j = 0; j <= 4; ++j){
for (i = 0; i <= 3; ++i){

total += a[i][j];
}
printf("\t%d", total);
total = 0;
}
puts("");
}
void printheader(void){
int i;
printf("Salesperson");
//printf("\t\t");
for (i = 0; i <= 4; ++i){
printf("\tp%d", i);
}
puts("");
}