This commit is contained in:
2025-05-23 03:26:13 +03:00
parent 191705c1dc
commit 4e9b002e99
14 changed files with 307 additions and 0 deletions

View 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();
}
}

View 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);
}
}

View 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);
}
}

View 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;
}

View 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);
}

23
DeitelC/Chapter6/dice.c Normal file
View 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;
}

View 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 */

View 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;
}

View 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
View 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);
}
}

View 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;
}

View 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;
}

View 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
View 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;
}