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
// correcting Cfuzz answer
#include <stdio.h>
#include <stdlib.h> // for rand function
#define ROWS 50
#define COLS 50
int main(void)
{
int A[ROWS][COLS];
int i=0, j=0;
int zero_cnt = 0;
int one_cnt = 0;
/* Initializing*/
for(i=0; i < ROWS; i++) {
for(j=0; j < COLS; j++) {
A[i][j] = rand() % 2;
}
}
// here one loop is sufficient
for(i=0; i < ROWS; i++) {
printf("A[%d][%d] = %2d\n", i, i, A[i][i]);
if(A[i][i] == 0)
{
zero_cnt++;
}
else //if(A[i][i] == 1)
{
one_cnt++;
}
}
printf("\nNumber of zeros in the diagonal = %d", zero_cnt);
printf("\nNumber of ones in the diagonal = %d", one_cnt);
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / pushpender singh
#include<stdio.h>
main
{
int a,b,c;
a[50][50];
b[][];
for(c=0;c>50;c+a)
{
a=a+c;
b=a;
}
printf("%d",b[][]);
}
}
Is This Answer Correct ? | 0 Yes | 2 No |
Answer / 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 |
What do you mean by dynamic memory allocation in c?
write a c program to print the values in words eg:- 143 written it has (one hundred and forty three)& 104, 114 are also written words
5 Answers Captronic, DELL, Google, IBM, Mithi, RCC, Wipro,
What does a function declared as pascal do differently?
What are the differences between new and malloc in C?
the statement while(i) puts the entire logic in loop. this loop is called a) indefinite loop b) definite loop c) loop syntax wrong d) none of the above
How do you define a function?
1) write a program to generate 1st n fibonacci prime numbers using Nested if 2) write a program to generate twin prime numbers from m to n using nested if 3) write a program to check whether a given integer is a strong number or not using nested if 4) Write a program to generate prime factors of a given integer using nested if 5)write a program to generate prime numbers from m to n using nested if 6)write a program to generate perfect numbers from m to n using nested if 7)write a program to generate the pallindromes from m to n using neste if 8)write a program to generate armstrong numbers from m to n using nested if 9)write a program to generate strong numbers from m to n using nested if
How to print "Hi World" without using semi colon?
GIven a sequence of characters. How will you convert the lower case characters to upper case characters. ( Try using bit vector - sol given in the C lib -> typec.h)
One of the Institutes contains 5 student groups. Every group contains 4 students. Institute wants to store student group’s details in array. Group should contain group member’s details (name and registration number and age), project name, and mark of the group.
What is c basic?
what is c?