how to initialize array of unknown size in c -


i doing homework assignment intro programming class in c.

i need write program looks @ int array of unknown size (we given initializer list test case use), , determine duplicates in array.

to make sure element found duplicate doesn't tested, want use parallel array original hold numbers of elements duplicates.

i need array same size original array, of course don't know till initializer list given us.

i tried using sizeof() achieve this, visual studio says error due variable size (const int size = sizeof(array1);) not being constant. not using sizeof correctly? or logic flawed?

perhaps there way approach this, have yet come one.

here code included below, hope comments don't make hard 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 = 0; // counter loop initialize hold elements array int b = 0; // counter used move through array1 , element number of element being tested int c = 0; // counter used move through holdelements , check see if element b has been tested or found duplicates int d = 0; // counter used move through array1 , check see if there duplicates  int e = 0; // counter used hold place in hold element @ next element new element number go. sorry if makes no sense int flag = 0; // used boolian make sure large while loop ends when reach negative 1 value. int flag2 = 0; // used boolian stop second while loop being infinite. stops loop when end of hold elements has been reached int flag3 = 0; // used close third while loop; boolian int numberofduplicates=0;// keeps track of number of duplicates found  (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, found @ index %d.\n", array1[b], b, d);                 holdelements[e] = d;                 d++;                 e++;                 numberofduplicates++;             }         }     }     b++; } printf("total duplicates found: %d\n", numberofduplicates); return 0; } 

redo following:

const int size = sizeof(array1)/sizeof(int); 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -