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 / cfuzz

/* THIS CODE IS WRONG...CUZ I CAN'T COUNT ZEROS*/


#include <stdio.h>

#define ROWS 50
#define COLS 50

int count_occur(int A[], int num_elements, int value);


int main(void)
{
int A[ROWS][COLS];
int i=0, j=0;
int num_occ, value=0;

/* Initializing*/

for(i=0; i < ROWS; i++) {
for(j=0; j < COLS; j++) {
A[i][j] = 0;
A[i][j] = 1;
A[i][j] = rand() % 2;

}
}

for(i=0; i < ROWS; i++) {
for(j=0; j < COLS; j++) {

if (i == j){
printf("%2d", A[i][j]);
}
}



}

for(value=0; value<1; value++)
{

num_occ = count_occur(A, 50, value);

if (value = 1){

printf("\n\nThe value %d was found %d times.\n", value,
num_occ);
}

else if (value = 0){

printf("\n\nThe value %d was found %d times.\n", value,
num_occ);
}


}

}

int count_occur(int A[], int num_elements, int value)
/* checks array a for number of occurrances of value */
{
int i, count=0;
for (i=0; i<num_elements; i++)
{
if (A[i] == value)
{
++count; /* it was found */
}
}
return(count);
}



Is This Answer Correct ?    0 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What happens if header file is included twice?

657


What is the difference between the local variable and global variable in c?

534


Why & is used in c?

716


write a program to print largest number of each row of a 2D array

1874


how could explain about job profile

1458






Differentiate between the = symbol and == symbol?

720


c language supports bitwise operations, why a) 'c' language is system oriented b) 'c' language is problem oriented c) 'c' language is middle level language d) all the above

619


When the macros gets expanded?

792


An application package has been provided to you without any documents for the following application. The application needs to be tested. How will you proceed?

675


Why is #define used?

792


Write a program to print fibonacci series using recursion?

590


difference between native and cross compilers

1673


Are pointers really faster than arrays?

569


how to execute a program using if else condition and the output should enter number and the number is odd only...

1661


How to write a multi-statement macro?

628