Answer the question
In order to leave comments, you need to log in
Problem solving in c++?
Hello, I have programmed a simple problem, in the solution of which it is necessary to use the declaration of a static variable:
Write a function with an integer parameter m, the result of which is equal to the sum of the parameters of the last three calls to this function.
The result is this code:
#include <iostream>
#include <cstdlib>
using namespace std;
void greeting(int n) {
static int i = 1, s = 0, a = 1, b = 2, c = 3, a1 = 0, b1 = 0, c1 = 0;
if (i == a) {
a1 = n;
a += 3;
}
else if (i == b) {
b1 = n;
b += 3;
}
else if (i == c) {
c1 = n;
c += 3;
}
i++;
if (a1 > 0 && b1 > 0 && c1 > 0) {
s = a1 + b1 + c1;
cout << s << endl;
}
else {
cout << s << endl;
}
}
int main() {
for (int i = 10; i <= 100; i += 10) {
greeting(i);
}
return 0;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question