C - Module Bank
C - Module Bank
int X=5;
int *pointer;
pointer=&X;
free(pointer)
(c) A C-language professor has a class of students. Disappointed with their lack of discipline, the
professor decides to cancel class if fewer than some number of students are present when class starts.
zero: Indicate student arrived exactly at right time
negative number: indicate student arrived before the class started
positive number: indicate the student arrived after the class is started.
k: indicate the threshold number of students.
If the number of students arrived is equal or greater than the threshold number then class is not
cancelled so return a string ”NO” else return the string ”YES”. (The string is dynamically allocated.).
char* angryProfessor(int k, int a_count, int* a)
{
}
4. (a) Describe the functionality below functions.
i.malloc() ii. calloc() iii. free()
(b) Design a function that concatenates two given input strings and returns a pointer to the beginning
of the concatenated string and find the length of the concatenated string.
(c) Design a function that finds one or more occurrences of a given character in the given string with
pointers.
1
5. (a) Analyse the following code and write the output with justification.
#include<stdio.h>
int main()
{
int a=100, b[ ]={10,20,30,40,50,60};
int *p;
*p=&b;
printf("w=%d\n", a+1);
printf("x=%d\n", b+1);
printf("y=%d\n" ,*b+1);
printf("z=%d\n", *(b+3));
}
(b) Write a code snippet to obtain the following using array of pointers.
i. print the list of Prime numbers in the array.
ii. Insert an element k in pth index.
iii. Delete an element at ith index.
(c) Design a function that compute the sizes of different pointer data type (int, char, float, double and
structure data types).
6. (a) Develop a function that read and display m × n matrix using pointers.
(b) Read an m × n matrix and find the row sums and column sums. Check whether any two rows have
the same sum; if so swap the elements of these two rows using pointers.
(c) Develop a C function that perform reshape operation of a given matrix without loosing the data.
Hint:
int** reshape(int **mat, int m,int n, int x,int y)
{
mat is a double pointer pointing to matrix.
m indicate rows of mat matrix.
n indicate the columns of mat matrix.
7. (a) int *pointer (declaration of int pointer type variable). why should be specified in the pointer decla-
ration where pointer always holds address of a memory location.
(b) Write a C function char ∗ string mul(char ∗ str, int n) that takes a string and a positive integer
as input. The function should dynamically allocate memory to create a new string by repeating the
original string n times. The function should then return a pointer to the resulting string. Ensure
that your implementation handles memory allocation failures and invalid input gracefully.
2
(c) Develop a C function to return dynamically allotted array for nth row of pascal triangle. Accept a
row number as input and return a pointer that is pointing to dynamically allotted array.
row-0 1
row-1 1 1
row-2 1 2 1
row-3 1 3 3 1
row-4 1 4 6 4 1
row-5 1 5 10 10 5 1
row-6 1 6 15 20 15 6 1
8. (a) Why do we need to do type casting before string data into a memory space create at run time?
(b) Perform the following string operations using pointers
i. Calculate Length of String
ii. Reverse a given String
iii. Concatenation of one string to another
iv. Copy one String into another
v. Compare two Strings
(c) There is a sequence of words in Camel Case as a string of letters, s, having the following properties:
It is a concatenation of one or more words consisting of English letters.
All letters in the first word are lower case.
For each of the subsequent words, the first letter is upper case and rest of the letters are lower
case.
Implement a function with above constraint
Given s, determine the number of words in s.
Example
s= oneTwoThree
There are 3 words in the string: ’one’, ’Two’, ’Three’.
9. (a) How are multidimensional arrays represented in C using pointers and explain how pointer arithmetic
is applied to access elements in a 2D array.
(b) You are given an mxn integer matrix.
matrix with the following two properties:
- Each row is sorted in non-decreasing order.
- The first integer of each row is greater than the last integer of the previous row.
Example 1:
1 3 5 7
10 11 16 20
23 30 34 60
3
Example 1: Input: nums = [2, 5, 1, 3, 4, 7], n = 3 Output: [2, 3, 5, 4, 1, 7] Explanation: Since x1 =
2, x2 = 5, x3 = 1, y1 = 3, y2 = 4, y3 = 7 then the answer is [2, 3, 5, 4, 1, 7].
Example 2: Input: nums = [1, 2, 3, 4, 4, 3, 2, 1] |, n = 4 output: [1, 4, 2, 3, 3, 2, 4, 1]
Example 3: Input: nums = [1, 1, 2, 2], n = 2 Output: [1, 2, 1, 2]
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* shuffle(int* nums, int numsSize, int n, int* returnSize)
{
10. (a) What is the null character in the context of C strings and Discuss the importance of the null-
terminated character sequence in C strings.
(b) A pangram is a string that contains every letter of the alphabet. Given a sentence determine whether
it is a pangram in the English alphabet. Ignore case. Return either pangram or not pangram as
appropriate.
Example
s = ’The quick brown fox jumps over the lazy dog’
The string contains all letters in the English alphabet, so return pangram.
Function Description
Complete the function pangrams in the editor below. It should return the string pangram if the
input string is a pangram. Otherwise, it should return not pangram.
(c) Given a string num that contains only digits and an integer target, return all possibilities to insert
the binary operators ’+’, ’-’, and/or ’*’ between the digits of num so that the resultant expression
evaluates to the target value. Note that operands in the returned expressions should not contain
leading zeros. For example, if Input: num = ”145”, target = 10, the Output: [”1+4+5”]