C ++理解一个公式 [英] C++ understanding a formula

查看:61
本文介绍了C ++理解一个公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个2d数组的测试任务,我需要编写代码来打印输出。

1 2 3 4

1 2 3 4

1 2 3 4

1 2 3 4



不知怎的,我做到了。我已经理解了循环和数组是如何工作的,但是我输入的公式,我并没有真正得到它。我试图做类似x + 1的事情,这意味着行+ 1,因为我需要在每次循环后添加1行。但是我这样做会产生相反的结果。喜欢,

1 1 1

2 2 2 2

3 3 3 3

4 4 4 4

当我做y + 1这是列+ 1它工作并给了我需要的输出。有人可以解释一下这个配方是如何工作的吗?我试着自己理解它,但我不能。

谢谢。以下是我写的代码。



我尝试过:



Hello, I had a test task for 2d arrays where i needed to write code to print out following output.
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

Somehow i did it. I have understanding that how loops and arrays work but, The formula i put in it, I didn't really get it. i was trying to do something like "x+1" which means row+1 because i needed 1 added in rows after every time it goes through loop. But i was getting opposite output by doing so. Like,
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
when i did "y+1" which is column+1 it worked and gave me the output i needed. Could someone please explain me how this formula works? I tried to understand it on my own but i can't.
Thanks. Below is the code i had written.

What I have tried:

#include <iostream>
using namespace std;
int main()
{
	int a[4][4];
	int x,y;
	for (x = 0; x<=3; x++)
	{
		for (y = 0; y<=3; y++)
		{
			a[x][y]=y+1;
			}
		cout << endl;
	}
	cout << endl;

	for (x = 0; x<=3; x++) {
		for (y = 0; y<=3; y++) {
			cout << " " << a[x][y];
		}
		cout << endl;
	}	
}

推荐答案

x 号码( 0 - 基于)。

y 号码( 0 - 基于)。

我为了获得你必须输出的所需输出(在每一行)列号( 1 - ),因此( y + 1)。 Simplex et unum。
x is the row number (0-based).
y is the column number (0-based).
I order to obtain the required output you have to put (in every row) the column number (1-based), hence (y+1). Simplex et unum.


Quote:

有人可以解释一下这个公式是如何工作的吗?我试图自己理解它,但我不能。

Could someone please explain me how this formula works? I tried to understand it on my own but i can't.



调试器也是一个很好的学习工具,用它来查看你的代码在做什么,然后尝试不同的代码解决方案和看看行为上的差异。

它会改善你的学习曲线。

-----

你的代码行为不像你那样期待,你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你的是什么应该这样做,它没有发现错误,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器只是为了显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


Debugger is also a great learning tool, use it to see what your code is doing, then experiment different code solutions and see the difference in behavior.
It will improve your learning curve.
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


你需要先用正确的值填充数组。想象一下它是一个棋盘。



一步使用调试器可以更好地查看代码的作用。调试器还有一个可扩展的窗格,您可以在其中查看内存。
You need to fill the array first with the correct values. Imagine it as a chess board.

Use the debugger in single step to better see what your code does. The debugger has also an extendable pane where you can view the memory.


这篇关于C ++理解一个公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆