0% found this document useful (0 votes)
3 views12 pages

Lab 8 Assignment Answers Q1-4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

ASSIGNMENT 8

Name: Ankush Kashyap , Roll no. 241036009


Name: Devank Kansal , Roll no. 241036012
Name: Rijul Sharma , Roll no. 241034012

Q1)a

#include <stdio.h>
void readArray(int arr[], int n) {
printf("Enter %d elements of the array: ",n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
}
void printArray(int arr[], int n) {
printf("Array elements are: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int search(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return 1;
}
}
return -1;
}
int main() {
int n, key;
printf("Enter size of the array: ");
scanf("%d", &n);
int arr[n];
readArray(arr, n);
printArray(arr, n);
printf("Enter the key to search: ");
scanf("%d", &key);
if (search(arr, n, key) == 1)
printf("Element %d is present in the array.\n", key);
else
printf("Element %d is not present in the array.\n", key);
return 0;
}

Output:

Q1)b

#include <stdio.h>
void readArray(int arr[], int n) {
printf("Enter %d elements of the array: ",n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
}
void printArray(int arr[], int n) {
printf("Array elements are: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int search(int arr[], int n, int key) {
if (n==0) {
return -1;
}
if (n==1) {
if(arr[0]==key)
return 1;
else
return -1;
}
if(arr[0]==key) {
return 1;
}
return search(arr+1, n-1, key);
}
int main() {
int n, key;
printf("Enter size of the array: ");
scanf("%d", &n);
int arr[n];
readArray(arr, n);
printArray(arr, n);
printf("Enter the key to search: ");
scanf("%d", &key);
if (search(arr, n, key) == 1)
printf("Element %d is present in the array.\n", key);
else
printf("Element %d is not present in the array.\n", key);
return 0;
}

Output:

Q2)a

#include <stdio.h>

void readString(char* str) {


printf("Enter a string: ");
scanf("%[^\n]s", str);
}

void printString(char* str) {


printf("The string is: %s\n", str);
}

int search(char* str, char key) {


for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == key) {
return 1;
}
}
return -1;
}

int main() {
char key;
char arr[100];

readString(arr);
printString(arr);

printf("Enter the character to search: ");


scanf(" %c", &key);

int result = search(arr, key);


if (result == 1)
printf("Character '%c' found in the string.\n", key);
else
printf("Character '%c' not found in the string.\n", key);

return 0;
}

Output:

Q2)b

#include <stdio.h>

void readString(char* str) {


printf("Enter a string: ");
scanf("%[^\n]s", str);
}

void printString(char* str) {


printf("The string is: %s\n", str);
}
int search(char* str, char key) {
if(str[0]=='\0'){
return -1;
}
if(str[0]==key){
return 1;
}
else {
return search(str+1, key);
}
}

int main() {
char key;
char arr[100];

readString(arr);
printString(arr);

printf("Enter the character to search: ");


scanf(" %c", &key);

int result = search(arr, key);


if (result == 1)
printf("Character '%c' found in the string.\n", key);
else
printf("Character '%c' not found in the string.\n", key);

return 0;
}

Output:

Q3)a

#include <stdio.h>
#include <stdlib.h>
void reverse_array(int arr[], int tem[], int n) {
for(int i=0; i<n; i++){
tem[i]=arr[n-1-i];
}
for(int i=0; i<n; i++){
arr[i]=tem[i];
}
}

int main() {
int n;

printf("Enter size of the array: ");


scanf("%d",&n);

int *arr = (int*)malloc(n*sizeof(int*));


int *tem = (int*)malloc(n*sizeof(int*));

printf("Enter %d elements of the array: ",n);


for(int i=0;i<n;i++) {
scanf("%d",&arr[i]);
}

reverse_array(arr, tem, n);

printf("Reversed array: ");


for(int i=0;i<n;i++) {
printf("%d ",arr[i]);
}

return 0;
}

Output:

Q3)b
#include <stdio.h>
#include <stdlib.h>

void reverse_array(int arr[], int n) {


int temp;
for (int i = 0; i < n / 2; i++) {
temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
}

int main() {
int n;

printf("Enter size of the array: ");


scanf("%d", &n);

int *arr = (int*)malloc(n * sizeof(int));


if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}

printf("Enter %d elements of the array: ", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

reverse_array(arr, n);

printf("Reversed array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

free(arr);
return 0;
}

Output:
Q3)c

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

void swap(int* n, int* m) {


int temp = *n;
*n = *m;
*m = temp;
}

void reverse_array(int arr[], int n) {


if (n <= 1) {
return;
}
swap(&arr[0], &arr[n - 1]);
reverse_array(arr + 1, n - 2);
}

int main() {
int n;

printf("Enter size of the array: ");


scanf("%d", &n);
int *arr = (int*)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}

printf("Enter %d elements of the array: ", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

reverse_array(arr, n);
printf("Reversed array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

free(arr);
return 0;
}

Output:

Q3)d

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

void swap(int* n, int* m) {


int temp = *n;
*n = *m;
*m = temp;
}

void reverse_array(int arr[], int n) {


for(int i=0; i<n/2; i++) {
swap(&arr[i], &arr[n - 1 - i]);
}
}

int main() {
int n;

printf("Enter size of the array: ");


scanf("%d", &n);

int* arr = (int*)malloc(n * sizeof(int));


if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}

printf("Enter %d elements of the array: ", n);


for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

reverse_array(arr, n);

printf("Reversed array: ");


for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

free(arr);

return 0;
}

Output:

Q4)a

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

int find_max(int arr[], int n) {


int a = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] > a) {
a = arr[i];
}
}
return a;
}

int main() {
int n;

printf("Enter size of the array: ");


scanf("%d", &n);

int* arr = (int*)malloc(n * sizeof(int));


if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}

printf("Enter %d elements of the array: ", n);


for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Maximum element in the array is %d", find_max(arr, n));

free(arr);

return 0;
}

Output:

Q4)b

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

int find_max_rec(int arr[], int n) {


if (n == 1) {
return arr[0];
}
int max_of_rest = find_max_rec(arr, n-1);
return (arr[n-1] > max_of_rest) ? arr[n-1] : max_of_rest;
}

int main() {
int n;
printf("Enter size of the array: ");
scanf("%d", &n);

int* arr = (int*)malloc(n*sizeof(int));

printf("Enter %d elements of the array: ", n);


for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Maximum element in the array is %d\n", find_max_rec(arr, n));
free(arr);
return 0;
}

Output:

You might also like