#define _CRT_SECURE_NO_WARNINGS 1
练习6-1
#include <stdio.h>
int min2(int a, int b)
{
return (a < b) ? a : b; //重要
}
int main()
{
int a, b;
puts("请输入两个整数:");
printf("整数a:"); scanf("%d", &a);
printf("整数b:"); scanf("%d", &b);
printf("较小的值是%d。\n", min2(a, b));
return 0;
}
练习6-2
#include <stdio.h>
int min3(int a, int b, int c)
{
int min = a;
if (b < min) min = b;
if (c < min) min = c;
return min;
}
int main()
{
int a, b, c;
puts("请输入三个整数:");
printf("整数a:"); scanf("%d", &a);
printf("整数b:"); scanf("%d", &b);
printf("整数c:"); scanf("%d", &c);
printf("最小的值是%d。\n", min3(a, b, c));
return 0;
}
练习6-3
#include <stdio.h>
int cube(int x)
{
return x * x * x;
}
int main()
{
int x;
printf("请输入一个数:");
scanf("%d", &x);
printf("它的立方是%d。\n", cube(x));
return 0;
}
练习6-4
#include <stdio.h>
int sqr(int x)
{
return x * x;
}
int pow4(int x)
{
return sqr(x) * sqr(x);
}
int main()
{
int x;
printf("请输入一个数:");
scanf("%d", &x);
printf("它的四次幂是%d。\n", pow4(x));
return 0;
}
练习6-5
#include <stdio.h>
int sumup(int n)
{
int sum = 0;
while (n > 0) //用while
{
sum += n;
n--;
}
return sum;
}
int main()
{
int x;
printf("请输入一个数:");
scanf("%d", &x);
printf("从1到%d之间所有整数的和是%d。\n", x, sumup(x));
return 0;
}
练习6-6
#include <stdio.h>
void alert(int ch, int n)
{
while (n-- > 0) //重要
{
putchar(ch);
Sleep(1000);
}
}
int main()
{
int x;
printf("请输入要发出响铃的次数:");
scanf("%d", &x);
alert('\a', x);
return 0;
}
练习6-7
#include <stdio.h>
void hello()
{
puts("你好。");
}
int main()
{
hello();
return 0;
}
练习6-8
#include <stdio.h>
#define NUMBER 8
int min_of(const int v[], int n)
{
int i;
int min = v[0];
for (i = 1; i < n; i++)
if (v[i] < min)
min = v[i];
return min;
}
int main()
{
int i;
int v[NUMBER] = { 0 };
for (i = 0; i < NUMBER; i++)
{
printf("v[%d] : ", i);
scanf("%d", &v[i]);
}
printf("最小值=%d\n", min_of(v, NUMBER));
return 0;
}
练习6-9
#include <stdio.h>
#define NUMBER 8
void rev_intary(int v[], int n)
{
int i;
for (i = 0; i < n / 2; i++)
{
int temp = v[i]; //重要
v[i] = v[n - 1 - i];
v[n - 1 - i] = temp;
}
}
int main()
{
int i;
int v[NUMBER] = { 0 };
for (i = 0; i < NUMBER; i++)
{
printf("v[%d] : ", i);
scanf("%d", &v[i]);
}
rev_intary(v, NUMBER);
puts("倒序排列:");
for (i = 0; i < NUMBER; i++)
printf("v[%d] = %d\n", i, v[i]);
return 0;
}
练习6-10
#include <stdio.h>
#define NUMBER 8
void intary_rcpy(int v1[], const int v2[], int n)
{
int i;
for (i = 0; i < n; i++)
{
v1[i] = v2[n - 1 - i]; //重要
}
}
int main()
{
int i;
int v1[NUMBER] = { 0 };
int v2[NUMBER] = { 0 };
for (i = 0; i < NUMBER; i++)
{
printf("v2[%d] : ", i);
scanf("%d", &v2[i]);
}
intary_rcpy(v1, v2, NUMBER);
for (i = 0; i < NUMBER; i++)
printf("v1[%d] = %d\n", i, v1[i]);
return 0;
}
练习6-11
#include<stdio.h>
#define NUMBER 7
int search_idx(const int v[], int idx[], int key, int n)
{
int i = 0;
int j = 0;
int sum = 0;
for (i = 0; i < n; i++)
{
if (v[i] == key)
{
idx[sum] = i; //重要
sum++;
}
}
return sum;
}
int main()
{
int v[NUMBER] = { 0 };
int idx[NUMBER];
int i;
for (i = 0; i < NUMBER; i++)
{
printf("v[%d] : ", i);
scanf("%d", &v[i]);
}
int k;
printf("请输入key:");
scanf("%d", &k);
int r = search_idx(v, idx, k, NUMBER);
for (i = 0; i < r; i++)
{
printf("%d ", idx[i]);
}
return 0;
}
练习6-12
#include<stdio.h>
void mat_mul(const int a[4][3], const int b[3][4], int c[3][3])
{
int i, j, k = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
c[i][j] = 0; //重要
for (k = 0; k < 4; k++)
{
c[i][j] += b[i][k] * a[k][j]; //重要
}
}
}
}
int main()
{
int a[4][3] = { 0 };
int b[3][4] = { 0 };
int c[3][3] = { 0 };
int i, j = 0;
printf("矩阵A:输入4*3 个数 以空格隔开:\n");
for (i = 0; i < 4; i++)
for (j = 0; j < 3; j++)
scanf("%d", &a[i][j]);
printf("矩阵B:输入3*4 个数 以空格隔开:\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
scanf("%d", &b[i][j]);
mat_mul(a, b, c);
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
printf("%d\t", c[i][j]);
printf("\n");
}
return 0;
}
练习6-13
#include <stdio.h>
void mat(const int a[4][3], const int b[4][3], int c[2][4][3])
{
int x, y, z;
for (x = 0; x < 2; x++)
{
for (y = 0; y < 4; y++)
{
for (z = 0; z < 3; z++)
{
if (x == 0)
{
c[x][y][z] = a[y][z];
}
else
c[x][y][z] = b[y][z];
}
}
}
}
void mat_print(const int m[4][3])
{
int i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
printf("%4d", m[i][j]);
putchar('\n');
}
}
void mat_print1(const int m[2][4][3])
{
int i, j, k;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 4; j++)
{
for (k = 0; k < 3; k++)
printf("%4d", m[i][j][k]);
putchar('\n');
}
}
}
int main()
{
int tensu1[4][3] = { {91, 63, 78}, {67, 72, 46}, {89, 34, 53}, {32, 54, 34} };
int tensu2[4][3] = { {97, 67, 82}, {73, 43, 46}, {97, 56, 21}, {85, 46, 35} };
int tensu[2][4][3];
mat(tensu1, tensu2, tensu);
puts("第一次考试的分数"); mat_print(tensu1);
puts("第二次考试的分数"); mat_print(tensu2);
puts("总分"); mat_print1(tensu);
return 0;
}
练习6-14
#include <stdio.h>
#define number 5
int main()
{
int i = 0;
static double v[number];
for (i = 0; i < number; i++)
{
printf("v[%d]=%.1f", i, v[i]);
putchar('\n');
}
return 0;
}
练习6-15
#include <stdio.h>
static i = 1;
void put_count()
{
//static i = 1;
printf("put_count:第%d次\n", i++); //注意
}
int main()
{
int i;
for (i = 0; i < 3; i++)
{
put_count();
}
return 0;
}