c++ - 你能帮我解决显示矩阵的问题吗?
问题描述
(谢谢大家这么快帮助我,但我没有解决我的问题,我重新编辑了我的帖子以更具体)
我想创建一个 10x10 矩阵来存储从 'a' 到 'j' 的 10 行 char 值。
我无法显示 10x10 的矩阵。有人可以解释我为什么吗?这里的代码:
此函数将字母 a 到 j 放在每一行中
void inizializza_tab(char tab[][9]){ //Carica con ~ le tabelle
for(int x=0;x<=9;x++)
{
for(int y=0;y<=9;y++){
tab[x][y]=y+97; //Tabella ASCII, onde del mare;
}
}}
此功能在显示屏上打印矩阵
void stampatab(char tab[][9]){
cout<<" ";
for(char d='A';d<='J';d++){ //Dispone le lettere da A a J
cout<<d<<" ";
}
cout<<endl;
for(int r=0;r<=9;r++)
{
cout<<"\n "<<r+1<<" ";//Dispone in colonna i numeri da 1 a 10
for(int c=0 ;c<=9 ;c++){
cout<<tab[r][c]<<" "; //Spazio fra le onde del mare;
}
cout<<endl<<endl;
}}
输出应该是:
A B C D E F G H I J
1 a b c d e f g h i j
2 a b c d e f g h i j
3 a b c d e f g h i j
4 a b c d e f g h i j
5 a b c d e f g h i j
6 a b c d e f g h i j
7 a b c d e f g h i j
8 a b c d e f g h i j
9 a b c d e f g h i j
10 a b c d e f g h i j
但实际上输出是:
A B C D E F G H I J
1 a b c d e f g h i a //wrong
2 a b c d e f g h i a //wrong
3 a b c d e f g h i a //wrong
4 a b c d e f g h i a //wrong
5 a b c d e f g h i a //wrong
6 a b c d e f g h i a //wrong
7 a b c d e f g h i a //wrong
8 a b c d e f g h i a //wrong
9 a b c d e f g h i a //wrong
10 a b c d e f g h i j //correct
解决方案
如果矩阵是 10x10,则必须从 0 到 10(不包括)开始。阅读文档。我还用@ThomasMatthews 建议的 char 值更改了 For 循环
void inizializza_tab(char tab[][10]){ //Carica con ~ le tabelle
for(int x=0;x<10;x++)
{
for(int y=0;y<10;y++){
tab[x][y]=y+97; //Tabella ASCII, onde del mare;
}
}
}
void stampatab(char tab[][10]){
char lett;
cout<<" ";
for(char d='A';d<='J';d++){ //Dispone le lettere da A a J
cout<<d<<" ";
}
cout<<endl;
for(int r=0;r<10;r++)
{
cout<<"\n "<<r+1<<" ";//Dispone in colonna i numeri da 1 a 10
for(int c=0 ;c<10;c++){
cout<<tab[r][c]<<" "; //Spazio fra le onde del mare;
}
cout<<endl<<endl;
}
}
推荐阅读
- haskell - sequenceA 如何在对列表上工作?
- javascript - Firefox 正在阻止同一来源的获取请求
- fixtures - Fitnesse 为全局变量赋值并在多个 Fixtures 中重用
- ms-access-2013 - 将列标题转换为行数据
- flutter - 使用 MpChart 覆盖堆积柱形图中的值
- javascript - 从 api 获取多个 url
- sql - 创建自定义时间戳存储桶 bigquery
- google-chrome - Apps Script Webapp 未向 Google Analytics 报告 - 适用于 firefox 并适用于 localhost
- c - malloc 造成的内存泄漏会永远存在吗?
- python - Python电报API。我不明白如何通过 Python lib 将图像发送到电报