-
Notifications
You must be signed in to change notification settings - Fork 2
/
Arrays2.c
59 lines (58 loc) · 1.57 KB
/
Arrays2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<stdio.h>
/* The task of this program again is to read an array and then interchange the largest and the smallest values*/
void read_array(int arr[],int size);
void interchange(int arr[],int size);
int largest_array(int arr[],int size);
int smallest_array(int arr[],int size);
void display(int arr[],int size);
void main(){
int num[100],size;
printf("Enter the size of the array\t");
scanf("%d",&size);
read_array(num,size);
interchange(num,size);
display(num,size);
}
void read_array(int arr[],int size){
for(int i=0;i<size;i++){
printf("Enter element %d\t",i+1);
scanf("%d",&arr[i]);
}
}
void interchange(int arr[],int size){
int large_pos = largest_array(arr,size);
int small_pos = smallest_array(arr,size);
int temp = arr[large_pos];
arr[large_pos] = arr[small_pos];
arr[small_pos] = temp;
printf("largest is %d",arr[large_pos]);
printf("\nsmallest is %d",arr[small_pos]);
}
int largest_array(int arr[],int size){
int largest_no = arr[0];
int large_pos;
for(int i=0;i<size;i++){
if(arr[i]>largest_no){
largest_no = arr[i];
large_pos = i;
}
}
return large_pos;
}
int smallest_array(int arr[],int size){
int smallest_no = arr[0];
int small_pos;
for(int i=0;i<size;i++){
if(arr[i]<smallest_no){
smallest_no = arr[i];
small_pos = i;
}
}
return small_pos;
}
void display(int arr[],int size){
printf("The lements of the array are");
for(int i=0;i<size;i++){
printf("\n%d",arr[i]);
}
}