forked from CPRO-Session1/Assignment4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduplicateArray.c
More file actions
54 lines (43 loc) · 1.25 KB
/
duplicateArray.c
File metadata and controls
54 lines (43 loc) · 1.25 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
/* Yael Kelmer.
This code asks the user to input an array with duplicate integers. The code then removes the duplicates and prints the new array.*/
#include <stdio.h>
int main ()
{
printf ("Please insert the length of the array (how many integers you want in your list of integers)\n");
int lengthArray;
scanf ("%d", &lengthArray);
printf ("Please insert the integers you want to be in the array (remember it has to match the length of the array). Insert duplicates of some integers and I will return an array without those duplicates. \n");
int userArray [lengthArray];
int i;
int userInput;
for (i= 0; i < lengthArray; i++) {
scanf ("%d", &userInput);
userArray [i] = userInput;
}
int newArray [lengthArray];
int j;
int newLength = 0;
for (i = 0; i < lengthArray; i++) {
int check = 0;
for (j = 0; j < newLength; j++) {
if (userArray [i] == newArray [j]) {
check = 1;
break;
}
}
if (check == 0) {
newArray [newLength] = userArray [i];
newLength++;
}
}
printf ("This is your original array: [ ");
for (i = 0; i < lengthArray; i++) {
printf ("%d ", userArray [i]);
}
printf ("]\n");
printf ("This is your new array: [ ");
for (i = 0; i < newLength; i++) {
printf ("%d ", newArray [i]);
}
printf ("]\n");
}