The first thing I notice is that you are modifying both the array index i
and the address of the first element arr
in order to track your current location in the array. So, you have to modify i
and arr
at the same time. This is unnecessary. You only need to modify i
and then use arr[i]
to get the number at that index. So, let's replace every instance of *arr
with arr[i]
and deleted all of the lines that modify arr
.
#include <stdio.h>
#define N 10
void print_set(int *arr, int n) {
int i = 1;
int duplicate_num, check_num, count;
printf(" %d", arr[0]); //printing the first number (arr[0]).
//starting to check the other number. if they new I'll print them. (start from arr[1]).
while (i < n) {
if (arr[i] != duplicate_num) {
printf(" %d", arr[i]);
check_num = arr[i];
// becouse I found new number, I change it no be equal to the first duplicate_num. (if there are more like him, I change them too).
while (i < n) {
if (arr[i] == check_num) {
arr[i] = duplicate_num;
}
i++;
count++;
}
i = i - count;
count = 0;
}
i++;
}
}
int main() {
int arr[N] = {4, 6, 9, 8, 6, 9, 6, 1, 6, 6};
print_set(arr, N);
return 0;
}
Next, the variables duplicate_num
and check_num
are not initialized with values. In fact, duplicate_num
is never assigned a value, so using it in the if (arr[i] != duplicate_num)
and arr[i] = duplicate_num
is undefined behavior. I see that it is used as a marker for array values that have been seen before (the technical term is "sentinel value"), therefore it needs to be a value that cannot appear in the array. Since you print arr[0]
outside the loop, this is actually a good value. Making this change:
#include <stdio.h>
#define N 10
void print_set(int *arr, int n) {
int i = 1;
int duplicate_num = arr[0], check_num, count;
printf(" %d", arr[0]); //printing the first number (arr[0]).
//starting to check the other number. if they new I'll print them. (start from arr[1]).
while (i < n) {
if (arr[i] != duplicate_num) {
printf(" %d", arr[i]);
check_num = arr[i];
// becouse I found new number, I change it no be equal to the first duplicate_num. (if there are more like him, I change them too).
while (i < n) {
if (arr[i] == check_num) {
arr[i] = duplicate_num;
}
i++;
count++;
}
i = i - count;
count = 0;
}
i++;
}
}
int main() {
int arr[N] = {4, 6, 9, 8, 6, 9, 6, 1, 6, 6};
print_set(arr, N);
return 0;
}
The variable check_num
is always assigned a value before it is used, so it is less of a problem. Still, you always need to make sure that you never use a variable until after it has been assigned a value.
Now, the variable count
seems unnecessary. You are using it to get back to the initial value of i
after marking all the duplicate numbers in the rest of the list. Instead, let's use a second index j
to loop through the rest of the list so we don't need to reset the value of i
afterwards.
#include <stdio.h>
#define N 10
void print_set(int *arr, int n) {
int i = 1, j = 0;
int duplicate_num = arr[0], check_num;
printf(" %d", arr[0]); //printing the first number (arr[0]).
//starting to check the other number. if they new I'll print them. (start from arr[1]).
while (i < n) {
if (arr[i] != duplicate_num) {
printf(" %d", arr[i]);
check_num = arr[i];
// becouse I found new number, I change it no be equal to the first duplicate_num. (if there are more like him, I change them too).
for (j = i; j < n; j++) {
if (arr[j] == check_num) {
arr[j] = duplicate_num;
}
}
}
i++;
}
}
int main() {
int arr[N] = {4, 6, 9, 8, 6, 9, 6, 1, 6, 6};
print_set(arr, N);
return 0;
}
Now we see that i
is only modified at the end of the loop block. Let's make this a standard for-loop.
#include <stdio.h>
#define N 10
void print_set(int *arr, int n) {
int i = 0, j = 0;
int duplicate_num = arr[0], check_num;
printf(" %d", arr[0]); //printing the first number (arr[0]).
//starting to check the other number. if they new I'll print them. (start from arr[1]).
for (i = 1; i < n; ++i) {
if (arr[i] != duplicate_num) {
printf(" %d", arr[i]);
check_num = arr[i];
// becouse I found new number, I change it no be equal to the first duplicate_num. (if there are more like him, I change them too).
for (j = i; j < n; j++) {
if (arr[j] == check_num) {
arr[j] = duplicate_num;
}
}
}
}
}
int main() {
int arr[N] = {4, 6, 9, 8, 6, 9, 6, 1, 6, 6};
print_set(arr, N);
return 0;
}
Finally, if you are using a modern version of C, you can declare your variables where you need them, rather than at the top of the function. Let's move the variable declarations to where the variable is first needed.
#include <stdio.h>
#define N 10
void print_set(int *arr, int n) {
int duplicate_num = arr[0];
printf(" %d", arr[0]); //printing the first number (arr[0]).
//starting to check the other number. if they new I'll print them. (start from arr[1]).
for (int i = 1; i < n; ++i) {
if (arr[i] != duplicate_num) {
printf(" %d", arr[i]);
int check_num = arr[i];
// becouse I found new number, I change it no be equal to the first duplicate_num. (if there are more like him, I change them too).
for (int j = i; j < n; j++) {
if (arr[j] == check_num) {
arr[j] = duplicate_num;
}
}
}
}
}
int main() {
int arr[N] = {4, 6, 9, 8, 6, 9, 6, 1, 6, 6};
print_set(arr, N);
return 0;
}