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
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 |
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 |
#include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }
main() { int y; scanf("%d",&y); // input given is 2000 if( (y%4==0 && y%100 != 0) || y%100 == 0 ) printf("%d is a leap year"); else printf("%d is not a leap year"); }
Write a function to find the depth of a binary tree.
13 Answers Adobe, Amazon, EFI, Imagination Technologies,
Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list (fixed list)
3 Answers Disney, Google, ZS Associates,
what is the output of the below program & why ? #include<stdio.h> void main() { int a=10,b=20,c=30; printf("%d",scanf("%d%d%d",&a,&b,&c)); }
Write a single line c expression to delete a,b,c from aabbcc
write a c program to Reverse a given string using string function and also without string function
main(){ int a= 0;int b = 20;char x =1;char y =10; if(a,b,x,y) printf("hello"); }
Given n nodes. Find the number of different structural binary trees that can be formed using the nodes.
16 Answers Aricent, Cisco, Directi, Qualcomm,
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); }
main() { int *j; { int i=10; j=&i; } printf("%d",*j); }
How we will connect multiple client ? (without using fork,thread)