Arrays

By Bipin Tiwari

 Important Questions

An array is a data structure that store number of data items as a  single entity.

  1. Write a program to initialize 3-D array and display its elements.
#include<stdio.h>

#include<conio.h>

int main()

 {

      int marks [3] [2] [4] =

   {

     { { 2,4,6,8} , {1,3,5,7}},

    { {9, 2, 5 ,8}, {5, 7 ,8, 2}},

   {{1, 4 ,8, 2}, {2, 4, 5, 3}}

  };

 int i, j ,k ;

 for (i=0; i<3; i<i++)

  {

     print f("Table number: %d" , i);

     for(j=0; j<2; j++)

    {

    print f("Row number: %d", j);

   for(k=0;k<4; k++)

    print f("%d", marks [i] [j] [k]);

    }

      }

  getch();

  return 0;

  }

Output

Table number:0




Row number: 0 2 4 6 8

Row number: 1 1 3 5 7




Table number:1

Row number: 0 9 2 5 8

Row number:1  5  7  8 2




Table number:2

Row number:0 1 4 8 2

Row number:1 2 4 5 3

 

Write a program to read two m*n matrices and display their sum. 

#include<stdio.h>

#include<conio.h>

#define m 3

#define n 3

 int main()

{

  int a [m] [n], b [m] [n], sum [m] [n], i, j;

  print f("Enter First matrix of size %d*%d row by row", m, n);

  for(i=0; i<m; i++)

  for(j=0; j<n; j++)

 scan f("%d", &a [i] [j]);

 print f("Enter second matrix of size %d %d row by row", m, n);

for(i=0; i<m; i++)

  for(j=0; j<n; j++)

scan f("%d", &b [i] [j]);

for(i=0; i<m; i++)

  for(j=0; j<n; j++)

sum [i] [j] = a [i] [j] + b [i] [j];

 print f("The sum matrix is ");

for(i=0; i<m; i++)

{

  for(j=0; j<n; j++)

 print f("%d", sum [i] [j]);

}

getch();

return 0;

};

Output

Enter the first matrix of size 3*3 row by row

     3     4     6

     1       3     6

      6      8      4

Enter the second matrix of size 3*3 row by row

      6      9      1

      1       4      7

       0      5      2

 The sum matrix is:

       9    13     7

       2     7      13

       6      13     6

 string

String is an array of characters (i.e characters are arranged one after another in memory. In other words, a character-array is also known as a string.

Write a program to read name of 5 different persons using  array of strings and display them.

#include<stdio.h>

#include<coino.h>

int main()

 {

   char names [5] [10];

        int i ;

  print f("Enter name of 5 persons")

   for("i=0; i<5;i++)

     scan f("%s", names [i] );

  print f("The names you have entered are");

 for("i=0; i<5;i++)

 print f("%s", names [i]);

 getch();

  return 0;

 }

Output

Enter name of 5 persons

 Ram

 Hari

 Krishna

 Gopal

  Sita

The names you have entered are:

 Ram  Hari   Krishan  Gopal  Sita

 

Important Questions
Comments
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments