510,909 views
31 votes
31 votes
2. Write a C program that generates following outputs. Each of the

outputs are nothing but 2-dimensional arrays, where ‘*’ represents

any random number. For all the problems below, you must use for

loops to initialize, insert and print the array elements as and where

needed. Hard-coded initialization/printing of arrays will receive a 0

grade. (5 + 5 + 5 = 15 Points)

i)

* 0 0 0

* * 0 0

* * * 0

* * * *

ii)

* * * *

0 * * *

0 0 * *

0 0 0 *

iii)

* 0 0 0

0 * 0 0

0 0 * 0

0 0 0 *

2. Write a C program that generates following outputs. Each of the outputs are nothing-example-1
User Emil Haas
by
2.7k points

1 Answer

26 votes
26 votes

Answer:

#include <stdio.h>

int main(void)

{

int arr1[4][4];

int a;

printf("Enter a number:\\");

scanf("%d", &a);

for (int i=0; i<4; i++)

{

for(int j=0; j<4; j++)

{

if(j<=i)

{

arr1[i][j]=a;

}

else

{

arr1[i][j]=0;

}

}

}

for(int i=0; i<4; i++)

{

for(int j=0; j<4; j++)

{

printf("%d", arr1[i][j]);

}

printf("\\");

}

printf("\\");

int arr2[4][4];

int b;

printf("Enter a number:\\");

scanf("%d", &b);

for (int i=0; i<4; i++)

{

for(int j=0; j<4; j++)

{

if(j>=i)

{

arr1[i][j]=b;

}

else

{

arr1[i][j]=0;

}

}

}

for(int i=0; i<4; i++)

{

for(int j=0; j<4; j++)

{

printf("%d", arr1[i][j]);

}

printf("\\");

}

printf("\\");

int arr3[4][4];

int c;

printf("Enter a number:\\");

scanf("%d", &c);

for (int i=0; i<4; i++)

{

for(int j=0; j<4; j++)

{

if(j!=i)

{

arr1[i][j]=c;

}

else

{

arr1[i][j]=0;

}

}

}

for(int i=0; i<4; i++)

{

for(int j=0; j<4; j++)

{

printf("%d", arr1[i][j]);

}

printf("\\");

}

printf("\\");

return 0;

}

Explanation:

arr1[][] is for i

arr2[][] is for ii

arr3[][] is for iii

2. Write a C program that generates following outputs. Each of the outputs are nothing-example-1
2. Write a C program that generates following outputs. Each of the outputs are nothing-example-2
User Ricardo Vargas
by
3.1k points