Array

In this array, we will ask the user to enter numbers yangterdiri stambuk of 7 digits. Each - each digit (from stambuk number) will be stored in an array that contained the variable a. The value of each - each array will be multiplied if the value is not 0 (zero) and "stored in a variable name it x. Then made ​​another variable call it b that has 7 arrays as well. The "content" variable b is the result of each - each array variable a plus x. Here's the code:

# include <iostream>
using namespace std;

void main () {
int a [7], x = 1;
for (int i = 0; i <7; + + i) {
court <<"number" <<i + 1 <<":";
cin>> a [i];
}

for (int j = 0; j <7; + + j) ​​{
if (a [j] == 0)
continue;

else {
x *= a [j];
}
}

int b [7];

for (int k = 0; k <7; + + k) {
b [k] = a [k] + x;
court <<b [k] <<"\ n";
}
}


Logic:
Of all the array (number - number) that is (already included), we do the "search". If the array is 0 (zero) then, the array we passed. If the array is not zero (because it certainly is worth more than zero, because logically, there is no stambuk a minus number, right?), then we multiply by the next array is also not zero. Then after that, we create another array of the other variables (of course) to "occupied", respectively - each by the results of each - each array variable which only added to the result of multiplying the value earlier.

Explanation of code:
First - first, we created a variable (a variable) to accommodate an array of 7 pieces, then the variable x to accommodate the result of multiplying the array variable a. The initial value of x is 1, assuming that no wholly stambuk number 0 (zero) (because klo so why should be made stambuk number, right?) that can be multiplied by a whole array of variables having the smallest possible value is 1. Do loop for each array variable a, with "insert" conditions for the array if the value is 0 (zero), then skipped (continue), and if its value is not zero, then multiplied by the variable x. Once completed, created a new variable with the same array, ie 7. Then the looping done more to fill all of the array with the results of x plus the individual - each array variable a.

Leave reply

Back to Top