###前提・実現したいこと
二重配列で4行4列の中に文字を入力してそれを、数字に変換するプログラムを作成したい。
###発生している問題・エラーメッセージ
実行すると、目標としている数値ではなく明後日の数値が出力される。
###該当のソースコード
C言語
#include <stdio.h>
#include <string.h>
int main(void){
char str[4][4];
int num[4][4];
for(int h=0; h<4; h++){
for(int i=0; i<4; i++){
printf("%d行目%d列目:", h+1, i+1);
scanf(" %s",&str[h][i]);
}
}
for(int x=0; x<4; x++){
for(int y=0; y<4; y++){
if (strcmp(&str[x][y], "a")==0){
num[x][y]=0;
}else if(strcmp(&str[x][y], "b")==0){
num[x][y]=1;
}else if(strcmp(&str[x][y], "c")==0){
num[x][y]=2;
}else if(strcmp(&str[x][y], "d")==0){
num[x][y]=3;
}else if(strcmp(&str[x][y], "e")==0){
num[x][y]=4;
}else if(strcmp(&str[x][y], "f")==0){
num[x][y]=5;
}else if(strcmp(&str[x][y], "g")==0){
num[x][y]=6;
}else if(strcmp(&str[x][y], "h")==0){
num[x][y]=7;
}else if(strcmp(&str[x][y], "i")==0){
num[x][y]=8;
}else if(strcmp(&str[x][y], "j")==0){
num[x][y]=9;
}else if(strcmp(&str[x][y], "k")==0){
num[x][y]=10;
}else if(strcmp(&str[x][y], "l")==0){
num[x][y]=11;
}
}
}
for(int w=0; w<4; w++){
for(int v=0; v<4; v++){
printf("%d,%d,%d\n",w,v,num[w][v]);
}
}
return 0;
}
「入力部分」
1行目(1):a
1行目(2):b
1行目(3):c
1行目(4):d
2行目(1):e
2行目(2):f
2行目(3):g
2行目(4):h
3行目(1):i
3行目(2):d
3行目(3):d
3行目(4):f
4行目(1):d
4行目(2):e
4行目(3):d
4行目(4):c
「出力結果」
0,0,1434622816
0,1,32767
0,2,171794432
0,3,1
1,0,0
1,1,1
1,2,236447008
1,3,1
2,0,236446912
2,1,1
2,2,0
2,3,0
3,0,0
3,1,0
3,2,0
3,3,0
回答2件
あなたの回答
tips
プレビュー