如何打印这个数组的四个角?我怎么知道这些列是否是严格升序的
How can I print the four corners of this array? and How do I find out if the columns are strictly ascending
#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;
}
相关文章:
- 我怎么知道数组包含多少个元素C++?
- 这是系统资源吗?(或者我怎么知道我是否需要删除指针) - 在 C++ 中使用 C
- 这些情况有什么区别?我怎么知道什么是临时对象?
- 我怎么知道我最后的指示
- 我怎么知道从哪里得到我丢失的 h 文件?
- 我怎么知道C++编译器是否制作线程安全的静态对象代码
- 我怎么知道USB设备是否已经在使用
- 我怎么知道哪种上下文处于活动状态
- 我怎么知道“ rehash”是否在插入unordered_map之后发生
- 我怎么知道谁拥有某个指针
- 我怎么知道使用caffe framwork和c ++程序的层中是否存在偏差
- 我怎么知道基类对象的指针数组中元素的类型,它将为派生类分配内存
- 我怎么知道,如果我在 STL 列表中找到了我搜索的号码
- 我怎么知道内存中的变量是否是指针
- 我怎么知道我是否需要在C 中删除某些内容
- 我怎么知道我的服务器是否在一段时间内没有连接
- 扫雷,我怎么知道有多少个 0 彼此相邻?
- 我怎么知道std :: vector的分配记忆大小
- 我怎么知道何时在我的代码中访问变量
- 如何打印这个数组的四个角?我怎么知道这些列是否是严格升序的