Write a C program using dynamic memory allocation to perform the following:
a) Enter an integer n and allocate memory for n number(s) of int variable(s).
b) Enter n elements and print them.
c) Enter new size, and reallocate memory accordingly. If the new size is greater than n,
then additional numbers (newsize - oldsize) are entered by user after reallocation.
d) Print all the elements after reallocation. [15 Marks]
Example:
Input : Enter the size of list : 5
Enter the integers into list: 1 2 3 4 5
The numbers in the list are: 1 2 3 4 5
Case1: Enter the new size of the list: 8
Enter 3 numbers: 6 7 8
Output : The numbers in the list are: 1 2 3 4 5 6 7 8
Case2: Enter the new size of the list: 3
Output : The numbers in the list are: 1 2 3
Write a C program to dynamically allocate a 2D Matrix for which the user inputs the
dimensions of the matrix.
a) Write a function to enter the elements of a matrix and return the matrix to main
function.
b) Write a function to print the elements of a matrix
c) Write a function to sort the elements of a matrix in ascending order and return the
sorted matrix to main ().
d) Print the elements of a sorted matrix.
e) Write a function to search the given key (through keyboard) row-wise using binary
search and return the key position in the matrix using row-col indices.
Example : [25 Marks]
Dimensions of matrix :
Rows = 3
Columns = 3
Enter the elements of a matrix
4 6 2
8 1 3
5 9 6
Matrix after arranging rows in ascending order
2 4 6
1 3 8
5 6 9
Search the key value in sorted matrix:
Key = 12
Key is not present in the matrix
Key = 6
Key is present in the matrix
Position of the key = (3,2)
Write a C program using pointers and dynamic memory allocation to perform the following:
(a) Enter the list of words through keyboard, where the size of the list N needs to be
specified by the user. Allocate the memory for each word as per its size.
(b) Once the list is available, sort the list.
(c) Split the sorted list into 2-D array based on the 1st character of the word.
Example: [30 Marks]
Size of the list = 15
List of words = arabic, apple, xerox, puppy, box, baby, xxx, cat, cot, sun, cup, doctor, elephant,
danger, public
List of sorted words = apple, arabic, baby, box, cat, cot, cup, danger, doctor, elephant, public,
puppy, sun, xerox, xxx
List of word in 2-D array =
apple, arabic
baby, box
cat, cot, cup
danger, doctor
elephant
public, puppy
sun
xerox, xxx
Write a C Program to open a new file named int_file, and read N (N specified by the user) positive
integers from a keyboard and store them in int_file.
Read the integers from int_file one by one and
write them into three files even_file, odd_file and prime_file, based on the following constraint:
(i)
even_file and prime_file should contain only even and prime numbers, respectively.
(ii) The rest of
the numbers should present in odd_file. Finally display the contents of all four files created in the
following order: int_file, even_file, prime_file and odd_file. (15M)
Example:
N = 10
Contents of int_file = 23, 77, 46, 99, 113, 996, 105, 773, 103, 12
Contents of even_file = 46, 996, 12
Contents of prime_file = 23, 113, 773, 47
Contents of odd_file = 77, 99, 105
Write a C Program to open a new file named int_file, and read N (N specified
by the user) positive integers from a keyboard and store them in int_file.
Read
the integers from int_file one by one and write them into three files even_file,
odd_file and prime_file, based on the following constraint:
(i) even_file and
prime_file should contain only even and prime numbers, respectively.
(ii) The
rest of the numbers should present in odd_file. Finally display the contents of
all four files created in the following order: int_file, even_file, prime_file and
odd_file. (15M)
Example:
N = 10
Contents of int_file = 23, 77, 46, 99, 113, 996, 105, 773, 103, 12
Contents of even_file = 46, 996, 12
Contents of prime_file = 23, 113, 773, 47
Contents of odd_file = 77, 99, 105
Write a C program to perform the following: (20M)
a. Consider an existing .c file, which contains some C program. Open the file
and display its contents.
b. Write a C function to encrypt the contents of the .c file with the following
encryption rule: Each character (source) of the file is replaced with a character
(target) whose ASCII code is m (0< m<=10, the integer value of m will be
provided by the user) more than the ASCII code of the source. The function
should return the encrypted file to the main(). Print the encrypted contents of
the file.
c. Write a C function to decrypt the encrypted file (encrypted by the key m)
based on the user specified key n. If the user specified decrypted key match
with encrypted key, then the original file contents can be recovered. The
function should return the decrypted file to the main().
Print the decrypted
contents of the file, and demonstrate the decryption process.
A student has taken N tests (N is provided by the user through keyboard) in
a particular subject, and their marks (range of marks = 0 - 100) have to be
stored in two text files namely, list1_file and list2_file, in arbitrary manner
specified by the user (say for example, user says marks of m tests (m< N) in
one file and the rest N-m tests in another file).
Here, you need to open two new
files and write the marks into these two files through keyboard by the user.
Read the contents of two files having a list of m and N-m scores (marks),
respectively, and display the same.
Merge the two lists in to a separate file
merge_list and display the contents of merge_list file.
Sort the contents of
merge_list file, and find the average of the top k (where k to be specified by the
user, k<=N) scores of the merge_list file. Store the sorted marks and append
the average of top k scores at the end of the file sorted_merge_file and display
its contents. (30M)
Example: N = 12
Size of list1_file (m) = 5
Size of list2_file (N-m) = 7
Contents of list1_file = 54 76 88 47 97
Contents of list2_file = 24 62 90 70 91 66 84
Contents of merge_list = 54 76 88 47 97 24 62 90 70 91 66 84
Sorted list = 24 47 54 62 66 70 76 84 88 90 91 97
k = 5
Average of k top marks = 90
Contents of sort_merge_list file = 24 47 54 62 66 70 76 84 88 90 91 97 90
List1.txt: 54 76 88 47 97
List2.txt: 24 62 90 70 91 66 84
Write a C program to create a new file (with some file name) and enter the
text through keyboard. Write appropriate C functions to determine and display
the following contents of the file:
a. Display the file contents.b. The longest and the shortest words.
c. The words with the minimum and maximum number of vowels.
d. All repeated words. (35M)
Example: Suppose, a new file by the name “file.txt” is created and the
following text is entered through the keyboard:
Congress MP, Sashi Tharoor used the following words to highlight the
dishonesty of a journalist:
Exasperating farrago of distortions,
misrepresentations and outright lies, being broadcast by an unprincipled
showman masquerading as a journalist.
The output of the program on this file for questions a, b, c and d are as
follows:
a. Congress MP, Sashi Tharoor used the following words to highlight the
dishonesty of a journalist: Exasperating farrago of distortions,
misrepresentations and outright lies, being broadcast by an unprincipled
showman masquerading as a journalist.
b. The longest word is “misrepresentrations”. The shortest word is “a”.
c. The words with the minimum number of vowels are “MP”, “by”. The word
with the maximum number of vowels is “misrepresentations”.
d. The repeated words are “the”, “a”
Extend 10(d) to perform the following.
Write a sorting function that takes as input an array s of students and the number of students.
Its return type should be void.
It should call the swap function whenever needed.
In main(), declare an array s of "student" structure. Populate this array from the input file.
The number of students is taken as input (n).
Sort s by nondecreasing order of CGPA of the students in s.
Print s on the terminal.
Just after taking n as input, your program should also take as input for two real variables, u and v.
Call binary search function to find the student whose CGPA = u.
Finally, print all the students whose CGPA lies in the range [u,v].
Define a structure _CAR which stores the following information:
(i) Model_name (a string not exceeding 10 characters without)
(ii) Name_Of_Manufacturer (a string not exceeding 10 characters)
(iii) Purchase_Price in Rupees (of float data type)
(iv) product_number (a string not exceeding 12 characters)
None of the above strings has any blank space in between. For example a record of a car may be as follows:
Zen
Maruti
435000
1267980
Write a program which implements the following function:
_CAR read_record( ) : It reads a record of car from the keyboard and returns the
record.
void print_record(_CAR) : It prints all information related to a record in a single line
passed as a parameter. Each information unit is printed separated by at least one blank
space.
For example,
Zen
Maruti
435000
1267980
_CAR get_cheapest_car(_CAR *): It returns the cheapest car from the list of cars
passed as an array to it.
_CAR *sort_ car(_CAR *): It sorts a list of cars in descending order of their prices and
returns the sorted list.
Write a main(.) function which does the following operations in a sequence:
(i) It reads information of N (to be read before) cars,
(ii) It prints the list with all records of a car in a single line,
(iii) It prints the information related to the cheapest car.
(iv) It prints distinct car models, and their numbers in the list.
(v) It prints top K costly models of cars given the value of K.
Prepare an input data file, which should have the number of cars in the first line (value of N
to be taken as 20 with at least 10 different models and at least four different manufacturers),
followed by information related to each car in each line.
For example the information of a list of 3 cars may be in the following form.
3
Zen Maruti 435000 1267980
I20 Hundai 526000 7893451
Indigo Tata-Motors 624000 6784532
The program displays the 5 top costly models of cars from your input list. Write your
results in an output file. Submit also the input data file prepared by you. Use dynamic memory
allocation while using an array and input-output redirection while running the program.
Read an integer n (< 100). Input an integer array Al] of n elements from the user. The array can have multiple occurrences of elements. You have to remove the multiple occurrences of elements and store the distinct elements in a new array. The memory for the new array has to be dynamically allocated using ma//oc such that its size is same as the number of distinct elements in AT].
For example, if array A is {3 4 5 2 4 64 2 5 6 3 5} then the new array is {3 4 5 1 2 6} of size 6.
Write a program to read an integer array containing n (where n is input by user and < 100) elements.
Also read a key k.
Now, print the elements of the array in ascending order of their absolute difference with the key k.
For example, if k = 3 and A[i] = -2, the absolute difference |k-A[i] = 5.
Assume that every element of the array is unique.
You should use the insertion sort algorithm.
Write a program that inserts a new integer x to a sorted integer array A[ ] (ascending order), such that the array still remains sorted.
The size of A[ ] is n (< 100), value of n is read from user.
Example, if A[] is {1,2,3, 4,6,7,8,9,10,1 1}, and x is 5. After insertion of x, the array A[] should be
{1,2,3,4,5,6,7,8,9,10,1 1}. You have to insert in the original array, and cannot use a new array.
In your program, n, A[ ] and x are first read from the user and then it should print the modified array.
Assume that no two entries in the array A[ ] have same values.
Also, the integer x, which is to be inserted, does not have a value which is same as any of the entries of the array before insertion.
A file input.dat stores two integers m (< 10) and n (< 10) in its first line. From next line onwards it stores a m x n integer matrix.
Write a program which reads this m x n matrix and stores in a 2-D array
The program should next transpose the matrix A and write it down in another file output.dat.
The new file should contain dimension of the transposed matrix in first line and the transposed matrix from next line onwards.
An input file input.dat stores a single character string in a single line. Maximum length of the string is 100.
The program reads the character string stored in the input file. It then reverses the string excluding the leading and trailing blank spaces.
The program next writes the reversed string in the output file output.dat.
Example:
Input file: illumination
Output file: noitanimulli
You can use any string library functions.
Suppose you have an input file input.dat. The input file can contain an arbitrary number of lines, each with an arbitrary number of characters.
Each line ends with the newline character ('\n')
Write a program that converts all lowercase alphabets in the file to uppercase alphabets, leaving all other (nonlowercase alphabet) characters unchanged, and prints the modified file in the output file output.dat.
You should create the output file if it does not exist. You should use fgets() call to read from the input file (not fscanf).
You can use any string library functions.
For example if the input file contains: iit-kgp 2021 Pabitra Mitra
Output file should contain:
IIT-KGP 2021
PABITRA MITRA
Consider a m x n 2-d matrix A of integers. It is given that elements in
each row of the matrix are sorted in ascending order.
In this
assignment, you will sort the elements of the array such that elements
in each row are sorted in ascending order AND all elements in the 2nd
row are ≥ any element in the 1st row, all elements in the 3rd row are ≥
any element in the 2nd row, and so on.
See example on next slide
Obviously, this may move any element in the array from its current
position to any other position. That is ok.
The matrix elements will be given in a file (one row in each line), you
will read the matrix from the file
See text file infile.txt given containing the matrix in the example
You will write the sorted matrix in another file outfile.txt row by row
(one row per line)
For example, if A is
1 5 10 12
4 6 7 9
1 5 8 10
then after sorting, A is modified to
1 1 4 5
5 6 7 8
9 10 10 12
Write a C function void SortMatrix(…) that takes as parameter a
n x m 2-d array (n, m < 10) representing a matrix, and sorts the
matrix in the form as described. Add appropriate parameters to
the function for this.
You can use one additional array (1-d or 2-d) to sort.
Given that the rows are already sorted in the matrix, you should use
concepts from mergesort. Otherwise there will be a small penalty of 20%.
Write a main function that does the following:
Reads in the dimensions n and m of the matrix (n, m < 10)
from the keyboard
Opens the file named infile.txt to read it. If the file with that
name does not exist (fopen() call returns error), your program
should give an error message and exit
Reads in the matrix elements from the file
Calls SortMatrix() to sort the matrix as described
Opens a file named outfile.txt for write
Write the sorted matrix elements in the file outfile.txt
Closes both infile.txt and outfile.txt with fclose() calls
Consider a set of students. Each student’s data is
maintained in a structure called STUDENT which has the
following fields:
(i) first name (string with max 20
characters),
(ii) last name (string with max 20
characters),
(iii) roll no. (string with max 10 characters),
(iv) department code (string with max 5 characters), and
(v) CGPA (float)
Define the STUDENT structure as above
Write a function
void SortStudents(struct STUDENT S[ ], int n)
that takes an array S of STUDENT structures with n elements, and
sorts it in ascending order (lexicographic) of first name
• If there are two students with same first name, the one with
the smaller (lexicographic) last name should come first.
Assume that there are no two students with both same first
name and same last name.
• You can use string functions like strcmp() to decide if one string is
smaller than the other or not
• You can write any additional function if you want, but add a
comment inside the function stating what that function does
Write a function
struct STUDENT *SearchStudents(struct STUDENT S[ ], int n, char *first, char *last)
that takes as parameters an array S of STUDENT structures with n
elements, already sorted by first name and last name as described
in the SortStudents() function, pointers to null-terminated strings
first and last, and returns the details of the student with the first
name first and last name last. If there is no such student, the
functions should return NULL. Assume that there are no two
student with both same first name and same last name
• If a matched student is found, malloc a STUDENT structure
inside the function, copy the student’s data in that structure, and
return the pointer to it, and assign it to a pointer in the main
function appropriately. Do not return the address of a local
struct STUDENT variable declared inside the function.
• For full marks, you should use concepts of binary search to
search, as the array given is sorted. You can write either
recursive or non-recursive version of binary search. Otherwise
there will be a small 20% penalty.
• Write a main function that does the following:
• Reads in a filename (a string with max 20 characters) from the
keyboard in a character array f.
• Opens the file specified by the string f for read. If the file does not
exist, print an error message and exit
• Reads in the student details from the file f in an array of
STUDENT records
• Note that the no. of student records is not known to you at
first. So do exactly the following.
• First read all the data one by one in a single STUDENT
structure (overwrite on the same structure) till the EOF is
reached. Count how many structures you read before the
EOF is hit. This is the no. of structures n in the file
• Close the file f (fclose())
• Malloc an array X of STUDENT structures of size n.
• Now open the file again with fopen(), and read n
structures from it in the array of STUDENT structures
created
• Malloc one more array of student structures A of size n
• Copy X to A (so now you have the same student structures in two
arrays, X and A)
• Call SortStudents() with appropriate parameters to sort A by first
name
• In a loop, does the following
• Reads in an integer k from the keyboard
• If k = 0, exits the program
• If k = 1, reads in a string x and a string y from the keyboard
and calls SearchStudents() with appropriate parameters to
search for student with first name x and last name y. It prints
the field if a matching student is found, or prints an
appropriate message
Read an input file (w10-1-in.txt) containing some positive integers and with 0 at the end.
For each integer x, print a string of length x in a new line of the output file (w10-1-out.txt), as shown in the example.
Example 1
w10-1-in.txt:
2 3 3 4 7 0
w10-1-out.txt:
**
***
***
****
*******
Example 2
w10-1-in.txt:
3 5 9 12 13 19 21 0
w10-1-out.txt:
***
*****
*********
************
*************
*******************
*********************
Declare an integer variable n.
Read its value from the keyboard and dynamically allocate an integer array a[ ] of length n.
Fill up the array by n integers value from the keyboard.
Print the size of a[ ] in bytes and its elements in the terminal.
Write a function that takes as input the address and the length of an array,
and shifts its content to the right by one place with wrap around, as
shown in the example.
Call this function from the main() with al] as input.
Print the array al] in the terminal after the function call.
Do this one more time.
All the prints should be inside main()
Example:
Enter number of elements: 5
Enter the elements: 7 3 6 1 4
size of a[ ] = 20
a[ ]=7 3 6 1 4
After function call:
a[ ]= 4 7 3 6 1
After function call:
a[ ]= 1 4 7 3 6
Declare a 2D integer array a[ ][ ] and define its size (#rows and #columns) taking user input. No need for dynamic allocation. Fill up it by
integer values from the user.
Write another function to find its saddle point(s) and print on the terminal.
If there is none, then print that message.
(An element is a saddle point if it is the unique maximum (or minimum) in its row but the unique minimum (resp., maximum) in its
column.) (100)
Examples:
1 5 7
1 2 1
7 3 4
In the above array a[1][1]= 2 is a saddle point.
1 5 7
1 2 2
7 3 4
In the above array, there is no saddle point. (Because the maximum in the 2nd row is not unique.)
5 1 4
6 2 7
8 3 9
In the above array, there are two saddle points:
a[0][0]= 5 and a[2][1] = 3
User supplies two positive integers m and n. Create two 2D integer arrays A and B such that
their sizes are mXn and 2X2 respectively. Let the user fill up these arrays.
Write a function that takes A and B as input from main(), and returns to main() the number of occurrences of B in A.
nt this value.
Example
Enter m, n: 4 3
Enter A (one row at a time):
0 0 1
0 1 0
1 0 1
1 0 0
Enter B (one row at a time):
0 1
1 0
Number of occurrences =2
Define a structure named rectangle having integer width and integer height.
Ask the user for an integer n to dynamically allocate a ID array
of structure rectangle and of size n.
Let the user supply the data for n rectangles.
Apply bubble Sort to sort the array in non-decreasing order of the rectangle areas.
Print the array.
Example
Enter n: 3
Enter width and height of 3 rectangles:
6 2
3 7
5 4
Sorted by area:
(6,2), (5,4), (3,7)
Take a positive integer n (2 to 10) as input to create an n-by-n square matrix A (2D integer array) [dynamically allocate].
Fill up this matrix by keyboard input in main().
Write a function void multiplyMat() that takes
an n-by-n square matrix A as input and computes A*A to store the result in B.
Call this function from main() and print the elements of the squared matrix A.
Examples:
Enter n (2 to 10): 2
Enter the elements of the matrix:
1 2
3 4
Squared matrix:
7 10
15 22
Define a structure named "student" with the following fields:
name and surname of 15 characters each, and cgpa as a real number.
Write a swap function that takes as input two student pointers a and b, and exchanges the data in the
student records pointed by them.
The return type of this function should be void.
In main(), declare two student-type variables, a and b.
Take user input to fill up their fields.
Call the swap function to exchange their data and then print the result from the main().
Example
Enter the name, surname, CGPA of 1st student: amar bose 7.25
Enter the name, surname, CGPA of 2nd student: bela sen 8.12
After swapping:
Student 1: bela sen 8.12
Student 2: amar bose 7.25