中山大学C++视频第6章_黎培兴.pptx

中山大学C视频课件第6章_黎培兴剖析

1;2;3;4;5;6;7;8;9;#include iostream.h void main(void) { char key[ ] = {a,c,b,a,d}; char c; int ques = 0, numques = 5, numcorrect = 0; cout Enter the numques question tests: endl;;; while (cin.get(c)) { if (c != \n) if (c == key[ques]) { numcorrect++; cout ; } else cout *; else { cout Score float(numcorrect)/numques*100 %; ques = 0; // reset variables numcorrect = 0; cout endl; continue; } ques++; } };运行结果: acbba ** Score 60% acbad Score 100% abbda * ** Score 40% bdcba ***** Score 0%;13;存储顺序 按行存放,上例中数组a的存储顺序为: ;分行给二维数组赋初值 例如:static int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}; 将所有数据写在一个{}内,按顺序赋值 例如:static int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12}; 可以对部分元素赋初值 例如:static int a[3][4]={{1},{0,6},{0,0,11}};;16;17;#include iostream.h void RowSum(int A[ ][4], int nrow) { int sum; for (int i = 0; i nrow; i++) { sum = 0; for(int j = 0; j 4; j++) sum += A[i][j]; cout Sum of row i is sum endl; } } ;void main(void) { int Table[3][4] = {{1,2,3,4},{2,3,4,5},{3,4,5,6}}; for (int i = 0; i 3; i++) { for (int j = 0; j 4; j++) cout Table[i][j] ; cout endl; } RowSum(Table,3); };运行结果: 1 2 3 4 2 3 4 5 3 4 5 6 Sum of row 0 is 10 Sum of row 1 is 14 Sum of row 2 is 18 ;21;22;23;24;//6-5.cpp #includeiostream.h #include Location.h Location::Location( ) { X=Y=0; coutDefault Constructor called.endl; } Location::Location(int xx,int yy) { X=xx; Y=yy; cout Constructor called.endl; };Locatuon::~Location( ) { coutDestructor called.endl; } void Location::Move(int x,int y) { X=x; Y=y; };int main( ) { coutEntering main...endl; Location A[2]; for(int i=0;i2;i++) A[i].Move(i+10,i+20); coutExiting main...endl; return 0; };运行结果: Entering main... Default Constructor called. Default Constructor called. Exiting main...

文档评论(0)

1亿VIP精品文档

相关文档