This C code performs a specific task using arrays. Let's break it down step by step:
1. Library and Namespace:
#include <bits/stdc++.h>
using namespace std;
This part includes libraries that provide various functionalities, including input-output operations and data structures.
2. Main Function:
int main()
{
// ... code ...
return 0;
}
This is the main function where the program execution starts and ends. It's where the program's logic is written.
3. Variables:
int r, c;
cin >> r >> c;
int arr[r][c];
int arrTemp[r][c];
- `r` and `c` are variables to hold the number of rows and columns respectively, input by the user.
- `arr` and `arrTemp` are 2D arrays of size `r` rows and `c` columns to store integer values.
4. Array Initialization:
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> arr[i][j];
arrTemp[i][j] = 0;
}
}
This code takes input for the `arr` array from the user and initializes all elements of `arrTemp` to 0.
5. Array Manipulation:
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (arr[i][j] == 1) {
for (int i1 = 0; i1 < r; i1++) {
arrTemp[i1][j] = 1;
}
for (int i1 = 0; i1 < c; i1++) {
arrTemp[i][i1] = 1;
}
}
}
}
- If an element in the `arr` array is equal to 1, this code sets the entire column and row of that element in `arrTemp` to 1.
6. Printing the Updated Array:
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << arrTemp[i][j];
if (j != c - 1) cout << ' ';
}
cout << endl;
}
This code prints the updated `arrTemp` array, showing the modifications made based on the input provided.
7. Return Statement:
return 0;
This statement ends the `main` function, indicating successful completion of the program.
8. Unreachable Code:
printf("for(m=0;m<r;m++)");
The `printf` statement after the `return 0;` will never execute as the `return` statement ends the function's execution.
Overall, this code takes user input for a 2D array, checks for elements with a value of 1, and updates another array based on that value. Finally, it prints the updated array.
C code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int r,c;
cin>>r>>c;
int arr[r][c];
int arrTemp[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin>>arr[i][j];
arrTemp[i][j] = 0;
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if(arr[i][j]==1){
for(int i1 = 0;i1<r;i1++){
arrTemp[i1][j] =1;
}
for(int i1 = 0;i1<c;i1++){
arrTemp[i][i1] =1;
}
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout<<arrTemp[i][j];
if(j!=c-1)cout<<' ';
}
cout<<endl;
}
return 0;
printf("for(m=0;m<r;m++)");
}
0 Comments
if you have any doubt, plz let me know