if( *p % 2 == 0 ) sum += *p ;
cout << "sum = " << sum << endl ;
}
3.const int n = 5 ;
#include
#include
void main()
{ int a[n][n]={ 0 }, i, j, k ;
for( k=1 , i=0 ; ifor( j=i; j>= 0; j-- , k++ )
a[j][i - j ] = k ;
for( i=0 ; i{ for( j=0; jcout << setw( 3 ) << a[i][j] ;
cout << endl ;
}
}
4.int f(int [],int);
#include
void main()
{ int a[] = { -1, 3, 5, -7, 9, -11 } ;
cout << f( a, 6 ) << endl ;
}
int f( int a[], int size )
{ int i, t=1 ;
for( i=0 ; iif( a[i]>0 ) t *= a[i] ;
return t;
}
5.int f( int [][3], int, int ) ;
#include
void main()
{ int a[][3] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 } ;
cout << f( a, 3, 3 ) << endl ;
}
int f( int a[][3], int row, int col )
{ int i, j, t=1 ;
for( i=0; ifor( j=0; j
{ a[i][j] ++ ;
if( i == j ) t *= a[i][j] ;
}
return t ;
}
6.#include
void test1( int *a1 )
{ a1 = new int( 5 ) ;
cout << "*a1 = " << *a1 << endl ;
}
void test2(int * & a2)
{ a2 = new int( 5 ) ;
cout << "*a2 = " << *a2 << endl ;
}
void main()
{ int *p = new int( 1 ) ;
test1( p ) ;
cout << "test1: *p1 = " << *p << endl ;
test2( p ) ;
cout << "test2: *p2 = " << *p << endl ;
}
7.#include
void main()
{ char s[] = "abccda" ;
int i ; char c ;
for( i = 1 ; ( c=s[i] ) != '\0'; i ++ )
{ switch( c )
{ case 'a' : cout << '%' ; continue ;
case 'b' : cout << '$' ; break ;
case 'c' : cout << '*' ; break ;
case 'd' : continue ;
}
cout << '#' << endl ;
}
}
8.#include
void main()
{ char *str[] = { "c++", "basic", "pascal" } ;
char **p ;
int i ;
p = str ;
for( i=0 ; i<3 ; i++ )
cout << *( p+i ) << endl ;
}
9.#include
void main()
{ char s1[] = "Fortran" , s2[] = "Foxpro" ;
char *p , *q ;
|