I'm not so sure. The requirement asks how many times the first element was "doubled" not "repeated." I think you need to ask the prof for clarification. Are you supposed to print:
- the number of times the first element is repeated?
- the number of times that 2*(first element) appears in the array?
- the number of times you have to double the first element to get the sum?
In fact, I'd ask for a whole lot of clarification.
- What is the output supposed to be? You haven't mentioned any.
- Why is the data stored in an array? It doesn't have to be.
- Are there any restrictions on the "random values" stored in a,b,and c? Do they have
to be integers? Within some range?
It's all just frustratingly ambiguous.
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 40 41 42
|
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int
main()
{
srand(time(0));
int m[900];
int n, k = 0;
int S=0;
cout << "Enter n= " << endl;
cin >> n;
for (int i = 0, R, a, b, c; i < n; i++) {
a = rand() % 30;
b = rand() % 30;
c = rand() % 30;
//1.
R = a + b - c;
m[i] = R;
//2.
S += m[i];
// 3.
if (m[i] == 2*m[0]) ++k;
}
// 3. 2*m[0] appears k times
k=0;
for (int d = m[0]; d < S; ++k, d*= m[0])
;
// 3. The first element must be doubled k+1 times to exceed the sum
// No output because the assignment doesn't ask for any.
return 0;
}
|