0% found this document useful (0 votes)
6 views

Partial Code

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Partial Code

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1.

Calculate the nth term

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

// Complete the following function.


****** ENTER CODE HERE ******
// Recursive case
return find_nth_term(n - 1, a, b, c) + find_nth_term(n - 2, a, b, c) +
find_nth_term(n - 3, a, b, c);
}

int main() {
int n, a, b, c;

// Reading inputs
scanf("%d %d %d %d", &n, &a, &b, &c);

// Finding the nth term


int ans = find_nth_term(n, a, b, c);

// Printing the result


printf("%d", ans);

return 0;
}

2. Student Marks Sum


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int marks_summation(int* marks, int number_of_students, char gender) {
****** ENTER CODE HERE ******
// If the gender is 'g' (girls), sum marks at odd indices
else if (gender == 'g') {
for (i = 1; i < number_of_students; i += 2) {
sum += marks[i];
}
}

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));

for (student = 0; student < number_of_students; student++) {


scanf("%d", (marks + student));
}

scanf(" %c", &gender);


sum = marks_summation(marks, number_of_students, gender);
printf("%d", sum);
free(marks);
return 0;
}

3. Sum of Digits of a five Digit


Number
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{

int n;
scanf("%d", &n);
//Complete the code to calculate the sum of the five digits on n.
****** ENTER CODE HERE ******
}

4. Sorting Array of Strings

#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) {

****** ENTER CODE HERE ******

}
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() {

****** ENTER CODE HERE ******

printf("sum of array is :%d\n", sum); free(arr);

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;
}

7. Binary Search Tree Insertion

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

struct node {

int data;
struct node *left;
struct node *right;

};

void preOrder( struct node *root) {

if( root == NULL )


return;
printf("%d ",root->data);
preOrder(root->left);
preOrder(root->right);

struct node* insert(struct node* root, int data) {

****** ENTER CODE HERE ******

int main() {

struct node* root = NULL;

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(){

****** ENTER CODE HERE ******


}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;

char* data = malloc(alloc_length);


while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) {
break;
}
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
break;
}
alloc_length <<= 1;
data = realloc(data, alloc_length);
if (!data) {
data = '\0';
break;
}
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
data = realloc(data, data_length);
if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);
if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}
return data;
}
char* ltrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
while (*str != '\0' && isspace(*str)) {
str++;
}
return str;
}
char* rtrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}

char* end = str + strlen(str) - 1;


while (end >= str && isspace(*end)) {
end--;
}
*(end + 1) = '\0';
return str;
}
char** split_string(char* str) {
char** splits = NULL;
char* token = strtok(str, " ");
int spaces = 0;
while (token) {
splits = realloc(splits, sizeof(char*) * ++spaces);
if (!splits) {
return splits;
}
splits[spaces - 1] = token;
token = strtok(NULL, " ");
}
return splits;
}
int parse_int(char* str) {
char* endptr;
int value = strtol(str, &endptr, 10);
if (endptr == str || *endptr != '\0') {
exit(EXIT_FAILURE);
}
return value;
}

10. Dynamic Arrays

#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;

// Initialize memory for total_number_of_books


total_number_of_books = (int*) calloc(total_number_of_shelves,
sizeof(int));

// Initialize memory for total_number_of_pages


total_number_of_pages = (int**) calloc(total_number_of_shelves,
sizeof(int*));

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);

****** ENTER CODE HERE ******

} 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);
}

for (i = 0; i < total_number_of_shelves; i++) {


if (*(total_number_of_pages + i)) {
free(*(total_number_of_pages + i));
}
}

if (total_number_of_pages) {
free(total_number_of_pages);
}

return 0;
}

11. Printing Tokens

#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);

****** ENTER CODE HERE ******


return 0;
}

Additional Programs

1. Remove duplicates from sorted


array
#include <stdio.h>
// Function to remove duplicates from a sorted array
int removeDuplicates(int* nums, int numsSize) {
// Edge case: if the array is empty, no unique elements exist
if (numsSize == 0) {
return 0;
}
// k will track the index of the last unique element
int k = 1;
int i;
// Start from the second element (index 1)
for (i = 1; i < numsSize; i++) {
// If the current element is different from the previous one, it's unique
if (nums[i] != nums[i - 1]) {
// Place the unique element at position k
nums[k] = nums[i];
k++; // Increment k to track the number of unique elements
}
}
// Return the number of unique elements
return k;
}
// Main function to test the removeDuplicates function
****** ENTER CODE HERE ******
// Print the modifed array and the number of unique elements
printf("Array after removing duplicates: ");
for (i = 0; i < newSize; i++) {
printf("%d ", nums[i]);
}
printf("\n");
printf("Number of unique elements: %d\n", newSize);
return 0;
}

2. Find the index of first


occurrence in a string
#include <stdio.h>
#include <string.h>
int main() {
char haystack[100];
char needle[100];
scanf("%s", haystack);
scanf("%s", needle);
int result = strStr(haystack, needle);
printf("l%d\n", result);
return 0;
}
int strStr(char* haystack, char* needle) {
int hsize = strlen(haystack);
int nsize = strlen(needle);
int res = -1;
int i = 0, j= 0;
while (haystack[i]!='\0' && needle[j]!='\0' ) {
****** ENTER CODE HERE ******
}

You might also like