代码答案形式的C语言程序设计第五版谭浩强第八章例题
时间: 2023-08-18 07:42:44 浏览: 279
谭浩强例题C代码
以下是第五版《代码答案形式的C语言程序设计》第八章的例题答案:
8.1
```c
#include <stdio.h>
#define N 5
int main()
{
int a[N], max, i;
printf("Enter array a:\n");
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
max = a[0];
for (i = 1; i < N; i++)
if (a[i] > max) max = a[i];
printf("max=%d\n", max);
return 0;
}
```
8.2
```c
#include <stdio.h>
#define N 5
int main()
{
int a[N], i;
printf("Enter array a:\n");
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Reverse array a:\n");
for (i = N - 1; i >= 0; i--)
printf("%d ", a[i]);
printf("\n");
return 0;
}
```
8.3
```c
#include <stdio.h>
#define N 5
int main()
{
int a[N], i;
printf("Enter array a:\n");
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("a[0]=%d, a[%d]=%d\n", a[0], N - 1, a[N - 1]);
return 0;
}
```
8.4
```c
#include <stdio.h>
#define N 5
int main()
{
int a[N], i, sum = 0;
printf("Enter array a:\n");
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
for (i = 0; i < N; i++)
sum += a[i];
printf("sum=%d, average=%.2f\n", sum, (float)sum / N);
return 0;
}
```
8.5
```c
#include <stdio.h>
#define N 5
int main()
{
int a[N], i, temp;
printf("Enter array a:\n");
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
for (i = 0; i < N / 2; i++)
{
temp = a[i];
a[i] = a[N - i - 1];
a[N - i - 1] = temp;
}
printf("Reverse array a:\n");
for (i = 0; i < N; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
```
8.6
```c
#include <stdio.h>
#define N 5
int main()
{
int a[N], i, j, temp;
printf("Enter array a:\n");
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
for (i = 0; i < N - 1; i++)
for (j = i + 1; j < N; j++)
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
printf("Sort array a:\n");
for (i = 0; i < N; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
```
8.7
```c
#include <stdio.h>
#define N 5
int main()
{
int a[N], i, j, temp;
printf("Enter array a:\n");
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
for (i = 0; i < N - 1; i++)
for (j = 0; j < N - i - 1; j++)
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
printf("Sort array a:\n");
for (i = 0; i < N; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
```
8.8
```c
#include <stdio.h>
#define N 5
int main()
{
int a[N], i;
printf("Enter array a:\n");
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Max 5 numbers in array a:\n");
for (i = 0; i < 5; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
```
8.9
```c
#include <stdio.h>
#define N 5
int main()
{
int a[N], i, j, temp;
printf("Enter array a:\n");
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
for (i = 0; i < N - 1; i++)
for (j = 0; j < N - i - 1; j++)
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
printf("Median number in array a:\n");
printf("%d\n", a[N / 2]);
return 0;
}
```
8.10
```c
#include <stdio.h>
#define N 5
int main()
{
int a[N], i, j, temp;
printf("Enter array a:\n");
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
for (i = 0; i < N - 1; i++)
for (j = 0; j < N - i - 1; j++)
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
printf("Mode number in array a:\n");
for (i = 0; i < N; i++)
{
int count = 0, k;
for (j = i; j < N; j++)
if (a[j] == a[i]) count++;
for (k = i - 1; k >= 0; k--)
if (a[k] == a[i]) break;
if (k < 0 && count > 1) printf("%d ", a[i]);
}
printf("\n");
return 0;
}
```
阅读全文