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

C - Module Bank

The document discusses pointers in C programming including pointer arithmetic, dynamic memory allocation, string operations using pointers, multidimensional arrays represented using pointers, and other pointer-related concepts. It provides examples of functions that can be implemented using pointers to manipulate arrays, strings and matrices.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

C - Module Bank

The document discusses pointers in C programming including pointer arithmetic, dynamic memory allocation, string operations using pointers, multidimensional arrays represented using pointers, and other pointer-related concepts. It provides examples of functions that can be implemented using pointers to manipulate arrays, strings and matrices.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1. (a) What is a pointer? Why we need pointer in C programming?

(b) Find the output of this code fragment

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

‘a’ is a pointer to array.


a count indicate the length of the array.
‘k’ indicate threshold number of students.
2. (a) Increment or decrement of a pointer variable, p, p + +/ + +p/ − −p/p − − moves the pointer
forward/backward from the current location depends on what?
(b) Design a function to swap two numbers when parameters are passed as
i. Call by value
ii. Call by reference
iii. Explain differences observed in the above two methods.
(c) Design a function to read two integer arrays ‘A’ and ‘B’ using pointers, return an array contains
common elements of both the arrays along with their physical address.
3. (a) Explain the meaning of the following types of pointers return by a function.
i.Void Pointer. ii. NULL Pointer. iii. Dangling Pointer.
(b) Design a function that receives a list of integers from a caller and removes the duplicate elements
from the given list. Input list should be unchanged and the function should return a list without
duplicate elements to its caller.
(c) Mahesh is the librarian at NTR library of Vignan University. He decided to re-arrange the all the
books in the shelf in the descending order of number of pages of the books i.e., maximum pages of the
book first and minimum pages of the book at the last. Given array of book names(array of strings)
and array of integers (pages). Write a C function which has to return a new array contain the names
of the books in the descending order.
char** arrangeBooks(char **books, int *pages, int count)
{

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

x and y indicate the rows and colums of matrix after reshape.


}

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

Hint: complete the below function.


int* pascal_row(int row)
{
--------------
--------------
---------------
}

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.

Given an integer target, return true if target is in matrix or false otherwise.


Example:

Example 1:

1 3 5 7
10 11 16 20
23 30 34 60

Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 Output: true


(c) Given the array nums consisting of 2n elements in the form [x1 , x2 , . . . , xn , y1 , y2 , . . . , yn ] — Return
the array in the form [x1 , y1 , x2 , y2 , . . . , xn , yn ].

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”]

You might also like