Can someone help with the following program C program. ? Its giving an error tha
ID: 3769821 • Letter: C
Question
Can someone help with the following program C program. ? Its giving an error thats given below
Output:
find the issue why the program wont compile.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
//program to solve Tri-diagonal solver of the equations inEq(7.19)- on the chapter7 #include<stdio.h> #include<math.h> int main(void) { double delx = 0.01 ; double X[101] ; double x ; double y ; double z ; double alpha = 0.01; int i ; int n = 100 ; int k ; int l ; int j ; int q ; int u = 1.0 ; double a[101] ; double b[101] ; double c[101] ; double d[101] ; double t[101] ; double alphastar = (2.0 * alpha ) / ( u * delx ) ; //Equ()7.19 for ( q=2; q<=n-1; q++) { t[q] = 0 ; } t[ 1 ] = 1 ; t[ n ] = 0 ; x = alphastar + 1 ; //constant y =- ( 2.0 * alphastar ) ; //constant z = alphastar - 1.0 ; //constant for ( i=2; i<=n-1; i++ ) { if ( i > 2 ) { a[ i ] = x ; } b[ i ] = y ; if ( i < n - 1 ) { c[ i ] = z ; } if ( i == 2 ) { d[ i ] =-( x * t[ 1 ] ) ; } if ( i == n - 1 ) { d[ i ] =- ( z * t[ n ] ) ; } } for ( i=2; i<=n-2; i++ ) { d[ i ] = d[ i ] / b[ i ] ; //Pivoting step c[ i ] = c[ i ] / b[ i ] ; // pivoting step b[ i ] = 1.0 ; d[ i + 1 ] = d[ i + 1 ] - a[ i + 1 ] * d[ i ] ; //Forward Reduction b[ i + 1 ] = b[ i + 1 ] - a[ i + 1 ] * c[ i ] ; } //Forward Reduction} t[ n - 1 ] = d[ n - 1 ] / b[ n - 1 ] ; for ( j=n-2; j>=2; j--){ // Back Substitution t[ j ] = d[ j ] - c[ j ] * t[ j + 1 ] ; } for( l=1; l<=n; l++ ) { X[ 1 ] = 0.0 ; if ( l > 1 ) { X[ l ] = X[ l - 1 ] + delx ; } } for ( k=1; k<=n; k++ ) { printf( "%2f %f ", X[ k ],t[k] ) ; } }
Explanation / Answer
The program is missing one closing bracket "}" of main function, add one more "}" at the last line. Always try to write programs with proper indentation that way you can have track of all opening braces and debugging will be easier. I did a bit of indentation to your program please chek below. Thanks
//program to solve Tri-diagonal solver of the equations inEq(7.19)- on the chapter7
#include<stdio.h>
#include<math.h>
int main(void)
{
double delx = 0.01 ;
double X[101] ;
double x ;
double y ;
double z ;
double alpha = 0.01;
int i ;
int n = 100 ;
int k ;
int l ;
int j ;
int q ;
int u = 1.0 ;
double a[101] ;
double b[101] ;
double c[101] ;
double d[101] ;
double t[101] ;
double alphastar = (2.0 * alpha ) / ( u * delx ) ;
//Equ()7.19
for ( q=2; q<=n-1; q++)
{
t[q] = 0 ;
}
t[ 1 ] = 1 ;
t[ n ] = 0 ;
x = alphastar + 1 ;
//constant y =- ( 2.0 * alphastar ) ;
//constant z = alphastar - 1.0 ; //constant
for ( i=2; i<=n-1; i++ )
{
if ( i > 2 )
{
a[ i ] = x ;
}
b[ i ] = y ;
if ( i < n - 1 )
{
c[ i ] = z ;
}
if ( i == 2 )
{
d[ i ] =-( x * t[ 1 ] ) ;
}
if ( i == n - 1 )
{
d[ i ] =- ( z * t[ n ] ) ;
}
}
for ( i=2; i<=n-2; i++ )
{
d[ i ] = d[ i ] / b[ i ] ; //Pivoting step
c[ i ] = c[ i ] / b[ i ] ; // pivoting step
b[ i ] = 1.0 ;
d[ i + 1 ] = d[ i + 1 ] - a[ i + 1 ] * d[ i ] ; //Forward Reduction
b[ i + 1 ] = b[ i + 1 ] - a[ i + 1 ] * c[ i ] ;
} //Forward Reduction}
t[ n - 1 ] = d[ n - 1 ] / b[ n - 1 ] ;
for ( j=n-2; j>=2; j--)
{
// Back Substitution t[ j ] = d[ j ] - c[ j ] * t[ j + 1 ] ; }
for( l=1; l<=n; l++ )
{
X[ 1 ] = 0.0 ;
if ( l > 1 ) {
X[ l ] = X[ l - 1 ] + delx ;
}
}
for ( k=1; k<=n; k++ ) {
printf( "%2f %f ", X[ k ],t[k] ) ;
}
}
}