从键盘上输入随机变量X 10个样本点值:X0,X1···· X9:
#include
#include //字符串流
#include //数学运算
using namespace std;
void Input_Function();
void Mean_Function();
void Standard_deviation();
const int MAX_SIZE = 10;
int Arr[MAX_SIZE];
int Arr_Sum = 0;
double Average = 0;
int Arr_size = 0;
int main() {
//输入处理
Input_Function();
//求平均值
Mean_Function();
//求方差
Standard_deviation();
return 0;
}
void Input_Function(){
int Temp = 0;
Arr_size = 0;
cout << "请输入数字(以空格分隔):"<< endl;
//读取整行输入
string input;
getline(cin,input);
//使用字符串串流解析整行输入
stringstream ss(input);
//输入数组操作
while (ss >> Temp){
if (Arr_size < MAX_SIZE){
Arr[Arr_size] = Temp;
Arr_size++;
} else{
cout << "数组已满,无法添加更多元素。" << endl;
break;
}
}
//打印数组并求和
cout << "输入数为:";
for (int i = 0; i < Arr_size; ++i) {
Arr_Sum += Arr[i];
cout << Arr[i] << " ";
}
cout << endl;
}
void Mean_Function(){
if (Arr_size > 0) {
Average = static_cast<double>(Arr_Sum) / Arr_size;
cout << "平均值为:" << Average << endl;
} else{
cout << "数组为空,无法计算平均值。" << endl;
}
}
void Standard_deviation(){
if (Arr_size > 1){
double variance = 0;
for (int i = 0; i < Arr_size; ++i) {
variance += pow(Arr[i] - Average,2);
}
variance /= (Arr_size - 1);
cout << "方差为:" << variance << endl;
} else{
cout << "数组元素不足以计算方差。" << endl;
}
}
以下三个问题要求用函数编写,从键盘输入一个4*4的矩阵A编程求:
该版本是用C++写的C语言代码,但存在一个问题,笔者不知道如何能很好使用C语言(这里应该用数组)来处理出现两个或者两个以上相同的最大值或者最小值。版本二采用C++的容器操作来解决此问题。
个人感觉版本一符合出题人本意,因为苏大机试大部分都是以C语言为主,这个题可能有点不严谨
#include
#include
using namespace std;
void Input_Function();
void Tran_Arr();
void New_Arr();
int Arr_A[4][4];
int Tran_Arr_A[4][4];
int Arr_B[4][4];
int Arr_AB[4][4];
int Max_element = INT_MIN; //整数类型最小值
int Min_element = INT_MAX; //整数类型最大值
int Max_row = 0;
int Max_column = 0;
int Min_row = 0;
int Min_column = 0;
int main() {
//矩阵A的最小值最大值及它们所在的行号和列号
Input_Function();
//矩阵A的转置
Tran_Arr();
//A*B
New_Arr();
system("pause");
return 0;
}
void Input_Function(){
int Temp = 0;
cout << "请输入4*4矩阵A的元素:" << endl;
//输入矩阵元素:
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
cout << "请输入第 " << (i + 1) << " 行,第 " << (j + 1) << " 列的元素:";
cin >> Arr_A[i][j];
//找最大值和最小值
Temp = Arr_A[i][j];
if (Temp > Max_element){
Max_element = Temp;
Max_row = i;
Max_column = j;
}
if (Temp <= Min_element) {
Min_element = Temp;
Min_row = i;
Min_column = j;
}
}
}
//输出矩阵
cout << "输入的矩阵为:" << endl;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
cout << Arr_A[i][j] << " ";
}
cout << endl;
}
// 输出最大值和最小值及其位置
cout << "矩阵A的最大值为 " << Max_element << ",位于第 " << (Max_row + 1) << " 行,第 " << (Max_column + 1) << " 列。" << endl;
cout << "矩阵A的最小值为 " << Min_element << ",位于第 " << (Min_row + 1) << " 行,第 " << (Min_column + 1) << " 列。" << endl;
}
void Tran_Arr(){
//矩阵A的转置
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
Tran_Arr_A[j][i] = Arr_A[i][j];
}
}
//输出矩阵A的转置
cout << "矩阵A的转置为:" << endl;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
cout << Tran_Arr_A[i][j] << " ";
}
cout << endl;
}
}
void New_Arr(){
//输入矩阵B
cout << "请输入4*4矩阵B的元素:" << endl;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
cout << "请输入第 " << (i + 1) << " 行,第 " << (j + 1) << " 列的元素:";
cin >> Arr_B[i][j];
}
}
//计算矩阵A和B的乘积
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
Arr_AB[i][j] = 0;
for (int k = 0; k < 4; ++k) {
Arr_AB[i][j] += Arr_A[i][k] * Arr_B[k][j];
}
}
}
// 输出矩阵乘积
cout << "矩阵 A 和 B 的乘积为:" << endl;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
cout << Arr_AB[i][j] << " ";
}
cout << endl;
}
}
#include
#include
#include
using namespace std;
void Input_Function();
void Tran_Arr();
void New_Arr();
int Arr_A[4][4];
int Tran_Arr_A[4][4];
int Arr_B[4][4];
int Arr_AB[4][4];
int Max_element = INT_MIN; //整数类型最小值
int Min_element = INT_MAX; //整数类型最大值
vector<pair<int, int>> Max_positions; // 存储最大值位置的向量
vector<pair<int, int>> Min_positions; // 存储最小值位置的向量
int main() {
//矩阵A的最小值最大值及它们所在的行号和列号
Input_Function();
//矩阵A的转置
Tran_Arr();
//A*B
New_Arr();
system("pause");
return 0;
}
void Input_Function(){
int Temp = 0;
cout << "请输入4*4矩阵A的元素:" << endl;
//输入矩阵元素:
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
cout << "请输入第 " << (i + 1) << " 行,第 " << (j + 1) << " 列的元素:";
cin >> Arr_A[i][j];
//找最大值和最小值
Temp = Arr_A[i][j];
if (Temp > Max_element) {
Max_element = Temp;
Max_positions.clear(); // 清空之前记录的最大值位置
Max_positions.push_back({i, j});
} else if (Temp == Max_element) {
Max_positions.push_back({i, j}); // 记录相同最大值的位置
}
if (Temp < Min_element) {
Min_element = Temp;
Min_positions.clear(); // 清空之前记录的最小值位置
Min_positions.push_back({i, j});
} else if (Temp == Min_element) {
Min_positions.push_back({i, j}); // 记录相同最小值的位置
}
}
}
//输出矩阵
cout << "输入的矩阵为:" << endl;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
cout << Arr_A[i][j] << " ";
}
cout << endl;
}
// 输出最大值和最小值及其位置
cout << "矩阵A的最大值为 " << Max_element << ",位于以下位置:" << endl;
for (const auto& pos : Max_positions) {
cout << "第 " << (pos.first + 1) << " 行,第 " << (pos.second + 1) << " 列" << endl;
}
cout << "矩阵A的最小值为 " << Min_element << ",位于以下位置:" << endl;
for (const auto& pos : Min_positions) {
cout << "第 " << (pos.first + 1) << " 行,第 " << (pos.second + 1) << " 列" << endl;
}
}
void Tran_Arr(){
//矩阵A的转置
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
Tran_Arr_A[j][i] = Arr_A[i][j];
}
}
//输出矩阵A的转置
cout << "矩阵A的转置为:" << endl;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
cout << Tran_Arr_A[i][j] << " ";
}
cout << endl;
}
}
void New_Arr(){
//输入矩阵B
cout << "请输入4*4矩阵B的元素:" << endl;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
cout << "请输入第 " << (i + 1) << " 行,第 " << (j + 1) << " 列的元素:";
cin >> Arr_B[i][j];
}
}
//计算矩阵A和B的乘积
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
Arr_AB[i][j] = 0;
for (int k = 0; k < 4; ++k) {
Arr_AB[i][j] += Arr_A[i][k] * Arr_B[k][j];
}
}
}
// 输出矩阵乘积
cout << "矩阵 A 和 B 的乘积为:" << endl;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
cout << Arr_AB[i][j] << " ";
}
cout << endl;
}
}
结果为版本二的
此代码为个人编写,题目来自互联网,使用平台为Clion,C++17标准。
由于博主才疏学浅,如有错误请多多指正,如有更好解法请多多交流!