-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_union_sorted.c
More file actions
73 lines (64 loc) · 1.72 KB
/
array_union_sorted.c
File metadata and controls
73 lines (64 loc) · 1.72 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*DAVIDE GIANNUBILO - Esercizi Linguaggio C
Dati 2 vettori in ingresso, unirli in un terzo vettore e stamparlo in ordine crescente
Given 2 arrays in input, merge them into a third arrays, sort it in ascending order and print it
*/
#include<stdio.h>
//Function prototypes
void inseriscivettore(int, int []);
void selectionsort(int, int[]);
int main()
{
int dim1, dim2, i;
printf("Inserisci la dimensione del primo vettore: ");
scanf("%d", &dim1);
printf("Inserisci la dimensione del secondo vettore: ");
scanf("%d",&dim2);
int dim3=dim1+dim2;
int vettore1[dim1], vettore2[dim2], vettore3[dim3];
inseriscivettore(dim1, vettore1);
inseriscivettore(dim2, vettore2);
//Index i declared inside the main function, so we can use in the second for cycle
for(i=0; i<dim1; i++)
{
vettore3[i]=vettore1[i];
}
for(int j=0; j<dim2; j++)
{
vettore3[i]=vettore2[j];
i++;
}
//Ordino il vettore
selectionsort(dim3, vettore3);
printf("Il vettore unito sara':");
for(int i=0; i<dim3; i++)
{
printf("%4d", vettore3[i]);
}
return 0;
}
void inseriscivettore(int dim, int vettore[])
{
for(int i=0; i<dim; i++)
{
printf("INSERISCI I VALORI NEL VETTORE 1 IN POSIZIONE %d: ", i);
scanf("%d", &vettore[i]);
}
}
void selectionsort(int dim, int a[])
{
int temp, min;
for(int i=0; i<dim-1; i++)
{
min=i;
for(int j=i+1; j<dim; j++)
{
//Tro il valore minimo all'interno dell'array
if(a[j]<a[min])
min=j;
}
//Si scambia con l'elemento alla posizione i
temp=a[min];
a[min]=a[i];
a[i]=temp;
}
}