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.
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.
//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(){
constint 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';
}
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <algorithm>
usingnamespace std;
bool six (int a)
{
if(a < 6.0)
returntrue;
elsereturnfalse;
}
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;
}
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.