程序设计基础课后答案第四章

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

4.1 阅读下列程序,写出执行结果

1.#include

void main()

{ int i, conut=0, sum=0 ;

float average ;

int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } ;

for( i=0; i<10; i++ )

{ if( a[i] % 2 == 0 ) continue ;

sum += a[ i ] ;

conut ++ ;

}

average = sum / conut ;

cout << "conut = " << conut << '\t' << "average = " << average << endl ;

}

2.#include

void main()

{ int a[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;

int *p = a , sum = 0 ;

for( ; p

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 ; i

for( j=i; j>= 0; j-- , k++ )

a[j][i - j ] = k ;

for( i=0 ; i

{ for( j=0; j

cout << 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 ; i

if( 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; i

for( 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 ;

相关文档
最新文档