AM I ON THE RIGHT TRACK DOES THIS WORK OR IS IT MY IMAGINATION?!?

Can someone help me out here or at least put me on the right path to figuring this question out...

Write a program that declares a 5000 element integer array and then use an appropriate loop to assign 5000 random integers between 0 and 50 to the elements of the array. Use a second loop to then process the array and count
the number of times 42 appears in the array (hint: how many of the array elements are == to 42?) . Output the result.

HERE IS MY HORRIBLE ATTEMPT!

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
#include<iostream>
#include<cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std;


int randomfunc(int);
int main()
{
const int l=5000;
int myarray[l];

int n=rand()%51;

for(int i=0;i<5001;i++)
{
	double rand = randomfunc(n);
	cout<<"# "<<i<<"   "<< rand<<endl;
}

return 0;
}

int randomfunc(int n)
{
	srand(time(0));
	n=rand()%51;
	return n;
}
Last edited on
yes there is a better way to do that (ignore the function name):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void pro1()
{
    int count = 1, array[5000], count2 = 0;
    
    for(;count <= 5000;count++)
    {
        srand((unsigned)time(0));
        array[count]=rand()%51;
    }
    
    for(count = 1;count <= 5000; count++)
    {
        if(array[count] == 42)
        {
            count2++;
        }
    }
     
    cout<<"The number 42 appears "<< count2 <<" times.\n";
    exit(1);
}
You're both making a mistake in your array bounds.

An array of 5000 elements has slots from 0 to 4999 (size-1). In jet's case, it will overstep by one since it writes to 0 to 5000 (<5001) and in Aramil's case it will count from 1 from 5000, which is the correct number, but will cause an error abecause your last iteration tries to write at count[5000], which doesn't exist.

Also, please don't post topics in caps. Very annoying.
Last edited on
Also: srand() doesn't belong within the loop. It has to be at the very begining of main(). Otherwise you'd get always the same value since the seed is always the same until the next second (which is a long time for a computer).
Last edited on
Why will it not count the number 42?

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
#include<iostream>
#include<cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std;


int main()
{

int myarray[5000];
int k=0,i=0;


for(i=0;i<5000;i++)
{
	myarray[i]= rand()%51;

	cout<<"# "<<i<<"   "<< myarray[i]<<endl;
}
if (myarray[i]==42)
{
	for(k=0;k<5000;k++)
	{ 
		
		cout<<"The total number of times 42 was used as a random number is: "<<k<<endl;
	}
}
else
{
cout<<"The number 42 was not used!"<<endl;
}

return 0;
}
the 42 counter isn't working properly because the "k" loop is only going to execute if it finds 42 in myarray[i]. the for loop always resets k to 0.

Instead, you should just have k increment by 1 if 42 is found within the first for loop, right after it couts the randomly generated number.

after the "i" for loop, you could use an if statement to determine if k is greater than 0 or not.
Last edited on
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
#include<iostream>
#include<cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std;


int randomfunc(int);
int main()
{
const int l=5000;
int myarray[l];
int count=0;

int n=rand()%51;
srand(time(0));
for(int i=0;i<5000;i++)
{
	
	double rand = randomfunc(n);
	myarray[i]=rand;
	
}
for(int j=0;j<5000;j++)
{
	if(myarray[j]==45)
		count++;
}
cout<<"\n\nThe count of 45 is "<<count<<"\n";
return 0;
}

int randomfunc(int n)
{
	//srand(time(0));
	n=rand()%51;
	return n;
}
Here's one that's cleaned up a little:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main() {
    int collection[5000];
    srand( time( 0 ) );
    for( int i = 0; i < 5000; ++i ) {
        collection[i] = rand() % 51; // range is 0-50 inclusive
//        std::cout << collection[i] << std::endl;
    }
    int count = 0;
    for( size_t i = 0; i < 5000; ++i ) {
        if( collection[i] == 42 ) {
            ++count;
        }
    }
    std::cout << "There are " << count << " 45's in the collection." << std::endl;
    return 0;
}
Last edited on
it should be 42s at the end not 45s
Yes! Line 16 above should say "42's". Thanks.
just making sure im not missing anything
Topic archived. No new replies allowed.