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 |
Write a program to check whether the number is prime and also check if it there i n fibonacci series, then return true otherwise return false
void main() { static int i=5; if(--i){ main(); printf("%d ",i); } }
#include<stdio.h> int main() { int a=3,post,pre; post= a++ * a++ * a++; a=3; pre= ++a * ++a * ++a; printf("post=%d pre=%d",post,pre); return 0; }
main() { char *cptr,c; void *vptr,v; c=10; v=0; cptr=&c; vptr=&v; printf("%c%v",c,v); }
respected sir, i did my MCA in 2013 when i am going to attend to an interview i was asked about my project how will i explain my project could please help me in this and my project title is "Social Networking Site For Social Responsibility"
How to reverse a String without using C functions ?
33 Answers Matrix, TCS, Wipro,
main() { struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }
why nlogn is the lower limit of any sort algorithm?
main() { signed int bit=512, i=5; for(;i;i--) { printf("%d\n", bit >> (i - (i -1))); } } a. 512, 256, 0, 0, 0 b. 256, 256, 0, 0, 0 c. 512, 512, 512, 512, 512 d. 256, 256, 256, 256, 256
main() { extern int i; i=20; printf("%d",sizeof(i)); }
int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); }
main() { unsigned int i=10; while(i-->=0) printf("%u ",i); }