gpt4 book ai didi

c++ - 我该如何实现这段代码?

转载 作者:行者123 更新时间:2023-11-28 01:54:14 24 4
gpt4 key购买 nike

所以我的导师给了我要实现的这段代码,基本上这里的总体目标是在将 PPM 图像写入文件后在其中制作棋盘格图案。将所有内容写入文件对我来说是不言自明的,问题是我得到的只是数组中的零。

我的导师给我的 block 。

        int col = ((w & 0x08) == 0) ^ ((h & 0x08) == 0);
raster[i][j].r = static_cast<float>(col);
raster[i][j].g = static_cast<float>(col & 0x00);
raster[i][j].b = static_cast<float>(col & 0x11);

这是我的。

#include <iostream>

using namespace std;

struct RGB{
float r;
float g;
float b;
};

int main(){
//Declare Variables
int h;
int w;

//User Feedback
cout << "Please input height then width" << endl;
cin >> h >> w;

RGB raster[h][w];

//Assignment loop i = h and j = w
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
int col = ((w & 0x08) == 0) ^ ((h & 0x08) == 0);
raster[i][j].r = static_cast<float>(col);
raster[i][j].g = static_cast<float>(col & 0x00);
raster[i][j].b = static_cast<float>(col & 0x11);

cout << raster[i][j].b << endl;
}
}

return 0;
}

如果有人能解释我做错了什么,我将不胜感激。到目前为止,我一直被这个问题所困,并尝试了很多不同的事情。

我发现了问题。我的 i 迭代器得出了一个非常高的值,而且它非常随机。有人知道为什么吗?

最佳答案

您永远不会更新 col 的值。

int col = ((w & 0x08) == 0) ^ ((h & 0x08) == 0);

W 和 h 是常量,因此 col 也是常量。

== 的结果只能是 0 或 1。由于 (1 或 0) xor (1 或 0) 只能给出 (1 或 0),因此 col 必须等于(1 或 0)。

设置时

raster[i][j].b = static_cast<float>(col & 0x11);

&什么都不做,因为你的 col 值只能是 1 或 0。

您确定 col 变量设置正确吗?

关于c++ - 我该如何实现这段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41807256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com