Skip to content

Commit a8b3945

Browse files
new file: 19_PRACTICESET2.c
new file: 20_arrays.c new file: 21_PRACTICESET3.c new file: 22_pointers.c new file: 23_pointers.c new file: 24_null_pointer.c new file: 25_pointer_arithematic.c new file: 26_strings.c renamed: 8_P1_multiplication_table.c -> 8_PRACTICESET1.c
1 parent 160422f commit a8b3945

9 files changed

+295
-0
lines changed

Diff for: 19_PRACTICESET2.c

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <math.h>
4+
#include <stdlib.h>
5+
6+
int main()
7+
{
8+
// taking input string
9+
char input[1000];
10+
printf("Input format <number>$<unit in lowercase>\n");
11+
scanf("%s", input);
12+
13+
// taking to which input string
14+
char toUnit[100];
15+
printf("To which unit\n");
16+
scanf("%s", toUnit);
17+
18+
// in = input number (float) , unit is unit of input number
19+
float in = 0;
20+
char unit[100] = {0};
21+
22+
// a for detecting $ sign
23+
int a = 1;
24+
25+
// just for iteration
26+
int j = 0;
27+
int k = 0;
28+
29+
// string storing the number
30+
char forn[100];
31+
32+
for (int i = 0; i < strlen(input); i++)
33+
{
34+
if (a)
35+
{
36+
if (input[i] == '$')
37+
{
38+
a = 0;
39+
continue;
40+
}
41+
forn[k++] = input[i];
42+
}
43+
else
44+
{
45+
unit[j++] = input[i];
46+
}
47+
}
48+
49+
// convertign string to float
50+
char *endPtr;
51+
in = strtof(forn, &endPtr);
52+
53+
// terminating unit as its length was 100
54+
unit[j] = '\0';
55+
56+
// iur = input unit row
57+
int iur = -1;
58+
int iuc = -1;
59+
60+
int numOfCols = 16;
61+
char *DISTANCE_UNITS[][16] = {{"pm","/","/","nm","/","/","um","/","/","mm", "cm", "dm", "m", "dam", "hm", "km"},
62+
{"pg","/","/","ng","/","/","ug","/","/","mg", "cg", "dg", "g", "dag", "hg", "kg"},
63+
{"pl","/","","nl","/","/","ul","/","/","ml", "cl", "dl", "l", "dal", "hl", "kl"}};
64+
65+
for (int i = 0; i < 3; i++)
66+
{
67+
for (int j = 0; j < numOfCols; j++)
68+
{
69+
if (!strcmp(unit, DISTANCE_UNITS[i][j])) // strcmp return 0 for same strings
70+
{
71+
iur = i;
72+
iuc = j;
73+
}
74+
}
75+
}
76+
// the ouput float out;
77+
float out = in * 1.0;
78+
79+
// our = output unit row
80+
int our = -1;
81+
int ouc = -1;
82+
83+
for (int i = 0; i < 3; i++)
84+
{
85+
for (int j = 0; j < numOfCols; j++)
86+
{
87+
if (!strcmp(toUnit, DISTANCE_UNITS[i][j]))
88+
{
89+
our = i;
90+
ouc = j;
91+
if (iuc != -1)
92+
{
93+
out = in * pow(10, (iuc - ouc));
94+
}
95+
}
96+
}
97+
}
98+
printf("%f %s",out,toUnit);
99+
}

Diff for: 20_arrays.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdio.h>
2+
3+
void printMatrix(int rows,int columns,int matrix[rows][columns]);
4+
5+
int main(){
6+
int n;
7+
scanf("%d",&n);
8+
9+
int arr1[n][n];
10+
for(int i=0;i<n;i++){
11+
for(int j=0;j<n;j++){
12+
scanf("%d",&arr1[i][j]);
13+
}
14+
}
15+
16+
printMatrix(n,n,arr1);
17+
18+
}
19+
20+
21+
22+
void printMatrix(int rows,int columns, int matrix[rows][columns]){
23+
for(int i=0;i<rows;i++){
24+
printf("|");
25+
for(int j=0;j<columns;j++){
26+
printf(" %d",matrix[i][j]);
27+
}
28+
printf(" |\n");
29+
}
30+
}

Diff for: 21_PRACTICESET3.c

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdio.h>
2+
3+
int fabbonachi(int);
4+
5+
int main(){
6+
int n;
7+
scanf("%d",&n);
8+
printf("%dth fabbonachi is %d",n,fabbonachi(n));
9+
}
10+
11+
int fabbonachi(int n){
12+
if(n==1 || n==0){
13+
return n;
14+
}
15+
else{
16+
return fabbonachi(n-1)+fabbonachi(n-2);
17+
}
18+
}

Diff for: 22_pointers.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdio.h>
2+
3+
int main(){
4+
int n=10;
5+
int *pn = &n;
6+
7+
printf("The value of n is %d",n);
8+
printf("\nThe address of n is %x",pn); // %x for hexdecimal
9+
printf("\nThe value at address %x is %d",pn,*pn);
10+
}
11+
12+
13+
/**
14+
Use of pointers :
15+
> Dynamic memory allocation
16+
> Arrays, functions and structures
17+
> Return multiple values from a function
18+
> pointer reduces the code and improves the perfomance
19+
*/

Diff for: 23_pointers.c

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
// pointer is a varible which store address of another variable
3+
// types of pointer = type of varibale whose address it is storing
4+
// types of pointer = int, float, char, array, string, function
5+
// decleared with * sign
6+
7+
// a pointer can also store address of other pointer
8+
9+
#include <stdio.h>
10+
11+
int main(){
12+
int n = 6;
13+
int *ptr; // * is just syntax for pointer, * donot have any role like indirection operator
14+
ptr = &n;
15+
16+
scanf("%d",ptr); // no & required, add we are giving the adress of n
17+
18+
printf("%d",n); // prints value of n
19+
20+
printf("\n%d",ptr); // prints adress of n which is the value of ptr
21+
22+
printf("\n%d",*ptr); // prints value of n, * working as its role of indirection operator
23+
24+
int a = 3;
25+
int b = *&a;
26+
printf("\n%d == %d",a,b); // b==a
27+
28+
}
29+
30+
// &amrit = address of amrit
31+
// *a = program treats value of a as an adress of some other variable and search for this address and goes for that varible's value
32+
// * is called indirection operator
33+
34+
// int *p = &a : decleration and assignment of pointer
35+
// p : address of a
36+
// *p : follow address
37+
38+
// * and & are just opposite of each other
39+
// int *i = &a;
40+
// int j = *i // j==a
41+
42+
// int* a is same as int *a

Diff for: 24_null_pointer.c

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
3+
int main(){
4+
// int n = NULL; cannot do this
5+
6+
int *ptr = NULL;
7+
printf("%d %d",ptr);
8+
}
9+
10+
// Null pointer : pointer not assigned any value (NULL)
11+
// Used to prevent garbage values

Diff for: 25_pointer_arithematic.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
3+
int main(){
4+
int a = 34;
5+
int *pa = &a;
6+
7+
printf("%d\n",pa);
8+
printf("%d",pa+1);
9+
return 0;
10+
}

Diff for: 26_strings.c

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <stdio.h>
2+
#include <string.h> // for strlen and strcpy
3+
#include <stdlib.h> // for malloc
4+
5+
int main(){
6+
7+
// 1 Array type string decleration
8+
9+
char str1[20] = "Hello"; // 'Hello' + '\0'
10+
printf("%s",str1);
11+
printf("\n%d",strlen(str1)); // strlen doesnot count the null terminator in length of string
12+
/*
13+
Here, "Hello" takes 6 spaces (5 characters + 1 null terminator), and the remaining 14 elements of the array are uninitialized.
14+
strlen will return 5, it doesnot count null terminator
15+
*/
16+
17+
// 2 Array type string decleration without array length
18+
19+
char str2[] = "Hwllo"; // Equivalent to str[6] = "Hwllo"
20+
printf("\n%s",str2);
21+
printf("\n%d",strlen(str2));
22+
23+
// yes the declration length counts the null character but strlen donot
24+
25+
// 3 Pointer to a string
26+
27+
char *str3 = "Helloworld"; // points to a string located in read-only memory
28+
printf("\n%s",str3);
29+
printf("\n%d",strlen(str3));
30+
31+
// cannot modify str3 later as readonly memory
32+
33+
// 4 Array of individual characters
34+
35+
char str4[6] = {'A', 'm', 'r', 'i', 't', '\0'};
36+
printf("\n%s",str4);
37+
printf("\n%d",strlen(str4));
38+
39+
// 5 strcpy method
40+
41+
char str5[6];
42+
strcpy(str5,"Aloka");
43+
44+
printf("\n%s",str5);
45+
printf("\n%d",strlen(str5));
46+
47+
// 6 Dynmaic allocation of strings
48+
49+
char *str6 = (char *)malloc(6*sizeof(char));
50+
strcpy(str6,"Hallo");
51+
printf("\n%s",str6);
52+
printf("\n%d",strlen(str6));
53+
54+
// donot forget to free the memeory
55+
free(str6);
56+
57+
58+
return 0;
59+
60+
61+
}
62+
63+
/**
64+
* String is not a datatype in c.
65+
* We represent string as an array of characters ending with Null terminator \0
66+
*/
File renamed without changes.

0 commit comments

Comments
 (0)