single dimension array

Help with HW,

check the code below , need help with condition nr.3, which I have issues solving it out:

1. a+b-c equation will receive random values which are considered elements of array that are going to be calculated for 'n' number of times.

2. calculate the sum of elements of the array

!!! 3. find how many times the value of the first element in the array has been doubled.

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;

cout<<"Enter n= "<<endl;
cin>>n;

for(int i=0,R,a,b,c,S; i<n; i++)
{
	a= rand()%30;
	b= rand()%30;
	c= rand()%30;
	
	//1.
	R= a+b-c;
	m[i] = R;
	
	//2.
	S+= m[i];
	
	
	
	if(R==m[i]) k++; //3. ????
	
	
	cout<<"m["<<i<<"]="<<m[i]<<endl;
	cout<<" i element got value from a+b-c "<<R<<endl;
	cout<<" Sum of elements of m[i]= "<<S<<endl;
	cout<<" First element value was doubled for "<<k<<endl;
}

   
return 0;
}

I don't know what that means, but note that you should turn on compiler warnings. You're are not initializing your S variable.
27:10: warning: 'S' may be used uninitialized in this function [-Wmaybe-uninitialized]

Flag is -Wall on GCC (at a minimum).

It would also be cleaner if you didn't jam all your variables in your for loop line, you can do
1
2
3
int a = rand() % 30;
int b = rand() % 30;
int c = rand() % 30;
instead.

But more importantly: Can you show an example of what should happen?

if(R==m[i])This check will always be true, since you just set m[i] = R; two lines before it.
Last edited on
Sounds like you want to know if the first element is repeated at all.

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
43
44
45
46
47
  #include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{
	srand(time(0));
	
int m[900];
int n,k=0;

cout<<"Enter n= "<<endl;
cin>>n;

for(int i=0,R,a,b,c,S = 0; i<n; i++)
{
	a= rand()%30;
	b= rand()%30;
	c= rand()%30;
	
	//1.
	R= a+b-c;
	m[i] = R;
	
	//2.
	S+= m[i];
	
	
	
	//if(R==m[i]) k++; //3. ????
	
	
	cout<<"m["<<i<<"]="<<m[i]<<endl;
	cout<<" i element got value from a+b-c "<<R<<endl;
	cout<<" Sum of elements of m[i]= "<<S<<endl;
	cout<<" First element value was doubled for "<<k<<endl;
}

for(int i=1; i < n; i++) {
    if(m[i] == m[0]) k++;
}
 cout << "First element repeats " << k << " more times." << endl;

   
return 0;
}
That makes a lot more sense, Manga! It's obvious in hindsight, ah well.
Last edited on
@ Manga you are perfectly right, thanks!
@ Ganado, thanks, I will keep in mind!
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;
}

The assignment text might have been lost in translation.
But no doubt, asking for clarification from the professor is a good idea before assuming anything from us.
Last edited on
Topic archived. No new replies allowed.