The product of those elements of an array that are smaller than 6

I was required to write a program that would insert 10 random numbers from [-10, 23] into T[10] and output a sum of them all.

Now I should write it so that it outputs "a product of those elements of an array that are smaller than 6". I don't know how to begin.


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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <algorithm>

using namespace std;


void well()
{
    double T[10];
    srand(time(NULL));
    double sum = 0;
    for (int i = 0; i < 10; i++)
    {

        T[i] = (33.0*rand() / RAND_MAX - 10.0);
        sum+=T[i];
        printf("%9.6f\n", T[i]);


    }


    cout<<"The sum of all the elements is: "<<sum<<endl;

 }
  int main()
 {
    well();
    return 0;
 }
Start with the number one. This is your running total.

Look at each member of the array. If it is smaller than six, multiply your running total by six. That's the new running total.

You basically do it the same way you'd do it one paper. You would look at each number, and if the number you're looking at is less than six, multiply the running total by that number.

This is programming. Thinking about the problem, developing a solution that you can express, expressing that solution in code. All the rest is memorising syntax and learning tools. Programming is thinking.
I thought of that but aren't there a simpler solution?

Looking at each member of an array will take quite some space but if that's necessary...
What space does it take?
Nevermind, I reminded myself of bool shortly after I posted.

But still, it doesn't multiply and instead posts those < 6 at the end.
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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <algorithm>

using namespace std;

bool six (int a)
{
    if(a < 6)
        return true;
    else
        return false;
}
void well()
{
    double total = 1;
    double mathematics;
    double T[10];
    srand(time(NULL));
    double sum = 0;
    for (int i = 0; i < 10; i++)
    {
        T[i] = (33.0*rand() / RAND_MAX - 10.0);
        sum+=T[i];
        printf("%9.6f\n", T[i]);}
        for(int i = 0; i < 10; i++)
        {

          if(six(T[i]))
    {
            mathematics=total*T[i];
            cout<<mathematics<<endl;
    }
    }

    cout<<"The sum of all the elements is: "<<sum<<endl;

 }
  int main()
 {
    well();
    return 0;
 }
You have now two nested loops. The counter on the inner loop is i and the loop iterates 10 times. The counter on the outer loop is i too and that loop iterates 10 times. Which i is i?

The outer loop executes the inner loop 10 times. The body of inner loop is thus executed 10*10 times. 100 times. Why?

On line 26, does the 'sum' change when adding up numbers?
On line 33, does the 'total' change when multiplying numbers?
Looking at each member of an array will take quite some space but if that's necessary...


"If that's necessary?"

Is there any way to come up with the answer, which depends on the values in the array, without looking at the array?

No. You must look at the values in the array. There is no way to know what those values are without looking at them. You could look at them as you put them in, you could look at them after you put them in, but you are going to have to look at them.
Last edited on
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
//sums all the eleements
double sum(double array[], int size);

//multiply all the elements
double prod(double array[], int size);

//populates `result' with the element from `array' that are smaller than limit
//returns the number of elements that passed.
int filter_less_than(double array[], int size, double result[], double limit);

int main(){
	const int size = 10;
	double T[size];

	srand(time(NULL));
	rand_populate(T, size, -10, 23);
	double Sum = sum(T, size);

	double aux[size];
	int used = filter_less_than(T, size, aux, 6);
	double Prod = prod(aux, used);

	std::cout << "The sum of all the elements is " << Sum << '\n';
	std::cout << "The product of the elements smaller than 6 is " << Prod << '\n';
}
code the functions
Last edited on
Why does it output such a bizzare multiplication result?



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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <algorithm>

using namespace std;

bool six (int a)
{
    if(a < 6.0)
        return true;
    else
        return false;
}
void well()
{
    double mathematics;
    double T[10];
    srand(time(NULL));
    double sum = 0;
    for (int i = 0; i < 10; i++)
    {
        T[i] = (33.0*rand() / RAND_MAX - 10.0);
        sum+=T[i];
        printf("%9.6f\n", T[i]);
    if(six(T[i]))
    {
        mathematics=(T[i]*mathematics)*1;
    }
    }
    cout<<"The product of all the elements that are smaller than 6 is: "<<mathematics<<endl;
    cout<<"The sum of all the elements is: "<<sum<<endl;

 }

  int main()
 {
    well();
    return 0;
 }
What is the value of 'mathematics' on the very first time that you use it on line 29?
Why do you multiply by 1 on line 29?
Thank you, now it works!

I did not know I had to set a value first, put that *1 at the end specifically so that the output won't be multiplied by 0 if it's alone.
But it would be. A 7 * 0 * 1 produces 0 just like the 7 * 0 does.

A variable has a value. Always. If the value has not been set, it can be anything -- undefined, but still a value. Therefore, it is good practice to always initialize your variables. Then you do know what you have.


Another note: Up to ten values in range [-10, 6[ are multiplied. There is a tiny chance that at least one of them is 0. The product can be 0 for valid reasons.
Topic archived. No new replies allowed.