I wrote c++ code which gets first character of progression (a1) difference (b) and size of progression (n)from user and my code must print arithmetic progression
but i keep getting "exit status -1". please help me :) i'm noob.
(sorry for bad english)
_You don't need to include algorithm
_you don't do anything with the variables a1 and b in your arth() function, so why use them?
_when you call arth() within itself, your program crashed because you're passing a rvalue as a function parameter: n--. You need to make that a Lvalue, so either return (--n) or return (n = n - 1).
Are you aware that all your function is, is a loop which decrements the value of n, until it is equal to 0, and then returns 1? If it is what you wanted to do, then it works.
At line 10, n-- evaluates to the current value of n and decrements n. As a result, you're calling arth() with the same value of n each time. That results in infinite recursion.
To fix this, just change n-- to n-1. You don't need to change the value current call's value of n, you just need to pass the smaller value to the next call.
That will get the code to return, but it will always return 1. To fix this, I think line 14 should return a1 instead of 1.
#include<iostream>
usingnamespace std;
void arth( int a, int b, int n )
{
cout << a << ' ';
if ( n > 1 ) arth( a + b, b, n - 1 );
}
int main()
{
int a, b, n;
cout << "Enter a, b, n: ";
cin >> a >> b >> n;
arth( a, b, n );
}