S
S
Sergo Zar2020-09-24 12:34:16
C++ / C#
Sergo Zar, 2020-09-24 12:34:16

Find the number 7 in a number from 100 to 1000?

I just started learning C++ and here is a problem. You need to generate 15 numbers from 100 to 1000 (including fractional ones) and output 15 numbers that contain the number 7.
But when compiling, the compiler writes:

1.cpp: In function ‘int main()’:
1.cpp:13:14: error: invalid operands of types ‘intanddouble’ to binary ‘operator%’
   13 |   r = rand() % 1000.0 + 100.0;
      |       ~~~~~~ ^ ~~~~~~
      |           |    |
      |           int  double
1.cpp:16:8: error: invalid operands of types ‘doubleandint’ to binary ‘operator%’
   16 |   if(r % 10 == 7){
      |      ~ ^ ~~
      |      |   |
      |      |   int
      |      double

Here is the code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(){
  srand(time(0));
  float r;
  int iter = 0;
  int seven = 0;
  while (true){

    r = rand() % 1000.0 + 100.0;
    iter++;
    if(r % 10 == 7){
      cout << r << endl;
      seven++;
    }
    if (iter == 1000 || seven == 15){
      break;
    }
  }
  return 0;
}


I understand the problem is that you cannot use the% operator with fractional numbers. But I don't know how else to do it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2020-09-24
@Sergomen

You need to generate 15 numbers from 100 to 1000 (including fractional ones) and output 15 numbers that contain the number 7.

Well, in the 15 generated numbers, there may not be 15 numbers with the number 7.
spoiler

#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <algorithm>
#include <iterator>
#include <iomanip>
#include <limits>
using namespace std;

// Не важно, пусть будет так.
auto getNRandomNumbersVecFromRange(const double low, const double hi, const int n){
  random_device rd;
  mt19937 gen(rd());
  uniform_real_distribution<> dis(low, hi);
  vector<double> randNumbersVec(n);
  generate_n(randNumbersVec.begin(), n, [&dis, &gen]{ return dis(gen); });
  return randNumbersVec;
}

int main()
{
  auto numbers = getNRandomNumbersVecFromRange(100.0, 1000.0, 15);

  cout << setprecision(numeric_limits<double>::digits10 + 1);

  copy(numbers.cbegin(), numbers.cend(), ostream_iterator<double>(cout, "\n"));

  cout << "\n" << "result:" << "\n"; // "\nresult\n"

  // А это важно --> to_string(n).find('7') != string::npos

  copy_if(numbers.cbegin(), numbers.cend(),
          ostream_iterator<double>(cout, "\n"),
          [](auto n) { return to_string(n).find('7') != string::npos; });
}

A
Anton Muravyov, 2020-09-24
@Murean

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

int main() {
  srand(time(0));
  int r;
  int iter = 0;
  int seven = 0;
  while (true) {

    r = rand() % 1000 + 100;
    iter++;
    if (r % 10 == 7) {
      cout << r << endl;
      seven++;
    }
    if (iter == 1000 || seven == 15) {
      break;
    }
  }
  return 0;
}

If you want also fractional ones, then you can divide, for example, by some number. If it’s quite simple, then 10.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question