Skip to content

Commit b3c3daf

Browse files
new file: Chapter_1-4_Project_1/Game.c
new file: Chapter_1-4_Project_1/Problem.c new file: Chapter_5/01_Functions_and_Recursion.c new file: Chapter_5/02_Function.c new file: Chapter_5/03_Return_Value.c
1 parent dc329c3 commit b3c3daf

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

Chapter_1-4_Project_1/Game.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <time.h>
4+
int main(){
5+
srand(time(0));
6+
int randomNumber = (rand()%100)+1;
7+
int no_of_guesses = 0;
8+
int guessed;
9+
do{
10+
printf("Guess the number: ");
11+
scanf("%d", &guessed);
12+
if(guessed>randomNumber){
13+
printf("Enter a lower number!\n");
14+
}else if(guessed<randomNumber){
15+
printf("Enter a higher number!\n");;
16+
}
17+
no_of_guesses++;
18+
}while(guessed != randomNumber);
19+
printf("The number is: %d\n", randomNumber);
20+
printf("You guessed the number in %d guesses\n", no_of_guesses);
21+
22+
return 0;
23+
}

Chapter_1-4_Project_1/Problem.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
Write a program that generates a random number and asks the player to guess it.
3+
If the player's guess is higher than the actual number, The program displays "Lower number please"
4+
If the player's guess is lower than the actual number, The program displays "Higher number please"
5+
When the user guesses the correct number, the program displays the number of guesses the player used to arrive at the number.
6+
7+
Hint: Use loop and use a random number generator.
8+
*/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
Somtimes our program is too big to keep track of a specific piece of code.
3+
Or sometimes we want to use a large code multiple times and places in a program.
4+
In these cases, instead of finding a code or writing a code again and again, we use functions.
5+
6+
Basically, function is a way to break our code into chunks so that it is possible for a programmer to reuse them.
7+
*/

Chapter_5/02_Function.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// What is a Function?
2+
/*
3+
A function is a block of code which performs a particular task.
4+
A function can be reused by the programmer in a given program any number of times.
5+
*/
6+
7+
/*
8+
Syntax:
9+
#include <stdio.h>
10+
// function prototype
11+
void display(); // datatype function_name(parameters);
12+
int main(){
13+
int a; // variable declaration
14+
display(); // function call
15+
return 0; // return statement
16+
}
17+
// function definition
18+
void display(){
19+
printf("Hi I am display function"); // The code that will execute on call
20+
}
21+
*/
22+
23+
/*
24+
Example:
25+
Instead of writing this:
26+
#include <stdio.h>
27+
int main(){
28+
int a = 1;
29+
int b = 2;
30+
int c = a+b;
31+
printf("The sum is %d", c);
32+
33+
int a1 = 12;
34+
int b1 = 23;
35+
int c1 = a1+b1;
36+
printf("The sum is %d", c1);
37+
38+
int a2 = 2;
39+
int b2 = 27;
40+
int c2 = a+b;
41+
printf("The sum is %d", c2);
42+
return 0;
43+
}
44+
45+
we will write:
46+
#include <stdio.h>
47+
int sum(int, int);
48+
49+
int sum(int x, int y){
50+
printf("The sum is %d\n", x+y);
51+
}
52+
int main(){
53+
int a = 1;
54+
int b = 2;
55+
sum(a, b);
56+
57+
int a1 = 12;
58+
int b1 = 23;
59+
sum(a1, b1);
60+
61+
int a2 = 2;
62+
int b2 = 27;
63+
sum(a2, b2);
64+
return 0;
65+
}
66+
67+
output:
68+
The sum is 3
69+
The sum is 35
70+
The sum is 29
71+
*/

Chapter_5/03_Return_Value.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
If we change the following code:
3+
#include <stdio.h>
4+
int sum(int, int);
5+
6+
int sum(int x, int y){
7+
printf("The sum is %d\n", x+y);
8+
}
9+
int main(){
10+
int a = 1;
11+
int b = 2;
12+
sum(a, b);
13+
14+
int a1 = 12;
15+
int b1 = 23;
16+
sum(a1, b1);
17+
18+
int a2 = 2;
19+
int b2 = 27;
20+
sum(a2, b2);
21+
return 0;
22+
}
23+
24+
output:
25+
The sum is 3
26+
The sum is 35
27+
The sum is 29
28+
29+
To:
30+
#include <stdio.h>
31+
int sum(int, int);
32+
33+
int sum(int x, int y){
34+
//printf("The sum is %d\n", x+y);
35+
return x+y;
36+
}
37+
int main(){
38+
int a = 1;
39+
int b = 2;
40+
int c = sum(a, b);
41+
printf("%d\n", c);
42+
43+
int a1 = 12;
44+
int b1 = 23;
45+
sum(a1, b1);
46+
47+
int a2 = 2;
48+
int b2 = 27;
49+
sum(a2, b2);
50+
return 0;
51+
}
52+
53+
output:
54+
3
55+
56+
The return will only work if we don't use a printf statement in the function and we call the function inside a variable in main()
57+
*/

0 commit comments

Comments
 (0)