CS1100 : Introduction to Programming (Lab 10)
1. Passing Arrays by Pointers: Write a C program that does the following:
1. Declare integer arrays of size 100 in the main() function.
2. Define a function named ArrayUpdate with the following parameters:
• An integer pointer ptr (which points to the beginning of an array).
• An integer i (index).
• An integer val (value).
The function should update the i-th entry of the array pointed to by ptr to the value val, but without directly
accessing it as an array element (ptr[i] should not be used).
3. In the main() function, use ArrayUpdate to update any array element. Direct assignments such as A[i] =
10; should not be used. Instead, write ArrayUpdate(A, i, 10);.
4. Similarly, do not read values directly into an array element (e.g., A[j]). Instead, read into a temporary variable and
use ArrayUpdate(A, i, temp).
5. Use these concepts to implement a program that:
• Reads in two arrays, A and B, from user input.
• Computes a third array, C, where each element is the sum of the corresponding elements of A and B.
• Prints the array C, with each element on a new line.
Input Format:
• The first line contains an integer n, the number of elements in arrays A and B (1 ≤ n ≤ 100).
• The second line contains n space-separated integers representing the elements of array A.
• The third line contains n space-separated integers representing the elements of array B.
Output Format:
• Print the array C, with each element on a new line.
Constraints:
• No assignment statement should be used in the main() function where an array entry appears on the left-hand side.
Example:
Input:
3
123
456
Output:
5
7
9
#include <stdio.h>
void ArrayUpdate(int* arr,int index, int val){
//updates the value according to the index position using pointer
*(arr+index) = val;
}
int main()
{
int arraySize;
int A[100],B[100],C[100];
scanf("%d", &arraySize);
int temp;
for(int i=0;i<arraySize;i++){
scanf("%d",&temp);
ArrayUpdate(A,i,temp); //store first array using pointer
}
for(int i=0;i<arraySize;i++){
scanf("%d",&temp);
ArrayUpdate(B,i,temp); //store second array using pointer
}
for(int i=0;i<arraySize;i++){
ArrayUpdate(C,i,A[i]+B[i]);
//add first array and second array and store result in third array
}
for(int i=0;i<arraySize;i++){
printf("%d\n",C[i]);
}
return 0;
}
2. String Function Using Pointers: You can use strlen (include string.h for this) for this problem. strlen(str)
will return an integer which is the length of the string represented by the character array str.
Write a function StrCat in C which takes in three strings (which are character arrays and hence the arguments must of
the type char *) and copies the concatenation of the first two strings (in that order) into the third string.
Write the main program which reads in two strings s and t from the input and concatenates the strings in both ways (s
after t and then t after s) and prints the two resulting strings. Note that StrCat function has to be invoked two times with
different arguments (pass them by reference).
Input: 1st and 2nd lines will contain the two strings. Input: Input:
Output: Print the two resulting strings in the output in two seperate asdfg b
lines. bvfrs c
Constraints: Length of the two strings can be different but both Output: Output:
will be ≤ 100 characters asdfgbvfrs bc
bvfrsasdfg cb
#include <stdio.h>
#include<string.h>
void StrCat(char* str1, char* str2, char* str3){
int i=0,j=0,k=0;
while(i<strlen(str1)){//coping first string to third array
str3[k++] = str1[i++];
}
//concatenating second string input after first string input
and storing it in third array
while(j<strlen(str2)){
*(str3+k++) = *(str2+j++);
}
}
int main()
{
char arr1[100],arr2[100];
scanf("%s",arr1);
scanf("%s",arr2);
int len1,len2;
//calculating length of input strings
len1 = strlen(arr1);
len2 = strlen(arr2);
char concatArr[len1+len2];
//concatenate the second array after first array and store into third array
StrCat(arr1,arr2,concatArr);
for(int i=0;i<(len1+len2);i++){
// or just printf("%s\n", concatArr); would do
printf("%c",concatArr[i]); //printing first result
}
printf("\n");
//concatenate the first array after second array and store into third array
StrCat(arr2,arr1,concatArr);
for(int i=0;i<(len1+len2);i++){
printf("%c",concatArr[i]); //printing second result
}
return 0;
}
3. Dynamic List Management Using Pointers: Write a C program that performs dynamic list management using malloc,
realloc, and free. The program should allow the user to:
1. Create a dynamic list:
• Read an integer n (1 ≤ n ≤ 1000), representing the initial number of elements in an array.
• Dynamically allocate memory for n integers using malloc.
• Read n integers from the user and store them in the dynamically allocated array.
2. Find and remove the smallest element:
• Define a function removeMin that:
– Takes a pointer to an integer array and its size as arguments.
– Finds the smallest element in the array.
– Removes the first occurrence of this element by shifting all subsequent elements left.
– Uses realloc to shrink the array by one element.
3. Extend the array dynamically:
• Read an integer m, representing the number of additional elements to add to the array.
• Use realloc to increase the array size.
• Read m additional elements and append them to the array.
4. Print the final array after all operations.
5. Free the allocated memory before terminating the program.
Input Format:
• The first line contains an integer n, the initial number of elements in the array.
• The second line contains n space-separated integers.
• The third line contains an integer m, the number of additional elements.
• The fourth line contains m space-separated integers.
Output Format:
• Print the array after removing the smallest element.
• Print the final array after adding the additional elements.
Examples
Input:
5
4 2 7 1 5
3
8 6 9
Output:
Array after removing the smallest element: 4 2 7 5
Final array after adding new elements: 4 2 7 5 8 6 9
#include <stdio.h>
#include <stdlib.h>
// Function to find and remove the smallest element
int *removeMin(int *arr, int *size) {
if (*size == 0) return arr; // Edge case: empty array
int minIndex = 0;
for (int i = 1; i < *size; i++) {
if (arr[i] < arr[minIndex]) {
minIndex = i;
}
}
// Shift elements left to remove the min element
for (int i = minIndex; i < (*size) - 1; i++) {
arr[i] = arr[i + 1];
}
// Reduce size and reallocate memory
*size -= 1;
arr = realloc(arr, (*size) * sizeof(int));
// Check if reallocation was successful
if (*size > 0 && arr == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
return arr; // Return updated pointer
}
// Function to add new elements after resizing the array
int *addElements(int *arr, int *size, int newElements) {
arr = realloc(arr, (*size + newElements) * sizeof(int));
// Check if reallocation was successful
if (arr == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
// Read new elements
for (int i = 0; i < newElements; i++) {
scanf("%d", &arr[*size + i]);
}
*size += newElements;
return arr; // Return updated pointer
}
// Function to print the array
void printArray(int *arr, int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int n, m;
// Read initial size and allocate memory
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));
// Read n elements
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Remove smallest element
arr = removeMin(arr, &n);
printf("Array after removing the smallest element: ");
printArray(arr, n);
// Read additional size and add elements
scanf("%d", &m);
arr = addElements(arr, &n, m);
// Print final array
printf("Final array after adding new elements: ");
printArray(arr, n);
// Free allocated memory
free(arr);
return 0;
}
4. Student Record Processing: Write a C program that manages student records using structures and arrays. The
program should:
1. Define a structure Student with the following fields:
• char name[100] - Student’s name
• int rollNo - Roll number
• float marks - Marks obtained
2. Read details of n students from the user (Take n also as input).
3. Implement a function findTopper that takes an array of students and its size as arguments and finds the student
with the highest marks.
4. Implement a function updateMarks that takes an array of students, its size, a roll number, and new marks as
arguments and updates the marks of the student with the given roll number.
5. In main(), do the following:
• Find and print the topper’s details.
• Update marks of a student (user provides roll number and new marks).
• Print the updated student list.
Input Format:
• The first line contains an integer n, the number of students.
• The next n lines contain student details:
– Name (string), Roll Number (int), Marks (float)
• The next line contains an integer rollNo (to update).
• The last line contains a float newMarks (new marks for the student).
Output Format:
• Print the topper (student with the highest marks).
• Print the updated student list.
Examples
Input:
3
Alice 101 87.5
Bob 102 92.0
Charlie 103 88.7
103
95.5
Output:
Topper: Bob (Roll No: 102, Marks: 92.0)
Updated Student List:
Alice (Roll No: 101, Marks: 87.5)
Bob (Roll No: 102, Marks: 92.0)
Charlie (Roll No: 103, Marks: 95.5)
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
// Structure to hold student details
typedef struct {
char name[100];
int rollNo;
float marks;
} Student;
// Function to find the topper
Student findTopper(Student students[], int n) {
Student topper = students[0];
for (int i = 1; i < n; i++) {
if (students[i].marks > topper.marks) {
topper = students[i];
}
}
return topper;
}
// Function to update marks of a student
void updateMarks(Student students[], int n, int rollNo, float newMarks) {
for (int i = 0; i < n; i++) {
if (students[i].rollNo == rollNo) {
students[i].marks = newMarks;
return;
}
}
}
int main() {
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
Student students[MAX_STUDENTS];
// Reading student details
for (int i = 0; i < n; i++) {
printf("Enter name, roll number, and marks of student %d: ", i + 1);
scanf("%s %d %f", students[i].name, &students[i].rollNo, &students[i].marks);
}
// Finding and printing the topper
Student topper = findTopper(students, n);
printf("\nTopper: %s (Roll No: %d, Marks: %.2f)\n", topper.name, topper.rollNo, top
// Updating marks
int rollNo;
float newMarks;
printf("\nEnter roll number to update marks: ");
scanf("%d", &rollNo);
printf("Enter new marks: ");
scanf("%f", &newMarks);
updateMarks(students, n, rollNo, newMarks);
// Printing updated student list
printf("\nUpdated Student List:\n");
for (int i = 0; i < n; i++) {
printf("%s (Roll No: %d, Marks: %.2f)\n", students[i].name, students[i].rollNo,
}
return 0;
}
5. Processing Arrays Using Recursion: Write a C program that performs operations on an array using recursion. The
program should:
1. Read an integer n representing the number of elements in the array.
2. Read n space-separated integers into an array.
3. Implement a findSum function that:
• Takes the array and its size as arguments.
• Returns the sum of all elements using recursion.
4. Implement a findMax function that:
• Takes the array and its size as arguments.
• Returns the maximum element using recursion.
5. Print the sum and the maximum element of the array.
Input Format:
• The first line contains an integer n, the number of elements in the array.
• The second line contains n space-separated integers representing the array elements.
Output Format:
• Print the sum of all elements.
• Print the maximum element in the array.
Examples
Input:
5
3 1 7 9 2
Output:
Sum of elements: 22
Maximum element: 9
#include <stdio.h>
// Recursive function to calculate sum of array elements
int findSum(int arr[], int n) {
if (n == 0)
return 0;
return arr[n - 1] + findSum(arr, n - 1);
}
// Recursive function to find maximum element in array
int findMax(int arr[], int n) {
if (n == 1)
return arr[0];
int maxRest = findMax(arr, n - 1);
return (arr[n - 1] > maxRest) ? arr[n - 1] : maxRest;
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Calculate sum using recursion
int sum = findSum(arr, n);
printf("Sum of elements: %d\n", sum);
// Find maximum element using recursion
int max = findMax(arr, n);
printf("Maximum element: %d\n", max);
return 0;
}