Compare commits
13 Commits
c71ffdd716
...
main
Author | SHA1 | Date | |
---|---|---|---|
8b59e98a7b | |||
4e9b002e99 | |||
191705c1dc | |||
5bcb1746fe | |||
50a27f1d6c | |||
785c2b0e50 | |||
991478f774 | |||
de35a9dd15 | |||
46e9d36a27 | |||
9e90bec9f7 | |||
24d05e18d6 | |||
00f742e3bc | |||
9b9861b7c4 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.out
|
13
DeitelC/Chapter4/asciiPrint.c
Normal file
13
DeitelC/Chapter4/asciiPrint.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
|
||||
for(int i = 0; i < 128; i++) {
|
||||
if(i % 10 == 0) {
|
||||
printf(" %c\n", i);
|
||||
} else {
|
||||
printf(" %c", i);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
20
DeitelC/Chapter4/barChart.c
Normal file
20
DeitelC/Chapter4/barChart.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
puts("Enter 5 different integers between 1 - 30!");
|
||||
for(int i = 0; i < 5; i++) {
|
||||
printf("Enter %d. integer: ",(i+1));
|
||||
int j;
|
||||
if(!scanf("%d", &j)) {
|
||||
puts("Enter a valid integer!");
|
||||
} else if((j < 1 || j > 30)) {
|
||||
puts("Integer not between 1-30!");
|
||||
}
|
||||
|
||||
for(int t = 0; t < j; t++) {
|
||||
printf("%s","*");
|
||||
}
|
||||
puts("");
|
||||
|
||||
}
|
||||
}
|
36
DeitelC/Chapter4/calculateSales.c
Normal file
36
DeitelC/Chapter4/calculateSales.c
Normal file
@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main (void){
|
||||
int i, j;
|
||||
float total = 0;
|
||||
while(1) {
|
||||
puts("Enter item number and how many sold: ");
|
||||
scanf("%d%d", &i, &j);
|
||||
if(i == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch(i) {
|
||||
default:
|
||||
puts("Invalid value!");
|
||||
continue;
|
||||
case 1:
|
||||
total += 2.98 * j;
|
||||
break;
|
||||
case 2:
|
||||
total += 4.50 * j;
|
||||
break;
|
||||
case 3:
|
||||
total += 9.98 * j;
|
||||
break;
|
||||
case 4:
|
||||
total += 4.49 * j;
|
||||
break;
|
||||
case 5:
|
||||
total += 6.87 * j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("Last weeks total retail price: %.2f\n", total);
|
||||
return 0;
|
||||
}
|
33
DeitelC/Chapter4/creditLimit.c
Normal file
33
DeitelC/Chapter4/creditLimit.c
Normal file
@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
|
||||
int accNum;
|
||||
float limit, debt;
|
||||
puts("Enter 3 customer details!");
|
||||
for(int counter = 0; counter < 3; counter++){
|
||||
puts(" ");
|
||||
puts("Enter account number: ");
|
||||
int a = scanf("%d", &accNum);
|
||||
puts("Enter credit limit: ");
|
||||
int b = scanf("%f", &limit);
|
||||
puts("Enter current debt: ");
|
||||
int c = scanf("%f", &debt);
|
||||
|
||||
|
||||
|
||||
if (!a || !b || !c) {
|
||||
puts ("Enter valid data!");
|
||||
}
|
||||
|
||||
printf("Account number: %d, Account old credit limit: %.2f, Account current debt: %.2f.\n", accNum, limit, debt);
|
||||
|
||||
if(limit/2 < debt) {
|
||||
puts("WARNING! ACCOUNT DEBT MORE THAN NEW LIMIT!");
|
||||
printf("New credit limit: %.2f, Current debt: %.2f\n", (limit/2), debt);
|
||||
} else {
|
||||
printf("New credit limit: %.2f, Current debt: %.2f\n", (limit/2), debt);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
48
DeitelC/Chapter4/diamond.c
Normal file
48
DeitelC/Chapter4/diamond.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
int odd;
|
||||
|
||||
// Prompt user for input
|
||||
puts("Enter an odd number: ");
|
||||
if (!scanf("%d", &odd) || odd % 2 == 0) {
|
||||
puts("Enter a valid odd integer!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int mid = odd / 2; // Middle point of the diamond
|
||||
|
||||
// Upper half of the diamond
|
||||
for (int i = 0; i < mid; i++) {
|
||||
// Print leading spaces
|
||||
for (int j = 0; j < mid - i; j++) {
|
||||
printf(" ");
|
||||
}
|
||||
// Print asterisks
|
||||
for (int j = 0; j < 2 * i + 1; j++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Middle line of the diamond
|
||||
for (int i = 0; i < odd; i++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// Lower half of the diamond
|
||||
for (int i = mid - 1; i >= 0; i--) {
|
||||
// Print leading spaces
|
||||
for (int j = 0; j < mid - i; j++) {
|
||||
printf(" ");
|
||||
}
|
||||
// Print asterisks
|
||||
for (int j = 0; j < 2 * i + 1; j++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
18
DeitelC/Chapter4/exercise4-24.c
Normal file
18
DeitelC/Chapter4/exercise4-24.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
|
||||
int i = 5, j = 7, k = 4, m = -2;
|
||||
|
||||
printf("%d\n", i == 5);
|
||||
printf("%d\n", j != 3);
|
||||
printf("%d\n", i >= 5 && j < 4);
|
||||
printf("%d\n", !m && k > m);
|
||||
printf("%d\n", !k || m);
|
||||
printf("%d\n", k - m < j || 5 - j >= k);
|
||||
printf("%d\n", j + m <= i && !0);
|
||||
printf("%d\n", !(j - m));
|
||||
printf("%d\n", !(k > m));
|
||||
printf("%d\n", !(j > k));
|
||||
printf("%d\n", !m);
|
||||
}
|
20
DeitelC/Chapter4/integerConverter.c
Normal file
20
DeitelC/Chapter4/integerConverter.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
for (int i = 0; i < 257; i++) {
|
||||
int t = i;
|
||||
int binary = 0;
|
||||
int mult = 1;
|
||||
|
||||
while (t > 0) {
|
||||
binary += (t % 2) * mult;
|
||||
t /= 2;
|
||||
mult *= 10;
|
||||
}
|
||||
|
||||
printf("Int: %d, Bin: %d, Octal: %o, Hex: %X\n", i, binary, i, i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
12
DeitelC/Chapter4/pi.c
Normal file
12
DeitelC/Chapter4/pi.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void){
|
||||
|
||||
float pi = 4.0;
|
||||
int sign = -1;
|
||||
for (int i = 3; i < 8092; i += 2) {
|
||||
pi += sign*(4.0/i);
|
||||
sign = -sign;
|
||||
}
|
||||
printf("%.6f\n",pi);
|
||||
}
|
49
DeitelC/Chapter4/test2.c
Normal file
49
DeitelC/Chapter4/test2.c
Normal file
@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
int odd;
|
||||
|
||||
// Prompt user for input
|
||||
puts("Enter an odd number: ");
|
||||
if (!scanf("%d", &odd) || odd % 2 == 0) {
|
||||
puts("Enter a valid odd integer!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Print the diamond pattern
|
||||
int mid = odd / 2; // Middle point of the diamond
|
||||
|
||||
// Upper half of the diamond
|
||||
for (int i = 0; i < mid; i++) {
|
||||
// Print leading spaces
|
||||
for (int j = 0; j < mid - i; j++) {
|
||||
printf(" ");
|
||||
}
|
||||
// Print asterisks
|
||||
for (int j = 0; j < 2 * i + 1; j++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Middle line of the diamond
|
||||
for (int i = 0; i < odd; i++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// Lower half of the diamond
|
||||
for (int i = mid - 1; i >= 0; i--) {
|
||||
// Print leading spaces
|
||||
for (int j = 0; j < mid - i; j++) {
|
||||
printf(" ");
|
||||
}
|
||||
// Print asterisks
|
||||
for (int j = 0; j < 2 * i + 1; j++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
29
DeitelC/Chapter5/carRental.c
Normal file
29
DeitelC/Chapter5/carRental.c
Normal file
@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
|
||||
double calculateCharges (int hours);
|
||||
|
||||
int main (void) {
|
||||
printf("%5s\t%5s\t%5s\n", "Car", "Hours", "Charge");
|
||||
printf("%5s\t%5d\t%5.2f\n", "1", 22, calculateCharges(22));
|
||||
printf("%5s\t%5d\t%5.2f\n", "2", 12, calculateCharges(12));
|
||||
printf("%5s\t%5d\t%5.2f\n", "3", 34, calculateCharges(34));
|
||||
printf("%5s\t%5d\t%5.2f\n", "4", 48, calculateCharges(48));
|
||||
printf("%5s\t%5d\t%5.2f\n", "5", 94, calculateCharges(94));
|
||||
}
|
||||
|
||||
double calculateCharges (int hours) {
|
||||
int dayMod = hours % 24;
|
||||
int days = hours / 24;
|
||||
|
||||
if (days >= 1) {
|
||||
return ((days * 50)+(hours * 0.5));
|
||||
} else {
|
||||
if (hours <= 8) {
|
||||
return ((hours * 0.5) + 25);
|
||||
} else if (hours <= 13) {
|
||||
return (((hours - 8) * 5) + (hours * 0.5) + 25);
|
||||
} else {
|
||||
return (50 + (hours * 0.5));
|
||||
}
|
||||
}
|
||||
}
|
20
DeitelC/Chapter5/coinTossing.c
Normal file
20
DeitelC/Chapter5/coinTossing.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include "flip.h"
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
int heads = 0, tails = 0;
|
||||
for(int i = 0; i < 100; i++){
|
||||
if(flip()) {
|
||||
heads++;
|
||||
} else {
|
||||
tails++;
|
||||
}
|
||||
}
|
||||
printf("%s%d%s%d\n", "Heads: ", heads, ", Tails: ", tails);
|
||||
return 0;
|
||||
}
|
23
DeitelC/Chapter5/currencyConversion.c
Normal file
23
DeitelC/Chapter5/currencyConversion.c
Normal file
@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void usdToEuro(double);
|
||||
void usdToYen(double);
|
||||
|
||||
int main(void) {
|
||||
double usd = 1;
|
||||
usdToEuro(usd);
|
||||
usdToYen(usd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void usdToEuro(double usd) {
|
||||
double euro = usd * 0.92;
|
||||
printf("%3s\t%s\n", "USD", "EURO");
|
||||
printf("%3.2f\t%3.2f\n", usd, euro);
|
||||
}
|
||||
|
||||
void usdToYen(double usd) {
|
||||
double yen = usd * 118.87;
|
||||
printf("%3s\t%s\n", "USD", "YEN");
|
||||
printf("%3.2f\t%3.2f\n", usd, yen);
|
||||
}
|
9
DeitelC/Chapter5/exercise5-42.c
Normal file
9
DeitelC/Chapter5/exercise5-42.c
Normal file
@ -0,0 +1,9 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
int c = '\0'; // variable to hold character input by user
|
||||
if ((c = getchar()) != EOF) {
|
||||
printf("%c", c);
|
||||
main();
|
||||
}
|
||||
}
|
23
DeitelC/Chapter5/exercise5-43.c
Normal file
23
DeitelC/Chapter5/exercise5-43.c
Normal file
@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
int mystery(int a, int b); // function prototype
|
||||
|
||||
int main(void) {
|
||||
printf("%s", "Enter two positive integers: ");
|
||||
int x = 0; // first integer
|
||||
int y = 0; // second integer
|
||||
scanf("%d%d", &x, &y);
|
||||
printf("The result is %d\n", mystery(x, y));
|
||||
}
|
||||
|
||||
// Parameter b must be a positive integer
|
||||
// to prevent infinite recursion
|
||||
int mystery(int a, int b) {
|
||||
// base case
|
||||
if (1 == b) {
|
||||
return a;
|
||||
} else if (b > 1) { // recursive step
|
||||
return a + mystery(a, b - 1);
|
||||
} else {
|
||||
return -a + mystery(a, b + 1);
|
||||
}
|
||||
}
|
14
DeitelC/Chapter5/exercise5-8.c
Normal file
14
DeitelC/Chapter5/exercise5-8.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int main (void) {
|
||||
double x;
|
||||
x = fabs(10.85);
|
||||
x = floor(10.85);
|
||||
x = fabs(-0.678);
|
||||
x = ceil(9.234);
|
||||
x = fabs(0.0);
|
||||
x = ceil(-34.87);
|
||||
x = ceil(-fabs(-12 - floor(-9.5)));
|
||||
printf("%.5f\n", x);
|
||||
}
|
29
DeitelC/Chapter5/fibonacci.c
Normal file
29
DeitelC/Chapter5/fibonacci.c
Normal file
@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
|
||||
unsigned long long int fibonacci(int n);
|
||||
|
||||
int main (void){
|
||||
|
||||
puts("Enter fibonacci step!");
|
||||
int n;
|
||||
|
||||
if(!scanf("%d", &n)){
|
||||
puts("invalid");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("%d. step of fibonacci: %llu\n", n, fibonacci(n));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned long long int fibonacci(int n) {
|
||||
|
||||
if(n == 0 || 1 == n){
|
||||
return n;
|
||||
} else {
|
||||
|
||||
return fibonacci(n - 1) + fibonacci (n - 2);
|
||||
}
|
||||
|
||||
}
|
19
DeitelC/Chapter5/findTheMax.c
Normal file
19
DeitelC/Chapter5/findTheMax.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
float f1, f2, f3, f4;
|
||||
puts("Enter 4 float!");
|
||||
scanf("%f%f%f%f", &f1, &f2, &f3, &f4);
|
||||
float listF[] = {f1, f2, f3, f4};
|
||||
|
||||
float temp;
|
||||
for(int i = 0; i < 3; i++) {
|
||||
if(listF[i] >= listF[i+1]) {
|
||||
temp = listF[i];
|
||||
} else if (listF[i+1] >= listF[i]) {
|
||||
temp = listF[i+1];
|
||||
}
|
||||
}
|
||||
printf("%s%f\n", "Biggest float: ", temp);
|
||||
return 0;
|
||||
}
|
6
DeitelC/Chapter5/flip.c
Normal file
6
DeitelC/Chapter5/flip.c
Normal file
@ -0,0 +1,6 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
int flip() {
|
||||
return rand() % 2;
|
||||
}
|
6
DeitelC/Chapter5/flip.h
Normal file
6
DeitelC/Chapter5/flip.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef FLIP_H
|
||||
#define FLIP_H
|
||||
|
||||
int flip();
|
||||
|
||||
#endif
|
19
DeitelC/Chapter5/greatestCommonDivisor.c
Normal file
19
DeitelC/Chapter5/greatestCommonDivisor.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int gcd (int, int);
|
||||
|
||||
int main (void) {
|
||||
printf("%d\n", gcd(15, 20));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int gcd (int x, int y) {
|
||||
if(!y) {
|
||||
return x;
|
||||
} else {
|
||||
return gcd(y, x % y);
|
||||
}
|
||||
}
|
42
DeitelC/Chapter5/guessTheNum.c
Normal file
42
DeitelC/Chapter5/guessTheNum.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
void guessTheNum();
|
||||
|
||||
int main(void) {
|
||||
srand(time(NULL));
|
||||
|
||||
guessTheNum();
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void guessTheNum()
|
||||
{
|
||||
int num = 1 + rand() % 1000;
|
||||
printf("%s\n%s\n%s\n", "I have a number between 1 and 1000.", "Can you guess my number?", "Please type your first guess.");
|
||||
int scanNum = 0;
|
||||
scanf("%d", &scanNum);
|
||||
char con;
|
||||
|
||||
if((scanNum > 1001) || (scanNum < 0)) {
|
||||
puts("Invalid integer!");
|
||||
}
|
||||
|
||||
do {
|
||||
while(scanNum != num) {
|
||||
if(scanNum < num) {
|
||||
puts("Too low!");
|
||||
scanf("%d", &scanNum);
|
||||
} else {
|
||||
puts("Too high");
|
||||
scanf("%d", &scanNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
while(scanf(" %c", &con));
|
||||
puts("FOUND!");
|
||||
}
|
16
DeitelC/Chapter5/hypotenuse.c
Normal file
16
DeitelC/Chapter5/hypotenuse.c
Normal file
@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
double hypotenuse (double side1, double side2);
|
||||
|
||||
int main (void) {
|
||||
double side1, side2;
|
||||
printf("Enter side 1 and side 2\n");
|
||||
scanf("%lf%lf", &side1, &side2);
|
||||
printf("%3s%3.2lf\n", "Hypotenuse: ", hypotenuse(side1, side2));
|
||||
}
|
||||
|
||||
double hypotenuse (double side1, double side2) {
|
||||
double hypo = (side1 * side1) + (side2 * side2);
|
||||
return sqrt(hypo);
|
||||
}
|
11
DeitelC/Chapter5/isEven.c
Normal file
11
DeitelC/Chapter5/isEven.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int isEven (int num);
|
||||
|
||||
int isEven (int num) {
|
||||
if (num % 2 == 0) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
7
DeitelC/Chapter5/isEvenCheck.c
Normal file
7
DeitelC/Chapter5/isEvenCheck.c
Normal file
@ -0,0 +1,7 @@
|
||||
#include <stdio.h>
|
||||
#include "isEven.c"
|
||||
|
||||
int main (void) {
|
||||
int a = isEven(2);
|
||||
printf("%d\n", a);
|
||||
}
|
17
DeitelC/Chapter5/isPerfect.c
Normal file
17
DeitelC/Chapter5/isPerfect.c
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int isPerfect(int);
|
||||
|
||||
int isPerfect(int num) {
|
||||
int temp = 0;
|
||||
for(int i = 1; i < num; i++) {
|
||||
if(num % i == 0) {
|
||||
temp += i;
|
||||
}
|
||||
}
|
||||
if (temp == num) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
18
DeitelC/Chapter5/isPerfectDemo.c
Normal file
18
DeitelC/Chapter5/isPerfectDemo.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include "isPerfect.c"
|
||||
|
||||
int main(void) {
|
||||
/*puts("Enter a int!");
|
||||
int num;
|
||||
scanf("%d", &num);
|
||||
|
||||
printf("%d\n", isPerfect(num));*/
|
||||
|
||||
for(int i = 0; i < 1000; i++) {
|
||||
if(isPerfect(i)) {
|
||||
printf("%d ", i);
|
||||
}
|
||||
}
|
||||
puts("");
|
||||
return 0;
|
||||
}
|
19
DeitelC/Chapter5/lcm.c
Normal file
19
DeitelC/Chapter5/lcm.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int lcm (int, int);
|
||||
|
||||
int lcm (int i1, int i2)
|
||||
{
|
||||
int i3 = 0;
|
||||
if (i1 >= i2) {
|
||||
i3 = i1;
|
||||
} else {
|
||||
i3 = i2;
|
||||
}
|
||||
for (int i = 2; i < i3; i++) {
|
||||
if (i1 % i == 0 && i2 % i == 0) {
|
||||
i3 = i;
|
||||
}
|
||||
}
|
||||
return i3;
|
||||
}
|
11
DeitelC/Chapter5/lcmDemo.c
Normal file
11
DeitelC/Chapter5/lcmDemo.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include "lcm.c"
|
||||
|
||||
int main (void)
|
||||
{
|
||||
int i1, i2 = 0;
|
||||
puts("Enter int1 and int 2");
|
||||
scanf("%d%d", &i1, &i2);
|
||||
printf("%s%d\n", "Lowest common integer: ", lcm(i1, i2));
|
||||
return 0;
|
||||
}
|
Binary file not shown.
43
DeitelC/Chapter5/math.c
Normal file
43
DeitelC/Chapter5/math.c
Normal file
@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
double hypotenuse (double side1, double side2);
|
||||
int smallest (int x, int y, int z);
|
||||
int intToFloat (double number);
|
||||
|
||||
int main (void) {
|
||||
printf("%.2f\n", cbrt(27.0));
|
||||
printf("%.2f\n", sqrt(900.0));
|
||||
printf("%.5f\n", exp(1));
|
||||
printf("%.5f\n", log(2.718282));
|
||||
printf("%.5f\n", log10(10));
|
||||
printf("%.5f\n", fabs(-10.11));
|
||||
printf("%.5f\n", ceil(-10.11));
|
||||
printf("%.5f\n", floor(-10.11));
|
||||
printf("%.5f\n", pow(2, 7));
|
||||
printf("%.5f\n", fmod(13.657, 2.333));
|
||||
printf("%.5f\n", sin(0.0));
|
||||
printf("%.5f\n", cos(0.0));
|
||||
printf("%.5f\n", tan(0.0));
|
||||
printf("%.5f\n", hypotenuse(3.0,4.0));
|
||||
printf("%d\n", smallest(3,3,4));
|
||||
printf("%d\n", intToFloat(3.4));
|
||||
}
|
||||
|
||||
double hypotenuse (double side1, double side2) {
|
||||
double final = (side1*side1) + (side2*side2);
|
||||
return sqrt(final);
|
||||
}
|
||||
|
||||
int smallest (int x, int y, int z) {
|
||||
if (x <= y && x <= z) {
|
||||
return x;
|
||||
} else if (y <= z && y <= x) {
|
||||
return y;
|
||||
} else {
|
||||
return z;
|
||||
}
|
||||
}
|
||||
int intToFloat (double number) {
|
||||
return number;
|
||||
}
|
14
DeitelC/Chapter5/randExample.c
Normal file
14
DeitelC/Chapter5/randExample.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main (void) {
|
||||
printf("%d\n", 1 + rand() % 6);
|
||||
printf("%d\n", 100 + rand() % 900);
|
||||
printf("%d\n", rand() % 19);
|
||||
printf("%d\n", 1000 + rand() % 1222);
|
||||
printf("%d\n", -1 + rand() % 2);
|
||||
printf("%d\n", -3 + rand() % 15);
|
||||
printf("%d\n", 3 * (1 + rand() % 10));
|
||||
printf("%d\n", 3 + 2 * (rand() % 9));
|
||||
printf("%d\n", 3 + 5 * (rand() % 7));
|
||||
}
|
13
DeitelC/Chapter5/rectangleOfAnyChar.c
Normal file
13
DeitelC/Chapter5/rectangleOfAnyChar.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void rectangleOfAnyChar (int side1, int side2, char a);
|
||||
|
||||
|
||||
void rectangleOfAnyChar (int side1, int side2, char a) {
|
||||
for(int i = 0; i < side1; i++) {
|
||||
for(int j = 0; j < side2; j++) {
|
||||
printf("%c", a);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
13
DeitelC/Chapter5/rectangleOfAsteriks.c
Normal file
13
DeitelC/Chapter5/rectangleOfAsteriks.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void rectangleOfAsteriks(int side1, int side2);
|
||||
|
||||
void rectangleOfAsteriks(int side1, int side2)
|
||||
{
|
||||
for (int i = 0; i < side1; i++) {
|
||||
for (int j = 0; j < side2; j++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
13
DeitelC/Chapter5/rectangleOfAsteriks.c~
Normal file
13
DeitelC/Chapter5/rectangleOfAsteriks.c~
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void rectangleOfAsteriks (int side1, int side2);
|
||||
|
||||
|
||||
void rectangleOfAsteriks (int side1, int side2) {
|
||||
for(int i = 0; i < side1; i++) {
|
||||
for(int j = 0; j < side2; j++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
6
DeitelC/Chapter5/rectangleOfAsteriksDemo.c
Normal file
6
DeitelC/Chapter5/rectangleOfAsteriksDemo.c
Normal file
@ -0,0 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include "rectangleOfAsteriks.c"
|
||||
|
||||
int main (void) {
|
||||
rectangleOfAsteriks(4,5);
|
||||
}
|
6
DeitelC/Chapter5/rectangleOfDemo.c
Normal file
6
DeitelC/Chapter5/rectangleOfDemo.c
Normal file
@ -0,0 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include "rectangleOfAnyChar.c"
|
||||
|
||||
int main (void) {
|
||||
rectangleOfAnyChar(4,5,'@');
|
||||
}
|
11
DeitelC/Chapter5/recursiveMain.c
Normal file
11
DeitelC/Chapter5/recursiveMain.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main (void) {
|
||||
static int count = 1;
|
||||
|
||||
printf("%d\n", count++);
|
||||
if (count <= 5) {
|
||||
main();
|
||||
}
|
||||
return 0;
|
||||
}
|
28
DeitelC/Chapter5/recursivePrime.c
Normal file
28
DeitelC/Chapter5/recursivePrime.c
Normal file
@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int isPrimeX (int, int);
|
||||
int isPrime (int);
|
||||
|
||||
int main (void) {
|
||||
printf("%d\n", isPrime(11));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int isPrime (int x) {
|
||||
return isPrimeX (x, 2);
|
||||
}
|
||||
|
||||
|
||||
int isPrimeX (int x, int i) {
|
||||
if (x <= 2) {
|
||||
return (x == 2) ? 1 : 0;
|
||||
}
|
||||
if (x % i == 0){
|
||||
return 0;
|
||||
}
|
||||
if (i * i > x) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return isPrimeX (x, i + 1);
|
||||
}
|
26
DeitelC/Chapter5/rootsOfQuadratic.c
Normal file
26
DeitelC/Chapter5/rootsOfQuadratic.c
Normal file
@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
void rootsOfQuad(int, int, int);
|
||||
|
||||
int main(void) {
|
||||
int a, b, c = 0;
|
||||
puts("Enter coefficients!");
|
||||
scanf("%d%d%d", &a, &b, &c);
|
||||
rootsOfQuad(a, b, c);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void rootsOfQuad(int a, int b, int c) {
|
||||
int discriminant = (b * b) - 4 * a * c;
|
||||
|
||||
if(discriminant) {
|
||||
int x1 = (-b - sqrt(discriminant))/2 * a;
|
||||
int x2 = (-b + sqrt(discriminant))/2 * a;
|
||||
printf("%s%d%d\n", "x1 and x2 are: ", x1, x2);
|
||||
} else {
|
||||
puts("Not reel!");
|
||||
}
|
||||
}
|
38
DeitelC/Chapter5/roundingNumbers.c
Normal file
38
DeitelC/Chapter5/roundingNumbers.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int roundToInteger(double number);
|
||||
int roundToTenths(double number);
|
||||
int roundToHundreths(double number);
|
||||
int roundToThousandths(double number);
|
||||
|
||||
|
||||
int main (void) {
|
||||
double num;
|
||||
printf("%s\n", "Enter a number!");
|
||||
scanf("%lf", &num);
|
||||
printf("%3s\t%3lf\t%3s\t%3d\n", "Orginal num:", num, "Rounded: ", roundToInteger(num));
|
||||
printf("%3s\t%3lf\t%3s\t%3d\n", "Orginal num:", num, "Rounded: ", roundToTenths(num));
|
||||
printf("%3s\t%3lf\t%3s\t%3d\n", "Orginal num:", num, "Rounded: ", roundToHundreths(num));
|
||||
printf("%3s\t%3lf\t%3s\t%3d\n", "Orginal num:", num, "Rounded: ", roundToThousandths(num));
|
||||
}
|
||||
|
||||
int roundToInteger(double number) {
|
||||
int y = floor(number + .5);
|
||||
return y;
|
||||
}
|
||||
|
||||
int roundToTenths(double number) {
|
||||
int y = floor(number * 10 + .5) / 10;
|
||||
return y;
|
||||
}
|
||||
|
||||
int roundToHundreths(double number) {
|
||||
int y = floor(number * 100 + .5) / 100;
|
||||
return y;
|
||||
}
|
||||
|
||||
int roundToThousandths(double number) {
|
||||
int y = floor(number * 1000 + .5) / 1000;
|
||||
return y;
|
||||
}
|
23
DeitelC/Chapter5/separatingDigits.c
Normal file
23
DeitelC/Chapter5/separatingDigits.c
Normal file
@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
puts("Enter integer");
|
||||
int scanV;
|
||||
scanf("%d", &scanV);
|
||||
int rem = 10000;
|
||||
int firstD = 1;
|
||||
|
||||
if(scanV < 32768 && scanV >= 1) {
|
||||
while(rem >= 1) {
|
||||
int digit = scanV / rem;
|
||||
if(!firstD || digit) {
|
||||
printf("%d ", digit);
|
||||
firstD = 0;
|
||||
scanV = scanV % rem;
|
||||
}
|
||||
|
||||
rem /= 10;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
24
DeitelC/Chapter5/sidesOfTriangle.c
Normal file
24
DeitelC/Chapter5/sidesOfTriangle.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
double sidesOfTriangle (double s1, double s2, double s3);
|
||||
|
||||
int main (void) {
|
||||
double s1, s2, s3;
|
||||
printf("%s\n","Enter sides of triangle");
|
||||
scanf("%lf%lf%lf", &s1, &s2, &s3);
|
||||
double area = sidesOfTriangle(s1, s2, s3);
|
||||
if(area >= 0) {
|
||||
printf("%3s%lf\n", "Area: ", area);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
double sidesOfTriangle (double s1, double s2, double s3) {
|
||||
double perimeter = s1 + s2 + s3;
|
||||
double u = perimeter / 2;
|
||||
if ((s1 < s2 + s3) && (s2 < s1 + s3) && (s3 < s1 + s2)) {
|
||||
return sqrt(u * (u - s1) * (u - s2) * (u - s3));
|
||||
}
|
||||
return -1;
|
||||
}
|
26
DeitelC/Chapter5/struck12.c
Normal file
26
DeitelC/Chapter5/struck12.c
Normal file
@ -0,0 +1,26 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void struck12(int, int, int);
|
||||
|
||||
int main(void) {
|
||||
puts("Enter time in 3 integer!(H,M,Sec)");
|
||||
int time1, time2, time3 = 0;
|
||||
|
||||
scanf("%d%d%d", &time1, &time2, &time3);
|
||||
struck12(time1, time2, time3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void struck12(int i1, int i2, int i3) {
|
||||
int total = (i1*3600) + (i2*60) + i3;
|
||||
|
||||
if(total > 43200) {
|
||||
total -= 43200;
|
||||
}
|
||||
int rem = 43200 - total;
|
||||
int sec = rem % 60;
|
||||
int h = rem / 3600;
|
||||
int m = (rem - (h * 3600) - sec) / 60;
|
||||
printf("%s%d%s%d%s%d%s\n", "Struck 12: ", h, " Hours, ", m, " Min, ", sec, " Secs left. ");
|
||||
}
|
18
DeitelC/Chapter5/sumOfDigits.c
Normal file
18
DeitelC/Chapter5/sumOfDigits.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int sumOfDigits(int);
|
||||
|
||||
int main(void) {
|
||||
int t = 7613;
|
||||
printf("%d\n", sumOfDigits(t));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sumOfDigits(int num) {
|
||||
int result = 0;
|
||||
while (num > 0) {
|
||||
result += num % 10;
|
||||
num /= 10;
|
||||
}
|
||||
return result;
|
||||
}
|
23
DeitelC/Chapter6/dice.c
Normal file
23
DeitelC/Chapter6/dice.c
Normal file
@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#define FACE 6
|
||||
#define FACES 12
|
||||
#define TIMES 3600
|
||||
|
||||
int main (void) {
|
||||
int freq[FACES + 1] = {0};
|
||||
srand(time(NULL));
|
||||
|
||||
for(size_t i = 0; i < TIMES; i++) {
|
||||
int dice1 = 1 + rand() % FACE;
|
||||
int dice2 = 1 + rand() % FACE;
|
||||
int diceTotal = dice1 + dice2;
|
||||
++freq[diceTotal];
|
||||
}
|
||||
|
||||
for(size_t i = 1; i <= FACES; i++) {
|
||||
printf("%s%zu%s%d\n", "FACE: ", i, " and freq: ", freq[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
21
DeitelC/Chapter6/exercise6-18.c
Normal file
21
DeitelC/Chapter6/exercise6-18.c
Normal file
@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
#define MAX 10
|
||||
/* function prototype */
|
||||
void functionName(const int b[], size_t startSubscript, size_t size);
|
||||
/* function main begins program execution */
|
||||
int main(void)
|
||||
{
|
||||
/* initialize p */
|
||||
int p[MAX] = {5, 7, 2, 1, 0, 4, 3, 0, 6, 8};
|
||||
puts("Answer is:");
|
||||
functionName(p, 0, MAX);
|
||||
puts("");
|
||||
} /* end main */
|
||||
/* What does this function do? */
|
||||
void functionName(const int b[], size_t startSubscript, size_t size)
|
||||
{
|
||||
if (startSubscript < size) {
|
||||
functionName(b, startSubscript + 1, size);
|
||||
printf("%d", b[startSubscript] * 5);
|
||||
} /* end if */
|
||||
} /* end function someFunction */
|
12
DeitelC/Chapter6/exercise6-4.c
Normal file
12
DeitelC/Chapter6/exercise6-4.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
#define SIZE 3
|
||||
|
||||
int main (void) {
|
||||
int table [SIZE] [SIZE] = {{1, 8}, {2, 4, 6}, {5}};
|
||||
for(size_t i = 0; i < SIZE; i++) {
|
||||
for(size_t j = 0; j < SIZE; j++) {
|
||||
printf("%d\n", table[i] [j]);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
32
DeitelC/Chapter6/exercise6-8.c
Normal file
32
DeitelC/Chapter6/exercise6-8.c
Normal file
@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
int m [3][3];
|
||||
for(size_t i = 0; i < 3; i++) {
|
||||
for(size_t j = 0; j < 3; j++) {
|
||||
m [i] [j] = 3;
|
||||
}
|
||||
}
|
||||
for(size_t i = 0; i < 3; i++) {
|
||||
for(size_t j = 0; j < 3; j++) {
|
||||
printf("%zu\t", m [i] [j]);
|
||||
}
|
||||
}
|
||||
int small[100] = {0};
|
||||
int large[200];
|
||||
for(size_t i = 0; i < 200; i++) {
|
||||
large[i] = 1;
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < 100; i++) {
|
||||
large[100+i] = small[i];
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < 200; i++) {
|
||||
printf("%d ,", large[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
25
DeitelC/Chapter6/facto.c
Normal file
25
DeitelC/Chapter6/facto.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#define SIZE 6
|
||||
int whatIsThis(const int b[], size_t p);
|
||||
// function prototype
|
||||
// function main begins program execution
|
||||
int main(void)
|
||||
{
|
||||
int x; // holds return value of function whatIsThis
|
||||
// initialize array a
|
||||
int a[SIZE] = {1, 2, 3, 4, 5, 6};
|
||||
x = whatIsThis(a, SIZE);
|
||||
printf("Result is %d\n", x);
|
||||
}
|
||||
// what does this function do?
|
||||
int whatIsThis(const int b[], size_t p)
|
||||
{
|
||||
// base case
|
||||
if (1 == p)
|
||||
{
|
||||
return b[0];
|
||||
}
|
||||
else { // recursion step
|
||||
return b[p - 1] * whatIsThis(b, p - 1);
|
||||
}
|
||||
}
|
20
DeitelC/Chapter6/matrixMul.c
Normal file
20
DeitelC/Chapter6/matrixMul.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main (void) {
|
||||
|
||||
int matrix1 [2][2] = {{2, 3}, {4, 5}};
|
||||
int matrix2 [2][2] = {{6, 7}, {8, 9}};
|
||||
int result [2][2] = {0};
|
||||
|
||||
puts("Result Matrix: ");
|
||||
for(int i = 0; i < 2; i++) {
|
||||
for(int j = 0; j < 2; j++) {
|
||||
for (int k = 0; k < 2; k++) {
|
||||
result [i][j] += matrix1 [i][k] * matrix2 [k][j];
|
||||
}
|
||||
printf("%d\t", result[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
25
DeitelC/Chapter6/salesCommissions.c
Normal file
25
DeitelC/Chapter6/salesCommissions.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
int weekPay = 200;
|
||||
puts("Enter gross sales: ");
|
||||
int bonus;
|
||||
scanf("%d", &bonus);
|
||||
bonus = (bonus * 9) / 100;
|
||||
int total = bonus + weekPay;
|
||||
|
||||
int range[9];
|
||||
int temp = (total/100) - 2;
|
||||
|
||||
if(temp < 10) {
|
||||
++range[temp];
|
||||
} else {
|
||||
++range[8];
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 9; i++) {
|
||||
printf("%d\n", range[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
21
DeitelC/Chapter6/selectionSort.c
Normal file
21
DeitelC/Chapter6/selectionSort.c
Normal file
@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void selectionSort(size_t size, int a[size]);
|
||||
|
||||
void selectionSort(size_t size, int a[size]) {
|
||||
for(size_t i = 0; i < size - 1; i++) {
|
||||
size_t minI = i;
|
||||
for(size_t j = i + 1; j < size; j++) {
|
||||
if(a[j] < a[minI]) {
|
||||
minI = j;
|
||||
}
|
||||
}
|
||||
|
||||
if(minI != i) {
|
||||
int temp = a[i];
|
||||
a[i] = a[minI];
|
||||
a[minI] = temp;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
38
DeitelC/Chapter6/sets.c
Normal file
38
DeitelC/Chapter6/sets.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#define SIZE 11
|
||||
|
||||
int main (void) {
|
||||
int sets1[SIZE] = {0};
|
||||
int sets2[SIZE] = {0};
|
||||
|
||||
for(size_t i = 0; i < SIZE; i++) {
|
||||
sets1[i] = i * 3;
|
||||
sets2[i] = i * 5;
|
||||
}
|
||||
|
||||
puts("Union of sets:");
|
||||
for(size_t i = 0; i < SIZE; i++) {
|
||||
for(size_t j = 0; j < SIZE; j++) {
|
||||
if(sets1[i] == sets2[j]) {
|
||||
printf("%d\t", sets1[i]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
puts("Intersection of sets:");
|
||||
for(size_t i = 0; i < SIZE; i++) {
|
||||
for(size_t j = 0; j < SIZE; j++) {
|
||||
if(sets1[i] != sets2[j]) {
|
||||
printf("%d\t", sets1[i]);
|
||||
printf("%d\t", sets2[j]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
44
DeitelC/Chapter6/test.c
Normal file
44
DeitelC/Chapter6/test.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#define PERSON 4
|
||||
#define PRODUCT 5
|
||||
|
||||
int main (void) {
|
||||
double sales[PERSON][PRODUCT] = {0};
|
||||
int person, product;
|
||||
double amount;
|
||||
|
||||
while(1) {
|
||||
puts("Enter Sale Person (1-4)");
|
||||
puts("Wrong data will be ignored! Enter -1 to stop");
|
||||
scanf("%d", &person);
|
||||
|
||||
if (person == -1) {
|
||||
break;
|
||||
} else if (person < 1 || person > PERSON) {
|
||||
puts("Invalid salesperson number! Try again.");
|
||||
continue;
|
||||
}
|
||||
|
||||
puts("Enter Product (1-5) and Amount:");
|
||||
scanf("%d%lf", &product, &amount);
|
||||
|
||||
if (product < 1 || product > PRODUCT || amount < 0) {
|
||||
puts("Invalid product or amount! Try again.");
|
||||
continue;
|
||||
}
|
||||
|
||||
sales[person - 1][product - 1] += amount;
|
||||
}
|
||||
|
||||
printf("%10s%10s%10s%10s%10s\n", "Product", "Person1", "Person2", "Person3", "Person4");
|
||||
for (int i = 0; i < PRODUCT; i++) {
|
||||
printf("%10s%d", "Product", i + 1);
|
||||
for (int j = 0; j < PERSON; j++) {
|
||||
printf("%10.2lf", sales[j][i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
44
DeitelC/Chapter6/totalSales.c
Normal file
44
DeitelC/Chapter6/totalSales.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include <stdio.h>
|
||||
#define PERSON 4
|
||||
#define PRODUCT 5
|
||||
|
||||
int main (void) {
|
||||
double sales[PERSON][PRODUCT] = {0};
|
||||
int person, product;
|
||||
double amount;
|
||||
|
||||
while(1) {
|
||||
puts("Enter Sale Person (1-4)");
|
||||
puts("Wrong data will be ignored! Enter -1 to stop");
|
||||
scanf("%d", &person);
|
||||
|
||||
if (person == -1) {
|
||||
break;
|
||||
} else if (person < 1 || person > PERSON) {
|
||||
puts("Invalid salesperson number! Try again.");
|
||||
continue;
|
||||
}
|
||||
|
||||
puts("Enter Product (1-5) and Amount:");
|
||||
scanf("%d%lf", &product, &amount);
|
||||
|
||||
if (product < 1 || product > PRODUCT || amount < 0) {
|
||||
puts("Invalid product or amount! Try again.");
|
||||
continue;
|
||||
}
|
||||
|
||||
sales[person - 1][product - 1] += amount;
|
||||
}
|
||||
|
||||
printf("%10s%10s%10s%10s%10s\n", "Product", "Person1", "Person2", "Person3", "Person4");
|
||||
for (int i = 0; i < PRODUCT; i++) {
|
||||
printf("%10s%d", "Product", i + 1);
|
||||
for (int j = 0; j < PERSON; j++) {
|
||||
printf("%10.2lf", sales[j][i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
60
DeitelC/Chapter6/turtle.c
Normal file
60
DeitelC/Chapter6/turtle.c
Normal file
@ -0,0 +1,60 @@
|
||||
#include <stdio.h>
|
||||
#define size 50
|
||||
|
||||
int main(void) {
|
||||
int turtle[size][size] = {0};
|
||||
int penUp = 1;
|
||||
double in = 0.00;
|
||||
int way = 0; // 0 right, 1 down, 2 left, 3 up
|
||||
int row = 0, column = 0;
|
||||
|
||||
while (1) {
|
||||
puts("Enter command between 1-9 (e.g., 5.12 for move 12 steps):");
|
||||
scanf("%lf", &in);
|
||||
|
||||
if (in == 9.00) {
|
||||
break;
|
||||
} else if (in == 1.00) {
|
||||
penUp = 1;
|
||||
} else if (in == 2.00) {
|
||||
penUp = 0;
|
||||
} else if (in == 3.00) {
|
||||
way = (way + 1) % 4; // Turn right
|
||||
} else if (in == 4.00) {
|
||||
way = (way - 1 + 4) % 4; // Turn left
|
||||
} else if (in == 6.00) {
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
for (size_t j = 0; j < size; j++) {
|
||||
printf("%c", turtle[i][j] ? '*' : ' ');
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
} else if (in >= 5.00 && in < 6.00) {
|
||||
float temp = (in - 5.00) * 100;
|
||||
int steps = (int)temp;
|
||||
|
||||
for (int i = 0; i < steps; i++) {
|
||||
if (!penUp) {
|
||||
turtle[row][column] = 1;
|
||||
}
|
||||
|
||||
switch (way) {
|
||||
case 0: // Right
|
||||
if (column < size - 1) column++;
|
||||
break;
|
||||
case 1: // Down
|
||||
if (row < size - 1) row++;
|
||||
break;
|
||||
case 2: // Left
|
||||
if (column > 0) column--;
|
||||
break;
|
||||
case 3: // Up
|
||||
if (row > 0) row--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
54
DeitelC/Chapter6/turtle0.c
Normal file
54
DeitelC/Chapter6/turtle0.c
Normal file
@ -0,0 +1,54 @@
|
||||
#include <stdio.h>
|
||||
#define size 50
|
||||
|
||||
int main (void) {
|
||||
int turtle[size][size] = {0};
|
||||
int penUp = 1;
|
||||
double in = 0.00;
|
||||
int way = 0; //0 right, 1 left
|
||||
int row, column = 0;
|
||||
|
||||
puts("Enter command between 1-9 with .0!");
|
||||
scanf("%.2lf", &in);
|
||||
|
||||
while(in != 9.00) {
|
||||
puts("Enter command between 1-9 with .0!");
|
||||
scanf("%.1lf", &in);
|
||||
|
||||
if(in == 2.00) {
|
||||
penUp = 0;
|
||||
} else if (in == 6.00) {
|
||||
for(size_t i = 0; i < size; i++) {
|
||||
for(size_t j = 0; j < size; j++) {
|
||||
printf("%d\n", turtle[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
} else if (in == 3.00) {
|
||||
way = 0;
|
||||
} else if (in == 4.00) {
|
||||
way = 1;
|
||||
} else if (in > 4.00 && in < 6.00) {
|
||||
float temp = (in * 100) - 500;
|
||||
int tIn = (int) temp;
|
||||
|
||||
if(!way && penUp == 0 && tIn < 50 && column < 50) {
|
||||
for(int i = 0; i < tIn; i++) {
|
||||
turtle[row][i] = 1;
|
||||
}
|
||||
column = tIn;
|
||||
|
||||
} else if(way && penUp == 0 && tIn < 50 && row < 50) {
|
||||
for(int i = 0; i < tIn; i++) {
|
||||
turtle[i][column] = 1;
|
||||
}
|
||||
row = tIn;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user