Assignment-3 Arunkumar K
Assignment-3 Arunkumar K
ROLL NO : 11
Programming and Data Structures Assignment-3
1.Construct an array of size n, and based on the user input out the element at
index k.
#include <stdio.h>
int main() {
int n, k;
// Input the size of the array
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n]; // Declare the array
// Input the elements of the array
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input the index k
printf("Enter the index to retrieve (0 to %d): ", n - 1);
scanf("%d", &k);
// Check if k is a valid index
if (k >= 0 && k < n) {
printf("Element at index %d is: %d\n", k, arr[k]);
} else {
printf("Invalid index! Please enter an index between 0 and %d.\n", n - 1);
}
return 0;
}
2.Given an array of size n, swap any two elements of the given array.
#include <stdio.h>
int main() {
int n, index1, index2, temp;
// Input the size of the array
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n]; // Declare the array
// Input the elements of the array
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input the two indices to swap
printf("Enter the indices of the elements to swap (0 to %d): ", n - 1);
scanf("%d %d", &index1, &index2);