您好,欢迎访问三七文档
编写一个函数。函数的三个参数是一个字符和两个整数。字符参数是需要输出的字符。第1个整数说明了在每行中该字符输出的个数,而第二个整数指的是需要输出的行数。编写一个调用该函数的程序。要求:输入字符为回车时,作为未输入字符处理;输入行列值时若不是数字,直接结束程序。编程考点:循环基本语法;break和continue#includestdio.hvoidchLineRow(charch,intc,intr);intmain(void){charch;intcol,row;printf(Enteracharacter(#toquit):);while((ch=getchar())!='#'){if(ch=='\n')continue;printf(Enternumberofcolumnsandnumberofrows:);if(scanf(%d%d,&col,&row)!=2)break;chLineRow(ch,col,row);printf(\nEnternextcharacter(#toquit):);}printf(Bye!\n);return0;}voidchLineRow(charch,intc,intr){intcol,row;for(row=0;rowr;row++){for(col=0;colc;col++)putchar(ch);putchar('\n');}return;}编写一个函数计算double类型数的某个整数次幂(不得调用系统的函数pow),注意0的任何次幂和任何数值的0次幂以及负数次幂并编写测试程序编程考点:if-else的嵌套#includestdio.hdoublepower(doublea,intb);/*ANSIprototype*/intmain(void){doublex,xpow;intn;printf(Enteranumberandtheintegerpower);printf(towhich\nthenumberwillberaised.Enterq);printf(toquit.\n);while(scanf(%lf%d,&x,&n)==2){xpow=power(x,n);/*functioncall*/printf(%.3gtothepower%dis%.5g\n,x,n,xpow);printf(Enternextpairofnumbersorqtoquit.\n);}printf(Hopeyouenjoyedthispowertrip--bye!\n);return0;}doublepower(doublea,intb)/*functiondefinition*/{doublepow=1;inti;if(b==0){//if(a==0)//printf(0tothe0undefined;using1asthevalue\n);pow=1.0;}elseif(a==0)pow=0.0;elseif(b0)for(i=1;i=b;i++)pow*=a;else/*b0*/pow=1.0/power(a,-b);returnpow;/*returnthevalueofpow*/}写一个计算降水量的程序,给定一个数组,记录的每年每月的降水量constfloatrain[YRS][MONTHS]={{10.2,8.1,6.8,4.2,2.1,1.8,0.2,0.3,1.1,2.3,6.1,7.4},{9.2,9.8,4.4,3.3,2.2,0.8,0.4,0.0,0.6,1.7,4.3,5.2},{6.6,5.5,3.8,2.8,1.6,0.2,0.0,0.0,0.0,1.3,2.6,4.2},{4.3,4.3,4.3,3.0,2.0,1.0,0.2,0.2,0.4,2.4,3.5,6.6},{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.2}};使用指针而不是使用下标进行计算,每年的降水总值、每年平均值,以及每月平均量编程考点:如何用指针定位二维数组的元素#includestdio.h#defineMONTHS12/*numberofmonthsinayear*/#defineYRS5/*numberofyearsofdata*/intmain(void){/*initializingrainfalldatafor1990-1994*/constfloatrain[5][12]={{10.2,8.1,6.8,4.2,2.1,1.8,0.2,0.3,1.1,2.3,6.1,7.4},{9.2,9.8,4.4,3.3,2.2,0.8,0.4,0.0,0.6,1.7,4.3,5.2},{6.6,5.5,3.8,2.8,1.6,0.2,0.0,0.0,0.0,1.3,2.6,4.2},{4.3,4.3,4.3,3.0,2.0,1.0,0.2,0.2,0.4,2.4,3.5,6.6},{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.2}};intyear,month;floatsubtot,total;printf(YEARRAINFALL(inches)\n);for(year=0,total=0;yearYRS;year++){/*foreachyear,sumrainfallforeachmonth*/for(month=0,subtot=0;monthMONTHS;month++)subtot+=*(*(rain+year)+month);printf(%5d%15.1f\n,1990+year,subtot);total+=subtot;/*totalforallyears*/}printf(\nTheyearlyaverageis%.1finches.\n\n,total/YRS);printf(MONTHLYAVERAGES:\n\n);printf(JanFebMarAprMayJunJulAugSepOct);printf(NovDec\n);for(month=0;monthMONTHS;month++){/*foreachmonth,sumrainfalloveryears*/for(year=0,subtot=0;yearYRS;year++)subtot+=*(*(rain+year)+month);printf(%4.1f,subtot/YRS);}printf(\n);return0;}编写一个程序,提示用户输入3个数据集,每个数据集包括5个double值。程序应当实现下列所有功能:A.把输入信息存储到一个3*5的数组中B.计算出每个数集(包含5个值)的平均值C.计算所有数值的平均数D.找出这15个数中的最大值打印出结果每个任务需要用一个单独的函数来实现。对于任务B需要编写计算并返回一维数组平均值的函数,循环三次调用该函数来实现任务B。对于任务C、D,函数应当把整个数组做为参数,并却完成任务B、C和D的函数应该向他的调用函数返回答案(推荐使用变长数组,参考程序为变长数组实现)编程考点:循环和二维数组的熟练应用,以及数组作为参数的传递#includestdio.h#defineROWS3#defineCOLS5voidstore(doublear[],intn);doubleaverage2d(introws,intcols,doublear[rows][cols]);doublemax2d(introws,intcols,doublear[rows][cols]);voidshowarr2(introws,intcols,doublear[rows][cols]);doubleaverage(constdoublear[],intn);intmain(void){doublestuff[ROWS][COLS];introw;for(row=0;rowROWS;row++){printf(Enter%dnumbersforrow%d\n,COLS,row+1);store(stuff[row],COLS);}printf(arraycontents:\n);showarr2(ROWS,COLS,stuff);for(row=0;rowROWS;row++)printf(averagevalueofrow%d=%g\n,row+1,average(stuff[row],COLS));printf(averagevalueofallrows=%g\n,average2d(ROWS,COLS,stuff));printf(largestvalue=%g\n,max2d(ROWS,COLS,stuff));printf(Bye!\n);return0;}voidstore(doublear[],intn){inti;for(i=0;in;i++){printf(Entervalue#%d:,i+1);scanf(%lf,&ar[i]);}}doubleaverage2d(introws,intcols,doublear[rows][cols]){intr,c;doublesum=0.0;for(r=0;rrows;r++)for(c=0;ccols;c++)sum+=ar[r][c];if(rows*cols0)returnsum/(rows*cols);elsereturn0.0;}doublemax2d(introws,intcols,doublear[rows][cols]){intr,c;doublemax=ar[0][0];for(r=0;rrows;r++)for(c=0;ccols;c++)if(maxar[r][c])max=ar[r][c];returnmax;}voidshowarr2(introws,intcols,doublear[rows][cols]){introw,col;for(row=0;rowrows;row++){for(col=0;colcols;col++)printf(%g,ar[row][col]);putchar('\n');}}doubleaverage(constdoublear[],intn){inti;doublesum=0.0;for(i=0;in;i++)sum+=ar[i];if(n0)returnsum/n;elsereturn0.0;}编写产生100个1-10的随机数的程序,并以降序排列rand()为C的标准随机数产生函数编程考点:一维数组实现选择排序,嵌套循环的基本使用#includestdio.h#includestdlib.hvoidprint(constintarray[],intlimit);voidsort(intarray[],intlimit);#defineSIZE100intmain(void){inti;intarr[SIZE];for(i=0;iSIZE;i++)arr[i]=rand()%10+1;puts(initialarray);print(arr,SIZE);sort(arr,SIZE);puts(\nsortedarray);print(arr,SIZE);return0;}/*sort.c--sortsanintegerarrayindecreasingorder*/voidsort(intarray[],intlimit){inttop,search,temp;for(top=0;toplimit-1;top++)for(search=top+1;searchlimit;search++)if(array[search]array[top]){temp=array[search];array[search]=array[top];array[top]=temp;}}/*print.c--
本文标题:C补强阶段作业
链接地址:https://www.777doc.com/doc-7028281 .html