【问题标题】:How can I print the four corners of this array? and How do I find out if the columns are strictly ascending如何打印这个数组的四个角?以及如何确定列是否严格升序
【发布时间】:2015-10-20 21:16:28
【问题描述】:
#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++ arrays


    【解决方案1】:

    打印矩阵角

    这里是如何打印矩阵的四个角。
    请注意,角点位于:(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] 在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;
      }
    }
    

    您可能想要调整规则并将相等的项目计为升序值。

    【讨论】:

      【解决方案2】:

      二维数组的角点可以通过以下方式获得: 设一个数组 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 2016-01-25
        相关资源
        最近更新 更多