本文最后更新于 2025年8月14日 星期四 10:51
编写一个程序:输入 a、b 和 c
的值,求这三个数的最大值和最小值。要求:把求最大值和最小值操作编写成一个函数,并使用指针作为形式参数把结果返回
main 函数。
函数原型建议如下:void fmaxmin(double, double, double, double* max, double* min);
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 <iostream> using namespace std;void fmaxmin (double , double , double , double *, double *) ;int main () { double a, b, c, maxim, minim; cin >> a >> b >> c; fmaxmin (a, b, c, &maxim, &minim); cout << "max = " << maxim << endl; cout << "min = " << minim << endl; return 0 ; }void fmaxmin (double x, double y, double z, double * p1, double * p2) { double u, v; if (x > y) { u = x; v = y; } else { u = y; v = x; } if (z > u) { u = z; } if (z < v) { v = z; } *p1 = u; *p2 = v; }
把以下程序中的 print() 函数改写为等价的递归函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <iostream> using namespace std;void print (int w) { for (int i = 1 ; i <= w; i++) { for (int j = 1 ; j <= i; j++) cout << i << " " ; cout << endl; } }int main () { print (5 ); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using namespace std;void print (int w) { if (w) { print (w - 1 ); for (int i = 1 ; i <= w; ++i) { cout << w << " " ; } cout << endl; } }int main () { print (5 ); }
以下程序用于输入一个矩阵的元素,并输出指定行的元素。请补充 inputAry
函数和 outputAry 函数。
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 <iostream> using namespace std;void inputAry (int * ary, int n) { cout << "输入" << n << "*" << n << "矩阵的各元素:\n" ; for (int i = 0 ; i < n * n; ++i) { cin >> ary[i]; } }void outputAry (const int * ary, int n, int k) { for (int i = 0 ; i < n; ++i) { cout << ary[n * k + i] << " " ; } cout << endl; }int main () { int *pa, n, k; cout << "输入矩阵的阶,n = " ; cin >> n; pa = new int [n * n]; inputAry (pa, n); cout << "输入行号,k = " ; cin >> k; outputAry (pa, n, k - 1 ); }
用随机函数产生 10
个互不相同的两位整数存放到一维数组中,并输出其中的素数。
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 #include <cstdlib> #include <ctime> #include <iostream> using namespace std;const int MAXN = 105 ;int a[15 ], prime[MAXN];bool existed[MAXN], vis[MAXN], p[MAXN];void findPrime (int n) { int cnt = 0 ; for (int i = 2 ; i <= n; ++i) { if (!vis[i]) { prime[cnt++] = i; p[i] = true ; } for (int j = 0 ; j < cnt && i * prime[j] <= n; ++j) { vis[i * prime[j]] = i; if (i % prime[j] == 0 ) { break ; } } } }int main () { findPrime (100 ); srand (int (time (NULL ))); for (int i = 1 ; i <= 10 ; ++i) { int temp = rand () % 90 + 10 ; while (existed[temp] == true ) { temp = rand () % 90 + 10 ; } existed[temp] = true ; a[i] = temp; } for (int i = 1 ; i <= 10 ; ++i) { cout << a[i] << " " ; } cout << endl; for (int i = 1 ; i <= 10 ; ++i) { if (p[a[i]]) { cout << a[i] << " " ; } } cout << endl; return 0 ; }
设计函数求一整型数组的最小元素及其下标。在主函数中定义和初始化该整型数组,调用该函数,并显示最小元素值和下标值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <iostream> using namespace std;int fMin (int a[], int size) { int minim = a[0 ], index = 0 ; for (int i = 1 ; i < size; ++i) if (a[i] < minim) { minim = a[i]; index = i; } return index; }int main () { int a[] = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }; int index; index = fMin (a, sizeof (a) / sizeof (int )); cout << "最小值:" << a[index] << endl; cout << "最小值下标:" << index << endl; return 0 ; }
编写程序,按照指定长度生成动态数组,用随机数对数组元素进行赋值,然后逆置该数组元素。例如,数组
A 的初值为 {6, 3, 7, 8, 2},逆置后的值为 {2, 8, 7, 3,
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 28 29 #include <cstdlib> #include <ctime> #include <iostream> using namespace std;int n;void write (int * p) { for (int i = 0 ; i < n; ++i) { cout << *(p + i) << " " ; } cout << endl; }int main () { int * p; cin >> n; p = new int [n]; srand (int (time (NULL ))); for (int i = 0 ; i < n; ++i) { *(p + i) = rand () % 10 ; } write (p); for (int i = 0 ; i < n / 2 ; ++i) { swap (*(p + i), *(p + n - i - 1 )); } write (p); return 0 ; }
编写如下两个功能的函数(不能调用 cstring 库中的字符串函数):
1)strCpy(char* str1, char* str2),功能:字符串拷贝函数,将
str2 字符串的内容拷贝到字符串 str1 处。
2)strCat(char* str1, char* str2),功能:字符串连接函数,将
str2 字符串的内容拷贝到字符串 str1 尾部。
在 main 函数中调用这两个函数以证明其功能正确。
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 #include <iostream> using namespace std;void strCpy (char * str1, char * str2) { char * p = str1; *p = *str2; while (*str2 != '\0' ) { ++p; ++str2; *p = *str2; } }void strCat (char * str1, char * str2) { char * p = str1; while (*p != '\0' ) ++p; *p = *str2; while (*str2 != '\0' ) { ++p; ++str2; *p = *str2; } }int main () { char str1[255 ], str2[255 ]; cout << "Input string 1: " ; cin >> str1; cout << "Input string 2: " ; cin >> str2; strCpy (str1, str2); cout << "Copied string: " << str1 << endl; strCat (str1, str2); cout << "Catenated string: " << str1 << endl; return 0 ; }