寫在 C 上。我已經撰寫了下一個代碼,但 3 個函式必須用 1 個替換,我不知道如何做。
該程式創建了 3 個陣列,但只有 1 個函式必須計算每列的負數并找到每列中的最大元素。代碼如下:
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int n = 0;
const int m = 3, k = 3, b = 4, u = 5;
int i, j;
void calc(float** array, int i, int j );
void calc1(float** array, int i, int j);
void calc2(float** array, int i, int j);
int main()
{
float** array = new float* [m];
for (int l = 0; l < m; l ) {
array[l] = new float[k];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < m; i ) {
for (int j = 0; j < k; j ) {
array[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < m; i )
{
for (int j = 0; j < k; j ) {
cout << setprecision(2) << setw(4) << array[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(array, i, j); // FUNCTION !!!
float** arr = new float* [b];
for (int l = 0; l < b; l ) {
arr[l] = new float[b];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < b; i ) {
for (int j = 0; j < b; j ) {
arr[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < b; i )
{
for (int j = 0; j < b; j ) {
cout << setprecision(2) << setw(4) << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(arr, i, j); // FUNCTION !!!
float** ar = new float* [u];
for (int l = 0; l < u; l ) {
ar[l] = new float[u];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < u; i ) {
for (int j = 0; j < u; j ) {
ar[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < u; i )
{
for (int j = 0; j < u; j ) {
cout << setprecision(2) << setw(4) << ar[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc2(ar, i, j); // FUNCTION !!!
}
void calc(float** array, int i, int j) {
int max = array[0][0];
for (int j = 0; j < k; j )
{
max = array[0][0];
for (int i = 0; i < k; i ) {
if (array[i][j] > max)
max = array[i][j];
if (array[i][j] < 0) {
n = 1;
}
}
cout << endl << "IN the [" << j 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j 1 << "] column is " << max << " maximal element" << endl;
}
}
void calc1(float** arr, int i, int j) {
int max = arr[0][0];
for (int j = 0; j < b; j )
{
max = arr[0][0];
for (int i = 0; i < b; i ) {
if (arr[i][j] > max)
max = arr[i][j];
if (arr[i][j] < 0) {
n = 1;
}
}
cout << endl << "IN the [" << j 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j 1 << "] column is " << max << " maximal element" << endl;
}
}
void calc2(float** ar, int i, int j) {
int max = ar[0][0];
for (int j = 0; j < u; j )
{
max = ar[0][0];
for (int i = 0; i < u; i ) {
if (ar[i][j] > max)
max = ar[i][j];
if (ar[i][j] < 0) {
n = 1;
}
}
cout << endl << "IN the [" << j 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j 1 << "] column is " << max << " maximal element" << endl;
}
}
就這樣。無用的資訊 看起來你的帖子主要是代碼;請添加更多詳細資訊。
uj5u.com熱心網友回復:
to 的引數calc()應該是陣列中的行數和列數。然后它應該使用這些作為for回圈中的限制。
此外,由于您正在計算每列的總負數和最大值,因此您必須每次通過列回圈重置這些變數。
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
const int m = 3, k = 3, b = 4, u = 5;
void calc(float** array, int rows, int cols);
int main()
{
float** array = new float* [m];
for (int l = 0; l < m; l ) {
array[l] = new float[k];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < m; i ) {
for (int j = 0; j < k; j ) {
array[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < m; i )
{
for (int j = 0; j < k; j ) {
cout << setprecision(2) << setw(4) << array[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(array, m, k); // FUNCTION !!!
float *arr = new float* [b];
for (int l = 0; l < b; l ) {
arr[l] = new float[b];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < b; i ) {
for (int j = 0; j < b; j ) {
arr[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < b; i )
{
for (int j = 0; j < b; j ) {
cout << setprecision(2) << setw(4) << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(arr, b, b); // FUNCTION !!!
float** ar = new float* [u];
for (int l = 0; l < u; l ) {
ar[l] = new float[u];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < u; i ) {
for (int j = 0; j < u; j ) {
ar[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < u; i )
{
for (int j = 0; j < u; j ) {
cout << setprecision(2) << setw(4) << ar[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(ar, u, u); // FUNCTION !!!
}
void calc(float** array, int rows, int cols) {
for (int j = 0; j < cols; j )
{
int n = 0;
int max = array[0][j];
for (int i = 1; i < rows; i ) {
if (array[i][j] > max)
max = array[i][j];
if (array[i][j] < 0) {
n = 1;
}
}
cout << endl << "IN the [" << j 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j 1 << "] column is " << max << " maximal element" << endl;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/338848.html
上一篇:Foreach回圈以檢查值
