nested loops help please

Feb 21, 2012 at 5:07am
Before you think i am being lazy, let me say that i really, really, tried finding my mistake(s). I am desperate right now.

My primary goal is to scan an array of ints of size N, and determine whether any integer repeats more than (N/2) times or not.
Basically strategy has been scanning my original array and comparing to all the occupied slots in an empty parrallel_array:

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
55
int compare (int cloneArray[], int parallelArray[], int i, int j)
{
    if (cloneArray[i] == parallelArray[j])
        return 1; ///true
    else
        return 0; ///false
}

int main()
{
    int repeat = 0;
    int vectorlength = 5;
    int array1[5] = {2,2,2,2,2};
    int array2[6] ;
    int j = 0, result;

  for (int w =0 ; w < vectorlength; w++)
    {
        cout <<"W: "<<w<<endl;

        j = 0; ///Index variable in parrallel array which begins empty
              ///its always going to be reset to 0 to start comparing from the beginning when getting the next w.
        while (w <= j) ///While i havent reached the las occupied row in parallel array(array2)
        {
            result = compare(array1, array2, w, j ); ///always start comparing from current i up to J
            //cout <<endl<<"J is:"<<j<<endl;

            cout << "compare(w) "<<w<<" & "<<"(j)"<<j<<endl;
            cout <<"res: "<<result<<endl<<endl;

            if (result == 1 )         ///if there is match, increase repeat, then check();
            {
                cout <<"YESSSS->match ";
                cout <<"array1["<<w<<"] to array2["<<j<<"] "<<endl;
                repeat++;
                //**Check();
                break;
            }

            if ( (result == 0 )&& (  w == j) ) ///After having compared up to the las occupied row(j)
            {
                cout << "no match from array1["<<w<<"] up to "<< j<<endl;

                cout << "copying array1["<<w<<"] to "<<"array2["<<j+1<<"] "<<endl; ///Put that unique number in next avail slot j+1
                array2[j+1] = array1[w];   ///
                j++;
                break;
            }
            cout<<"NO MATCH UPTO J:"<<j<<" compare to next j"<<endl;
            j++; ///If neither of the two previous if were met, continue comparing my current array1[i] to next array2[j++];
        }
    }
    
    return 0;
}


array2 is longer to deal with the problem of 'next available slot' and starting off the comparison comparing to index zero to index zero, this is not a big problem. and the Check() is called everytime there to see if there is a march but the problem is i can never get a match, repeat variable is never incremented.

Help would be greatly appreciated.
Thank you
Last edited on Feb 21, 2012 at 5:12am
Feb 21, 2012 at 8:33am
check() is not defined, and is commented.
Feb 22, 2012 at 1:04pm
comparing to all the occupied slots in an empty parrallel_array:


An array with values will have no matches with an empty array.

Work through the loop giving w and j their values to see what's happening.

Are the ints in the array restricted to a certain range? There may be a simpler way to do it if so.
Topic archived. No new replies allowed.