C语言作业EX08&EX09(修改一次,解决gets会吃上一个\n的问题)
这就是编程的颜色!
Exercise 8: Arrays
(这个太简单就不写注释了)
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;
}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 array1
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
- 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 |
|
- 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 |
|
(ii) The four strings in the original order, but with each string backwards. (e.g. I ma reteP naP)
1 |
|
(iii) The four strings in reverse order with each string backwards. (e.g. naP reteP ma I)
1 |
|
1 |
|
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!