데일리 codeup/이차원 배열
새로운 2차원 배열의 선언_220713
hayo_su
2022. 7. 13. 14:05
새로운 2차원 배열의 선언
숫자 직사각형
정수 n과 m의 값을 입력받아 숫자로 이루어진 직사각형을 출력하는 프로그램을 아래 예를 참고하여 작성해보세요. 단, 2차원 배열을 꼭 사용하여 해결해보세요.
#include <iostream>
using namespace std;
int main(){
int a,b, cnt = 1;
cin >> a >> b;
int arr[a][b];
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
arr[i][j]=cnt;
cnt++;
}
}
for(int x=0;x<a;x++){
for(int y=0;y<b;y++){
cout<<arr[x][y]<<' ';
}
cout<<endl;
}
return 0;
}
두 배열의 곱
3행 3열의 배열 두 개가 주어지면 두 배열의 같은 위치에 있는 숫자의 곱을 출력하는 프로그램을 작성해보세요.
#include <iostream>
using namespace std;
int main(){
int arr[3][3];
int tmp;
for(int i=0;i<3;i++) for(int j=0;j<3;j++) arr[i][j] = 1;
for(int a=0;a<2;a++){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cin >> tmp;
arr[i][j] *= tmp;
}
}
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout<<arr[i][j]<<' ';
}
cout<<endl;
}
return 0;
}
두 개의 격자 비교하기
n x m 크기의 2차원 격자가 두 개 주어지고, 새로운 2차원 격자를 만들려고 합니다.
주어진 두 격자에서 같은 위치에 존재하는 숫자의 값이 같다면 0, 그렇지 않다면 1을 적어주려 합니다.
새로운 2차원 격자를 만들어 이를 해결하는 프로그램을 작성해보세요.
#include <iostream>
using namespace std;
int main(){
int n, m;
cin >> n >> m;
int arr1[n][m], arr2[n][m];
for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>arr1[i][j];
for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>arr2[i][j];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(arr1[i][j] == arr2[i][j]) cout<<0<<' ';
else cout<<1<<' ';
}
cout<<endl;
}
return 0;
}