English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

A sequence an is given with the following rule:
a(n+2)=2a(n+1) + 3a(n). , a(0)=2, a(1)=3
Write a C program to compute a(20)
any suggestions?

2007-02-19 16:06:33 · 5 answers · asked by Val 4 in Computers & Internet Programming & Design

5 answers

#include
void main (int argc, char* argv[])
{
int n;
int a[30];
a[0] = 2;
a[1] = 3;
for(n=0 ; n<30; n++)
{
a[n+2] = 2*a[n+1] + 3*a[n];
printf("a(%d) = %d", n+2 , a[n+2] );
}
}

2007-02-19 18:21:48 · answer #1 · answered by Mena M 3 · 0 0

A simple recursive function would do:

long SolveEquation(int m) {
if (m < 0) return 0;
if (m == 0) return 2;
if (m == 1) return 3;
return 2 * SolveEquation(m - 1) + 3 * SolveEquation(m - 2);
}

Call this with something like:
cout<
The result is: 4358480502

2007-02-19 23:23:51 · answer #2 · answered by vin 2 · 0 0

//a(n+2)=2a(n+1) + 3a(n). , a(0)=2, a(1)=3
//Write a C++ program to compute a(20)

#include
using namespace std;

int main(){
int a[30];
int n,x;
a[0]=2;
a[1]=3;

while(1) {
system("cls");

cout << "\n a(n+2)=2a(n+1) + 3a(n). , a(0)=2, a(1)=3";
cout << "\n a C++ program to compute a(x) ";
cout << "\n\n\ta[ ? ]= ";
cin >> x;

for(n=0;n<=x;n++){
a[n+2]=2*a[n+1]+3*a[n];
printf("a[%d]=%d\n",n,a[n]);

}
cout << "\na["<< x << "] = " << a[x];
cout << endl;
system("pause");
}
}

2007-02-19 16:36:46 · answer #3 · answered by iyiogrenci 6 · 0 0

recursion? no one on the internet is going to do your homework for you.

2007-02-19 16:10:32 · answer #4 · answered by iammisc 5 · 1 0

use a for loop

2007-02-19 16:11:06 · answer #5 · answered by standard_air 2 · 1 0

fedest.com, questions and answers