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
an expression contains relational operators, assignment operators, and arithmatic operstors. In the absence of parentheses, they will be evaluated in which of the following order a) assignment, relational, arithematic b) arithematic, relational, assignment c) relational, arithematic, assignment d) assignment, arithematic, relational
What is the difference between exit() and _exit() function in c?
Why is event driven programming or procedural programming, better within specific scenario?
What is build process in c?
How can I rethow can I return a sequence of random numbers which dont repeat at all?
What are dangling pointers? How are dangling pointers different from memory leaks?
List the variables are used for writing doubly linked list program.
Is there a way to have non-constant case labels (i.e. Ranges or arbitrary expressions)?
In the DOS enveronment, normal RAM that resides beyond the 1mb mark. a) expanded memory b) swapped memory c) Extended memory d) none
What is the purpose of the following code? Is there any problem with the code? void send(int count, short *to, short *from) { /* count > 0 assumed */ register n = (count + 7) / 8; switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while (--n > 0); } }
1. Write a function to display the sum of two numbers in the following ways: By using (i) pass by value (ii) pass by address a. function with argument and with return value b. function with argument and without return value c. without argument , with return value d. without argument , without return value Note: Use pass by address.
How are variables declared in c?
How can I call a function with an argument list built up at run time?
How macro execution is faster than function ?
In c programming write a program that will print 10 multiples of 3 except 15,18,21 using looping