Duplicate Numbers, Output not right?

I'm trying to make a program to input the various amounts of applications put in at a store over 12 months, and to show any duplicate amount of applications between any months. I've got most of the code down I believe, but for some reason, my printf isn't showing the proper values from my array (dup[]). Could someone help me proofread this? Knowing my luck, it's something very tiny and stupid that I've missed.

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
void sort(int a1[]){
     int temporary;
     int x, z;
     int dup[11];
     
     for(x = 1; x < 12; x++)
     {
           for(z = 12 - 1; z >= x; z--)
           {
                 if(a1[z] > a1[z-1])
                 {
                          temporary = a1[z];
                          a1[z] = a1[z-1];
                          a1[z - 1] = temporary;
                 }
           }
     }
     
     x = 0;
     for(z = 0; z < 12; z++)
     {
           if(a1[z] = a1[z+1]);
           {
                    if(a1[z+1] = dup[x]);
                    else
                    {
                        dup[x] = a1[z];
                        x + 1;
                    }
           }
     }
     printf("Duplicate numbers: %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d", dup[0], dup[1], dup[2], dup[3], dup[4], dup[5], dup[6], dup[7], dup[8], dup[9], dup[10]);
     return;                     
}
Last edited on
lines 22 & 24 should use the equality operator == not assignment =

Also, as a matter of style line 32 should use a for loop, instead of printing each item individually.

HTH
Hmmm....I changed lines 22 and 24 to the equality operator (How'd I miss that? *shame*). Line 32 isn't going to be part of the final product as is, mostly had it there to make sure my output was right before actually setting up how I want the output to be. I tried changing it up for the time being to each dup[] getting their own output, but they're still showing what I believe to be the address of the element rather than the actual value, and not sure for the life of me why it's occurring....

I've made some slight changes to the code, and now have verified that dup[] is getting the values properly through the validity check for actual duplicate numbers. So I wonder why I'm still getting output for all my dup[] in the printf as addresses

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
void sort(int a1[]){
     int temporary;
     int x, z;
     int dup[11];
     
     for(x = 1; x < 12; x++)
     {
           for(z = 12 - 1; z >= x; z--)
           {
                 if(a1[z] > a1[z-1])
                 {
                          temporary = a1[z];
                          a1[z] = a1[z-1];
                          a1[z - 1] = temporary;
                 }
           }
     }
     
     x = 0;
     z = 0;
     for(z = 0; z < 12; z++)
     {
           if(a1[z] == a1[z+1])
           {
                    if(a1[z+1] == dup[x]);
                    
                    else
                    {
                        dup[x] = a1[z];
                        x + 1;
                        printf("%d is the dup[x] \n", dup[x]);
                    }
           }
           
           else;
     }
     for(x = 0; x < 12; x++)
     {
           printf("%d \n", dup[x]);
     }
     return;                     
}
Last edited on
Some problems:

Lines 6 & 8: x starts at 0 and increases, z starts at 11 and decreases, the end condition is z >= x, so it stops halfway.

line 19: x is set to 0, but x is not changed inside the loops. Maybe that was your intention on line 30?

line 20: Not needed because it is set to 0 in the for loop.

line 25: semicolon after the if condition, followed by non existent statement, makes this useless. If it did work then the comparison would always be dup[0] as mentioned above. The dup array is not initialised at this point.

line 35: Not needed

line 41: Not needed for a void function.

HTH
Topic archived. No new replies allowed.