Question #23702

write a programe to find and count the total occurencies of multiple of 2,3,4,5 in 100 elements

Expert's answer

#include <ctime>

#include <cstdlib>

#include <iostream>

#include <conio.h>

using namespace std;

void fill_array(int (&array)[100])

{

srand((int)time(NULL));

for (int j = 0; j < 100; j++)

array[j] = rand() % 5 + 1;

}

void print_array(int (&array)[100])

{

for (int q = 0; q < 100; q++)

{

if (q % 10 == 0)

cout << endl;

cout << "& " << array[q];

}

cout << endl;

}

int count_multiples(int (&array)[100])

{

int result = 0;

bool multipleFound;

for (int b = 0; b < 96; b++)

{

multipleFound = true;

for (int num = 2; num <= 5; num++)

if (array[b + num - 2] != num)

{

multipleFound = false;

break;

}

if (multipleFound)

result++;

else

b += 3;

}

}

int main()

{

int array[100];

fill_array(array);

print_array(array);

cout << "The number of multiples of {2, 3, 4, 5} found: " << count_multiples(array) << endl;

_getch();

return 0;

}

#include <cstdlib>

#include <iostream>

#include <conio.h>

using namespace std;

void fill_array(int (&array)[100])

{

srand((int)time(NULL));

for (int j = 0; j < 100; j++)

array[j] = rand() % 5 + 1;

}

void print_array(int (&array)[100])

{

for (int q = 0; q < 100; q++)

{

if (q % 10 == 0)

cout << endl;

cout << "& " << array[q];

}

cout << endl;

}

int count_multiples(int (&array)[100])

{

int result = 0;

bool multipleFound;

for (int b = 0; b < 96; b++)

{

multipleFound = true;

for (int num = 2; num <= 5; num++)

if (array[b + num - 2] != num)

{

multipleFound = false;

break;

}

if (multipleFound)

result++;

else

b += 3;

}

}

int main()

{

int array[100];

fill_array(array);

print_array(array);

cout << "The number of multiples of {2, 3, 4, 5} found: " << count_multiples(array) << endl;

_getch();

return 0;

}

## Comments

## Leave a comment