C语言作业EX08&EX09(修改一次,解决gets会吃上一个\n的问题)

这就是编程的颜色!

Exercise 8: 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
    #include <stdio.h>
    int main()
    {
    int i,j;
    float sum=0,num[10];
    printf("Please Enter Ten Numbers:");
    for(i=0;i<10;i++)
    scanf("%f",&num[i]);
    for(i=0;i<10;i++)
    sum+=num[i];

    printf("The average of them is %f",sum/10);
    return 0;

    }
  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 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>
    int main()
    {
    float nums[2][5];
    int i,j;
    float maxrow1=0,minrow1=0,maxrow2=0,minrow2=0;
    for(j=0;j<2;j++)
    for(i=0;i<5;i++)
    scanf("%f",&nums[j][i]);
    maxrow1=minrow1=nums[0][0];
    maxrow2=minrow2=nums[1][0];
    for(i=0;i<5;i++)
    {
    if(maxrow1<nums[0][i])
    maxrow1=nums[0][i];
    if(maxrow2<nums[1][i])
    maxrow2=nums[1][i];
    if(minrow1>nums[0][i])
    minrow1=nums[0][i];
    if(minrow2>nums[1][i])
    minrow2=nums[1][i];
    }
    printf("The maximun and minimun of the value in row one are:");
    printf("%f %f\n",maxrow1,minrow1);

    printf("The maximun and minimun of the value in row two are:");
    printf("%f %f\n",maxrow2,minrow2); //这个是(a)

    printf("The maximum of the whole array is: ");
    if(maxrow1>=maxrow2)
    printf("%f",maxrow1);
    else printf("%f",maxrow2);

    printf("\nThe minimum of the whole array is: ");
    if(minrow1>=minrow2)
    printf("%f",minrow2);
    else printf("%f",minrow1); //这个是(b)
    //不知道是分开写还是合着写
    return 0;


    }

    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
    30
    31
    #include <stdio.h>
    int i;
    int nums[20];
    int repeat(int num);
    int main()
    {
    int a=0;
    for(i=0;i<20;i++)
    {
    do{
    printf("\nPlease Enter 20 integer Numbers between 10 and 100:");
    scanf("%d",&nums[i]);
    }while(nums[i]>100||nums[i]<10);
    a = repeat(nums[i]);
    if(a==1)
    printf("%d\n",nums[i]);
    }
    return 0;

    }

    int repeat(int num){
    int j;
    for(j=0;j<i;j++)
    {
    if(num==nums[j])
    return 0;
    }
    return 1;
    }

Exercise 9: More Arrays – String

  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
    24
    #include <stdio.h>
    int less100(int miles);
    int main()
    {
    char name[10000];
    int distance[5];
    int i,a=0;
    for(i=0;i<5;i++)
    {
    printf("Please Enter Name And Distance From London Respectively: ");
    scanf("%s %d",&name,&distance[i]);
    a = less100(distance[i]);
    if (a == 1)
    printf("\n%s is less than 100 miles from London\n\n",name);
    }
    return 0;
    }

    int less100(int miles){
    if (miles < 100)
    return 1;
    return 0;
    }

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
#include <stdio.h>
//如果城镇中有空格的存在,这个可以读取,但是上一个不行
int less100(int miles);
int main()
{
char name[10000];
int distance[5];
int i,a=0;

for(i=0;i<5;i++)
{
printf("Please Enter Name : ");
gets(name);
printf("\n");
fflush(stdin); //清空缓冲区
printf("Please Enter The Distance From London : ");
scanf("%d",&distance[i]);
printf("\n");
fflush(stdin); //清空缓冲区
a = less100(distance[i]);
if (a == 1)
printf("%s\n\n",name);
}
return 0;
}

int less100(int miles){
if (miles < 100)
return 1;
return 0;
}

  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)
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
int main()
{
char strings1[100],strings2[100],strings3[100],strings4[100];
printf("Please type in four character strings\n");
scanf("%s %s %s %s",&strings1,&strings2,&strings3,&strings4);
printf("%s %s %s %s\n",strings4,strings3,strings2,strings1);


return 0;
}

(ii) The four strings in the original order, but with each string backwards. (e.g. I ma reteP naP)
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
#include <stdio.h>
#include <string.h>
//取长度的函数strlen需要用到这个头文件
char *rev(char *string);
char result[100];//定义result为全局变量
int main()
{
int i;
char string[100];
printf("Please type in four character strings\n");
for(i=0;i<4;i++)
{
scanf("%s",&string);
rev(string);
printf("%s ",result);
}
printf("\n");
return 0;
}

char *rev(char *str){
//注意定义为地址,用指针回传,但是要回传一次就输出,无法储存后再整体输出
int len,i;
len = strlen(str);
for(i=0;i<len;i++)
result[i]=str[len-i-1];
result[i]='\0';
return result;
}

(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
#include <stdio.h>
#include <string.h>
//这个写的有点啰嗦
char *rev(char *string);
char result[100];
int main()
{
int i;
char string1[100],string2[100],string3[100],string4[100];
char *a,*b,*c,*d;
printf("Please type in four character strings\n");
scanf("%s %s %s %s",&string1,&string2,&string3,&string4);
printf("%s ",rev(string4));
printf("%s ",rev(string3));
printf("%s ",rev(string2));
printf("%s\n",rev(string1));
return 0;
}

char* rev(char* str){
int len,i;
len = strlen(str);
for(i=0;i<len;i++)
result[i]=str[len-i-1];
result[i]='\0';
return result;


}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <string.h>
//这个我比较喜欢
int main()
{
char result[100],str[100];
int len,i;
printf("Please type in four character strings :");
gets(str);
len = strlen(str);
for(i=0;i<len;i++)
result[i]=str[len-i-1];
result[i]='\0';
puts(result);
return 0;
}