大一下 C语言作业合集(EX01-EX11)

Hi, 由于文件存放年代久远,且我在当时学习过程中可能有改动,仅供参考。

EX01 Programming Environment and Basic Input/Output

1.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 as e2-1b. Please do not overwrite the first program.

1
2
3
4
5
6
7
8
#include <stdio.h>
int main ()
{
printf("This is my first program!\n\n\n\n");
getchar();

return 0;
}

1
2
3
4
5
6
7
8
#include <stdio.h>
int main ()
{
printf("This is my second program!\n\n\n\n");
getchar();

return 0;
}

2.Write a program that prints the number 1 to 4 on the same line. Write the program using the following methods:
(a)Using four “printf” statements.
(b)Using one “printf” statement with no conversion specifier (i.e. no ‘%’).
(c)Using one “printf” statement with four conversion specifiers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
#include<stdlib.h>

int main()
{
printf("1 ");
printf("2 ");
printf("3 ");
printf("4 \n");
// the first four statements are as the teacher asked.
printf("1 2 3 4\n");
//the second statement is for "using one printf".
int a=1,b=2,c=3,d=4;
printf("%d %d %d %d \n",a,b,c,d);
//the last statement is for conversion specifier.

system ("pause");
}

3.(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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>

int main()
{
int a, minutes;
printf("Please input the number of days from 0-15 \n");
scanf("%d",&a);
minutes = a * 24 * 60 ;
printf("the minutes is %d \n",minutes);
getchar();

return 0;

}

1
2
3
4
5
6
7
8
9
10
11
#include<stdio.h>
int main()
{
int a,hour;
a = 180;
hour = a / 60;
printf("180 minutes equal to %d hours",hour);

getchar();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
int main()
{
int a;
float hour;
a = 174;
hour = a / 60;
printf("180 minutes equal to %f hours",hour);

getchar();
return 0;
}

EX02 Data Types and Arithmetic Operations

  1. 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).
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int netprice;
    float salestax;
    netprice = 889;
    salestax = 889.00 * 0.06;
    float total;
    total = netprice + salestax;
    printf("The total purchase price is %f \n\n\n",total);
    system("pause");
    }

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 “”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Please type the radius of the circle \n");
float radius;
scanf("%f",&radius);
float diameter,circumference,area;
diameter = 2 * radius;
circumference = 2 * 3.14159 * radius;
area = 3.14159 * radius * radius;
printf("The diameter of the circle is %f \n",diameter);
printf("The circumference of the circle is %f \n",circumference);
printf("The area of the circle is %f \n",area);
system("pause");
}

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? 6000
Annual interest rate percentage? 4.25
Balance after one year: 6255

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Balance calculation program \n");
printf("Starting balance? ");
int balance;
scanf("%d \n",&balance);
printf("Annual interest rate percentage? ");
float rate;
scanf("&4.2f \n",rate);
float addbalance;
addbalance = balance + balance * rate * 0.01 ;
printf("Balance after one year : %f \n",addbalance);

system("pause");
}

EX03 Selection Structure

1.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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Please type your grades!\n");
int grade;
scanf("%d",&grade);
if ((grade >= 60)&&(grade <= 100))
printf("Pass\n");
else if ((grade >= 0)&&(grade <=59))
printf("Fail\n");
else
printf("Error\n");

system("pause");
}

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Please enter a integer number you want to test\n");
int num,tester;
scanf("%d",&num);
tester = num % 2;
if (tester == 0)
printf("Even\n");
else
printf("Odd\n");

system("pause");
}

3.Write a program that reads in three integers and then determines and prints the largest in the group.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,c,max;
printf("Please enter three integers to be determined \n ");
scanf("%d%d%d",&a,&b,&c);
max = a;
if (max <=b)
max = b;
if (max <=c)
max = c;
printf("max = %d \n",max);

system("pause");
}

EX04 ‘Switch’ Statement and Simple “While” Repetition Statement

  1. Write a program that reads three integers an abbreviated date (for example: 26 12 94) and that 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.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    printf("Please type in 3 integers to be converted, for example:26 12 94 \n");
    int day,month,year;
    scanf("%d%d%d",&day,&month,&year);
    switch(day)
    {
    case 1:
    printf("1st ");
    break;
    case 2:
    printf("2nd ");
    break;
    case 3:
    printf("3rd ");
    break;
    case 11:
    printf("11st ");
    break;
    case 12:
    printf("12nd ");
    break;
    case 13:
    printf("13rd ");
    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)
    {
    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;
    case 10:
    printf("October ");
    break;
    case 11:
    printf("November ");
    break;
    case 12:
    printf("December ");
    break;
    }
    if(year>=0 && year<=16)
    printf("20%d",year);
    else
    printf("19%d",year);

    system("pause");
    }

2.Write a C program that uses a while loop to calculate and print the sum of the even integers from 2 to 30.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i=2, sum=0;
while(i<=30)
{
sum = sum + i;
i = i + 2;
}
printf("The Sum from 0 to 30 is %d \n",sum);

system("pause");
}

  1. A large chemical company pays its sales staff on a commission basis. They receive £200 per week 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.00
    Salary is: 650.00

    Enter sales in pounds (-1 to end): 00.00
    Salary is: 200.00

    Enter sales in pounds (-1 to end): 1088.89
    Salary is: 298.00

    Enter sales in pounds (-1 to end): -1

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    #define CUTOFF -1
    float sales,salary;
    sales = 0;
    while (sales > CUTOFF)
    {
    printf("Please type in your gross sales for this week in pounds (-1 to end):");
    scanf("%f",&sales);
    salary = 200 + sales * 0.09 ;
    if(sales>CUTOFF)
    printf("Salary is %6.2f \n",salary);
    }
    system("pause");
    }

Optional:

  1. A mail order company sells five different products whose retail prices are shown in the following table:
    Product Number Retail Price (in pounds)
    1 2.98
    2 4.50
    3 9.98
    4 4.49
    5 6.87
    Write a C program that reads in a series of pairs of numbers as follows:
    (1). Product number
    (2). Quantity sold for one day
    Your 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).
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int numday,number,quantity;
    float total;
    numday = 1;

    while(numday<=7)
    {
    printf("Please type in the product number and the quantity sold for day %d ",numday);
    scanf("%d%d",&number,&quantity);
    numday = numday + 1;
    switch(number)
    {
    case 1:
    total = total + 2.98 * quantity;
    break;
    case 2:
    total = total + 4.50 * quantity;
    break;
    case 3:
    total = total + 9.98 * quantity;
    break;
    case 4:
    total = total + 4.49 * quantity;
    break;
    case 5:
    total = total + 6.87 * quantity;
    break;
    default:
    printf("Error!\n");
    }

    }
    printf("The total retail value of all products sold in this week is %6.2f \n",total);

    system("pause");
    }

EX05 For and Do…while Repetition Statements

  1. 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 2^1, 2^2, …, 2^10). Use a for loop to do the same.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int i = 1;
    int j = 1;

    do//This is the first loop for DO/WHILE repetiton.
    {
    j = 2 * j ;
    i++;
    printf("%d ",j);
    }while(i<=10);

    printf("\n");//This is to turn to the new line.

    for(int i=1,j=1 ; i<=10;i++)//This is the second loop for FOR repetiton.
    {
    j = 2 * j ;
    printf("%d ",j);
    }

    system("pause");
    }
  2. The constant can be calculated by the infinite series:

    Pi = 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 if the user enters ‘3’, then the program should calculate Pi as being 4 - 4/3 + 4/5.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int term;
    int i = 1, j = 1;
    float Pi = 0;
    float k;

    printf("Please type in how mant terms in the series should be used.");
    scanf("%d",&term);
    do
    {
    k=(float)4/(2*i-1)*j*1.00;
    Pi=Pi+k;
    j=j*(-1);
    i++;
    }
    while(i<=term);
    printf("%f",Pi);

    system("pause");
    }
  3. 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.
    编辑器阵亡,详情见题目。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int n=5,t=1,h=10;
    while(t<=n)
    {
    int i=1;
    while(i<h+t)
    {
    if(i<=h-t)
    {
    printf(" ");
    }
    else if(i>h-t&&i<h+t)
    {
    printf("*");
    }
    i++;
    }
    printf("\n");
    t++;
    }
    t=4;h=10;
    while(t>0)
    {
    int i=1;
    while(i<h+t)
    {
    if(i<=h-t)
    {
    printf(" ");
    }
    else if(i>h-t&&i<h+t)
    {
    printf("*");
    }
    i++;
    }
    printf("\n");
    t--;
    }

    system("pause");
    }
  4. Write a program to print a table as follows:

11= 1
2
1= 2 22= 4
3
1= 3 32= 6 33= 9
….
91= 9 92=18 93=27 94=36 95=45 96=54 97=63 98=72 9*9=81

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i=1,j=1;

for(i=1;j<=9;i++)
{
for(j=1;j<=i;j++)
{
printf("%d*%d=%d ",i,j,i*j);
}
printf("\n");
}

system("pause");
}

EX06 Simple Functions

1.Write a C program that reads several numbers and uses the function round_to_nearest to round each of these numbers to the nearest integer. The program should print both the original number and the rounded number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<stdio.h>
#include<stdlib.h>

int round_to_nearest(float);

int main()
{
float num_a;int num_b;
num_a=1.0;
while(num_a != 0)
{
printf("please type in the number you want to round,and enter 0 to quit ");
scanf("%f",&num_a);
if (num_a==0)
printf("This program is about to close,please press any key to quit \n");
else
{
num_b = round_to_nearest(num_a);
printf("the original number is %f ,and the rounded number is %d \n",num_a,num_b);
}
}
system("pause");


}
int round_to_nearest(float num_a)//Method two
{
return (int) (num_a + 0.5);
}


/*
int round_to_nearest(float num_a)//Method One
{ int num,num_b;
num=(int)num_a;
if (num-num_a>0.5 || num-num_a<-0.5)
num_b=num+1;
else
num_b=num;
return num_b;
}
*/

2.Write a program that reads three pairs of numbers and adds the larger of the first pair, the larger of the second pair and the larger of the third pair. Use a function to return the larger of each pair.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<stdio.h>
#include<stdlib.h>

float larger_number(float,float);

int main()
{
float num1,num2,larger,total=0;
int a=1;
while (a<=3)
{
printf("Please type in a pair of numbers ");
scanf("%f%f",&num1,&num2);
larger=larger_number(num1,num2);
total=total+larger;
printf("The larger one in this pair of numbers is %f\n",larger);
a++;
}
printf("The total of three pairs of numbers is %f \n",total);

system("pause");
}
float larger_number(float num1,float num2)
{
float larger;
if(num1>num2)
larger=num1;
else
larger=num2;
return larger;
}

  1. A car park charges a £2.00 minimum fee to park for up to 3 hours, and an additional £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 Charge
    1 1.5 2.00
    2 4.0 2.50
    3 24.0 10.00
    TOTAL 29.5 14.50

    The program should use the function calculate_charges to determine the charge for each customer.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    #include<stdio.h>
    #include<stdlib.h>

    float calculate_charges(float);

    int main()
    {
    float hour1,hour2,hour3,charge1,charge2,charge3,total_hours=0,total_charges=0;

    printf("Please type in three car's parking hours (from 0 to 24) \n");
    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);
    total_hours=hour1+hour2+hour3;
    total_charges=charge1+charge2+charge3;
    printf("TOTAL%7.2f%9.2f\n",total_hours,total_charges);




    system("pause");
    }

    float calculate_charges(float hour)
    {
    float charge;
    if(hour>0&&hour<=3)
    charge=2;
    else if(hour>3&&hour<=19)
    charge= 2.0+0.5*(hour-3);
    else if(hour>19&&hour<=24)
    charge=10;
    else
    printf("Unvalid value of \"hour\" \n");
    return charge;
    }

EX07 More Functions

  1. 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, and 0 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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #include<stdio.h>
    #include<stdlib.h>
    int even(int);
    int main()
    {
    int num;
    printf("Please input an integer: (Crtl+Z or -1 to stop)\n");
    num=1;
    while(scanf("%d", &num)!=EOF)
    {
    if(even(num)==1)
    printf("%d is an even number \n",num);
    else if(even(num)==0)
    printf("Sorry, %d is not an even number,try again!\n",num);
    }
    system("pause");
    }

    int even(int num)
    {
    return (num+1)%2;
    }
  2. Write a C program that uses the function integerPower1(base, exponent) to return the value of:

    base^exponent
    

    so 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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    int integerPower1(int,int);
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    { int base,exponent,integerPower1(int,int);
    printf("Please type in the base and exponent:\n");
    scanf("%d%d",&base,&exponent);
    printf("The result is %d\n",integerPower1(base,exponent));

    system("pause");
    }
    int integerPower1(int base,int exponent)
    {
    int ans=1;
    for(int i=1;base>0&&exponent>0&&i<=exponent;i++)
    ans=ans*base;
    return ans;
    }
  3. Write a C program that uses the recursive function integerPower2(base, exponent) to return the value of:

    base^exponent
    

    so 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 - 1
    

    and the base case will be when exponent is 1 since : base1 = base.)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int base, exponent, integerPower2(int, int);
    printf("Please input base and exponent:\n");
    scanf("%d %d", &base, &exponent);
    printf("Result is %d\n", integerPower2(base,exponent));



    system("pause");
    }
    int integerPower2(int m, int n)
    {
    if (n==1)
    { return m; }
    else
    {
    return m * integerPower2(m, n - 1);
    }
    }

EX08 Arrays

1.Write a program that reads ten numbers supplied by the user into a single subscripted array, and then prints out the average of them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
#include<stdlib.h>
int main()
{
int numbers[10],sum=0;
float average=0;
for(int i =0;i<10;i++)
{
printf("Please type in an interger:\n");
scanf("%d",&numbers[i]);
}
for (int i=0;i<10;i++)
{average=average+numbers[i];}

printf("Average is %f\n",average/10);

system("pause");
}

  1. 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 array
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int numbers[2][5],max1,min1,max2,min2,maxall,minall;
    for(int i=0;i<2;i++)
    {
    for(int j=0;j<5;j++)
    { printf("Please type in the integer:");
    scanf("%d",&numbers[i][j]);
    }
    printf("\n");}

    for(int i=0;i<2;i++)
    {
    for(int j=0;j<5;j++)
    { printf("%d",numbers[i][j]);
    }
    printf("\n");}

    max1=min1=numbers[0][0];
    for(int j=0;j<5;j++)
    {
    max1=max1>numbers[0][j]? max1:numbers[0][j];
    min1=min1<numbers[0][j]? min1:numbers[0][j];
    }
    max2=min2=numbers[1][0];
    for(int j=0;j<5;j++)
    {
    max2=max2>numbers[1][j]? max2:numbers[1][j];
    min2=min2<numbers[1][j]? min2:numbers[1][j];
    }

    maxall=max1>max2? max1:max2;
    minall=min1<min2? min1:min2;
    printf("The maximum in row 1 is %d\n", max1);
    printf("The minimum in row i is %d\n", min1);
    printf("The maximum in row 2 is %d\n", max2);
    printf("The minimum in row 2 is %d\n", min2);
    printf("The maximum is %d\n", maxall);
    printf("The minimum is %d\n", minall);
    system("pause");
    }

3.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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i=0,duplicate=0,a[20];
do{
printf("Please type an interger between 10 and 100 inclusively, or it will not be printed \n");
scanf("%d",&a[i]);
if(a[i]>=10&&a[i]<=100)
{
i++;
continue;
}
for(int j=0;j<i;j++)
if(a[i]==a[j])
{
duplicate=1;break;
}
else
continue;
if(duplicate==0)
{printf("%d\n",a[i]);
}
else
duplicate=0;i++;

}while(i<20);
system("pause");
}

EX09 More Arrays

1.Write a program that enters 5 names of towns and their respective distance (an integer) 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
#include<stdlib.h>
main()
{
int distance[5];
char town[5][20];
int i=0;
for(i=0;i<5;i++)
{
printf("Please type in the name of town:\n");
// gets(town[i]);
scanf("%s",town[i]);
printf("Please type in its distance to London:\n");
scanf("%d",&distance[i]);
}
for(i=0;i<5;i++)
{
if(distance[i]<100)
printf("The distance from %s to London is less than 100 miles.\n",town[i]);
}

system("pause");
}

  1. Write a program that prompts the user to type in four character strings, places these 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)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    #include<stdio.h>
    #include<stdlib.h>
    main()
    {
    char string[4][20];
    printf("Please type in 4 strings.\n");
    scanf("%s %s %s %s",string[0],string[1],string[2],string[3]);

    for(int i=3;i>=0;i--)
    {
    printf("%s ",string[i]);}
    printf("\n ");

    for(int i=0;i<4;i++)
    {
    int j;
    for(j=0;string[i][j]!='\0';j++);
    for(j=j-1;j>=0;j--)
    printf("%c",string[i][j]);
    printf(" "); }
    printf("\n");

    for(int i=3;i>=0;i--)
    {
    int j;
    for(j=0;string[i][j]!='\0';j++);
    for(j=j-1;j>=0;j--)
    printf("%c",string[i][j]);
    printf(" "); }
    printf("\n");

    system("pause");
    }

    /*for(int i=3;i>=0;i--)
    {
    for(int j=4;j>=0;j--)
    {
    if(string[i][j]!='\0')
    printf("%c",string[i][j]);
    }
    printf(" ");
    }
    printf("\n ");*/

EX10 Pointers

  1. Write a program that reads 5 integers into an array, and then uses four different methods of accessing the members of an array to print them out in reverse order.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int number[5],*NPtr;
    NPtr=number;
    printf("Please type in 5 integers:");

    for(int i=0;i<5;i++)
    {
    scanf("%d",&number[i]);
    }
    printf("\n");
    printf("METHOD ONE\n");
    for(int i=4;i>=0;i--)
    {
    printf("%d ",number[i]);
    }
    printf("\n");
    printf("METHOD TWO\n");
    for(int i=4;i>=0;i--)
    {
    printf("%d ",NPtr[i]);
    }
    printf("\n");
    printf("METHOD THREE\n");
    for(int i=4;i>=0;i--)
    {
    printf("%d ",*(number+i));
    }
    printf("\n");
    printf("METHOD FOUR\n");
    for(int i=4;i>=0;i--)
    {
    printf("%d ",*(NPtr+i));
    }
    printf("\n");

    system("pause");
    }
  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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    float f[8],*fPtr,total=0;
    fPtr=f;
    printf("Please type in 8 floats:");
    for(int i=0;i<8;i++)
    {
    scanf("%f",&f[i]);
    }
    printf("The second, fourth, sixth and eighth members of the array are as followed:\n");
    for(int i=1;i<8;i+=2)
    {
    printf("%f ",*(fPtr+i));
    }
    printf("\n");
    for(int i=0;i<8;i+=2)
    {
    total=total+*(fPtr+i);
    }
    printf("and the sum of the first, third, fifth and seventh is: %f \n",total);

    system("pause");
    }
  3. Write a program that use a SINGLE FUNCTION (用一个函数)to find and return simultaneously both the lowest and highest values in an array of type int. Suppose the size of the array is 6.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    #include<stdio.h>
    #include<stdlib.h>

    void SingleFunction(int *,int *);
    int main()
    {
    int num[6],i;
    printf("Please type in 6 numbers: \n");
    for(i=0;i<6;i++)
    {
    scanf("%d",&num[i]);
    }
    SingleFunction(num,num);
    system("pause");
    }
    void SingleFunction(int *max,int *min)
    {
    int maxnum,minnum;
    maxnum=*max;
    minnum=*min;
    for(int i=0;i<6;i++)
    {
    maxnum=maxnum >= *(max+i) ? maxnum : *(max+i);
    minnum=minnum <= *(min+i) ? minnum : *(min+i);
    }
    printf("The max is %d, and the min is %d\n",maxnum,minnum);
    }

EX11 Files

  1. Write a program that enters the name and the annual salary of 5 employees from the keyboard, calculates the monthly salary of each employee assuming that tax is deducted at a flat rate of 20%. The program prints the name, the annual salary and the monthly payment (net) of each employee in a file. The program adds an employee’s number to each employee before printing the names and values:
    No. Employee’s Name Annual salary Monthly payment after tax
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    float annualsalary[5],monthlysalary[5];
    char employee[5][20];
    FILE *fptr;

    if((fptr=fopen("salary.txt","w"))==NULL)
    printf("Error in opening the file \n");
    else
    {
    for(int i=0;i<5;i++)
    {
    printf("Please type in employee's name and annual salary:\n");
    scanf("%s%f",&employee[i],&annualsalary[i]);}
    }
    fprintf(fptr,"No. Employee's Name Annual salary Monthly payment after tax\n");
    for(int i=0;i<5;i++)
    {
    monthlysalary[i]=annualsalary[i]/12*0.8;
    fprintf(fptr," %-4d %-10s %13.3f%25.3f\n",i+1,employee[i],annualsalary[i],monthlysalary[i]);
    }
    fclose(fptr);
    printf("OK. The program has printed the name, the annual salary and the monthly payment of each employee in a file named salary.txt \n");

    system("pause");
    }

2.Write a program that builds a file with information for 20 towns. Each line in the file contains name of a town followed by the distance of the town from London in miles (an integer).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
#include<stdlib.h>
int main()
{
int distance[20];
char town[20][20];
FILE *fp;
if((fp=fopen("town.txt","w"))==NULL)
printf("Error in opening the file \n");
else
{
for(int i=0;i<20;i++)
{
printf("Please type in No.%2d town's name and its distance from London:\n",i+1);
scanf("%s %d",&town[i],&distance[i]);
}
fprintf(fp,"No. Town's Name Distance from London \n");
for(int i=0;i<20;i++)
{
fprintf(fp,"%2d %11s %20d \n",i+1,town[i],distance[i]);
}
}
fclose(fp);
printf("OK. The program has built a file named town.txt with information for 20 towns. \n");

system("pause");
}

3.Write a program reads the file created by the above program (in Question 2) and prints the names of the towns that are less than 100 miles from London to ANOTHER file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<stdio.h>
#include<stdlib.h>
int main()
{
int distance[20],a;
char town[20][20];
FILE *input,*output;

output=fopen("townanother.txt","w");
if((input=fopen("town.txt","r"))==NULL)
printf("Error in opening the file \n");
else
{
fseek(input, 41, SEEK_SET);
fprintf(output, "No. Town's Name Distance from London\n");
for(int i = 0; i < 20; i++)
{
fscanf(input, "%2d %11s %20d \n", &a, &town[i], &distance[i]);
if(distance[i] < 100)
{
fprintf(output, "%2d %11s %20d \n", i + 1, town[i], distance[i]);
}
}
printf("OK. It has printed the names of the towns that are less than 100 miles from London to another file.\n");
}
fclose(input);
fclose(output);
system("pause");
}

好啦,以上是2016年C语言课程的所有练习题,大作业的代码咱们下次再见~