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

main() { int x=5; clrscr(); for(;x<= 0;x--) { printf("x=%d ", x--); } } a. 5, 3, 1 b. 5, 2, 1, c. 5, 3, 1, -1, 3 d. –3, -1, 1, 3, 5

2 Answers   HCL,


Program to Delete an element from a doubly linked list.

4 Answers   College School Exams Tests, Infosys,


How can I Create a C program in splitting set of characters to specific subsets. Example: INPUT SET OF CHARACTERS: Therefore, my dear brothers and sisters, stand firm. Let nothing move you. Always give yourselves fully to the work of the Lord, because you know that your labor in the Lord is not in vain. SPLIT INTO HOW MANY CHARACTERS PER SUBSETS: 10 OUTPUT: Therefore, my dear b rothers an d sisters, stand fir m. Let not hing move you. Alway s give you rselves fu lly to the work of t he Lord, b ecause you know that your labo r in the L ord is not in vain.

0 Answers  


why the range of an unsigned integer is double almost than the signed integer.

1 Answers  


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

2 Answers  






what will be the output of this program? void main() { int a[]={5,10,15}; int i=0,num; num=a[++i] + ++i +(++i); printf("%d",num); }

3 Answers   Wipro,


writte a c-programm to display smill paces

2 Answers  


what is oop?

3 Answers  


What is your nationality?

1 Answers   GoDB Tech,


main() { show(); } void show() { printf("I'm the greatest"); }

2 Answers  


main() { static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i)); } }

1 Answers  


main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); }

2 Answers   Wipro,


Categories