Find and print the occurrence of a specific value

Creating program for school assignment that prints random values. I have to create an if statement to find and print how many a value occurs, specially the value 42. I'm not sure how to do this with an if statement.



#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
int count = 1000;
int range = 100;
int value = 42;
int found = 0;
int seed;
cout << "Enter seed: ";
cin >> seed;
void srandom(unsigned int seed);

char *initstate(unsigned int seed, char *state, size_t n);
char *setstate(char *state);

for (int i=0; i < count; i++)
{
int num = random() % range;
cout << num << " ";
}
cout << "\nthe value " << value
<< " was found " << found << " times\n";
return 0;
}
Please the code tags as it makes it easier to read your program!

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

int main()
{
int count = 1000;
int range = 100;
int value = 42;
int found = 0;
int seed;
cout << "Enter seed: ";
cin >> seed;
void srandom(unsigned int seed);

char *initstate(unsigned int seed, char *state, size_t n);
char *setstate(char *state);

for (int i=0; i < count; i++)
{
int num = random() % range;
cout << num << " ";
}
cout << "\nthe value " << value
<< " was found " << found << " times\n";
return 0;
}
in order to do this you must store your numbers in an array or vector. Once that is done you create a for loop cycling through it looking for the variable "value" which is 42. So in the for loop place an if statement which says if (array[i] = 42) the body of this if statement would be found++ so it would cycle through the entire array and add 1 to the found variable.

Also it would be better to seed the random number generator with time to create a more random sequence of numbers.
Last edited on
std::generate to populate std::vector<int> with random numbers, in turn generated using <random>
create std::map<int, int> from the vector, key being the number and value being its occurrence frequency
map[42] gives # of occurrences of the no. 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
#include <iostream>
#include <random>
#include <vector>
#include <chrono>
#include <algorithm>
#include <map>

constexpr auto SIZE = 1000;
constexpr auto low_bound = 0;
constexpr auto up_bound = 100;

int main()
{
    auto seed = std::chrono::system_clock::now().time_since_epoch().count();//seed
    std::default_random_engine dre(seed);//engine
    std::uniform_int_distribution<int> di(low_bound,up_bound);//distribution

    std::vector<int> data(SIZE);
    std::generate(data.begin(), data.end(), [&dre, &di]{ return di(dre);});
    //http://en.cppreference.com/w/cpp/algorithm/generate

    std::sort(data.begin(), data.end());
    std::map<int, int> myMap{};
    for (const auto& elem : data)myMap[elem]++;

    std::cout << "No. of occurrences of the number 42: " << myMap[42] << "\n";
}

Thanks for the replies.

My problem is that I am not allowed to alter the code, i.e. store the numbers in an array or vector. My only task is to add an if statement to search for said value and its occurrence.

I have changed the code to seed the random number generator.


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

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
   int count = 1000;
   int range = 100;
   int value = 42;
   int found = 0;
   int seed;
   cout << "Enter seed: ";
   cin >> seed;
   srand( time(0) );
   for (int i=0; i < count; i++)
   {
      int num = random() % range;
      cout << num << " ";
   }

    cout << "\nthe value " << value
        << " was found " << found << " times\n";
    }
   return 0;
}





Your only task is to add an if statement ...

What must an if statement have?
See http://www.cplusplus.com/doc/tutorial/control/

What are you actually interested in? The num.
If you do run your program as is, you should see that the num is a thousand values.
The only thing you need to do is to count every time that the num is something.
You still need to store the numbers into an array or vector.
Declare a vector : vector<int>vectorOfNum;

between line 18 and 19 should be: vectorOfNum.push_back(num); This pushes all the numbers into the vector for you to search.

Then outside of your for loop create another one to search through the vector : for(int i = 0; i < vectorOfNum.size(); i++)

inside the for loop should now be your IF statement. if(num == value){found++}

And it should work. I apologize if there are any errors in the code. I did not do this in a compiler.
kingkush wrote:
You still need to store the numbers into an array or vector.

No! Nobody needs those numbers.
The only question to answer is: "How many times did value 42 show up?"
keskiverto: OP is a bit ambiguous. title mentions
specific value
but the body states
how many a value occurs, specially the value 42
which might suggest or be interpreted as recording all values with a special note of #42
Topic archived. No new replies allowed.