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
What is the use of f in c?
What is typedef example?
What tq means in chat?
How to create struct variables?
What does sizeof int return?
What Is The Difference Between Null And Void Pointer?
#define MAX(x,y) (x) >(y)?(x):(y) main() { inti=10,j=5,k=0; k= MAX(i++,++j); printf("%d..%d..%d",i,j,k); }
diff between exptected result and requirement?
If null and 0 are equivalent as null pointer constants, which should I use?
What is structure padding and packing in c?
5 Write an Algorithm to find the maximum and minimum items in a set of ānā element.
Why c is called a mid level programming language?
Here is a good puzzle: how do you write a program which produces its own source code as output?
What are local static variables? How can you use them?
Explain how can I prevent another program from modifying part of a file that I am modifying?