diff --git a/Week 3/Solutions/LOC Assignment 3/q1.c b/Week 3/Solutions/LOC Assignment 3/q1.c new file mode 100644 index 0000000..8693456 --- /dev/null +++ b/Week 3/Solutions/LOC Assignment 3/q1.c @@ -0,0 +1,30 @@ +#include +#include + +void remove_char(char *s, char c) +{ + int j = 0; + for (int i = 0; s[i] != '\0'; i++) + { + if (s[i] != c) + { + s[j++] = s[i]; + } + } + s[j] = '\0'; +} + +void main() +{ + + char s[100], c; + + printf("Enter a string: "); + fgets(s, sizeof(s), stdin); + printf("Enter character to be removed: "); + scanf("%c", &c); + + remove_char(s, c); + + printf("Character removed from string gives: %s\n", s); +} diff --git a/Week 3/Solutions/LOC Assignment 3/q2.c b/Week 3/Solutions/LOC Assignment 3/q2.c new file mode 100644 index 0000000..799cd14 --- /dev/null +++ b/Week 3/Solutions/LOC Assignment 3/q2.c @@ -0,0 +1,32 @@ +#include +#include + +void count_chars(char *s, int f[]) +{ + int i; + for (i = 0; s[i] != '\0'; i++) + { + f[s[i]]++; + } +} + +void main() +{ + char s[100]; + int f[256] = {0}; + int i; + + printf("Enter the string: "); + fgets(s, sizeof(s), stdin); + + count_chars(s, f); + + printf("Character frequency in the string :"); + for (i = 0; i < 256; i++) + { + if (f[i] > 0) + { + printf("'%c' appeared %d times in the string \n", i, f[i]); + } + } +} diff --git a/Week 3/Solutions/LOC Assignment 3/q3.c b/Week 3/Solutions/LOC Assignment 3/q3.c new file mode 100644 index 0000000..3010c98 --- /dev/null +++ b/Week 3/Solutions/LOC Assignment 3/q3.c @@ -0,0 +1,26 @@ +#include + +void add_numbers(int *x, int *y, int *r) +{ + *r = *x + *y; +} + +void main() +{ + int n, m, add; + int *p1, *p2, *p_add; + + printf("Enter a number: "); + scanf("%d", &n); + + printf("Enter another number to add: "); + scanf("%d", &m); + + p1 = &n; + p2 = &m; + p_add = &add; + + add_numbers(p1, p2, p_add); + + printf("%d added with %d gives %d\n", n, m, add); +} diff --git a/Week 3/Solutions/LOC Assignment 3/q4.c b/Week 3/Solutions/LOC Assignment 3/q4.c new file mode 100644 index 0000000..791583d --- /dev/null +++ b/Week 3/Solutions/LOC Assignment 3/q4.c @@ -0,0 +1,25 @@ +#include + +void factorial(int *n, int *r) +{ + *r = 1; + for (int i = 1; i <= *n; i++) + { + *r *= i; + } +} + +void main() +{ + int n, f, *p_n, *p_f; + + printf("Enter number to find factorial: "); + scanf("%d", &n); + + p_n = &n; + p_f = &f; + + factorial(p_n, p_f); + + printf("%d factorial is %d\n", n, f); +} diff --git a/Week 3/Solutions/LOC Assignment 3/q5.txt b/Week 3/Solutions/LOC Assignment 3/q5.txt new file mode 100644 index 0000000..e624f30 --- /dev/null +++ b/Week 3/Solutions/LOC Assignment 3/q5.txt @@ -0,0 +1,16 @@ +5a)char ****k; + +ans(ii) k is a pointer to a pointer to a pointer to a pointer to a char + + +5b) void main() +{ +int k = 5; +int *p = &k; +int **m = &p; +**m = 6; +printf("%d\n", k); +} + + +ans= 6 \ No newline at end of file diff --git a/Week 3/Solutions/LOC Assignment 3/q6.txt b/Week 3/Solutions/LOC Assignment 3/q6.txt new file mode 100644 index 0000000..d70f839 --- /dev/null +++ b/Week 3/Solutions/LOC Assignment 3/q6.txt @@ -0,0 +1,8 @@ +6)Choose the correct statement about structures in C program: +a. Structure elements can be initialized at the time of declaration. +b. Structure members cannot be initialized at the time of declaration +c. Only integer members of structure can be initialized at the time of declaration +d. None of the above + +ans= +b. Struture members cannot be initialized at the time of declaration \ No newline at end of file diff --git a/Week 3/Solutions/LOC Assignment 3/q7.txt b/Week 3/Solutions/LOC Assignment 3/q7.txt new file mode 100644 index 0000000..beb363b --- /dev/null +++ b/Week 3/Solutions/LOC Assignment 3/q7.txt @@ -0,0 +1,7 @@ +What is the size of a C structure? How the size of a C structure is calculated + +ans- +The size of members of a structure depends on their own data types, like int has a size of 4 bytes, char is 1 byte, so while calculating the total size of the struct, the size of individual members are added but this is also affected by padding added by the compiler for proper alignment of the members. When a structure member is followed by a member of larger size then the compiler adds padding to the align the members in a proper manner so that it can be accessed in the memory more conviniently. + +The size of structure and be calculated by using sizeof() function. +e.g. If a struct consists of int, long int and long long int then the total size of the structure is calculated by adding the size of int. long int and long long int i.e. 4, 4 and 8 respectively which gives 16 as the result. \ No newline at end of file diff --git a/Week2/Solutions/LOC Assignment 2/A1.c b/Week2/Solutions/LOC Assignment 2/A1.c new file mode 100644 index 0000000..c976935 --- /dev/null +++ b/Week2/Solutions/LOC Assignment 2/A1.c @@ -0,0 +1,29 @@ +#include +int main() +{ + int Size, i, num, occr = 0; + + printf("Enter the Array size = "); + scanf("%d", &Size); + + int arr[Size]; + + printf("Enter the Array %d elements : ", Size); + for (i = 0; i < Size; i++) + { + scanf("%d", &arr[i]); + } + + printf("Please Enter the Array Item to Know = "); + scanf("%d", &num); + + for (i = 0; i < Size; i++) + { + if (arr[i] == num) + { + occr++; + } + } + + printf("%d Occurred %d Times.\n", num, occr); +} diff --git a/Week2/Solutions/LOC Assignment 2/A10.c b/Week2/Solutions/LOC Assignment 2/A10.c new file mode 100644 index 0000000..4a2af51 --- /dev/null +++ b/Week2/Solutions/LOC Assignment 2/A10.c @@ -0,0 +1,36 @@ +#include +#define MAX_SIZE 100 + +int main() +{ + char str[MAX_SIZE]; + int alphabets, digits, others, i; + + alphabets = digits = others = i = 0; + printf("Enter any string : "); + gets(str); + + while (str[i] != '\0') + { + if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) + { + alphabets++; + } + else if (str[i] >= '0' && str[i] <= '9') + { + digits++; + } + else + { + others++; + } + + i++; + } + + printf("Alphabets = %d\n", alphabets); + printf("Digits = %d\n", digits); + printf("Special characters = %d", others); + + return 0; +} \ No newline at end of file diff --git a/Week2/Solutions/LOC Assignment 2/A2.c b/Week2/Solutions/LOC Assignment 2/A2.c new file mode 100644 index 0000000..3508284 --- /dev/null +++ b/Week2/Solutions/LOC Assignment 2/A2.c @@ -0,0 +1,27 @@ +#include +int main() +{ + int arr1[50], arr2[50], size1, size2, i, k, merge[100]; + printf("Enter Array 1 Size: "); + scanf("%d", &size1); + printf("Enter Array 1 Elements: "); + for (i = 0; i < size1; i++) + { + scanf("%d", &arr1[i]); + merge[i] = arr1[i]; + } + k = i; + printf("Enter Array 2 Size: "); + scanf("%d", &size2); + printf("Enter Array 2 Elements: "); + for (i = 0; i < size2; i++) + { + scanf("%d", &arr2[i]); + merge[k] = arr2[i]; + k++; + } + printf("\nThe new array after merging :\n"); + for (i = 0; i < k; i++) + printf("%d ", merge[i]); + return 0; +} \ No newline at end of file diff --git a/Week2/Solutions/LOC Assignment 2/A3.c b/Week2/Solutions/LOC Assignment 2/A3.c new file mode 100644 index 0000000..e69de29 diff --git a/Week2/Solutions/LOC Assignment 2/A4.c b/Week2/Solutions/LOC Assignment 2/A4.c new file mode 100644 index 0000000..ac48dfc --- /dev/null +++ b/Week2/Solutions/LOC Assignment 2/A4.c @@ -0,0 +1,17 @@ +#include +int main() +{ + int rows, i, j, number = 1; + printf("Enter the number of rows: "); + scanf("%d", &rows); + for (i = 1; i <= rows; i++) + { + for (j = 1; j <= i; ++j) + { + printf("%d ", number); + ++number; + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/Week2/Solutions/LOC Assignment 2/A5.c b/Week2/Solutions/LOC Assignment 2/A5.c new file mode 100644 index 0000000..4867376 --- /dev/null +++ b/Week2/Solutions/LOC Assignment 2/A5.c @@ -0,0 +1,23 @@ +#include + +int main() +{ + int a[5] = {5, 6, 7, 2, 8}, n = 5; + int b[n], i; + for (i = 0; i < n; i++) + { + b[i] = a[i]; + } + printf("The first array is :"); + for (i = 0; i < n; i++) + { + printf("%d ", a[i]); + } + + printf("\nThe second array is :"); + for (i = 0; i < n; i++) + { + printf("%d ", b[i]); + } + return 0; +} \ No newline at end of file diff --git a/Week2/Solutions/LOC Assignment 2/A6.c b/Week2/Solutions/LOC Assignment 2/A6.c new file mode 100644 index 0000000..316ee7e --- /dev/null +++ b/Week2/Solutions/LOC Assignment 2/A6.c @@ -0,0 +1,31 @@ +#include + +void main() +{ + int n, i, j, mn, mc = 0; + printf("Enter the number of elements in the array: "); + scanf("%d", &n); + int arr[n]; + printf("Enter the elements of the array:\n"); + for (i = 0; i < n; i++) + { + scanf("%d", &arr[i]); + } + for (i = 0; i < n; i++) + { + int count = 0; + for (j = i; j < n; j++) + { + if (arr[j] == arr[i]) + { + count++; + } + } + if (count > mc) + { + mc = count; + mn = arr[i]; + } + } + printf("The maximum occurring integer is %d", mn); +} \ No newline at end of file diff --git a/Week2/Solutions/LOC Assignment 2/A7.c b/Week2/Solutions/LOC Assignment 2/A7.c new file mode 100644 index 0000000..88e4b18 --- /dev/null +++ b/Week2/Solutions/LOC Assignment 2/A7.c @@ -0,0 +1,39 @@ +#include +void moveNegative(int arr[], int n) +{ + int i, j = 0, temp; + for (i = 0; i < n; i++) + { + if (arr[i] < 0) + { + if (i != j) + { + temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + j++; + } + } +} + +void main() +{ + int arr[] = {8, 7, -6, -1, 3, -4, 2, 0}; + int n = sizeof(arr) / sizeof(arr[0]); + int i; + + printf("Original array: "); + for (i = 0; i < n; i++) + { + printf("%d ", arr[i]); + } + + moveNegative(arr, n); + + printf("\nArray with negative elements moved to one side: "); + for (i = 0; i < n; i++) + { + printf("%d ", arr[i]); + } +} \ No newline at end of file diff --git a/Week2/Solutions/LOC Assignment 2/A8.c b/Week2/Solutions/LOC Assignment 2/A8.c new file mode 100644 index 0000000..33c4b07 --- /dev/null +++ b/Week2/Solutions/LOC Assignment 2/A8.c @@ -0,0 +1,29 @@ +#include +#include +void main() +{ + char str[100]; + int i, len, vowel, consonant; + + printf("Enter any string: "); + gets(str); + + vowel = 0; + consonant = 0; + len = strlen(str); + + for (i = 0; i < len; i++) + { + if (str[i] >= 'a' && str[i] <= 'z') + { + + if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') + vowel++; + else + consonant++; + } + } + + printf("Total number of vowel = %d\n", vowel); + printf("Total number of consonant = %d\n", consonant); +} \ No newline at end of file diff --git a/Week2/Solutions/LOC Assignment 2/A9.c b/Week2/Solutions/LOC Assignment 2/A9.c new file mode 100644 index 0000000..7a5d71c --- /dev/null +++ b/Week2/Solutions/LOC Assignment 2/A9.c @@ -0,0 +1,16 @@ +#include +int main() +{ + int rows, i, j; + printf("Enter number of rows:"); + scanf("%d", &rows); + for (i = rows; i >= 1; --i) + { + for (j = 0; j <= i; ++j) + { + printf("*"); + } + printf("\n"); + } + return 0; +} \ No newline at end of file