Skip to content

Commit 4c46158

Browse files
committed
Uploading Codes & Notes
1 parent af98032 commit 4c46158

31 files changed

+303
-1
lines changed

Notes/Chapter. 10/10_01_file_basics.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include<stdio.h>
2+
3+
int main(){
4+
FILE *ptr;
5+
// ptr = fopen("sample2.txt", "r"); //--> for reading the file
6+
//ptr = fopen("sample2.txt", "w"); //--> for writing to a file
7+
return 0;
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include<stdio.h>
2+
3+
int main(){
4+
FILE *ptr;
5+
int num;
6+
int num2;
7+
ptr = fopen("Harry.txt", "r");
8+
fscanf(ptr, "%d", &num);
9+
fscanf(ptr, "%d", &num2);
10+
fclose(ptr);
11+
printf("The value of num is %d\n", num);
12+
printf("The value of num2 is %d\n", num2);
13+
return 0;
14+
}

Notes/Chapter. 10/10_03_quick_quiz.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include<stdio.h>
2+
3+
int main(){
4+
FILE *ptr;
5+
int num;
6+
int num2;
7+
ptr = fopen("Harr3y.txt", "r");
8+
if (ptr == NULL){
9+
printf("This file does not exist\n");
10+
}
11+
else{
12+
fscanf(ptr, "%d", &num);
13+
fscanf(ptr, "%d", &num2);
14+
fclose(ptr);
15+
printf("The value of num is %d\n", num);
16+
printf("The value of num2 is %d\n", num2);
17+
}
18+
return 0;
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include<stdio.h>
2+
3+
int main(){
4+
FILE *fptr;
5+
int number = 45;
6+
fptr = fopen("generated.txt", "w");
7+
fprintf(fptr, "The number is %d\n", number);
8+
fprintf(fptr, "Thanks for using fprintf", number);
9+
fclose(fptr);
10+
11+
return 0;
12+
}

Notes/Chapter. 10/10_05_fgetc_fputc.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<stdio.h>
2+
3+
int main(){
4+
FILE *ptr;
5+
// fgetc demo for reading a file
6+
// ptr = fopen("getcdemo.txt", "r");
7+
// char c = fgetc(ptr);
8+
// printf("The value of my character is %c\n", fgetc(ptr));
9+
// printf("The value of my character is %c\n", fgetc(ptr));
10+
// printf("The value of my character is %c\n", fgetc(ptr));
11+
// printf("The value of my character is %c\n", fgetc(ptr));
12+
// printf("The value of my character is %c\n", fgetc(ptr));
13+
14+
ptr = fopen("putcdemo.txt", "w");
15+
putc('c', ptr);
16+
putc('c', ptr);
17+
putc('c', ptr);
18+
fclose(ptr);
19+
return 0;
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include<stdio.h>
2+
3+
int main(){
4+
FILE *ptr;
5+
char c;
6+
ptr = fopen("getcdemo.txt", "r");
7+
c = fgetc(ptr);
8+
while(c!=EOF){
9+
printf("%c", c);
10+
c = fgetc(ptr);
11+
}
12+
return 0;
13+
}

Notes/Chapter. 10/10_07_pr_01.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include<stdio.h>
2+
3+
int main(){
4+
int a, b, c;
5+
FILE *ptr;
6+
ptr = fopen("pr01.txt", "r");
7+
fscanf(ptr, "%d %d %d", &a, &b, &c);
8+
printf("The values of a b and c is %d %d %d\n", a, b, c);
9+
10+
return 0;
11+
}

Notes/Chapter. 10/10_08_pr_02.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include<stdio.h>
2+
3+
int main(){
4+
FILE *ptr;
5+
int num;
6+
printf("Enter the integer you need the table of\n");
7+
scanf("%d", &num);
8+
ptr = fopen("table.txt", "w");
9+
for(int i=0; i<10; i++){
10+
fprintf(ptr, "%d X %d = %d\n", num, i+1, num*(i+1));
11+
}
12+
fclose(ptr);
13+
printf("Successfully generated table of %d to table.txt\n", num);
14+
return 0;
15+
}

Notes/Chapter. 10/10_09_pr_03.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include<stdio.h>
2+
3+
int main(){
4+
FILE *ptr1;
5+
FILE *ptr2;
6+
ptr1 = fopen("a.txt", "r");
7+
ptr2 = fopen("b.txt", "w");
8+
9+
char c = fgetc(ptr1);
10+
while(c!=EOF){
11+
fputc(c, ptr2);
12+
fputc(c, ptr2);
13+
c = fgetc(ptr1);
14+
}
15+
fclose(ptr1);
16+
fclose(ptr2);
17+
return 0;
18+
}
589 KB
Binary file not shown.

Notes/Chapter. 10/Chapter 10.pdf

1.81 MB
Binary file not shown.

Notes/Chapter. 10/Harry.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
34 88

Notes/Chapter. 10/a.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
for problem number 3

Notes/Chapter. 10/b.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ffoorr pprroobblleemm nnuummbbeerr 33

Notes/Chapter. 10/generated.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The number is 45
2+
Thanks for using fprintf

Notes/Chapter. 10/getcdemo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is my getc demo file

Notes/Chapter. 10/pr01.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
23 54 67

Notes/Chapter. 10/putcdemo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ccc

Notes/Chapter. 10/sample.txt

Whitespace-only changes.

Notes/Chapter. 10/sample2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sdfsdf

Notes/Chapter. 10/table.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
34 X 1 = 34
2+
34 X 2 = 68
3+
34 X 3 = 102
4+
34 X 4 = 136
5+
34 X 5 = 170
6+
34 X 6 = 204
7+
34 X 7 = 238
8+
34 X 8 = 272
9+
34 X 9 = 306
10+
34 X 10 = 340

Notes/Chapter. 11/11_01_malloc.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
int main(){
5+
int *ptr;
6+
// Sizeof operator in C
7+
// printf("The size of int on my pc is %d\n", sizeof(int));
8+
// printf("The size of float on my pc is %d\n", sizeof(float));
9+
// printf("The size of char on my pc is %d\n", sizeof(char));
10+
ptr = (int *) malloc(6 * sizeof(int));
11+
for(int i=0; i<6;i++){
12+
printf("Enter the value of %d element: \n", i);
13+
scanf("%d", &ptr[i]);
14+
}
15+
16+
for(int i=0; i<6;i++){
17+
printf("The value of %d element is: %d\n", i, ptr[i]);
18+
}
19+
return 0;
20+
}

Notes/Chapter. 11/11_02_calloc.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
int main(){
5+
int *ptr;
6+
ptr = (int *) calloc(6, sizeof(int));
7+
// for(int i=0; i<6;i++){
8+
// printf("Enter the value of %d element: \n", i);
9+
// scanf("%d", &ptr[i]);
10+
// }
11+
12+
for(int i=0; i<6;i++){
13+
printf("The value of %d element is: %d\n", i, ptr[i]);
14+
}
15+
return 0;
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
int main(){
5+
int *ptr;
6+
int n;
7+
printf("How many integers do you want to enter:\n");
8+
scanf("%d", &n);
9+
ptr = (int *) calloc(n, sizeof(int));
10+
for(int i=0; i<n;i++){
11+
printf("Enter the value of %d element: \n", i);
12+
scanf("%d", &ptr[i]);
13+
}
14+
15+
for(int i=0; i<n;i++){
16+
printf("The value of %d element is: %d\n", i, ptr[i]);
17+
}
18+
return 0;
19+
}

Notes/Chapter. 11/11_04_free_demo.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+
4+
int main(){
5+
int *ptr;
6+
int *ptr2;
7+
// Sizeof operator in C
8+
// printf("The size of int on my pc is %d\n", sizeof(int));
9+
// printf("The size of float on my pc is %d\n", sizeof(float));
10+
// printf("The size of char on my pc is %d\n", sizeof(char));
11+
ptr = (int *) malloc(600 * sizeof(int));
12+
for(int i=0; i<600;i++){
13+
ptr2 = (int *) malloc(600000 * sizeof(int));
14+
printf("Enter the value of %d element: \n", i);
15+
scanf("%d", &ptr[i]);
16+
free(ptr2);
17+
}
18+
19+
for(int i=0; i<6;i++){
20+
printf("The value of %d element is: %d\n", i, ptr[i]);
21+
}
22+
return 0;
23+
}

Notes/Chapter. 11/11_05_realloc.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
int main(){
5+
int *ptr;
6+
ptr = (int *) malloc(6 * sizeof(int));
7+
for(int i=0; i<6;i++){
8+
printf("Enter the value of %d element: \n", i);
9+
scanf("%d", &ptr[i]);
10+
}
11+
12+
for(int i=0; i<6;i++){
13+
printf("The value of %d element is: %d\n", i, ptr[i]);
14+
}
15+
16+
// Reallocate ptr using realloc()
17+
ptr = realloc(ptr, 10*sizeof(int));
18+
19+
for(int i=0; i<10;i++){
20+
printf("Enter the value of %d element: \n", i);
21+
scanf("%d", &ptr[i]);
22+
}
23+
24+
for(int i=0; i<10;i++){
25+
printf("The value of %d element is: %d\n", i, ptr[i]);
26+
}
27+
28+
29+
return 0;
30+
}

Notes/Chapter. 11/11_06_pr_04.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
int main(){
5+
int *ptr;
6+
ptr = (int*) malloc(5*sizeof(int));
7+
for(int i=0; i<5;i++){
8+
printf("Enter the value of %d element: \n", i);
9+
scanf("%d", &ptr[i]);
10+
}
11+
12+
for(int i=0; i<5;i++){
13+
printf("The value of %d element is: %d \n", i, ptr[i]);
14+
}
15+
16+
ptr = realloc(ptr, 10*sizeof(int));
17+
for(int i=0; i<10;i++){
18+
printf("Enter the value of %d element: \n", i);
19+
scanf("%d", &ptr[i]);
20+
}
21+
22+
for(int i=0; i<10;i++){
23+
printf("The value of %d element is: %d \n", i, ptr[i]);
24+
}
25+
return 0;
26+
}

Notes/Chapter. 11/11_07_pr05.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
int main(){
5+
int *ptr;
6+
ptr = (int*) malloc(10*sizeof(int));
7+
for(int i=0; i<10;i++){
8+
ptr[i] = 7*(i+1);
9+
printf("The value of 7 X %d = %d \n", i+1,ptr[i]);
10+
}
11+
12+
ptr = realloc(ptr, 15*sizeof(int));
13+
printf("\nAfter reallocating.....\n\n");
14+
for(int i=0; i<15;i++){
15+
ptr[i] = 7*(i+1);
16+
printf("The value of 7 X %d = %d \n", i+1,ptr[i]);
17+
}
18+
19+
return 0;
20+
}
Binary file not shown.

Notes/Chapter. 11/Chapter 11.pdf

1.43 MB
Binary file not shown.

Notes/Message

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)