c语言作业答案
C语言作业及答案3

)。
A)char
B) void
C)float
D)int
8、设有说明 int (* prt)[M]; 其中的标识符 prt 是(
)。
A)M 个指向整型变量的指针
B)指向 M 个整型变量的函数指针
C)一个指向具有 M 个整型元素的一维数组的指针
D)具有 M 个指针元素的一维指针数组,每个元素都只能指向整型变量
)。
4、C 程序以函数为单位进行编译(
)。
5、由多个源文件组成一个程序时,主函数只能出现在一个源文件中(
)。
6、从变量的作用域角度来分,可以分为静态变量和动态变量(
)。
7、如果一个字符数组中包含一个以上结束符’\0’,则遇最后一个’\0’时输出才结束(
)。
8、gets( )函数的作用是从终端输入一个字符串到字符数组,并且将’\0’也装入该数组(
9、已知函数说明语句:void *fx( );则它的含义是(
)。
A)函数 fx 的返回值是一个空类型的指针
B)函数 fx 的返回值可以是任意的数据类型
C)函数 fx 无返回值
D)指针 fx 指向一个函数,该函数无返回值
10、对于下述定义,不正确的叙述是(
)。
union data
{ int i;
char c;
C)9,9
D)10,10
4、以下叙述正确的是()。
A)可以把 define 和 if 定义为用户标识符
B)可以把 define 定义为用户标识符,但不能把 if 定义为用户标识符
C)可以把 if 定义为用户标识符,但不能把 define 定义为用户标识符
D)define 和 if 都不能定义为用户标识符
C语言练习作业及参考答案

二、选择题
1、在C语言中(以16位PC机为例),5种基本数据类型的存储空间长度的排列顺序为____A___。
A) char<int<long int<=float<double
B) char=int <long int<=float<double
B)C程序的每行中只能写一条语句
C)C语言本身没有输入输出语句
D)在对一个C程序进行编译的过程中,可发现注释中的拼写错误
7、以下叙述不正确的是___D_____。
A)一个C源程序可由一个或多个函数组成
B)一个C源程序必须包含一个main函数
C)C程序的基本组成单位是函数
D)在C程序中,注释说明只能位于一条语句的后面
longd=32768;
float e=4.56;
b=a;
a=c=d;
printf("%d,%u,%u,%ld\n",a,b,c,d);
a=e;
printf("%d,%f\n",a,e);
ห้องสมุดไป่ตู้return 0;
}
本题假设int型数据占两个字节。
(提示:%u表示以无符号整型的格式输出;%ld表示以长整型的格式输出)
8、C语言规定:在一个源程序中,main函数的位置是___A_____。
A)必须在最开始
B)必须在系统调用的库函数的后面
C)可以任意
D)必须在最后
习题
一、程序题
1、阅读程序写运行结果
(1)#include <stdio.h>
int main()
C语言作业答案

第一章:第6题方法一:#include <stdio.h>int main(){int a,b,c,max;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf("The largest number is %d\n",max); return 0;}方法二:#include <stdio.h>int main(){int max(int x,int y);int a,b,c,d;printf("please input a,b,c:\n");scanf("%d,%d,%d",&a,&b,&c);d=max(max(a,b),c);printf("The largest number is %d\n",d);return 0;}int max(int x,int y){ return (x>y)?x:y;}第三章第3题#include <stdio.h>#include <math.h>int main(){float d=300000,p=6000,r=0.01,m;m=log10(p/(p-d*r))/log10(1+r);printf("m=%6.2f\n",m);return 0;}第4题#include <stdio.h>int main(){char c1,c2;c1=97;c2=98;printf("c1=%c,c2=%c\n",c1,c2);printf("c1=%d,c2=%d\n",c1,c2);return 0;}(1)运行时会输出什么信息?为什么?解:运行时输出:第1行是将c1,c2按%c的格式输出,97是字符a的ASCII代码,98是字符b的ASCII代码。
C语言课后作业答案

}
else ch=ch;
printf("%c\n",ch);
}
3-5-2大写转化成小写或者小写变大写
#include <stdio.h>
void main()
{
char ch;
printf("请输入一个字符:");
scanf("%c",&ch);
ch=ch;
{
if(ch>='A'&&ch<='Z')ch=ch+32;
printf("a=%d,b=%d\n",a,b);
}
}
水仙花数1
#include <stdio.h>
void main()
{
int m,i,j,k,n=0;
for (m=100;m<=999;m++)
{
i=m/100;
j=m/10-i*10;
k=m%10;
if (m==i*i*i+j*j*j+k*k*k) n++,
switch(ch)
{
case '+':printf("%f+%f=%f\n",x,y,x+y);break;
case '-':printf("%f-%f=%f\n",x,y,x-y);break;
case '*':printf("%f*%f=%f\n",x,y,x*y);break;
case '/':printf("%f/%f=%f\n",x,y,x/y);break;
C语言基础知识作业(含解答)

基础知识作业解答(1)一、选择题:1.下面叙述错误的是:a)C程序中,各种括号应成对出现。
b)C程序中,赋值号的左边不可以是表达式。
c)C程序中,变量名的大小是有区别的。
d)C程序中,若未给变量赋初值,则变量的初值自动为0。
答案:d)解答:C程序中,若未给变量赋初值,则变量的初值不一定为0。
按变量被定义的位置来区分,变量可分为局部变量和全局变量;在{ … } 之间定义的变量称为局部变量,编译系统对局部变量是不进行初始化的,即,不赋初值。
未赋初值的局部变量的值为机内随机值。
在{ … } 之外定义的变量称为全局变量,编译系统自动对全局变量进行初始化。
未赋初值的全局部变量的值为0。
2.下面叙述正确的是:a)C程序中的变量定义语句可以写在函数体中任何位置。
b)C程序中不能有空语句。
c)C程序中的变量必须先定义后使用。
d)C程序中所有简单数据类型都可以准确无误的表示。
答案:c)解答:C程序中,变量定义语句的作用是通知编译系统为变量分配存储空间,所以必须先定义后使用。
3.以下合法的用户标识符的是:a)long b)\t c)5a d)user答案:d)解答:C程序中,合法的用户标识符的定义是:以字母或下划线开头的由字母、数字和下划线组成的字符串,不可以使用系统的关键字作为用户的标识符。
常见的关键字有:int, float ,char ,double ,long, short,unsigned,if,switch,case,break, continue ,do ,for ,while ,struct ,union, auto, static, extern, register;标准函数名可以用作用户标识符,但不提倡。
4.C程序中,合法的关键字的是:a)Float b)while c)Int d)Integer答案:b)5.下面选项中,优先级最高的运算符是:a)&& b)/= c) ! d)<=答案:c)6.下面选项中,优先级最低的运算符是:a)!= b) || c)++ d),答案:d)7.C程序中,运算对象必须为整型数据的运算符是a)++ b) % c) / d) *答案:b)8.假设x,y,z为整型变量,且x=2,y=3,z=10,则下列表达式中值为1的是:a)x && y||z b)x>z c) (!x && y)||(y>z) d)x && !z ||!(y && z)答案:a)解答:C程序中,进行逻辑运算时,所有的非0数都处理成逻辑真;0处理成逻辑假。
C语言作业答案

一、思考题1、常量和变量有什么区别?它们分别是如何定义的?常量是指在C程序运行过程中其值不变的量,变量是一个值的存放处,其值在程序运行过程中可以被改变。
常量有两种形式:字面常量与符号常量,C语言可以使用以下两种形式定义符号常量(1)用关键字const定义定义格式为:const 类型名常量名=值;(2)用编译预处理命令#define定义格式为:#define <常量名> <值常量>变量的定义格式为:数据类型变量名1,变量名2,…,变量名n;或数据类型变量名1=初值1,变量名2=初值2,…,变量名n=初值n;2、变量的要素有哪些?请用一个例子说明?名字、类型、值和地址int a = 1;int是类型,a是名字,1是值,地址是编译器给a变量分配的内存空间3、变量在整个程序中能有相同的值吗?为什么?请举例说明。
可以,因为变量的值可以被改变,但不是一定被改变。
如果程序中没有其它不同的赋值,变量将保持不变。
4、为什么程序需要有短整型(short)和长整型(long)?整型的值集理论上是所有整数,但由于受到计算机存储单元的限制,C语言的整型只能表示所有整数的一个有限子集,因此,整数有它的表示范围限制,且不同的C语言系统可表示整数的范围可能不同。
不同范围的整型数据所占有的内存空间大小不同。
程序开发人员通过选择能够满足范围要求的类型(short或long),可以减少数据存储空间。
比如,当短整型可以满足数据范围要求时,就没有必要选择需要更多存储空间的长整型。
5、字符’8’和数字8有何不同?字符’8’是字符型,数字8是整型6、C语言有字符串变量吗?没有7、C语言中的运算符“/”和“%”有何区别?C语言中除法运算符“/”与数学中的运算符“÷”的用法和功能完全一样吗?“/”是除法,“%”是取余数,“/”与“÷”的用法和功能不完全一样,“÷”得到的是两数相除后得到的真实值,“/”得到的是两数相除后取整的值。
C语言全部题目及答案
C 语言全部题目及答案Exercise1:Programming Environment and Basic Input/Output1. Write a program that prints “This is my first program!” on the screen.(a) Save this program onto your own disk with the name of e2-1a;(b) Run this program without opening Turbo C;(c) Modify this program to print “This is my second program!”, then save it ase2-1b. Please do not overwrite the first program.2. Write a program that prints the number 1 to 4 on the same line. Write the programusing the following methods:(a) Using four “printf” statemen ts.(b) Using one “printf” statement with no conversion specifier (i.e. no ‘%’).(c) Using one “printf” statement with four conversion specifiers3 .(a) Write a program that calculates and displays the number of minutes in 15 days.(b) Write a program that calculates and displays how many hours 180 minutes equal to.(c) (Optional) How about 174 minutes?ANSWERS:#include<stdio.h>int main(){printf("1"); printf("2"); printf("3"); printf("4");return 0;}return 0; }#include<stdio.h>int main(){float days,minutes; days = 15; int main() {printf("This program!");return 0;int main(){float minutes,hours;printf("The numberminutes in 15 are %f\n", minutes);printf("This program!"); int main() {return 0; }#include<stdio.h> #include<stdio.h> #include<stdio.h>of days firstmyisExercise 2: Data Types and Arithmetic Operations1. You purchase a laptop computer for $889. The sales tax rate is 6 percent. Write and execute a C program that calculates and displays the total purchase price (net price + sales tax).2 .Write a program that reads in the radius of a circle and prints the circle’s diameter, circumference and area. Use the value 3.14159 for “ ”.3 .Write a program that reads in two numbers: an account balance and an annual interest rate expressed as a percentage. Your program should then display the new balance after a year. There are no deposits or withdraws – just the interest payment. Your program should be able to reproduce the following sample run:Interest calculation program. Starting balance? 6000Annual interest rate percentage? 4.25}#include<stdio.h>int main() {printf("%d%d%d%d",1,2,3,4);return 0; }hours = minutes / 60; printf("180 minutes equal to %f hours\n", hours);return 0; }#include<stdio.h>int main() {float minutes,hours;#include<stdio.h>int main(){float net_price,sales_tax,total;net_price = 889;sales_tax = net_price * 0.06;total = net_price + sales_tax;printf("The total purchase price is %g", total);return 0;}#include<stdio.h>int main(){printf("Please input a number as radius:\n");float radius,diameter,circumference,area;scanf("%f",&radius);printf("The diameter is %g\n",diameter = radius * 2);printf("The circumference is %g\n",circumference = radius * 2 * 3.14159); printf("The area is %g\n", area = radius * radius * 3.14159);return 0;}#include<stdio.h>{float SB,percentage,NB;printf("Interest calculation program\n\nPlease enter the Starting Balance:"); scanf("%f",&SB);printf("Please enter the Annual interest rate percentage:");scanf("%f",&percentage);NB = SB * percentage / 100 + SB;printf("\nThe Balance after one year is:%g",NB);return 0;}Exercise 3: Selection structure1 . Write a C program that accepts a student’s numerical grade, converts thenumerical grade to Passed (grade is between 60-100), Failed (grade is between 0-59), or Error (grade is less than 0 or greater than 100). 2 . Write a program that asks the user to enter an integer number, then tells theuser whether it is an odd or even number. 3 . Write a program that reads in three integers and then determines and prints thelargest in the group.ANSWER: #include<stdio.h>#include<stdio.h>int main() {#include<stdio.h>int main() {int grade;printf("Please enter the grade:");scanf("%d",&grade);if (grade >= 60 && grade <= 100) printf("Passed."); else if (grade >= 0 && grade <60)#include<stdio.h>int main() {int a,b,c;printf("Please enter 3 integer numbers\n"); printf("Then I'll tell you which is thelargest\n"); scanf("%d%d%d",&a,&b,&c); if (a > b && a > c) printf("%d is the largest",a);else if (b > a && b > c) printf("%d is the largest",b); else if (c > a && c > b)int a;printf("Please enter an integer number\n");printf("Then I'll tell you whether it's an odd or even number");scanf("%d",&a);if (a%2 == 0)printf("%d is an even number",a);elseprintf("%d is a odd number",a);return 0;}Exercise 4: ‘switch’statement and simple “while” repetition statement1. Write a program that reads three integers an abbreviated date (for example: 26 12 94) andthat will print the date in full; for example: 26th December 1994. The day should be followed by an appropriate suffix, ‘st’,‘nd’,‘rd’ or ‘th’. Use at least one switch statement.2 .Write a C program that uses a while loop to calculate and print the sum of the even integersfrom 2 to 30.3. A large chemical company pays its sales staff on a commission basis. They receive £ 200 perweek plus 9% of their gross sales for that week. For example, someone who sells £ 5000 of chemicals in one week will earn £ 200 plus 9% of £5000, a total of £650. Develop a C program that will input each salesperson’s sales for the previous week, and print out their salary.Process one person’s figures at a time.Enter sales in pounds(-1to end):5000.00Salary is:650.00Enter sales in pounds(-1to end):00.00Salary is:200.00Enter sales in pounds(-1to end):1088.89Salary is:298.00Enter sales in pounds(-1to end):-1Optional:4. A mail order company sells five different products whose retail prices are shown in thefollowing table:Product Number1 2Retail Price (in pounds)2.984.503 4 59.98 4.49 6.87Write a C program that reads in a series of pairs of numbers as follows:(1). Product number(2). Quantity sold for one dayYour program should use a switch statement to help determine the retail price for each product, and should use a sentinel-controlled loop to calculate the total retail value of all products sold in a given week (7days).ANSWER:#include<stdio.h>int main(){printf("Please enter three numbers for date:");int day,month,year;scanf("%d %d %d",&day,&month,&year);if(day>31)printf("Error");else{switch (day){case 1:printf("1st");break;case 2:printf("2nd");break;case 3:printf("3rd");break;case 21:printf("21st"); break;case 22:printf("22nd"); break;case 23:printf("23rd"); break;case 31:printf("31st"); break;default:printf("%dth",day); }}switch(month){#include <stdio.h>int main(){int a,b;a=0;b=2;while (b<=30){a=a+b;b=b+2;}printf("The sum of the even integers from 2 to 30 is %d",a);return 0;}Exercise 5: ‘for’ and ‘do … while ” repetition statements1. Write a program which uses a do/while loop to print out the first 10 powers of 2 other than 0 (ie. it prints out the values of 21, 22, ..., 210). Use a for loop to do the same.2. The constant can be calculated by the infinite series:= 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +....case 1: printf("January ");break;case 2: printf("February ");break;case 3: printf("March ");break;case 4: printf("April ");break;case 5: printf("May ");break;case 6: printf("June ");break;case 7: printf("July ");break;case 8: printf("August ");break;case 9: printf("September ");break;#include<stdio.h> int main() { float a,b;while (a>0 ) {printf("Enter sales in pounds (-1 to end):");scanf("%f",&a); b=200+a*0.09; if (a==-1) printf(" ");else printf("Salary is %.0f\n",b); }return 0;Write a C program that uses a do/while loop to calculate using the series. The program should ask the user how many terms in the series should be used. Thus ifthe user enters ‘3’, then the program should calcula te as being 4 - 4/3 + 4/5.Nested repetition3. Write a program that prints the following diamond shape. You may use printf statements that print either a single asterisk (*) or a single blank. Maximize your use of repetition (with nested for statements) and minimize the number of printf statements.*****************************************4. Write a program to print a table as follows:1*1= 1 2*1= 2 3*1= 3 ….9*1= 9ANSWER:#include<stdio.h> int main(){int a,b;a=0;b=1;do{a++;9*4=36 9*5=459*6=54#include <stdio.h>int main(){double c,pie,p;int a,b,d,n;printf("Enterterms:");scanf("%d",&a);printf("Pie=");9*7=63 9*8=729*9=81#include <stdio.h>int main(){int row,a,b,j;row=1;j=4;while(row<=5){for(a=j;a>=1;a=a-1) printf(" ");2*2= 43*2= 69*3=27 9*2=183*3= 9#include <stdio.h>int main () { int i, j;for (i=1; i<=9; i++) {for (j=1; j<=9; j++) if (i>=j)printf("%1d*%1d=%2d ", i, j, i*j); printf("\n"); }getchar(); return 0;int main() { int a;for(a=2;a<=1024;a=a *2) printf("%d",a); return 0; }p=p+pie;if(n>1&&n<=a) {if(n%2!=0) printf("+"); else printf("-"); }printf("4/%d",d); n++; }printf("\n=%f",p); return 0; }}row=1; j=1;while(row<=5){ for(a=1;a<=j;a=a +1) printf(" "); for(b=1;b<=9-2*j;b++) printf("*"); for(a=1;a<=j;a=a +1) printf(" "); printf("\n"); row++;Exercise 6: Simple Functions1. Write a C program that reads several numbers and uses the functionround_to_nearest to round each of these numbers to the nearest integer.Theprogram should print both the original number and the rounded number.2. Write a program that reads three pairs of numbers and adds the larger of the firstpair,the larger of the second pair and the larger of the third e a function to return the larger of each pair.3.A car park charges a£2.00minimum fee to park for up to3hours,and anadditional£0.50for each hour or part hour in excess of three hours.The maximum charge for any given24-hour period is£10.00.Assume that no car parks for more than24hours at a time.Write a C program that will calculate and print the parking charges for each of3 customers who parked their car in the car park yesterday.The program should accept as input the number of hours that each customer has parked,and output the results in a neat tabular form,along with the total receipts from the three customers:Charge2.002.50 10.0014.50The program should use the function calculate_charges to determine the charge for each customer.ANSWER: #include<stdio.h> int main() {float larger_Number(float,float); float num1,num2,total=0;int a=0;while (a<3) { void round_to_nearest(float); float num; while (5){ total=total+larger_Number(num1,num2);a=a+1; }#include<stdio.h> #include<math.h> int main() {printf("Please input a number:"); scanf("%f",&num);TOTAL Hours 29.524.0 Car 4.0 1.5 2 3 1#include <stdio.h> int main() {float calculate_charges(float);float hour1,hour2,hour3,charge1,charge2,charge3,total1=0,total2=0;printf("Please input three car's parking hours:"); scanf("%f%f%f",&hour1,&hour2,&hour3);charge1=calculate_charges(hour1); charge2=calculate_charges(hour2); charge3=calculate_charges(hour3);printf("Car Hours Charge\n"); printf("1%10.1f%10.2f\n",hour1,charge1); printf("2%10.1f%10.2f\n",hour2,charge2);void round_to_nearest(float num1) {int near_integer; intsmall_integer=floor(num1);if (num1-small_integer>=0.5) near_integer=ceil(num1); elsenear_integer=floor(num1);printf("\nThe nearest integer of the number is:%d\n",near_integer); }printf("\nThe total is %f",total);return 0; }float larger_Number(float num1,float num2) { float larger;if (num1>=num2) {printf("%f",num1); larger=num1; } else {printf("3%10.1f%10.2f\n",hour3,charge3);total1=hour1+hour2+hour3;total2=charge1+charge2+charge3;printf("TOTAL%7.2f%9.2f",total1,total2);return 0;}float calculate_charges(float hour){float charge;if (hour<=3)charge=2;if (hour>3 && hour<=19)charge=2.+0.50*(hour-3.);if (hour>19 && hour<=24)charge=10;return(charge);}Exercise 7: More Functions1.Write a program that uses sentinel-controlled repetition to take an integer as input,and passes it to a function even which uses the modulus operator to determine if the integer is even. The function even should return 1 if the integer is even, and0 if it is not.base exponentThe program should take the value returned by the function even and use it to print out a message announcing whether or not the integer was even.2.Write a C program that uses the function integerPower1(base,exponent)to return the value of:base exponentso that, for example, integerPower1(3, 4) gives the value 3 * 3 * 3 * 3.Assume that exponent is a positive, non-zero integer, and base is an integer.The function should use a for loop, and make no calls to any math library functions.3. Write a C program that uses the recursive function integerPower2(base,exponent) to return the value of:base exponentso that, for example, integerPower2(3, 4) gives the value 3 * 3 * 3 * 3. Assume that exponent is a positive, non-zero integer, and base is an integer.The function should make no calls to any math library functions.(Hint:the recursive step will use the relationship:base exponent=base.base exponent-1and the base case will be when exponent is1since:base1=base.)ANSWER:#include<stdio.h>int main(){int a,b;int judge(int);void judge1(int);printf("Please enter a number");scanf("%d",&a);while(a>=-1){b=judge(a);judge1(b);printf("Please evter a number");scanf("%d",&a);}return 0;}int judge(int x){if (x%2!=0)return (0);elsereturn (1);}void judge1(int x){if (x==1)printf("It's even\n"); elseprintf("It's odd\n");#include<stdio.h>int main(){int integerPower2(int,int);int base,exponent,result;printf("This program cancalculate the power\n");printf("Enter a integer basenumber:\n");scanf("%d",&base);printf("Enter a non-zerointeger number as the exponent:\n");scanf("%d",&exponent);result=integerPower2(base,expo nent);printf("The power is:%d",result); return 0;}int integerPower2(int x,int y){if(y==1)return (x);elsereturn (x*integerPower2(x,y-1)); }#include <stdio.h>int main(){int integerPower1(int,int);int base,exponent,answer;printf("Let us calculate the power\n");} printf("Please enter a integer base number:\n");scanf("%d",&base);printf("Please enter a non-zero integer number asthe exponent:\n");scanf("%d",&exponent);answer=integerPower1(base,exponent);printf("So the power is:%d",answer);return 0;}int integerPower1(int x,int y){int i,a;Exercise 08: Arrays1.Write a program that reads ten numbers supplied by the user into a singlesubscripted array,and then prints out the average of them.2. Write a program that reads ten numbers supplied by the user into a 2 by 5 array,and then prints out the maximum and minimum values held in:(a)each row(2rows)(b) the whole array3 .Use a single-subscripted array to solve the following problem. Read in 20numbers,each of which is between10and100,inclusive.As each number is read, print it only if it is not a duplicate of a number already read.Prepare for the “worst case”in which all 20 numbers are different. Use the smallest possible array to solve this problem.#include<stdio.h>int main(){#define MAXGRADES 10 int grades[MAXGRADES]; int i, total = 0;#include<stdio.h>int main(){#define MAXNUM 5int number[MAXNUM]; int i,j,a;ANSWER: #include<stdio.h> int main() {#define NUMROWS 2 #define NUMCOLS 5int number[NUMROWS][NUMCOLS]; int i,j,max1,max2,min1,min2;for (i=0;i<NUMROWS;i++){ printf("Please input 5 numbers with space between each other:\n"); for (j=0;j<NUMCOLS;j++) scanf("%d",&number[i][j]); }max1=number[0][0]; min1=number[0][0]; max2=number[1][0];{ printf("Enter a number:"); scanf("%d",&grades[i]); }printf("\nThe average of the numbers\n"); for(i=0;i<MAXGRADES;i++) { printf("%d",grades[i]); total+=grades[i]; }average = total/10.0;for(i=0;i<MAXNUM;i++) scanf("%d",&number[i]);a=0;for(i=0;i<MAXNUM;i++) { for(j=0;j<i;j++) {if(number[i]==number[j])a++;}min2=number[1][0];for(j=0;j<NUMCOLS;j++) { if(number[0][j]>=max1) max1=number[0][j];if(number[0][j]<=min1)min1=number[0][j];if(number[1][j]>=max2)max2=number[1][j];if(number[1][j]<=min2)min2=number[1][j];}printf("The max of the first row is %d\n",max1);printf("The min of the first row is %d\n",min1);printf("The max of the second row is %d\n",max2);printf("The min of the second row is %d\n",min2);printf("The max of two rows is ");if (max1>=max2)printf("%d\n",max1);else printf("%d\n",max2);printf("The min of two rows is ");if (min1<=min2)printf("%d\n",min1);else printf("%d\n",min2);return 0;}Exercise 9: More Arrays1. Write a program that enters 5 names of towns and their respective distance (aninteger) from London in miles. The program will print of the names of the towns that are less than 100 miles from London. Use arrays and character strings to implement your program.2. Write a program that prompts the user to type in four character strings, placesthese in an array of strings, and then prints out: (e.g. I am Peter Pan)(i) The four strings in reverse order. (e.g. Pan Peter am I)(ii) The four strings in the original order, but with each string backwards. (e.g. I ma reteP naP)(iii) The four strings in reverse order with each string backwards. (e.g. naP reteP ma I)#include<stdio.h>int main(){char character[5][20];int integer[5];int i;for(i=0;i<5;i++){ printf("Please enter a name of a town:\n");scanf("%s",character[i]);printf("Enter its distance from London in miles:\n");scanf("%d",&integer[i]);}printf("The towns that are less than 100 miles from London are:\n"); for(i=0;i<5;i++){if(integer[i]<100)printf("%s",character[i]);}return 0;}Exercise 10: Pointers1. Write a program that reads 5 integers into an array, and then uses four differentmethods of accessing the members of an array to print them out in reverse order.//Pan Peter am I #include<stdio.h> int main() {char four[4][81]; int i;printf("Please input four words:\n");for(i=0;i<4;i++) scanf("%s",four[i]);printf("The four strings in reverse order is :\n"); for(i=3;i>=0;i--) { printf("%s",four[i]);//naP reteP ma I #include <stdio.h> int main() {char four[4][81]; int i,j;printf("Please input four words:\n"); for(i=0;i<4;i++)scanf("%s",four[i]); printf("The four strings are:\n"); for(i=3;i>=0;i--) {for(j=0;four[i][j]!='\0';j++); for(j=j-1;j>=0;j--) {2. Write a program that reads 8 floats into an array and then prints out the second,fourth, sixth and eighth members of the array, and the sum of the first, third, fifth and seventh, using pointers to access the members of the array.3. (Optional) Write a program that use a SINGLE FUNCTION (用一个函数)tofind and return simultaneously both the lowest and highest values in an array of type int. Suppose the size of the array is 6.ANSWER:#include<stdio.h>int main(){int num1[5],i;printf("Please input 5 numbers:\n");for (i=0;i<5;i++)scanf("%d",&num1[i]);//the first wayprintf("\nPrint them out in reverse order in the first way:\n");for (i=4;i>=0;i--)printf("%d",num1[i]);//the second wayprintf("\nPrint them out in reverse order in the second way:\n");int *num2;num2 = &num1[0];for (i=4;i>=0;i--)printf("%d",*(num2+i));//the third wayprintf("\nPrint them out in reverse order in the third way:\n");for(i=4;i>=0;i--)printf("%d",*(num1+i));//the fourth wayprintf("\nPrint them out in reverse order in the fourth way:\n");int *num4;num4 = &num1[4];for (i=0;i<5;i++)printf("%d",*(num4-i));return 0;}#include<stdio.h>int main(){float num[8];int i;printf("Please input 8 floats:\n");for (i=0;i<8;i++)scanf("%f",&num[i]);//first, print out the second,fourth,sixth and eighthmembers of the arrayfor (i=1;i<8;i=i+2)printf("%f",*(num+i));//second, print out the sum of thefirst,third,fifthand seventhfloat total;for (i=0;i<8;i=i+2)total = total+*(num+i); #include<stdio.h>void SingleFunction(int *,int *); int main(){int num[6];int i;printf("Please input 6numbers:\n"); for (i=0;i<6;i++)scanf("%d",&num[i]);SingleFunction(num,num); return 0;}void SingleFunction(int *max,int *min) {int i,maxnum,minnum; maxnum = *max++;minnum = *min++;for(i=-1;i<5;i++)。
C语言课后作业题参考答案
C 语言课后作业题参考答案第二章2.2 用赋值表达式表示下列计算1) c b a x y +=2) 2/5262)(lne d a x -+= 3) y X aX X=+sin cos π24) R R R R =++1111123 5)232)4(7)2(5)2(31x x x x x y ++++=参考答案:1) y=pow(x,a+pow(b,c))或者:exp((a+exp(c*log(b)))*log(x))2) x=pow(log(sqrt(a+d*d))-exp(26),5.0/2)或者:exp(5.0/2*log(log(sqrt(a+d*d))-exe(26)))3) y=sin(x)/(a*x)+fabs(cos(3.1415926*x/2))4) R=1.0/(1.0/R1+1.0/R2+1.0/R3)5) y=x/(1+(x/(3+(pow(2*x,2)/5+(pow(2*x,3)/(7+pow(4*x,2)))))))2.6 编写程序,输入两个整数,分别求它们的和、差、积、商、余数并输出。
参考答案:#include<stdio.h>void main(){int m,n;printf("请输入2个整数\n");scanf("%d%d",&m,&n);printf("%d+%d=%d\n",m,n,m+n);printf("%d-%d=%d\n",m,n,m-n);printf("%d*%d=%d\n",m,n,m*n);printf("%d 除以%d :%f\n",m,n,(float)m/n);printf("%d 除以%d 的余数:%d\n",m,n,m%n);} 2.9 已知摄氏温度C 与华氏温度F 的转换关系是)32(95-=F C ,编写一个摄氏温度与华氏温度转换的程序,输入C ,输出F.参考答案:#include<stdio.h>void main(){float f,c;printf("请输入摄氏温度的值:\n");scanf("%f",&c); //输入33//f=9*c/5+32; //结果为91.4f=9.0/5*c+32; //结果为91.4//f=9/5*c+32; //结果为65printf("转换为华氏温度是:%f\n",f);} 第3章 分支程序设计3.3 编写程序,输入一个字母,若其为小写字母,将其转换成相应的大写字母,然后输出。
C语言1_7章课后作业答案
C语言1-7章课后作业答案第一章 C语言概述【习题1-1】简述C语言的特点。
答:C语言的特点:C语言简洁、紧凑、使用灵活、方便;运算符丰富;数据结构丰富;C是结构式语言;C语法限制不太严格,程序设计自由度大;C语言允许直接访问物理地址;C语言程序生成代码质量高;C语言适用范围大,可移植性好。
【习题1-2】请从以下的4个选项中选择一个正确答案。
(1)A (2)C (3)B (4)D(5)A (6)C【习题1-3】填空。
(1)英文字母、下划线、数字(2)函数(3) 函数头、函数体(4)独立(5);、#、/*…*/、{、}、 main、main【习题1-4】请指出以下哪些是合法的标识符?哪些又是合法的用户标识符。
101 int 3ip x_1 x+1 count 1234Xy x%y if while a.bc x&y _ _1_112 Abc name x break for x=y合法的标识符有:x_1、int、count、Xy、if、while、Abc、name、x、break、for合法的用户标识符有:x_1、count、Xy、Abc、name、x【习题1-5】简述上机调试运行C程序的操作步骤。
答:上机运行C语言程序的操作步骤为:启动Visual C++ 6.0→建立工程→创建源程序文件→输入(编辑)源程序内容→保存源程序文件→编译源程序文件→连接目标程序→执行程序→开始另一个程序。
【习题1-6】上机运行本章的3个例题。
略【习题1-7】参照本章例题,编写一个C语言源程序,输出以下信息:**************Nice to meet you!**************编写程序如下:#include<stdio.h>void main(){printf("**************\n");printf("Nice to meet you!\n");printf("**************\n");}第二章基本数据类型和运算符【习题2.1】上机编辑并调试本章所有例题。
C语言习题带答案
练习5-1答案一、选择题1-合法的数组说明语句是(B)。
A. int a[]=H string n; C. char a二”string";B・ int a[]={0J,2,345};D. char a[习二「0; TJ2「3字4字2.以下对一维整型数组a的说明正确的是(D)。
A.inta(10); B・ int n=10, a[n]; C・ intn; D. #define SIZE 10scanf("%d", &n); int a[SIZE];int a[n];3.已知:inta[10];,则对a数组元素的正确引用是(D)。
A・ a[I0] B・ a[3.5] C・ a(5) D. a[10-10]4.以下对一维数组a进行正确初始化的语句是(C)。
A・ int a[l0]=(05 0, 0, 0. 0); B. int a[10]={};C. int a[]={0};D. int a[2]={10, 9,8};5.对以下说明语句的正确理解是(B)。
int a[10]={6, 7, 8, 9, 10);A.将5个初值依次赋给a[l]至a[5]B.将5个初值依次赋给a[0]至a[4]C・将5个初值依次赋给a[6]至a[10]D・因为数组长度与初值的个数不相同,所以此语句不正确二、填空题6.求所有不超过200的N值,N的平方是具有对称性质的回文数。
所谓回文数就是将一个数从左到右与从右到左读都是一样的,例如:34543和1234321都是回文数。
例如:满足题意要求的数有:N=l, 11*11 = 121; N=lll, lll*lll=12321o#include <stdio.h>main(){int m[16], n, i, t, count=0; long a, k;printf(H Result is:\n H);for (n=10; n<200: n++) { k=0;t=l;a=n*n;for (i=I; a!=0; i++) {①;a/=10;} for (; i>l; i~){ k+=m[i-l]*t;②;)if (③)printf(H%2d:%10d %10d\n H, ++count, n, n*n);})则①为(m[i]=a%10;).②为(嘗二10;)、③为(k=n*n)o7•设数组a包括10个整型元素。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
No.: Name:第1讲C语言概述注:本系列习题中符号表示一串字符中的空格,符号表示下划线,符号ê表示换行回车,即键盘上Enter键。
一、选择题1. 一个C程序的执行是从A 。
A) 本程序main函数开始,到main函数结束B) 本程序文件第一个函数开始,到最后一个函数结束C) 本程序文件第一个函数开始,到本main函数结束D) 本程序main函数开始,到本程序文件最后一个函数结束2. 以下叙述不正确的是D 。
A) 一个C源程序必须包含一个main函数B) 一个C源程序可由一个或多个函数组成C) C程序的基本组成单位是函数D) 在C程序中,注释说明只能位于一条语句的后面3. 以下叙述正确的是C 。
A) 在对一个C程序进行编译的过程中,可发现注释中的拼写错误B) 在C程序中,main函数必须位于程序的最前面C) 语言本身没有输入输出语句D) C程序的每行中只能写一条语句4. 一个C语言程序是由B 。
A) 一个主程序和若干个子程序组成B) 函数组成C) 若干过程组成D) 若干子程序组成5. 以下叙述中正确的是C 。
A) C语言比其他语言高级B) C语言可以不用编译就能被计算机识别执行C) C语言以接近英语国家的自然语言和数学语言作为语言的表达形式D) C语言出现的最晚,具有其他语言的一切优点6. 下列叙述中正确的是D 。
A) C语言编译时不检查语法B) C语言的子程序有过程和函数两种C) C语言的函数可以嵌套定义D) C语言的函数可以嵌套调用7. 以下叙述中正确的是A 。
A) 构成C程序的基本单位是函数B) 可以在一个函数中定义另一个函数C) main函数必须放在其他函数之前D) 所有被调用的函数一定要在调用之前进行定义8. C语言规定,在源程序中,main函数的位置C 。
A) 必须在最开始B) 必须在系统调用的库函数的后面C) 可以任意D) 必须在最后9. 下列选项中不是结构化程序基本结构的是B 。
A) 顺序结构B) 分支结构C) 选择结构D) 循环结构10. 下列四组选项中,均不是C语言关键字的是A 。
A) define IF type B) getc char printfC) include case scanf D) while go pow11. 下列四组选项中,均是不合法的标识符的是B 。
A) W P 0 do B) b-a goto intC) float la0 A D) -123 abc TEMP12. 下列可用于C语言标识符的一组是B 。
A) void, define, WORD B) a3 b3, 123, CarC) For, -abc, IF Case D) 2a, DO, sizeof13. 以面的C程序中,错误的是D 。
A) main() B) main(){ int x,y,z; { int x,y,z;x=0;y=x-1; x=0;y=x+1;z=x+y;} z=x+y;}C) main() D) main(){ int x,z; { int x,y,z;int y; x=0;y=x+1;x=0;y=x+1; z=x+y,}z=x+y;}14. C语言中的标识符只能由字母,数字和下划线三种字符组成,且第一个字符C 。
A) 必须为字母B) 必须为下划线C) 必须为字母或下划线D) 可以是字母,数字和下划线中任一字符15. 下面各选项中,均是C语言标识符的选项组是B 。
A) 33 we auto B) 23 me 3ewC) 43 3e else D) ER -DF 32二、填空题1. 国际上C语言常见的三个标准是:标准C 、ANSI C 和ISO C 。
2. C语言源程序的文件格式为*.c和*.h 。
3. 请描述C程序中main函数的作用:接受系统调用,开始程序的执行,直至程序结束。
三、程序编写题(请在本页背面作答)1. 编写一个C程序,输出以下信息*******************************Very Good!*******************************2. 编写程序,实现求整数10、20和35的平均值(提示:注意给出的是三个整数,求出的平均值不是整数)1No.: Name:第2讲C语言基本数据类型一、选择题1. 若x、i、j、k都是int型变量,则计算下面表达式后,x的值为C 。
x=(i=4,j=16,k=32)A) 4 B) 16 C) 32 D) 522. 下列四组选项中,均是合法转义字符的是A 。
A) ‘\"’ ‘\\’ ‘\n’ B) ‘\’ ‘\017’ ‘\"’C) ‘\018’ ‘\f’ ‘xab’ D) ‘\\0’ ‘\101’ ‘xlf’3. 下面正确的字符常量是D 。
A) "c" B) ‘\\’’ C) ‘’ D) ‘K’4. 以下叙述不正确的是D 。
A) 在C程序中,逗号运算符的优先级最低B) 在C程序中,MAX和max是两个不同的变量C) 若a和b类型相同,在计算了赋值表达式a=b后,b中的值将放入a中,b中的值不变D) 从键盘输入数据时,对于整型变量只能输入整型数值,对于实型变量只能输入实型数值5. 以下叙述正确的是C 。
A) 在C程序中,每行只能写一条语句B) 若a是实型变量,C程序中允许赋值a=10,因此实型变量中允许存放整型数C) 在C程序中,%是只能用于整数运算的运算符D) 在C程序中,无论是整数还是实数,都能被准确无误地表示6. 已知字母A的ASCII码为十进制数65,且c2为字符型,则执行语句c2=‘A’+‘6’-‘3’后,c2的值为A 。
A) D B) 68 C) 不确定的值D) C7. sizeof(float)是B 。
A) 一个双精度型表达式B) 一个整型表达式C) 一种函数表达式D) 一个不合法的表达式8. 设C语言中,一个int型数据在内存中占2个字节,则unsigned int 型数据的取值范围为C 。
A) 0~255 B) 0~32767C) 0~65535 D) 0~21474836479. 设有说明:char w; int x; float y; double z;则表达式w*x+z-y值的数据类型D 。
A) float B) char C) int D) double10. 设以下变量均为int类型,则值不等于7的表达式是C 。
A) (x=y=6,x+y,x+1) B) (x=y=6,x+y,y+1)C) (x=6,x+1,y=6,x+y) D) (y=6,y+1,x=y,x+1)11. C语言中的基本数据类型包括B 。
A) 整型、实型、逻辑型B) 整型、实型、字符型C) 整型、字符型、逻辑型D) 字符型、实型、逻辑型12. 若变量已正确定义并赋值,以下符合C语言语法的表达式是B 。
A) a:=b+1 B) a=b=c+2C) int 18.5%3 D) a=a+7=c+b13. C语言中运算对象必须是整型的运算符是A 。
A) %= B) / C) = D) <=14. 若变量a,i已正确定义,且i已正确赋值,合法的语句是B 。
A) a==1 B) ++i; C) a=a++=5; D) a=int(i);15. int a=7;float x=2.5,y=4.7;则表达式x+a%3*(int)(x+y)%2/4的值是A 。
A) 2.500000 B) 2.750000C) 3.500000 D) 0.00000016. 在16位C编译系统上,若定义long a;,则能给a赋40000的正确语句是D 。
A) a=20000+20000; B) a=4000*10;C) a=30000+10000; D) a=4000L*10L;17. 设有int x=11;则表达式(x++*1/3)的值是A 。
A) 3 B) 4 C) 11 D) 1218. 下列数据中,不合法的C语言实型数据的是C 。
A) 0.123 B) 123e3 C) 2.1e3.5 D) 789.019. 若变量a是int类型,并执行了语句:a=‘A’+1.6;,则正确的叙述是D 。
A) a的值是字符CB) 不允许字符型和浮点型相加C) a的值是浮点型D) a的值是字符‘A’的ASCII值加上120. 设变量n为float类型,m为int类型,则以下能实现将n中的数值保留小数点后两位,第三位进行四舍五入运算的表达式是B 。
A) n=(n*100+0.5)/100.0 B) m=n*100+0.5,n=m/100.0C) n=n*100+0.5/100.0 D) n=(n/100+0.5)*100.021. 下面四个选项中,均是不正确的8进制数或16进制数的选项是D 。
A) 016 0x8f 018 B) 0abc 017 0xaC) 010 -0x11 0x16 D) 0a12 7ff -12322. 以下选项中,与k=n++完全等价的表达式是A 。
A) k=n,n=n+1 B) n=n+1,k=n C) k=++n D) k+=n+123. 下面均是合法整型常量的选项是A 。
A) 160 -0xffff 011 B) -0xcdf 01a 0xeC) -01 986012 0668 D) -0x48a 2e5 0x24. 假定x和y为double型,则表达式x=2,y=x+3/2的值是D 。
A) 3.500000 B) 3 C) 2.000000 D) 3.00000025. 以下变量x,y,z均为double类型且已正确赋值,不能正确表示数学式子x÷y÷z的C语言表达式是A 。
A) x/y*z B) x*(1/(y*z) C) x/y*1/z D) x/y/z26. 已知int k,m=1;执行语句k=-m++;后,k的值是A 。
A) -1 B) 0 C) 1 D) 227. 已知int m;float k;正确的语句是D 。
A) (int k)%m B) int(k)%mC) int(k%m) D) (int)k%m28. 不能进行++和--运算的数据类型为D 。
A) 指针B) 整型C) 长整型D) 常量2No.: Name:29. putchar函数可以向终端输出一个D 。
A) 整型变量表达式B) 实型变量值C) 字符串D) 字符或字符型变量值30. printf函数中用到格式符%5s,其中数字5表示输出的字符串占用5列。
字符串长度大于5,则输出按方式B ;如果字符串长度小于5,则输出按方式C 。
A) 从左起输出该字符串,右补空格B) 按原字符长从左向右全部输出C) 右对齐输出该字符串,左补空格D) 输出错误信息31. 阅读以下程序,当输入数据的形式为:25,13,10ê,则正确的输出结果为D 。