1.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, const char *argv[])
{
int size;
printf("Please enter your array's size: ");
scanf("%d", &size);
printf("Please enter your numbers: ");
int arr[size];
for(int i = 0; i < size; i++)
scanf("%d", &arr[i]);
int max = INT_MIN;
int second_max = INT_MIN;
int min = INT_MAX;
int second_min = INT_MAX;
for(int i = 0; i < size; i++) {
if(arr[i] > max)
max = arr[i];
if(arr[i] < min)
min = arr[i];
}
for(int i = 0; i < size; i++) {
if(arr[i] > second_max && arr[i] != max)
second_max = arr[i];
if(arr[i] < second_min && arr[i] != min)
second_min = arr[i];
}
printf("max:%d second_max:%d min:%d second_min:%d\n",
max, second_max, min, second_min);
printf("max_diff:%d, min_sum:%d, max_sum:%d\n",
max - min, min + second_min, max + second_max);
return 0;
}
array's size: 10
Please enter your numbers: 0 1 2 3 4 5 6 7 8 9
max:9 second_max:8 min:0 second_min:1
max_diff:9, min_sum:1, max_sum:17
2.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, const char *argv[])
{
int size;
printf("Please enter your array's size: ");
scanf("%d", &size);
printf("Please enter your numbers: ");
int arr[size];
for(int i = 0; i < size; i++)
scanf("%d", &arr[i]);
int max_index = 0, max = INT_MIN;
int min_index = size, min = INT_MAX;
for(int i = 0; i < size; i++) {
if(arr[i] > max){
max_index = i;
max = arr[i];
}
if(arr[i] < min){
min_index = i;
min = arr[i];
}
}
int temp = arr[min_index];
arr[min_index] = arr[max_index];
arr[max_index] = temp;
for(int i = 0; i < size; i++)
printf("%d ", arr[i]);
putchar(10);
return 0;
}
Please enter your array's size: 10
Please enter your numbers: 0 1 2 3 4 5 6 7 8 9
9 1 2 3 4 5 6 7 8 0
3.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, const char *argv[])
{
int size;
printf("Please enter your array's size: ");
scanf("%d", &size);
printf("Please enter your numbers: ");
int arr[size];
for(int i = 0; i < size; i++)
scanf("%d", &arr[i]);
int key;
printf("Please enter your key: ");
scanf("%d", &key);
int count = 0, pos;
for(int i = 0; i < size; i++) {
if(arr[i] == key) {
count++;
pos = i;
}
}
if(!count)
printf("key:%d not exists in arr.\n", key);
else if(count == 1)
printf("key:%d 's index is %d.\n",key, pos);
else if(count > 1)
printf("key:%d exists %d times.\n", key, count);
return 0;
}
Please enter your array's size: 10
Please enter your numbers: 0 1 2 3 4 5 6 7 8 9
Please enter your key: 5
key:5 's index is 5.
4.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
typedef enum Mode{
ASCEND,
DESCEND
}Mode;
void bubbleSort(int[], int, Mode);
void selectionSort(int[], int, Mode);
void swap(int*, int*);
int main(int argc, const char *argv[])
{
int size;
printf("Please enter your array's size: ");
scanf("%d", &size);
printf("Please enter your numbers: ");
int arr[size];
for(int i = 0; i < size; i++)
scanf("%d", &arr[i]);
int flag;
printf("Please enter your choice<0:bubbleSort, 1:selectionSort>: ");
scanf("%d", &flag);
Mode mode;
printf("Please enter your mode<0:ascend, 1:descend>: ");
scanf("%d", (int *)&mode);
if(flag == 1)
selectionSort(arr, size, mode);
else if(flag == 0)
bubbleSort(arr, size, mode);
else printf("Invalid choice!\n");
for(int i = 0; i < size; i++)
printf("%d ", arr[i]);
putchar(10);
return 0;
}
void bubbleSort(int arr[], int size, Mode mode){
for(int i = 1; i < size; i++)
for(int j = 0; j < size - i; j++)
if(mode == ASCEND){
if(arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
else if(mode == DESCEND){
if(arr[j] < arr[j+1])
swap(&arr[j], &arr[j+1]);
}
else{
printf("Invalid mode!\n");
return;
}
}
void selectionSort(int arr[], int size, Mode mode){
for(int i = 0; i < size - 1; i++){
int temp = i;
for(int j = i + 1; j < size; j++)
if(mode == ASCEND){
if(arr[j] < arr[temp])
temp = j;
}
else if(mode == DESCEND){
if(arr[j] > arr[temp])
temp = j;
}
else{
printf("Invalid mode!\n");
return;
}
if(temp != i)
swap(&arr[i], &arr[temp]);
}
}
inline void swap(int* a, int* b){
int temp = *a;
*a = *b;
*b = temp;
}
Please enter your array's size: 10
Please enter your numbers: 0 1 2 3 4 5 6 7 8 9
Please enter your choice<0:bubbleSort, 1:selectionSort>: 0
Please enter your mode<0:ascend, 1:descend>: 1
9 8 7 6 5 4 3 2 1 0
5.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, const char *argv[])
{
int size;
printf("Please enter your array's size: ");
scanf("%d", &size);
printf("Please enter your numbers: ");
int arr[size];
for(int i = 0; i < size; i++)
scanf("%d", &arr[i]);
for(int i = 0, j = size - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
for(int i = 0; i < size; i++)
printf("%d ", arr[i]);
putchar(10);
return 0;
}
Please enter your array's size: 10
Please enter your numbers: 0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
6.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, const char *argv[])
{
int size;
printf("Please enter your array's size: ");
scanf("%d", &size);
printf("Please enter your numbers: ");
int arr[size];
for(int i = 0; i < size; i++)
scanf("%d", &arr[i]);
int key;
printf("Please enter your key: ");
scanf("%d", &key);
for(int i = 0; i < size; i++)
if(arr[i] == key)
arr[i] = 0;
for(int i = 0; i < size; i++)
printf("%d ", arr[i]);
putchar(10);
return 0;
}
Please enter your array's size: 10
Please enter your numbers: 3 1 4 1 5 9 2 6 5 3
Please enter your key: 5
3 1 4 1 0 9 2 6 0 3
7.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, const char *argv[])
{
int size;
printf("Please enter your array's size: ");
scanf("%d", &size);
printf("Please enter your numbers: ");
int arr[size];
int res[size];
for(int i = 0; i < size; i++)
scanf("%d", &arr[i]);
int k;
printf("Please enter the k to right shift the arr: ");
scanf("%d", &k);
for(int i = 0, j = k; i < size; i++, j++) {
res[j%size] = arr[i];
}
for(int i = 0; i < size; i++) {
printf("%d ", res[i]);
}
putchar(10);
return 0;
}
Please enter your array's size: 10
Please enter your numbers: 0 1 2 3 4 5 6 7 8 9
Please enter the k to right shift the arr: 5
5 6 7 8 9 0 1 2 3 4