c语言题目
- 格式:doc
- 大小:34.00 KB
- 文档页数:3
c语言的考试题目答案及解析1. 题目:以下哪个选项是C语言中合法的变量名?A. 2variableB. variable-nameC. $variableD. variable!答案:B解析:在C语言中,变量名必须以字母或下划线开头,后面可以跟字母、数字或下划线。
选项A以数字开头,选项C和D包含非法字符($和!),因此它们都是不合法的变量名。
2. 题目:以下哪个选项是C语言中的整型数据类型?A. floatB. doubleC. intD. char答案:C解析:C语言中整型数据类型包括int、short int、long int等。
选项A和B是浮点型数据类型,选项D是字符型数据类型。
3. 题目:以下哪个选项是C语言中正确的数组声明?A. int array[10];B. int [10] array;C. int array[];D. int array[10] = {1, 2, 3};答案:A解析:在C语言中,数组的声明需要指定数组类型和数组名,以及数组的大小。
选项A正确地声明了一个整型数组,大小为10。
选项B 的顺序不正确,选项C没有指定数组大小,选项D虽然可以初始化数组,但不是纯粹的数组声明。
4. 题目:以下哪个选项是C语言中正确的函数声明?A. int function();B. int function int x;C. int function(int x);D. int function(int);答案:C解析:在C语言中,函数声明需要指定返回类型、函数名以及参数列表。
选项A没有参数列表,选项B的参数列表格式错误,选项D没有参数名。
选项C正确地声明了一个返回整型值的函数,接受一个整型参数。
5. 题目:以下哪个选项是C语言中正确的字符串字面量?A. "Hello, World!"B. 'Hello, World!'C. "Hello" "World!"D. "Hello\"World!"答案:A解析:在C语言中,字符串字面量是用双引号括起来的字符序列。
c语言期末考试题目及详细答案一、选择题(每题2分,共20分)1. 在C语言中,以下哪个关键字用于声明一个函数?A. intB. returnC. voidD. function答案:C2. 以下哪个选项是C语言中的合法标识符?A. 2variableB. variable2C. variable-nameD. variable name答案:B3. C语言中,用于定义一个结构体的关键字是?A. structB. unionC. enumD. typedef答案:A4. 若有定义 int a = 10;,则表达式 a++ 的结果是?A. 9B. 10C. 11D. 无法确定答案:C5. 下列哪个选项不是C语言的标准输入输出库函数?A. printf()B. scanf()C. getchar()D. sort()答案:D6. 在C语言中,以下哪个运算符用于计算两个整数的乘积?A. %B. /C. *D. ^答案:C7. 若有定义 int a = 5, b = 10;,则表达式 a % b 的结果是?A. 2B. 5C. 0D. 1答案:B8. 在C语言中,哪个关键字用于声明一个指针?A. *B. &C. %D. #答案:A9. 下列哪个选项是C语言中的合法字符串字面量?A. "Hello, World!"B. 'Hello, World!'C. "Hello World!"D. 'Hello World!'答案:A10. 在C语言中,以下哪个选项用于定义一个数组?A. int a[];B. int a[10];C. int a = 10;D. int a = {1, 2, 3};答案:B二、填空题(每题3分,共15分)1. C语言中,用于定义一个字符型变量的关键字是________。
答案:char2. 若有定义 int x = 3;,则表达式 x + x 的结果是________。
C语言全部题目及答案Exercise 1: 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 th is 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?Exercise 2: Data Types and Arithmetic Operations1. You purchase a laptop computer for $889. The sales tax rate is 6 percent. Write andexecute 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.25Balance after one year: 6255ANSWER:#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>int main(){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 the numerical 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 the user whether it is an odd or even number.3.Write a program that reads in three integers and then determines and prints the largest in the group.ANSWER:#include<stdio.h>#include<stdio.h>int main(){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 integers from 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 (-1 to end): 5000.00Salary is: 650.00Enter sales in pounds (-1 to end): 00.00Salary is: 200.00Enter sales in pounds (-1 to end): 1088.89Salary is: 298.00Enter sales in pounds (-1 to end): -1Optional:4. A mail order company sells five different products whose retail prices are shown in thefollowing table:Product Number Retail Price (in pounds)1 2.982 4.503 9.984 4.495 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).Exercise 5: ‘for’ and ‘do … while” repetition statements1. Write a program which uses a do/while loop to print out the first 10 powers of2 other than 0 (ie. it prints out the values of 21, 22, ..., 210). Use a for loop todo the same.2. The constant π can be calculated by the infinite series:π = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +....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= 12*1= 2 2*2= 43*1= 3 3*2= 6 3*3= 9….9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81#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;}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 pair. Use a function to return the larger of each pair.3. A car park charges a £2.00 minimum fee to park for up to 3 hours, and anadditional £0.50 for each hour or part hour in excess of three hours. The maximum charge for any given 24-hour period is £10.00. Assume that no car parks for more than 24 hours at a time.Write a C program that will calculate and print the parking charges for each of 3 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:Car Hours Charge1 1.5 2.002 4.0 2.503 24.0 10.00TOTAL 29.5 14.50The program should use the function calculate_charges to determine the charge for each customer.#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);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.The 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 is 1 since : base1 = base.)Exercise 08: Arrays1. Write a program that reads ten numbers supplied by the user into a single subscripted 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 (2 rows) (b) the whole array3.Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, 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.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];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.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;}。
c语言简单考试题及答案一、选择题(每题2分,共10分)1. 在C语言中,下列哪个选项是合法的变量名?A. 2variableB. variable2C. -variableD. variable-variable答案:B2. 下列哪个选项是C语言中的整型数据类型?A. intB. floatC. doubleD. char答案:A3. 下列哪个选项是C语言中的字符串?A. "Hello"B. 'Hello'C. helloD. 123答案:A4. 在C语言中,以下哪个运算符用于比较两个值是否相等?A. ==B. !=C. =D. <=答案:A5. C语言中,哪个函数用于计算两个整数的和?A. pow()B. sqrt()C. sum()D. add()答案:D二、填空题(每空1分,共10分)1. C语言中,用于声明变量的关键字是______。
答案:int2. 在C语言中,表示逻辑“与”的运算符是______。
答案:&&3. C语言中,表示逻辑“或”的运算符是______。
答案:||4. 在C语言中,用于输入的函数是______。
答案:scanf()5. 在C语言中,用于输出的函数是______。
答案:printf()三、简答题(每题5分,共20分)1. 请简述C语言中数组的定义方式。
答案:在C语言中,数组是通过指定元素类型和元素数量来定义的。
例如,int arr[5] 定义了一个包含5个整数的数组。
2. 请解释C语言中的指针是什么?答案:C语言中的指针是一种特殊的变量,它存储了另一个变量的内存地址。
指针可以用来直接访问和修改内存中的数据。
3. 请说明C语言中函数的定义格式。
答案:C语言中函数的定义格式为:返回类型函数名(参数类型参数名,...){ 函数体 }。
4. 请简述C语言中结构体的作用。
答案:结构体在C语言中用于创建复杂的数据类型,它允许将多个不同类型的数据项组合成一个单一的数据结构。
c语言笔试题目100及最佳答案1. 以下哪个关键字用于定义一个结构体?A. structB. unionC. enumD. typedef答案:A2. 在C语言中,以下哪种数据类型是用于存储字符的?A. intB. charC. floatD. double答案:B3. 以下哪个选项不是C语言中的控制语句?A. ifB. whileC. forD. switch答案:D4. 在C语言中,以下哪个运算符用于执行算术运算?A. %B. &&C. ==D. +答案:D5. 如何定义一个具有10个元素的整型数组?A. int array[10];B. int array(10);C. int array[10] = {0};D. int array = 10;答案:A6. 在C语言中,以下哪个函数用于将字符串转换为浮点数?A. atoiB. atofC. itoaD. sprintf答案:B7. 在C语言中,以下哪个函数用于计算数组中元素的数量?A. sizeofB. lengthC. countD. size答案:A8. 在C语言中,以下哪个关键字用于定义一个函数?A. functionB. defC. voidD. int答案:C9. 在C语言中,以下哪个关键字用于声明一个全局变量?A. externB. staticC. globalD. local答案:A10. 在C语言中,以下哪个函数用于打开一个文件?A. fopenB. openC. readD. write答案:A11. 在C语言中,以下哪个函数用于关闭一个文件?A. fcloseB. closeC. endD. finish答案:A12. 在C语言中,以下哪个函数用于读取一个字符?A. getcharB. getcC. fgetcD. scanf答案:A13. 在C语言中,以下哪个函数用于写入一个字符?A. putcharB. putcC. fputcD. printf答案:A14. 在C语言中,以下哪个函数用于格式化输出?A. printfB. sprintfC. fprintfD. all of the above答案:D15. 在C语言中,以下哪个函数用于格式化输入?A. scanfB. sscanfC. fscanfD. all of the above答案:D16. 在C语言中,以下哪个函数用于计算字符串的长度?A. strlenB. lengthC. sizeD. count答案:A17. 在C语言中,以下哪个函数用于连接两个字符串?A. strcatB. strncatC. strcpyD. strncpy答案:A18. 在C语言中,以下哪个函数用于复制一个字符串?A. strcatB. strncatC. strcpyD. strncpy答案:C19. 在C语言中,以下哪个函数用于比较两个字符串?A. strcmpB. strcpyC. strcatD. strncpy答案:A20. 在C语言中,以下哪个函数用于查找字符串中子串的位置?A. strstrB. strchrC. strrchrD. strcspn答案:A。
C语言试题(答案带解析)题目:编写一个C语言程序,实现以下功能:1. 输入一个字符串,长度不超过100个字符。
2. 统计并输出字符串中字母、数字、空格和其他字符的数量。
3. 删除字符串中所有的空格,并输出处理后的字符串。
4. 查找字符串中第一次出现字母 'a' 的位置,并输出位置(位置从0开始计算)。
5. 检查字符串是否为回文(忽略大小写和空格),如果是,输出"Yes",否则输出"No"。
试题代码如下:```c#include <stdio.h>#include <string.h>#include <ctype.h>int main() {char str[101];int alpha_count = 0, digit_count = 0,space_count = 0, other_count = 0;int i, a_position = -1;int len, is_palindrome = 1;// 输入字符串printf("Enter a string (up to 100 characters): ");fgets(str, 101, stdin);// 删除换行符len = strlen(str);if (str[len - 1] == '\n') {str[len - 1] = '\0';len--;}// 统计字符数量for (i = 0; i < len; i++) {if (isalpha(str[i])) {alpha_count++;if (str[i] == 'a') {a_position = i;}} else if (isdigit(str[i])) {digit_count++;} else if (isspace(str[i])) {space_count++;} else {other_count++;}}// 输出字符数量printf("Letters: %d\n", alpha_count); printf("Digits: %d\n", digit_count); printf("Spaces: %d\n", space_count); printf("Others: %d\n", other_count); // 删除空格并输出处理后的字符串char new_str[101];int j = 0;for (i = 0; i < len; i++) {if (!isspace(str[i])) {new_str[j++] = str[i];}}new_str[j] = '\0';printf("String without spaces: %s\n", new_str);// 查找字母 'a' 的位置if (a_position != -1) {printf("First occurrence of 'a' is at position: %d\n", a_position);} else {printf("'a' not found in the string.\n");}// 检查字符串是否为回文int start = 0, end = j - 1;while (start < end) {if (tolower(new_str[start]) !=tolower(new_str[end])) {is_palindrome = 0;break;}start++;end--;}if (is_palindrome) {printf("Yes\n");} else {printf("No\n");}return 0;}```解析:1. 程序首先通过 `fgets` 函数读取用户输入的字符串,并检查是否有多余的换行符。
c语言上机考试题及答案 C语言上机考试题及答案一、选择题1. 下列哪个选项不是C语言的基本数据类型?A. intB. floatC. stringD. double答案:C2. C语言中,哪个关键字用于声明一个函数?A. classB. functionC. structD. void答案:D3. 在C语言中,数组的下标从哪个数字开始?A. 0B. 1C. -1D. 任意数字答案:A二、填空题1. C语言中,用于定义变量的关键字是______。
答案:int2. 在C语言中,表示逻辑“与”的运算符是______。
答案:&&3. C语言中,用于循环结构的关键字有______和______。
答案:for,while三、编程题1. 编写一个C程序,计算并输出1到100之间所有奇数的和。
```cinclude <stdio.h>int main() {int sum = 0;for (int i = 1; i <= 100; i++) {if (i % 2 != 0) {sum += i;}}printf("The sum of odd numbers from 1 to 100 is: %d\n", sum);return 0;}```答案解析:该程序使用一个for循环遍历1到100的整数,通过`i % 2 != 0`判断是否为奇数,如果是,则累加到`sum`变量中。
最后输出所有奇数的和。
2. 编写一个C程序,实现字符串的反转。
```cinclude <stdio.h>include <string.h>void reverseString(char str[]) {int len = strlen(str);for (int i = 0; i < len / 2; i++) {char temp = str[i];str[i] = str[len - i - 1];str[len - i - 1] = temp;}}int main() {char str[] = "Hello, World!";reverseString(str);printf("Reversed string: %s\n", str);return 0;}```答案解析:该程序定义了一个`reverseString`函数,通过交换字符串首尾字符的方式实现字符串的反转。
复习题
【实验题1】程序填充:输入15个字符,统计英文字母、空格、数字和其它字
符的个数。源程序如下:
#include
void main( )
{ int i,digit,blank ,letter,other; /*line 3 */
char ch; /* 定义字符型变量ch*/
digit=blank=letter=other=0; /*line 5 */
printf("Enter 15 characters:");
i=1;
do{
ch = getchar() ; /*为ch输入一个字符*/
if (‘A’<=ch&&ch<=’Z’||‘a’<=ch&&ch<=’z’) /* ch是英文字母*/
letter++;
else if ( ‘0’<=ch&&ch<=’9’ ) /*ch是数字*/
digit++;
else if ( ch==’ ’ ) /*ch是空格*/
blank++;
else /*ch是其他字符*/
other++;
i= i+1;
}while( i<= 15 );
printf("digit=%d,letter=%d,blank=%d,other=%d\n", digit,letter,blank,other);
}
编译、连接并运行程序,输入a B5c&d*!221Gh? (注意a和B之间有一个空格)
则结果显示:
【实验题2】
阅读下列程序并回答问题,在每小题提供的若干可选答案中,挑选一个正确答案
【程序】
#include
int main()
{
int i, j, t, a[3][3] = {1,2,3,4,5,6,7,8,9};
for(i = 0; i < 3; i++)
for(j = 0; j <= i/2; j++){
t = a[i][j], a[i][j] = a[i][2-j], a[i][2-j] = t;
}
printf("%d\n", a[1][0]);
printf("%d\n", a[2][1]);
return 0;
}
程序运行时,第1行输出 6 。//答案有些问题
A、1 B、2 C、3 D、4
程序运行时,第2行输出 8 。
A、6 B、7 C、8 D、9
【实验题3】编程题
输入一批整数(以零或负数为结束标志),求奇数的乘积。
运行示例:
Enter an integer:1 2 3 4 5 -1
sum=15
【程序】
#include
int main()
{
int sum=1,t;
scanf("%d", &t);//输入第一个数
while(t > 0)//如果输入的数既不是0,也不是负数,则进入处理
{
if(t%2 != 0)
sum*=t;// sum*=t等价于sum=sum*t
scanf("%d", &t); //再输入第一个数
}
printf("sum=%d\n", sum);
return 0;
}
【实验题4】编程题
(1)定义函数f(n)计算n+(n+1)+(n+2)+……+(2n-1),函数返回值类型是double 。
(2)定义函数main(),输入正整n,计算并输出下列算式的值。要求调用函数
f(n)计算n+(n+1)+(n+2)+……+(2n-1)。
)12()1(154313211nnn
s
#include
double f(int n)//该函数求第i项对应的分母(实参i传递给形参n)
{
double sum = 0.0;
int i;
for(i=1; i<=n; i++)// n+(n+1)+(n+2)+……+(2n-1)改写为n+0+n+1+...+n+n-1
sum = sum + n+i-1;
return sum;
}
void main()
{
int i, n;
double s = 0.0;
scanf("%d", &n);
for(i=1; i<=n; i++)//一共要计算n项的和
s = s + 1.0/f(i);
printf("%f", s);
}