我想打印最大值的行,它在c++中有4到5列
I want to print the largest value row which has 4 5 columns in c++
我想打印最大值的行,它在c++中有4-5列。我有两个类,一个是cpu,另一个是进程。如何将一个值从进程发送到cpu,并在将该值更新到数组后,cpu将其发送回进程。
请给我举个简单的例子。
在两个类之间传递值
通常,一个类有一个receive方法,另一个类用一个值调用receive方法。
class CPU
{
const unsigned int MAX_ROWS = 10U;
const unsigned int MAX_COLS = 45U;
int matrix[MAX_ROWS][MAX_VALUES];
public:
void receive(unsigned int row, unsigned int column, int value)
{
matrix[row][col] = value;
}
};
class Process
{
CPU & m_cpu_object;
public:
Process(CPU & cpu_object)
: m_cpu_object(cpu_object)
{ ; }
void send_to_cpu(void)
{
for (unsigned int row = 0; row < 5; ++row)
{
for (unsigned int column = 0; column < 10; ++col)
{
m_cpu_object.receive(row, column, row * column);
}
}
}
};
int main(void)
{
CPU my_cpu;
Process process_1(my_cpu);
process.send_to_cpu();
return 0;
}
查找具有最大值的行
该解决方案与查找一组值中的最大值非常相似。
您需要维护两个变量,一个包含运行中的最大值,另一个维护包含最大值的行索引。
处理完所有行后,打印由行索引指示的行。
示例:
unsigned int row_of_max = 0;
int max_value = -123456789;
for (unsigned int row = 0; row < MAXIMUM_ROWS; ++row)
{
for (unsigned int col = 0; col < MAXIMUM_COLUMNS; ++col)
{
if (maxtrix[row][col] > max_value)
{
row_of_max = row;
max_value = maxtrix[row][col];
}
}
}
行或列的数量,无论是4或5,都不重要;算法仍然是一样的。
相关文章:
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 0-1背包代码中的错误.我的代码中有什么错误
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 如何检查一个c++字符串中有多少相同的字符/数字
- std中有类似find_last_of的函数,而string中没有
- C++编译器中有哪些非确定性的例子?
- Eclipse CDT:单个项目中有多个C++文件
- 如何在不知道C++中有多少可选参数的情况下在循环中使用va_arg?
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 是否可以在C++中有一个"generic"模板参数,该参数可以是非类型模板参数或类型?
- 如果我不知道每个列表中有多少个数字,我如何将给定数量的数字列表作为输入?
- "ABC" 和 "ABC" ) 在C++中有什么区别?
- 我的输出中有一个额外的 0,为什么会这样
- 我可以在具有一个标头和一个接口的 cpp 文件中有多个嵌入吗?
- 我的代码中有错误,未声明的标识符
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- C++ 检查结果数组中有多少次数字
- 命名空间中有 2 个同名的类
- 处理程序的模块列表中有一个错误的模块"WebSocketModule"
- 使用 valgrind 检查我的链表暗示中的内存泄漏,让我"肯定丢失:1 个块中有 40 个字节"