2021/12/10
1.编写函数int fun(int x, int *p)
功能是:求出能整除x且不是偶数的各整数,并按照从小到大的顺序放在p指向的内存中,函数返回值为这些整数的个数。
例如: x的值为30,则数组中的数应为1,3,5,15,函数返回值为4。
#include<iostream>
using namespace std;
int fun(int x, int* p) {
int n = 0;
for (int i = 3; i < x; i=i+2) {
if (x % i == 0) {
*p = i;
n = n + 1;
p = p + 1;
}
}
return n;
}
int main() {
int a[1000] = { 0 };
int x = 0;
cin >> x;
cout << "数组中的数为:" ;
cout << "1" << " ";
for (int i = 0; i < fun(x,a) ; i++) {
cout << a[i] << " ";
}
cout << endl;
cout<<"这些整数的个数为:"<<fun(x, a)+1;
return 0;
}
2.实现一个3*4的整型矩阵的转置。
要求:矩阵元素终端输入;转置功能另写一个函数实现;指针实现元素互换。
#include<iostream>
#include<iomanip>
using namespace std;
void f(int(* a)[4], int x = 4, int y = 4) {
int b;
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
b = *(*(a + i) + j);
*(*(a + i) + j) = *(*(a + j) + i);
*(*(a + j) + i) = b;
}
}
}
int main() {
int a[4][4] = { 0 };
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cin >> a[i][j];
}
}
cout << "原来的矩阵为:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << setw(4)<< a[i][j] ;
}
cout << endl;
}
f(a);
cout << "转置的矩阵为:" << endl;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
cout << setw(4) << a[i][j];
}
cout << endl;
}
return 0;
}
3.以下代码功能为“求解二维数组a[3][4]的平均值”。补充函数部分代码。注:函数fun1()与fun2()实现的功能相同,aver1=aver2。
int main()
{float score[3][4]={ {65,67,70 ,60}, {80,87,90,81}, {90,99,100,98} };
cout<<"aver1="<<fun1(score,3)<<endl;
cout<<"aver2="<<fun2(*score,12)<<endl;
return 0;
}
#include<iostream>
using namespace std;
float fun1(float (*a)[4], int x = 3) {
float sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
sum = sum + a[i][j];
}
}
return sum / 12;
}
float fun2(float a[4], int n = 12) {
float sum = 0;
for (int i = 0; i < 12; i++) {
sum = sum + *(a + i);
}
return sum / 12;
}
int main(){
float score[3][4] = { {65,67,70 ,60}, {80,87,90,81}, {90,99,100,98} };
cout << "aver1=" << fun1(score, 3) << endl;
cout << "aver2=" << fun2(*score, 12) << endl; \
return 0;
}
4.课后题4
#include<iostream>
using namespace std;
void f(int* a, int n, int m) {
int b[10000];
for (int i = 0; i < n; i++) {
b[(i + m ) % n] = a[i];
}
for (int i = 0; i < n; i++) {
a[i] = b[i];
}
}
int main() {
int n = 0;
int m = 0;
int a[10000] = { 0 };
int b[10000] = { 0 };
cout << "输入整数个数:";
cin >> n;
cout << "输入后移位数:";
cin >> m;
cout << "输入" << n << "个整数:";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
f(a,n,m);
cout << "调整后的" << n << "数为:";
for (int i = 0; i < n; i++) {
cout << a[i] <<" ";
}
return 0;
}
5.课后题5
#include<iostream>
using namespace std;
int main() {
int n = 0, x = 0, k = 0, m = 3;
int a[10000];
int* p = a;
cin >> n;
x = n;
for (int i = 0; i < n; i++) {
a[i] = 1;
}
while (x > 1) {
for (int i = 0; i < n; i++) {
if (*(a + i) != 0) {
k = k + 1;
}
if (k == m) {
*(a + i) = 0;
x = x - 1;
k = 0;
}
}
}
for (int i = 0; i < n; i++) {
if (*(a + i) != 0) {
cout << "留下的人排在第" << i+1 << "号" << endl;
}
}
return 0;
}
6.在主函数中输入5个字符串,另写一个函数实现它们的排序,并在主函数中输出排序后的字符串。
要求:用指针数组实现,考虑字符串不等长的情况。
#include<iostream>
using namespace std;
void f(char a[][80]) {
char* b[5];
char* temp;
int x = 0;
for (int i = 0; i < 5; i++) {
b[i] = a[i];
}
for (int i = 0; i < 4; i++) {
x = i;
for (int j = i + 1; j < 5; j++) {
if (strcmp(b[x],b[j])>0) {
x = j;
}
}
if (x != i){
temp = b[x];
b[x] = b[i];
b[i] = temp;
}
}
for (int i = 0; i < 5; i++) {
cout << b[i] << endl;
}
}
int main() {
char a[5][80];
for (int i = 0; i < 5; i++) {
cin >> a[i];
}
f(a);
return 0;
}
7.课后题13
#include<iostream>
#include<cmath>
using namespace std;
float f(float (*p)(float), float a, float b, int n) {
int i;
float x, h, s;
h = (b - a) / n;
x = a;
s = 0;
for (i = 1; i <= n; i++) {
x = x + h;
s = s + (*p)(x) * h;
}
return s;
}
int main() {
float a1, b1, a2, b2, a3, b3, (*p)(float), c;
int n = 20;
cin >> a1 >> b1;
cin >> a2 >> b2;
cin >> a3 >> b3;
p = sin;
c = f(p, a1, b1, n);
cout << "sin(x)" << "从" << a1 << "到" << b1 << "的定积分为:" << c << endl;
p = cos;
c = f(p, a2, b2, n);
cout << "cos(x)" << "从" << a2 << "到" << b2 << "的定积分为:" << c << endl;
p = exp;
c = f(p, a3, b3, n);
cout << "exp(x)" << "从" << a3 << "到" << b3 << "的定积分为:" << c << endl;
return 0;
}
实验体会:
- 注意指针的初始位置
- 交换地址时候注意交换顺序
- 注意输出时是否输出更改后的内容
- 注意顺序的更迭
- 注意指针定义的意义