0

I am doing a homework assignment for an intro to programming class in c.

I need to write a program that looks at an int array of unknown size (we are given a initializer list as the test case to use), and determine all the duplicates in the array.

To make sure that an element that was already found to be a duplicate doesn't get tested, I want to use a parallel array to the original that would hold the numbers of all the elements that were duplicates.

I need this array to be the same size as the original array, which of course we don't really know till the initializer list is given to us.

I tried using sizeof() to achieve this, but visual studio says that is an error due to the variable size (const int size = sizeof(array1);) not being constant. Am I not using sizeof correctly? Or is this logic flawed?

Perhaps there is another way to approach this, but I have yet to come up with one.

Here is the code included below, hope the comments don't make it too hard to read.

// Dean Davis
// Cs 1325
// Dr. Paulk
// Duplicates hw

#include <stdio.h>

int main()
{
    int array1[] = {     0,0,0,0,123,124,125,3000,3000,82,876,986,345,1990,2367,98,2,444,993,635,283,544,    923,18,543,777,234,549,864,39,97,986,986,1,2999,473,776,9,23,397,15,822,1927,1438,1937,1956,7, 29,- 1 };
const int size = sizeof(array1);
int holdelements[size]; 
int a = 0; // counter for the loop to initialize the hold elements array
int b = 0; // counter used to move through array1 and be the element number of the element being tested
int c = 0; // counter used to move through holdelements and check to see if the element b has already been tested or found as duplicates
int d = 0; // counter used to move through array1 and check to see if there are any duplicates 
int e = 0; // counter used to hold place in hold element at the next element where a new element number would go. sorry if that makes no sense
int flag = 0; // used as a boolian to make sure then large while loop ends when we reach a negative one value.
int flag2 = 0; // used as a boolian to stop the second while loop from being infinite. stops the loop when the end of hold elements has been reached
int flag3 = 0; // used to close the third while loop; is a boolian
int numberofduplicates=0;// keeps track of the number of duplicates found

for (a; a < size; a++)
{
    if (a == (size - 1))
        holdelements[a] = -1;
    else
        holdelements[a] = -2;
}

while (!flag)
{
    flag2 = 0;
    flag3 = 0;
    if (array1[b] == -1)
        flag = 1;
    else
    {
        while ((!flag) && (!flag2))
        {
            if (holdelements[c] == -1)
                flag2 = 1;
            else if (array1[b] == holdelements[c])
            {
                b++;
                c = 0;
                if (array1[b] == -1)
                    flag = 1;
            }
        }
        while (!flag3)
        {
            if (array1[d] == -1)
                flag3 = 1;
            else if (array1[b] == array1[d] && b != d)
            {
                printf("Duplicate of %d, index %d, was found at index %d.\n", array1[b], b, d);
                holdelements[e] = d;
                d++;
                e++;
                numberofduplicates++;
            }
        }
    }
    b++;
}
printf("Total Duplicates Found: %d\n", numberofduplicates);
return 0;
}
1
  • Use a C99 compliant compiler. Commented Jul 5, 2015 at 21:02

1 Answer 1

1

redo to the following:

const int size = sizeof(array1)/sizeof(int);
Sign up to request clarification or add additional context in comments.

8 Comments

Or even const int size = sizeof(array1)/sizeof(*array1); to be ready for a type change from int to something else. :-)
Ok so I did that, and now size had the right value, whereas before it was too large, but visual Studio still says that size is not a constant in the following line, and therefore can't be used as the size of holdelement
what compiler are you using?
Visual Studio, source is saved as . C
VS is not conforming to the recent C standards. This shows here by the fact that it is not accepting an object evaluation as array bounds.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.