Partial Code
Partial Code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n, a, b, c;
// Reading inputs
scanf("%d %d %d %d", &n, &a, &b, &c);
return 0;
}
return sum;
}
int main()
{
int number_of_students;
char gender;
int sum,student;
scanf("%d", &number_of_students);
int *marks = (int *) malloc(number_of_students * sizeof (int));
int main()
{
int n;
scanf("%d", &n);
//Complete the code to calculate the sum of the five digits on n.
****** ENTER CODE HERE ******
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int lexicographic_sort(const char* a, const char* b) {
return strcmp(a, b);
}
int lexicographic_sort_reverse(const char* a, const char* b) {
return strcmp(b, a);
}
int sort_by_number_of_distinct_characters(const char* a, const
char* b) {
int count_a = 0, count_b = 0;
int char_count[26] = {0};
const char* p;
for (p = a; *p; p++) {
if (!char_count[*p - 'a']) {
char_count[*p - 'a'] = 1;
count_a++;
}
}
memset(char_count, 0, sizeof(char_count));
for (p = b; *p; p++) {
if (!char_count[*p - 'a']) {
char_count[*p - 'a'] = 1;
count_b++;
}
}
if (count_a == count_b) {
return strcmp(a, b);
} else {
return count_a - count_b;
}
}
int sort_by_length(const char* a, const char* b) {
}
void string_sort(char** arr,const int len,int (*cmp_func)(const
char* a, const char* b)){
int i, j;
for (i = 0; i < len - 1; i++) {
for (j = i + 1; j < len; j++) {
if (cmp_func(arr[i], arr[j]) > 0) {
char* temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main()
{
int n;
scanf("%d", &n);
char** arr;
arr = (char**)malloc(n * sizeof(char*));
int i;
for(i = 0; i < n; i++){
*(arr + i) = (char*)malloc(1024 * sizeof(char));
scanf("%s", *(arr + i));
*(arr + i) = (char*)realloc(*(arr + i), strlen(*(arr + i))+ 1);
}
string_sort(arr, n, lexicographic_sort);
for(i = 0; i < n; i++)
printf("%s\n", arr[i]);
printf("\n");
string_sort(arr, n, lexicographic_sort_reverse);
for(i = 0; i < n; i++)
printf("%s\n", arr[i]);
printf("\n");
string_sort(arr, n, sort_by_length);
for(i = 0; i < n; i++)
printf("%s\n", arr[i]);
printf("\n");
string_sort(arr, n, sort_by_number_of_distinct_characters);
for(i = 0; i < n; i++)
printf("%s\n", arr[i]);
printf("\n");
}
5. 1D Array in C
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
return 0;
}
6. Array Reversal
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, arr[1000], i;
scanf("%d", &n);
****** ENTER CODE HERE ******
printf("\n");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node {
int data;
struct node *left;
struct node *right;
};
int main() {
int t;
int data;
scanf("%d", &t);
while(t-- > 0) {
scanf("%d", &data);
root = insert(root, data);
}
preOrder(root);
return 0;
}
8. Permutation of Strings
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int next_permutation(int n, char **s)
{
int i,j;
int k = -1;
for ( i = 0; i < n-1; i++) {
if (strcmp(s[i], s[i+1]) < 0)
k = i;
}
if (k == -1) return 0;
int l = -1;
for ( i = k+1; i < n; i++) {
if (strcmp(s[k], s[i]) < 0)
l = i;
}
****** ENTER CODE HERE ******
}
int main()
{
char **s;
int n,i;
scanf("%d", &n);
s = calloc(n, sizeof(char*));
for ( i = 0; i < n; i++)
{
s[i] = calloc(11, sizeof(char));
scanf("%s", s[i]);
}
do
{
for ( i = 0; i < n; i++)
printf("%s%c", s[i], i == n - 1 ? '\n' : ' ');
} while (next_permutation(n, s));
for ( i = 0; i < n; i++)
free(s[i]);
free(s);
return 0;
}
9. 2D Arrays
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
char* ltrim(char*);
char* rtrim(char*);
char** split_string(char*);
int parse_int(char*);
int main(){
#include <stdio.h>
#include <stdlib.h>
/*
* This stores the total number of books in each shelf.
*/
int* total_number_of_books;
/*
* This stores the total number of pages in each book of each shelf.
* The rows represent the shelves and the columns represent the books.
*/
int** total_number_of_pages;
int main()
{
int total_number_of_shelves;
scanf("%d", &total_number_of_shelves);
int i;
int total_number_of_queries;
scanf("%d", &total_number_of_queries);
while (total_number_of_queries--) {
int type_of_query;
scanf("%d", &type_of_query);
if (type_of_query == 1) {
int x, y;
scanf("%d %d", &x, &y);
} else if (type_of_query == 2) {
int x, y;
scanf("%d %d", &x, &y);
printf("%d\n", *(*(total_number_of_pages + x) + y));
} else {
int x;
scanf("%d", &x);
printf("%d\n", *(total_number_of_books + x));
}
}
if (total_number_of_books) {
free(total_number_of_books);
}
if (total_number_of_pages) {
free(total_number_of_pages);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
char *s; int i;
s = malloc(1024 * sizeof(char));
scanf("%[^\n]", s);
s = realloc(s, strlen(s) + 1);
Additional Programs