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

Assignment-3 Arunkumar K

Uploaded by

Mr.K .Arun Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Assignment-3 Arunkumar K

Uploaded by

Mr.K .Arun Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

NAME : 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);

// Check if indices are valid


if (index1 >= 0 && index1 < n && index2 >= 0 && index2 < n) {
// Swap the elements
temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
// Output the array after swapping
printf("Array after swapping elements at indices %d and %d:\n", index1,
index2);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
} else {
printf("Invalid indices! Please enter indices between 0 and %d.\n", n - 1);
}
return 0;
}

You might also like