Unnecessary Figures

I've been poking around for a while here and can't seem to get headway. My overall program is finished, however before I begin documenting and clearing any stray test code I have an issue whereby I end up seeing an unnecessary figure in an output.
Should show only 50 zeros, 20 per line
00000000000000000000
00000000000000000000
0000000000012005821977

The numbers after zero number 50 are what's bothering. Any insight would be helpful.


header
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
#ifndef MYLIST_H
#define MYLIST_H

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::boolalpha;

class myList
{
public:

    myList();
   static const int array_Size=1000;
   static const int array_Size2=50;


    int sum(int position);
    void reset();
    void plainCopy();
    void printArray(int choice,int perLine,bool order);
    bool promptAndInsert(int array, int arraysize);
    void compareArrays();

private:
    int myIntArray[array_Size];
    int myArrayCopy[array_Size2];
};

#endif // MYLIST_H


source code
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "mylist.h"

myList::myList()
{
    for(int i=0;i<array_Size;i++)
    {
        myIntArray[i]=9;
    }

    for(int i=0;i<array_Size2;i++)
    {
          myArrayCopy[i]=0; // Can also go myArrayCopy[]={0};
    }

    myIntArray[4]=19;
    myIntArray[6]=19;

}

int myList::sum(int position)
{
    int final=0;
int newpos;

for(newpos=0;newpos<array_Size;newpos+=position)//Runs as long as the postion is less than the array size
{
  final=myIntArray[newpos]+final;//Adds the value of the current point in the array to the final value
}

return final;
}

void myList::reset()
{

    for(int count=0;count<1000;count++)
    {
        myIntArray[count]=count;
    }

}


void myList::plainCopy()
{

    for(int recount=0;recount<50;recount++)
    {
        myArrayCopy[recount]=myIntArray[recount];//Copies the contents of the original array into the copy
    }

}

void myList::printArray(int choice,int perLine,bool order)
{
    int current;

    if(choice==2)//Runs if the choice is the copy array
    {
        if(order==false)// Prints in normal order
        {
            if((perLine>1)&&(perLine<array_Size2))// Prints in multiple digits per line
              {
                for(current=0;current<array_Size2;current+perLine)
                {
                    for(int i=0;i<perLine;i++)
                    {
                        cout<<myArrayCopy[current];
                        current++;
                    }
                    cout<<endl;
                }
              }
            else// Prints in single digits per line
            {
                for(current;current<array_Size2;current++)
                {
                    cout<<myArrayCopy[current]<<endl;

                }

            }


        }
        else//Prints in reverse Order
          {
              if((perLine>1)&&(perLine<array_Size))
                {
                  int c=array_Size-1;
                  for(c;c>=0;c=c-perLine)
                  {
                      for(int i=c;i>(c-perLine);i--)
                      {
                          cout<<" "<<myIntArray[i];
                      }
                  }
                  cout<<endl;

                }
              else
              {
                  int c=array_Size-1;

                  for(c;c>=0;c--)
                  {
                      cout<<myIntArray[c]<<endl;

                  }

              }

          }
    }




    else// Runs for the original array
    {
        if(order==false)
        {
            if((perLine>1)&&(perLine<array_Size))
              {
                for(current=0;current<array_Size;current+perLine)
                {
                    for(int i=0;i<perLine;i++)
                    {
                        cout<<myIntArray[current];
                        current++;
                    }
                    cout<<endl;
                }
              }
            else
            {
                for(current;current<array_Size;current++)
                {
                    cout<<myIntArray[current]<<endl;

                }

            }


        }
      else
        {
            if((perLine>1)&&(perLine<array_Size))
              {
                int c=array_Size-1;
                for(c;c>=0;c=c-perLine)
                {
                    for(int i=c;i>(c-perLine);i--)
                    {
                        cout<<" "<<myIntArray[i];
                    }
                }
                cout<<endl;

              }
            else
            {
                int c=array_Size-1;

                for(c;c>=0;c--)
                {
                    cout<<myIntArray[c]<<endl;

                }

            }

        }

    }

}

bool myList::promptAndInsert(int array, int arraysize)
{


    if (array==2)
    {
        if((arraysize>1)&&(arraysize<array_Size2))
        { for(int i=0;i<arraysize;i++)
        {
            cout<<"Please enter a value: ";
            cin>>myArrayCopy[i];
        }
            return true;
        }
        else
            return false;


    }

    else
        if((arraysize>1)&&(arraysize<array_Size))
        {
            for(int i=0;i<arraysize;i++)
        {

                cout<<"Please enter a value: ";
                cin>>myIntArray[i];



        }

    return true;
        }
       else
            return false;

}


void myList::compareArrays()
{
    int i=0;
    int tempArray[array_Size2]={0};

    for(;i<array_Size2;i++)
    {
        if(myArrayCopy[i]==myIntArray[i])
        {
            tempArray[i]=myIntArray[i];
        }
        else
        {
            tempArray[i]=0;
        }
    }




    for(int u=0;u<50;u++)
    {
        if(tempArray[u]!=0)
        {
            cout<<"found "<<u<<" at index "<<u<<endl;
        }

    }




}


Here's the driver because I couldn't fit it in the original.
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
56
57
58
59
60
61
62
63
64
65
#include "mylist.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main(void)
{
    const int ARRAY1 = 1;
    const int ARRAY2 = 2;
    cout<<"const int ARRAY1 = 1;"<<endl;
    cout<<"const int ARRAY2 = 2;"<<endl;
    myList list;
    cout<<"list.printArray(ARRAY1 ,20, false); "<<endl;
    cout<<"About to print the content of myIntArray, 20 values per line, in regular order"<<endl<<"Should show 1000 nines, 20 per line"<<endl;
    list.printArray(ARRAY1 ,20, false); //print content of myIntArray, 20 values per line, in regular order

    cout<<"list.printArray(ARRAY2 ,20,false); "<<endl;
    cout<<"About to print the content of myCopyArray, 20 values per line, in regular order"<<endl<<"should show 50 zeros, 20 per line"<<endl;
    list.printArray(ARRAY2 ,20,false); //print content of myArrayCopy, 20 values per line, in regular order

    cout<<"list.printArray(98, 20, false); "<<endl;
    cout<<"About to print the content of an array 98, 20 values per line, in regular order."<<endl
        <<"Problem is, there are only two options for the first parameter, 1 or 2"<<endl<<"Should default to myIntArray and show 1000 nines, 20 per line"<<endl;
    list.printArray(98, 20, false); //print content of array 98, 20 values per line, in regular order  Problem is, there are only two options for first parameter, 1 or 2

    cout<<"list.plainCopy()"<<endl;
    cout<<"About to copy the first 50 elements of myIntArray to myCopyArray"<<endl;
    list.plainCopy();

    cout<<"list.printArray(ARRAY2 ,10,false);"<<endl;
    cout<<"About to print the content of myCopyArray, 10 values per line, in regular order"<<endl;
    list.printArray(ARRAY2 ,10,false);

    int multiple = 400;

    cout<<" "<<endl;
    int total = list.sum(multiple); //this is saying to sum myIntArray[0], myIntArray[400], myIntArray[800]
    cout<<"The sum of values for positions that are multiples of "<<multiple<<" is: "<<total<<endl;
    multiple =100;
    cout<<"The sum of values for positions that are multiples of "<<multiple<<" is: "<<list.sum(multiple)<<endl; //sums positions [0], [100], [200], [300], [400], [500], [600], [700], [800],[900],
    list.reset();
    list.printArray(ARRAY1 ,20,true);
    int numValuesToInsert = 5;

    bool result = list.promptAndInsert(ARRAY2 , numValuesToInsert );
    if(result==true) //can also be written as if(result == true)
        cout<<"Insert successful!"<<endl;
    else
        cout<<"Insert failed...try again."<<"Cannot insert "<<numValuesToInsert <<" into array."<<endl;

    numValuesToInsert = 400; //this should cause the function to return false if it is passed to array 2.
    if(  list.promptAndInsert(ARRAY2 , numValuesToInsert ) == true )
        cout<<"Insert successful!"<<endl;
    else
        cout<<"Insert failed...try again."<<"Cannot insert "<<numValuesToInsert <<" into array."<<endl;

    cout<<"About to compare both arrays.  Each match should be shown with the value and location within the arrays"<<endl;
    list.compareArrays();

    return 0;
}




You are accessing the array out of bounds.

Compile with warnings
In member function ‘void myList::printArray(int, int, bool)’:
64:59: warning: for increment expression has no effect [-Wunused-value]
76:28: warning: statement has no effect [-Wunused-value]
91:24: warning: statement has no effect [-Wunused-value]
105:24: warning: statement has no effect [-Wunused-value]
125:58: warning: for increment expression has no effect [-Wunused-value]
137:28: warning: statement has no effect [-Wunused-value]
152:22: warning: statement has no effect [-Wunused-value]
166:22: warning: statement has no effect [-Wunused-value]
I'm aware of the warnings, and the statements do serve a purpose. I figured more or less that since it goes beyond the array size there might be repercussions but didn't expect that to be one. My guess is it works for the second one perfectly since it's evenly divisible by 20. Any suggestions for a proper limiter?
Suggestions:
(a) add an extra check to the inner loop, current<array_Size2
1
2
3
4
5
6
7
8
9
10
11
12
    if ((perLine>1) && (perLine<array_Size2))
    {
        for (current=0; current<array_Size2; )
        {
            for(int i=0; i<perLine && current<array_Size2; i++)
            {
                cout<<myArrayCopy[current];
                current++;
            }
            cout<<endl;
        }
    }

or
(b) my preferred solution, trigger the line-break when required.
1
2
3
4
5
6
7
8
9
10
    if ((perLine>1) && (perLine<array_Size2))
    {
        for (current=0; current<array_Size2; current++)
        {
            cout << myArrayCopy[current];

            if (current>0 && current%perLine == 0)
                cout << endl;
        }
    }
> and the statements do serve a purpose
No, they don't. They have no effect.

1
2
3
4
5
6
7
8
9
                current=0;
                while(current<array_Size2)
                {
                    for(int i=0;i<perLine and current<array_Size2;i++, current++)
                    {
                        cout<<myArrayCopy[current];
                    }
                    cout<<endl;
                }
I thought about it before I went to sleep fully and came to the same conclusion. As for the use of the while statements I can use them but I was simply trying to avoid usage to get somewhat more familiar with using only for statements. Hence why declaration for some variables aren't done until the for statement.
Thank you all for your contributions and I appreciate all the input.
Topic archived. No new replies allowed.