如何打印这个数组的四个角?我怎么知道这些列是否是严格升序的

How can I print the four corners of this array? and How do I find out if the columns are strictly ascending

本文关键字:我怎么知道 升序 是否是 何打印 打印 数组 四个      更新时间:2023-10-16
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main(int argc, char *argv[]) {
   ofstream outData;
   ifstream inData;
   string inString;
   int matrix[12][12];
   int rowSize, colSize;
   inData.open("C:\Users\JSU\Documents\ArrayInput.txt");
    inData >> rowSize >> colSize;
    cout << "Row= " << rowSize << "t Col= "<<colSize<< endl;
    for (int r=0; r <rowSize; r++){
        for (int c=0; c <colSize; c++){
            inData >> matrix[r][c] ;
        }
    }
    // print the input values
    cout << "For the "<< rowSize << " x " << colSize << " array:"<<endl;
    for (int r=0; r <rowSize; r++){
        for (int c=0; c <colSize; c++){
            cout << matrix[r][c] << "  ";
        }
        cout<<endl;
    }
    cout << endl<<endl;

    // find the largest in each row
    for (int c=0; c <colSize; c++){
        int largest = matrix[0][c]; //make the first cell value as the largest until you find otherwise 
        int smallest = matrix[0][c]; //make the first cell value as the smallest until you find otherwise 
        int sum=0;
        for (int r=0; r <rowSize; r++){
            if (matrix[r][c] > largest)   //found a new larger value than the largest
                largest = matrix[r][c];
            if (matrix[r][c] < smallest)   //found a new smaller value than the smallest
                smallest = matrix[r][c];
            sum = sum + matrix[r][c];
        }
        cout << "The sum of column "<< c + 1 << " is " << sum << endl; 
        cout << "The average of column "<< c + 1 << " is "<<fixed<<setprecision(2)<< (double)sum/rowSize << endl;
        cout << "The largest value in column "<< c + 1 << " is " << largest << endl; 
        cout << "The smallest value in column "<< c + 1 << " is " << smallest << endl; 
        cout << "Is column " << c + 1 << " strictly ascending? " << endl;

        cout<<endl;
    }
    inData.close();
return 0;

}

这是我的代码,我已经完成了几乎所有我需要的。我只是不知道如何计算数组的四个角,我需要打印一些东西,说cout <<"是列"<<C + 1 <<"严格升天?"& lt; & lt;endl;我不知道该怎么做

打印矩阵角

下面是如何打印矩阵的四个角。
注意,角位于:(0,0),(0,MAX_COLUMNS), (MAX_ROWS, 0)和(MAX_ROWS, MAX_COLUMNS)。

const unsigned int MAX_ROWS = 12U;
const unsigned int MAX_COLS = 12U;
unsigned int matrix[MAX_ROWS][MAX_COLS];
//...
cout << matrix[0][0] << endl;
cout << matrix[MAX_ROWS][0] << endl;
cout << matrix[0][MAX_COLS] << endl;
cout << matrix[MAX_ROWS][MAX_COLS] << endl;

升序列值

根据定义,如果column[n] <列[n>在for循环中实现:

bool is_ascending = true;
for (unsigned int col = 0;
     (col < MAX_COLS-1) && is_ascending;
     ++col)
{
  if (matrix[row][col] >= matrix[row][col + 1])
  {
    is_ascending = false;
  }
}

您可能需要调整规则,让相等的项作为升序值计数。

二维数组的角可由下式求得:设数组a ={1,2,3,4,5,6,7,8,9},则示例数组的角元素将为1,3,7和9。因此,我们的程序就像:-

#include<iostream>
using namespace std;
int main()
{
int a[][3]={1,2,3,4,5,6,7,8,9};
for(int i=0; i<3; i++){
    for(int j=0; j<3; j++){
        if(i%2==0||j%2==0)
        cout << a[i][j];
        else
        cout << " ";
    }
    cout << endl;
}

return 0;
}