我不能让我的C ++程序在我想要打印的矩阵中打印字符 [英] I cant get my C++ program to print the characters in a matrix I want it to print

查看:79
本文介绍了我不能让我的C ++程序在我想要打印的矩阵中打印字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题的一个小版本,基本上,在我的完整版中,我给矩阵中的每个值一个字母,我更改字母值,打印它们等。

一切看起来都很好,除了打印部分,因为它通常会随机发出一个随机的ASCII字符,这取决于我对程序做的小改动,试图让它工作。

希望你可以帮助我。



#include< stdio.h>

#include< stdlib.h>



int main(){

char matrix [2] [2] = {'X'};

printf( %c,矩阵[2] [2]);

}



我尝试过:



我尝试了很多东西,只是想改变屏幕上的打印值。



我试过的一些(伪代码):



%c

%s

X

'X'

const char matrix [2] [2]

const char * matrix [2] [2]

cout<< matrix [2] [2]



X ='X';

矩阵[2] [2] = {X};

This is a small version of my problem, basically, In my "full version" I give every one of the values in the matrix a letter, and I change the letter values, print them, etc.
Everything looks to be working fine, except the printing part, as it commonly spits out a random ASCII character, depending on small changes I make to the program to try to make it work.
Hope you can help me.

#include <stdio.h>
#include <stdlib.h>

int main(){
char matrix[2][2]={'X'};
printf("%c",matrix[2][2]);
}

What I have tried:

I have tried lots of things, just getting to change the printed value in the screen.

Some of what I've tried (pseudocode):

%c
%s
"X"
'X'
const char matrix[2][2]
const char *matrix[2][2]
cout<<matrix[2][2]

X='X';
matrix[2][2]={X};

推荐答案

嗯......这里有一些事情。 />
首先,您的示例代码显示您创建了一个2 x 2元素数组 - 即总共四个元素 - 然后尝试打印该数组的第九个元素。在C和C ++中,数组索引从零开始,因此2 x 2数组在位置只有有效数据

Um...there are a few things here.
The first is that your sample code shows you creating a 2 x 2 element array - i.e. a total of four elements - and then trying to print the ninth element of that array. in C and C++, array indexes start at zero, so a 2 x 2 array will only have valid data at locations
matrix[0][0]
matrix[0][1]
matrix[1][0]
matrix[1][1]



矩阵[2] [ 2]完全超出数组的范围。



第二个是你的输出语句只是试图从数组输出一个元素 - 所以即使索引是正确的,它永远不会打印所有元素。

尝试使用嵌套的表示循环:


matrix[2][2] is completely outside the bounds of the array.

The second is that your output statement is only ever trying to output a single element from the array - so even if the indexing was correct it would never print all elements.
Try using nested for loops:

for (i = 0; i < sizeOfFirstDimension; i++)
   {
   for (j = 0; j < sizeOfSecondDimension; j++)
      {
      cout << matrix[i][j];
      }
   }

看看它是否更好。


你正在创建一个2x2的矩阵数组,但只给它一个元素。这导致问题和C程序只是吐出垃圾值。考虑一下,

You are creating a matrix array of 2x2, but only giving it one element. That causes the problem and C program is just spitting out garbage value. Consider this,
#include <stdio.h>
#include <stdlib.h>

int main(){
    char matrix[2][2] = { {'V', 'X'}, { 'Y', 'Z' }};
    
    printf("Value is: %c\n", matrix[1][1]); // 2nd element of 2nd array.
}

// Output:
// Value is: Z



您可以在您的机器上测试此程序,或者在免费在线IDE和终端 [ ^ ]



此外,我还过来看到你也不是很擅长数组处理(长度和索引不一样)。如果数组的大小为5,则最后一个元素位于索引4处(索引从0开始)。我只能再次推荐阅读C教程,

C programming.com - 学习C和C ++编程 - Cprogramming.com [ ^ ]

C教程 [ ^ ]


You can test this program in your machine, or online at, Free Online IDE and Terminal[^]

Besides, I also came over to see that you are also not very good at array handling (length and index is not the same). If an array is of size 5, last element is at index 4 (index starts at 0). I can only recommend reading C tutorials once again,
C programming.com - Learn C and C++ Programming - Cprogramming.com[^]
C Tutorial[^]


char matrix[2][2]={'X'};



在此声明 matrix 并将值存储在 matrix [0] [0]


Here you declare matrix and store a value in matrix[0][0]

printf("%c",matrix[2][2]);



matrix [2] [2] do不存在,C / C ++基于零。



使用调试器查看代码正在做什么。它允许你逐行执行第1行并在执行时检查变量。



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

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



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

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


matrix[2][2] do not exist, C/C++ is zero based.

Use the debugger to see what your code is doing. It 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[^]

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 find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于我不能让我的C ++程序在我想要打印的矩阵中打印字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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