[英]The problem is finding the largest negative, least positive numbers
In general, the task, most of which I did: Task一般来说,任务,其中大部分是我做的:任务
My problem is 2 things: 1) How to find the smallest positive and largest negative number.我的问题是两件事:1)如何找到最小的正数和最大的负数。 2) How to find their position (and yes, I could not find the positions of the smaller and maximum numbers either). 2)如何找到他们的位置(是的,我也找不到较小和最大数字的位置)。
Here is my code:这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
setlocale(LC_ALL, "");
const int rows = 5;
const int cols = 5;
int pos = 0;
int neg = 0;
int null = 0;
int iMin = 0;
int jMin = 0;
int iMax = 0;
int jMax = 0;
double average;
double sum = 0;
ifstream matrix("C:\\Folder\\Matrix.txt", ios::in);
int arr[rows][cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
matrix >> arr[i][j];
cout << arr[i][j] << " " << "\t";
}
cout << endl;
}
cout << endl;
matrix.close();
ofstream final_matrix("C:\\Folder\\Final matrix.txt", ios::out);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (arr[i][j] > 0)
{
pos++;
}
else if (arr[i][j] < 0)
{
neg++;
}
else
{
null++;
}
if (arr[i][j] < arr[iMin][jMin])
{
iMin = i;
jMin = j;
}
if (arr[i][j] > arr[iMax][jMax])
{
iMax = i;
jMax = j;
}
sum += arr[i][j];
}
}
int prod = rows * cols;
cout << "The number of positive numbers in the matrix: " << pos << endl;
final_matrix << "The number of positive numbers in the matrix: " << pos << endl;
cout << "The number of negative numbers in the matrix: " << neg << endl;
final_matrix << "The number of negative numbers in the matrix: " << neg << endl;
cout << "The number of zeros in the matrix: " << null << endl;
final_matrix << "The number of zeros in the matrix: " << null << endl;
cout << "The minimum number in the matrix: " << arr[iMin][jMin] << endl;
final_matrix << "The minimum number in the matrix: " << arr[iMin][jMin] << endl;
cout << "The maximum number in the matrix: " << arr[iMax][jMax] << endl;
final_matrix << "The maximum number in the matrix: " << arr[iMax][jMax] << endl;
cout << "Arithmetic mean of the matrix: " << sum / prod << endl;
final_matrix << "Arithmetic mean of the matrix: " << sum / (rows * cols) << endl;
final_matrix.close();
cin.get();
}
Below I also present the result of the code (it showed only what is displayed on the console; in addition, the results are still saved in a separate file).下面我还展示了代码的结果(它只展示了控制台上显示的内容;此外,结果仍然保存在单独的文件中)。 Work工作
You can use sets for that.您可以为此使用集合。
Here is the solution:这是解决方案:
#include <iostream>
#include <fstream>
#include <string>
#include<set>
using namespace std;
int main()
{
setlocale(LC_ALL, "");
const int rows = 5;
const int cols = 5;
int pos = 0;
int neg = 0;
int null = 0;
int iMin = 0;
int jMin = 0;
int iMax = 0;
int jMax = 0;
//for larget negative
int ineg=0;
int jneg=0;
//for lowest positive
int ipos=0;
int jpos=0;
double average;
double sum = 0;
ifstream matrix("Matrix.txt", ios::in);
int arr[rows][cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
matrix >> arr[i][j];
cout << arr[i][j] << " " << "\t";
}
cout << endl;
}
cout << endl;
matrix.close();
ofstream final_matrix("Final_matrix.txt", ios::out);
set<int, greater<int>> s;
set<int> s1;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (arr[i][j] > 0)
{
pos++;
}
else if (arr[i][j] < 0)
{
neg++;
}
else
{
null++;
}
if (arr[i][j] < arr[iMin][jMin])
{
iMin = i;
jMin = j;
}
if (arr[i][j] > arr[iMax][jMax])
{
iMax = i;
jMax = j;
}
sum += arr[i][j];
if(arr[i][j]<0)
{
s.insert(arr[i][j]);
}
if(arr[i][j]>0)
{
s1.insert(arr[i][j]);
}
}
}
int prod = rows * cols;
set<int>::iterator it1 = s1.begin();
set<int, greater<int> > ::iterator it = s.begin();
cout << "The number of positive numbers in the matrix: " << pos << endl;
final_matrix << "The number of positive numbers in the matrix: " << pos << endl;
cout << "The number of negative numbers in the matrix: " << neg << endl;
final_matrix << "The number of negative numbers in the matrix: " << neg << endl;
cout << "The number of zeros in the matrix: " << null << endl;
final_matrix << "The number of zeros in the matrix: " << null << endl;
cout << "The minimum number in the matrix: " << arr[iMin][jMin] << endl;
final_matrix << "The minimum number in the matrix: " << arr[iMin][jMin] << endl;
cout << "The maximum number in the matrix: " << arr[iMax][jMax] << endl;
final_matrix << "The maximum number in the matrix: " << arr[iMax][jMax] << endl;
cout << "The lowest positive number in the matrix: " << *it1 << endl;
final_matrix << "The lowest positive number in the matrix: " << *it1 << endl;
cout << "The largest negative number in the matrix: " << *it << endl;
final_matrix << "The largest negative number in the matrix: " << *it <<endl;
cout << "Arithmetic mean of the matrix: " << sum / prod << endl;
final_matrix << "Arithmetic mean of the matrix: " << sum / (rows * cols) << endl;
final_matrix.close();
cin.get();
}
If you want coordinates also, use unordered_map where you can insert a pair of your coordinates.如果您还需要坐标,请使用 unordered_map,您可以在其中插入一对坐标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.