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.

Answers were Sorted based on User's Feedback



Write a C program that defines a 2-dimentional integer array called A [50][50]. Then the elements o..

Answer / lee

#include<stdio.h>
#include <stdlib.h>
#include<conio.h>
int main()
{
int A[50][50],i,j,z=0,k,o=0;
for( i = 0 ; i < 50 ; i++ )
{
for( j = 0 ; j < 50 ; j++ )
{
A[i][j]=rand() % 2;
}
}

printf("The diagonal elements are : \n");
for(i=0;i<50;i++)
{
for(j=0;j<50;j++)
{
if(i==j)
{
printf("%d\t",A[i][j]);
if(A[i][j]==0)
z++;
else
o++;
}
}
}

printf("The no. of zeroes : \t %d\nThe no. of ones : \t %d",z,o);
getch();
return 0;
}

Is This Answer Correct ?    4 Yes 1 No

Write a C program that defines a 2-dimentional integer array called A [50][50]. Then the elements o..

Answer / cfuzz

/* this is my code, only thing I'm missing is counting
zeros...WHAT THE HELL am I doing WRONG?*/

#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] = 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 1 No

Post New Answer

More C Code Interview Questions

program to find magic aquare using array

4 Answers   HCL,


Write a routine that prints out a 2-D array in spiral order

3 Answers   Microsoft,


What is the output of the program given below main() { signed char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

1 Answers  


find simple interest & compund interest

2 Answers  


main() { char *p = "hello world"; p[0] = 'H'; printf("%s", p); } a. Runtime error. b. “Hello world” c. Compile error d. “hello world”

5 Answers   HCL,






void ( * abc( int, void ( *def) () ) ) ();

1 Answers  


print a semicolon using Cprogram without using a semicolon any where in the C code in ur program!!

35 Answers   Tata Elxsi, TCS, VI eTrans,


#include<stdio.h> void fun(int); int main() { int a; a=3; fun(a); printf("\n"); return 0; } void fun(int i) { if(n>0) { fun(--n); printf("%d",n); fun(--n); } } the answer is 0 1 2 0..someone explain how the code is executed..?

1 Answers   Wipro,


enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); }

2 Answers  


writte a c-programm to display smill paces

2 Answers  


Who could write how to find a prime number in dynamic array?

1 Answers  


how to programme using switch statements and fuctions, a programme that will output two even numbers, two odd numbers and two prime numbers of the users chioce.

0 Answers   Mbarara University of Science and Technology,


Categories