#include
#include
void swapLargestSmallest(int a[], int b[], int n);
int main() {
int a[20], b[20], n, i;
clrscr();
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("\nEnter the terms:\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[i] = a[i]; // Copy to b[]
}
// Call the function BEFORE printing
swapLargestSmallest(a, b, n);
printf("\nThe Array entered by user is:\n");
for (i = 0; i < n; i++) {
printf("%d\t", b[i]);
}
printf("\nThe Array after interchanging the largest and smallest element:\n");
for (i = 0; i < n; i++) {
printf("%d\t", a[i]);
}
getch();
return 0;
}
void swapLargestSmallest(int a[], int b[], int n) {
int sml = a[0], lar = a[0];
int spos = 0, lpos = 0, temp, i;
for (i = 0; i < n; i++) {
if (a[i] <= sml) {
sml = a[i];
spos = i;
}
if (a[i] >= lar) {
lar = a[i];
lpos = i;
}
}
// Swap smallest and largest
temp = a[spos];
a[spos] = a[lpos];
a[lpos] = temp;
}
视频信息
答案文本
视频字幕
This C program demonstrates array manipulation by swapping the largest and smallest elements. The program structure includes standard headers, a main function that handles user input and output, and a custom function called swapLargestSmallest that performs the core logic. The program uses two arrays: array b stores the original values entered by the user, while array a gets modified during the swapping process. The main function first prompts the user to enter the number of elements and then the array values. After copying the values to both arrays, it calls the swapping function and displays both the original and modified arrays to show the result of the swap operation.
Let's analyze the main function step by step. First, we declare variables including two integer arrays a and b with size 20, along with variables n for the number of elements and i for loop iteration. The program then prompts the user to enter the number of terms and reads the array elements into array a. A crucial step is copying all elements from array a to array b using the assignment b[i] equals a[i]. This preserves the original values since array a will be modified by the swapping function. After calling the swapLargestSmallest function, the program displays both arrays: array b shows the original values entered by the user, while array a shows the result after swapping the largest and smallest elements.
The finding extremes algorithm works by initializing variables to track both the smallest and largest values along with their positions. We start by setting sml and lar to the first element, and spos and lpos to zero. Then we iterate through the array using conditional checks. For the smallest element, we use less than or equal to, which ensures we find the last occurrence if there are duplicates. Similarly, for the largest element, we use greater than or equal to. Let's trace through the example array containing 5, 2, 8, 1, 9. Initially, both sml and lar are 5. When we encounter 2, it becomes our new smallest. Then 8 becomes the largest. At position 3, we find 1 which becomes the new smallest. Finally, 9 at position 4 becomes our largest value.
The swapping mechanism uses a three-step process with a temporary variable to safely exchange the positions of the largest and smallest elements. First, we store the smallest element in a temporary variable called temp. This prevents data loss when we overwrite its position. In our example, temp equals 1, which is the value at position 3. Next, we move the largest element to the position of the smallest element. So a[3] becomes 9, the value that was at position 4. Finally, we move the value stored in temp to the position where the largest element was. So a[4] becomes 1. The temporary variable is essential because without it, we would lose one of the values during the direct assignment. After the swap, our array transforms from [5, 2, 8, 1, 9] to [5, 2, 8, 9, 1], successfully exchanging the positions of the smallest and largest elements.
Let's walk through a complete execution example with sample input. We have n equals 6 and the array contains 12, 3, 45, 7, 23, 1. First, the program copies the input array to both arrays a and b, preserving the original values in array b. Next, the swapLargestSmallest function finds the extremes: the smallest value is 1 at position 5, and the largest value is 45 at position 2. The swapping process then begins: temp stores the value 1, then position 5 gets the value 45, and finally position 2 gets the value from temp, which is 1. The final output shows the original array b containing 12, 3, 45, 7, 23, 1, while the modified array a shows 12, 3, 1, 7, 23, 45. Notice how only the positions of the smallest and largest elements have been exchanged, demonstrating the successful completion of the swap operation.