From a92349ee3833c70cf43cbb6f4c5a548369459b4b Mon Sep 17 00:00:00 2001 From: kaltinsoy Date: Fri, 19 Sep 2025 16:32:47 +0300 Subject: [PATCH] pointer --- DeitelC/Chapter7/deckOfCards17-21.c | 96 +++++++++++++++++++++++++++++ DeitelC/Chapter7/die.c | 20 +++++- DeitelC/Chapter7/dieP.c | 46 ++++++++++++++ DeitelC/Chapter7/test2.c | 45 ++++++++++++++ DeitelC/Chapter7/test3.c | 47 ++++++++++++++ w3resource/pointers/e1-1.c | 19 ++++++ w3resource/pointers/e1-2.c | 13 ++++ w3resource/pointers/e1-3.c | 19 ++++++ w3resource/pointers/e1-4.c | 14 +++++ w3resource/pointers/e1-5.c | 24 ++++++++ w3resource/pointers/e1-6.c | 12 ++++ w3resource/pointers/e1-7.c | 21 +++++++ 12 files changed, 374 insertions(+), 2 deletions(-) create mode 100644 DeitelC/Chapter7/deckOfCards17-21.c create mode 100644 DeitelC/Chapter7/dieP.c create mode 100644 DeitelC/Chapter7/test2.c create mode 100644 DeitelC/Chapter7/test3.c create mode 100644 w3resource/pointers/e1-1.c create mode 100644 w3resource/pointers/e1-2.c create mode 100644 w3resource/pointers/e1-3.c create mode 100644 w3resource/pointers/e1-4.c create mode 100644 w3resource/pointers/e1-5.c create mode 100644 w3resource/pointers/e1-6.c create mode 100644 w3resource/pointers/e1-7.c diff --git a/DeitelC/Chapter7/deckOfCards17-21.c b/DeitelC/Chapter7/deckOfCards17-21.c new file mode 100644 index 0000000..2664939 --- /dev/null +++ b/DeitelC/Chapter7/deckOfCards17-21.c @@ -0,0 +1,96 @@ +#include +#include +#include + +#define SUITS 4 +#define FACES 13 +#define CARDS 52 + +void shuffle(int deck[][FACES]); +void deal(int deck[][FACES], const char *suits[], const char *faces[]); +void fiveCard(int deck[][FACES], const char *suits[], const char *faces[]); + +int main(void) +{ + int deck[SUITS][FACES] = {0}; + srand((unsigned)time(NULL)); + shuffle(deck); + + const char *suits[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"}; + const char *faces[FACES] = {"Ace", "Deuce", "Three", "Four", "Five", + "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; + + deal(deck, suits, faces); + fiveCard(deck, suits, faces); + + return 0; +} + +void shuffle(int deck[][FACES]) { + for(size_t card = 1; card <= CARDS; ++card) { + size_t row, column; + do { + row = rand() % SUITS; + column = rand() % FACES; + } while(deck[row][column] != 0); + deck[row][column] = (int)card; + } +} + +void deal(int deck[][FACES], const char *suits[], const char *faces[]) { + for(size_t card = 1; card <= CARDS; ++card) { + for(size_t suit = 0; suit < SUITS; ++suit) { + for(size_t face = 0; face < FACES; ++face) { + if(deck[suit][face] == (int)card) { + /*printf("Card: %5s of %-8s%c", + faces[face], + suits[suit], + (card % 4 == 0) ? '\n' : '\t');*/ + } + } + } + } + putchar('\n'); +} + +void fiveCard(int deck[][FACES], const char *suits[], const char *faces[]) { + int hand[5][2] = {0}; + int count5 = 0; + + while (count5 < 5) { + int row = rand() % SUITS; + int column = rand() % FACES; + + if(hand[row][column] != 0) { + hand[count5][0] = row; + hand[count5][1] = column; + printf("Card: %d: %s, %s\n", count5, faces[column], suits[row]); + } + deck[row][column] = 0; + count5++; + } + checkHand(hand, suits, faces); +} + +void checkHand(hand[][2], const char *suits[], const char *faces[]) { + int fourOfaKind = 0; + int threeOfaKind = 0; + int twoPairs = 0; + int onePair = 0; + int flush = 0; + int straight = 0; + int same = 0; + + for(int i = 0; i < 5; i++) { + int sameVal[FACES] = {0}; + int pairs = 0; + + sameVal[hand[i][1]]++; + } + + + + + + +} diff --git a/DeitelC/Chapter7/die.c b/DeitelC/Chapter7/die.c index 773e9fb..17d28d5 100644 --- a/DeitelC/Chapter7/die.c +++ b/DeitelC/Chapter7/die.c @@ -8,6 +8,8 @@ int main(void) int alice = 0; int superAlice = 0; int notSuperBob = 0; + int superBob = 0; + int notSuperAlice = 0; srand(time(NULL)); for(int i = 0; i < 1000; ++i){ @@ -23,16 +25,30 @@ int main(void) int tempDie = (die % 2) ? die - 1 : die; if(tempDie % 2){ ++notSuperBob; + ++superBob; } else { ++superAlice; + if(tempDie > 3) { + ++notSuperAlice; + } else { + ++superBob; + } } } else { - ++notSuperBob; + ++superBob; + ++superAlice; } } - +/* int bobtempDie = tempDie; + if (tempDie < 4 && tempDie % 2 != 0) { + bobtempDie = tempDie * 2; + (bobtempDie % 2) ? ++superBob : ++notSuperAlice; + printf("bobtempDie: %d\n", bobtempDie); + } +*/ printf("Normal Alice: %d, Normal Bob: %d\n", alice, bob); printf("Super Alice: %d, NotSuperBob: %d\n", superAlice, notSuperBob); + printf("Super Bob: %d, notSuperAlice: %d\n", superBob, notSuperAlice); return 0; } diff --git a/DeitelC/Chapter7/dieP.c b/DeitelC/Chapter7/dieP.c new file mode 100644 index 0000000..7cc190c --- /dev/null +++ b/DeitelC/Chapter7/dieP.c @@ -0,0 +1,46 @@ +#include +#include +#include + +void useAliceSuperPower(int *num); +void useBobSuperPower(int *num); + +int main(void) +{ + int bob = 0; //Bob + int alice = 0; + srand(time(NULL)); + + + int die = 1 + rand() % 6; + + printf("%d\n", die); + puts("Die rolled! Alice super power ? (1/x)"); + scanf("%d", &alice); + puts("Bob super power? (1/x)"); + scanf("%d", &bob); + + if (alice == 1) useAliceSuperPower(&die); + if (bob == 1) useBobSuperPower(&die); + + if(die % 2 == 0) { + printf("Winner Bob! Die: %d\n", die); + } else { + printf("Winner Alice! Die: %d\n", die); + } + + + return 0; +} + +void useAliceSuperPower(int *num) { + if((*num) % 2 && (*num) != 1) { + (*num)--; + } + } + +void useBobSuperPower(int *num) { + if((*num) < 4 && (*num) % 2 != 0) { + (*num) = (*num) * 2; + } + } diff --git a/DeitelC/Chapter7/test2.c b/DeitelC/Chapter7/test2.c new file mode 100644 index 0000000..f583748 --- /dev/null +++ b/DeitelC/Chapter7/test2.c @@ -0,0 +1,45 @@ +#include +#include +#include + +void useAliceSuperPower(int *num); +void useBobSuperPower(int *num); + +int main(void) +{ + int bob = 0; + int alice = 0; + srand(time(NULL)); + + for(int i = 0; i < 1000; ++i){ + int die = 1 + rand() % 6; + + useAliceSuperPower(&die); + useBobSuperPower(&die); + + // Apply modulo 6 to keep within 1-6 range + die = (die - 1) % 6 + 1; + + if(die % 2) { + ++bob; + } else { + ++alice; + } + } + + printf("Alice: %d, Bob: %d\n", alice, bob); + return 0; +} + +void useAliceSuperPower(int *num) { + if(*num % 2 && *num > 1) { + (*num)--; + } +} + +void useBobSuperPower(int *num) { + // More reasonable power: add 1 to even numbers + if(*num % 2 == 0 && *num < 6) { + (*num)++; + } +} diff --git a/DeitelC/Chapter7/test3.c b/DeitelC/Chapter7/test3.c new file mode 100644 index 0000000..2f72c72 --- /dev/null +++ b/DeitelC/Chapter7/test3.c @@ -0,0 +1,47 @@ +#include +#include +#include + +void useAliceSuperPower(int *num); +void useBobSuperPower(int *num); + +int main(void) +{ + int bob = 0; + int alice = 0; + srand(time(NULL)); + + for(int i = 0; i < 1000; ++i){ + int die = 1 + rand() % 6; + + useAliceSuperPower(&die); + useBobSuperPower(&die); + + // Keep within 1-6 range + if(die < 1) die = 1; + if(die > 6) die = 6; + + if(die % 2) { + ++bob; + } else { + ++alice; + } + } + + printf("Alice: %d, Bob: %d\n", alice, bob); + return 0; +} + +void useAliceSuperPower(int *num) { + // Only subtract 1 if it's odd and greater than 1 + if(*num % 2 && *num > 1) { + (*num)--; + } +} + +void useBobSuperPower(int *num) { + // HALF the power: Only add 1 (not double) + if(*num < 6) { + (*num)++; + } +} diff --git a/w3resource/pointers/e1-1.c b/w3resource/pointers/e1-1.c new file mode 100644 index 0000000..32d2b21 --- /dev/null +++ b/w3resource/pointers/e1-1.c @@ -0,0 +1,19 @@ +#include + +int main(void) { + int m = 10; + int *z = &m; + int n, o; + + printf("\n\n Pointer : Show the basic declaration of pointer :\n"); + printf("-------------------------------------------------------\n"); + printf(" Here is m=10, n and o are two integer variable and *z is an integer"); + printf("\n\n z stores the address of m = %p\n", z); // Printing the address stored in z using %p + printf("\n *z stores the value of m = %i\n", *z); // Printing the value pointed to by z using *z + printf("\n &m is the address of m = %p\n", &m); // Printing the address of m using &m + printf("\n &n stores the address of n = %p\n", &n); // Printing the address of n using &n + printf("\n &o stores the address of o = %p\n", &o); // Printing the address of o using &o + printf("\n &z stores the address of z = %p\n\n", &z); // Printing the address of z using &z + + return 0; +} diff --git a/w3resource/pointers/e1-2.c b/w3resource/pointers/e1-2.c new file mode 100644 index 0000000..1a158cf --- /dev/null +++ b/w3resource/pointers/e1-2.c @@ -0,0 +1,13 @@ +#include + +int main(void) { + int m = 29; + int *ab = &m; + printf("m: %d, *ab: %d, ab: %d, address m: %d\n", m, *ab, ab, &m); + m = 32; + printf("m: %d, *ab: %d, ab: %d, address m: %d\n", m, *ab, ab, &m); + *ab = 7; + printf("m: %d, *ab: %d, ab: %d, address m: %d\n", m, *ab, ab, &m); + + return 0; +} diff --git a/w3resource/pointers/e1-3.c b/w3resource/pointers/e1-3.c new file mode 100644 index 0000000..14aa38a --- /dev/null +++ b/w3resource/pointers/e1-3.c @@ -0,0 +1,19 @@ +#include + +int main(void) { + int m = 300; + float fx = 300.600006; + char cht = 'z'; + + int *p1 = &m; + float *p2 = &fx; + char *p3 = &cht; + + printf("The & operator: m=%p, fx=%p, cht=%p\n", &m, &fx, &cht); + printf("The & and * operator: m=%d, fx=%f, cht=%c\n", *(&m), *(&fx), *(&cht)); + printf("Using only pointer variable: m=%p, fx=%p, cht=%p\n", p1, p2, p3); + printf("Using only pointer operator: m=%d, fx=%f, cht=%c\n", *p1, *p2, *p3); + + + return 0; +} diff --git a/w3resource/pointers/e1-4.c b/w3resource/pointers/e1-4.c new file mode 100644 index 0000000..c99941c --- /dev/null +++ b/w3resource/pointers/e1-4.c @@ -0,0 +1,14 @@ +#include + +int main(void) { + puts("Num adder with pointer!"); + int num1, num2; + int *p1 = &num1; + int *p2 = &num2; + puts("Enter p1 and p2!"); + scanf("%d", &num1); + scanf("%d", &num2); + printf("Total:%d\n", (*p1) + (*p2)); + + return 0; +} diff --git a/w3resource/pointers/e1-5.c b/w3resource/pointers/e1-5.c new file mode 100644 index 0000000..1f7d8f8 --- /dev/null +++ b/w3resource/pointers/e1-5.c @@ -0,0 +1,24 @@ +#include + +void sumOfNums(int *num1, int *num2); + +int main(void) +{ + + int num1, num2; + puts("Enter num1 and num2"); + scanf("%d %d", &num1, &num2); + + int *p1 = &num1; + int *p2 = &num2; + + sumOfNums(p1, p2); + + + + return 0; +} + +void sumOfNums(int *num1, int *num2) { + printf("Sum of Nums: %d\n", (*num1) + (*num2)); +} diff --git a/w3resource/pointers/e1-6.c b/w3resource/pointers/e1-6.c new file mode 100644 index 0000000..dae9088 --- /dev/null +++ b/w3resource/pointers/e1-6.c @@ -0,0 +1,12 @@ +#include + +int main(void) +{ + int num1, num2; + int *p1 = &num1; + int *p2 = &num2; + puts("Enter nums!"); + scanf("%d %d", p1, p2); + (*p1 > *p2) ? printf("%d > %d", *p1, *p2) : printf("%d <= %d", *p1, *p2); + return 0; +} diff --git a/w3resource/pointers/e1-7.c b/w3resource/pointers/e1-7.c new file mode 100644 index 0000000..d8e3f84 --- /dev/null +++ b/w3resource/pointers/e1-7.c @@ -0,0 +1,21 @@ +#include + +int main(void) +{ + puts("Input the number of elements to store in the array: "); + int arr = 0; + scanf("%d", &arr); + int nums[arr] = {}; + + for(size_t j = 0; j < arr; j++) { + printf("For Element %d enter num!\n", j); + int num = 0; + scanf("%d", &num); + nums[j] = num; + } + + for(size_t j = 0; j < arr; j++) { + printf("Element %d = %d\n", j, nums[j]); + } + return 0; +}