Write a C program that defines a 2-dimentional integer array
called A [50][50]. Then the elements of this array should
randomly be initialized either to 1 or 0. The program should
then print out all the elements in the diagonal (i.e.
a[0][0], a[1][1],a[2][2], a[3][3], ……..a[49][49]). Finally,
print out how many zeros and ones in the diagonal.
Answer Posted / senthil
// correcting Cfuzz answer
#include <stdio.h>
#include <stdlib.h> // for rand function
#define ROWS 50
#define COLS 50
int main(void)
{
int A[ROWS][COLS];
int i=0, j=0;
int zero_cnt = 0;
int one_cnt = 0;
/* Initializing*/
for(i=0; i < ROWS; i++) {
for(j=0; j < COLS; j++) {
A[i][j] = rand() % 2;
}
}
// here one loop is sufficient
for(i=0; i < ROWS; i++) {
printf("A[%d][%d] = %2d\n", i, i, A[i][i]);
if(A[i][i] == 0)
{
zero_cnt++;
}
else //if(A[i][i] == 1)
{
one_cnt++;
}
}
printf("\nNumber of zeros in the diagonal = %d", zero_cnt);
printf("\nNumber of ones in the diagonal = %d", one_cnt);
}
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
When do you not use the keyword 'return' when defining a function a) Always b) Never c) When the function returns void d) dfd
What are high level languages like C and FORTRAN also known as?
What is the process to create increment and decrement stamen in c?
why use functions a) writing functions avoids rewriting the same code over and over b) using functions it becomes easier to write programs and keep track of what they are doing c) a & b d) none of the above
What is the difference between near, far and huge pointers?
What are integer variable, floating-point variable and character variable?
explain what is a newline escape sequence?
How do you print an address?
What is a pointer in c plus plus?
What is the difference between text and binary i/o?
Where register variables are stored in c?
What is the difference between single charater constant and string constant?
What does c in a circle mean?
Where is c used?
Explain what are linked list?