NEWB alert, lol. This was homework that I failed because i didnt turn it in on time. I didnt turn it in due to the fact that i took an online course, my book sucks and my instructor only checks his e-mail once a wk. that and i couldnt for the life of me figure out why my search "for loop" in my printInt function wont return the proper count. everything compiles o.k. my loop looks exactly like the one in the book and the online lecture. If someone can point out my mistake so i can use it properly next time i would be very grateful.
my whole program:

``````#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;

void printInts ( int randomArray[], int size, int& extra )
{
int counter;
for ( counter = 0; counter <size-1; counter++ )
{
cout<<setw(30)<<" Random number "<<(counter + 1)<<" generated "<<randomArray[counter]<<endl;
}
cout << " \n ";
cout <<setw(40)<<" The extra random number is: "<< randomArray[20]<< "\n" <<endl;
extra+= randomArray[20];
int counter1 = 0;
for ( counter1 = 0; counter1 <randomArray[size-1]; counter1++ )
{
if ( randomArray[counter1] == extra )
{

(counter1++);
}
}
cout << setw(38) << " The extra number occured " << counter1 << " times " << endl;
}

void getRandom ( int randomArray[], int size )
{
int counter;
int raNum;
int rand_max = 10;
int rand_min = 1;

srand(static_cast<unsigned int>(time(0)));
for ( counter = 0; counter <size; counter++ )
{
raNum = (rand() % (rand_max - rand_min + 1)) + rand_min;
randomArray[counter] = raNum;
}

int main ()
{

const int size = 21;
int randomArray[size];
int extra;

system ("color 1f ");
cout << " \n\n ";
cout << setw(40 + ( 32 / 2 )) << " 20 Random #'s will be generated " <<endl;
cout << " \n ";
getRandom ( randomArray, size );
printInts ( randomArray, size, extra );
cout << " \n ";
system ("pause");
return 0;
}``````
WaltP commented: "NEWB alert, lol." Grow up! -4

## All 8 Replies

with limited knowledge of your problem combined with other generalities (such as your 'for loop') one guess i would make is that your loop condition in line #12 seems suspicious. since array elements are zero based, it is somewhat uncommon to see size-1 as a loop condition. this could cause you to examine one less element in your array and therefore return results less than what you would expect.

with limited knowledge of your problem combined with other generalities (such as your 'for loop') one guess i would make is that your loop condition in line #12 seems suspicious. since array elements are zero based, it is somewhat uncommon to see size-1 as a loop condition. this could cause you to examine one less element in your array and therefore return results less than what you would expect.

that is purposeful actually. my array size is 21 but i only want to search through 20 cells. the 21st cell is the random number im trying to count.

Can you explain the plus sign at line 18?

Can you explain the plus sign at line 18?

it means the variable "extra" is equal to the number in array cell 20.
the long way is: extra = extra + randomArray[20].
its a shorter way of coding

It means the variable "extra" is equal to the number in array cell 20.
The long way is: extra = extra + randomArray[20]

These two explanations are sort of contradictory. So, is it equal, or is it increased by?

So back to base 1, can you explain the plus sign. And I don't mean syntax. I mean, why did you put it there?

These two explanations are sort of contradictory. So, is it equal, or is it increased by?

So back to base 1, can you explain the plus sign. And I don't mean syntax. I mean, why did you put it there?

its not contradictory you need to read up on your c++ obviously. very common use as is "index++" for example, that is an increment. ive already explained why its there. does anyone actually " help" on this forum?

I believe that the problem arises from using counter1 as both the loop index and the counter for the number of times the extra number appears. Try the following code instead:

``````void printInts ( int randomArray[], int size, int& extra )
{
int counter;
for ( counter = 0; counter < size-1; counter++ )
{
cout<<setw(30)<<" Random number "<<(counter + 1)<<" generated "<<randomArray[counter]<<endl;
}
cout << " \n ";

extra = randomArray[size - 1];

cout <<setw(40)<<" The extra random number is: "<< extra << "\n" <<endl;

int counter1 = 0;

for ( counter = 0; counter < size - 1; counter++ )
{
if ( randomArray[counter] == extra )
{
counter1++;
}
}
cout << setw(38) << " The extra number occured " << counter1 << " times " << endl;
}``````

perfect!! thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.