Module4 Questions Answers
Module4 Questions Answers
in
MODULE 4
1. Write the syntax for declaring two dimensional arrays with suitable example.
A two dimensional array is declared as:
data_type array_name[row_size][column_size];
Therefore, a two dimensional m x n array is an array that contains m x n data elements
and each element is accessed using two subscripts, i and j where i<=m and j<=n.
For example, if we want to store the marks obtained by 3 students in 5 different subjects,
then we can declare a two-dimensional array as
int marks[3][5];
A two-dimensional array called ‘marks’ is declared that has m(3) rows and n(5) columns.
The first element of the array is denoted by marks[0][0], second element as marks[0][1], and
so on. Here, marks[0][0] stores the marks obtained by the first student in the first subject,
marks[1][0] stores the marks obtained by the second student in the first subject, and so on.
2. Demonstrate the representation of 2D array in memory with a suitable example.
For example, if we want to store the marks obtained by 3 students in 5 different subjects,
then we can declare a two-dimensional array as
int marks[3][5];
A two-dimensional array called ‘marks’ is declared that has m(3) rows and n(5) columns.
The first element of the array is denoted by marks[0][0], second element as marks[0][1], and
so on. Here, marks[0][0] stores the marks obtained by the first student in the first subject,
marks[1][0] stores the marks obtained by the second student in the first subject, and so on.
The memory representation of above two dimensional array is shown below:
3. Explain how a single row can be passed into a 2D array with an example.
A row of a two-dimensional array can
be passed by indexing the array name
with the row number. When we send
a single row of a two-dimensional
array, then the called function receives
a one-dimensional array. Figure illustrates
how a single row of a two-dimensional array
is passed to the called function.
4. With a neat diagram, Explain three dimensional array. Write a C program to read and
display 2x2x2 array.
Three dimensional array:
A three dimensional m1 x m2 x m3 array is
a collection of m1 * m2 * m3 elements.
Figure shows a three dimensional array
having three pages, four rows and 2 columns.
Program: #include <stdio.h>
int main ( )
{
int i, j, mat[3][3], t_mat [3][3];
printf ( "Enter the elements of the matrix: \n" );
for ( i=0; i<3; i++ )
{
for ( j=0; j<3; j++ )
{
scanf ( "%d", &mat[ i ][ j ] );
} }
printf ( "The elements of the matrix are: \n " );
5. What are strings? Mention reading strings & writing strings along with their syntax.
In C programming, a string is a sequence of characters terminated with a null character.
This means that, after the last character, a null character (‘\0’) is stored to signify the end
of the character array.
H e l l o \0
For example, char str[ ] = “Hello”; This will be stored as
Reading Strings: If we declare a string by writing
char str [100];
Then str can be read by using three ways:
1. using scanf( ) function
The above statement would print only the first three characters in a total field of five
characters. Also these three characters are right justified in the allocated width. To make
the string left justified, we must use a minus sign. For example,
printf( "%-5.3s", str );
The next method is by using puts() function. The string can be displayed by writing
puts( str );
puts( ) is a simple function that overcomes the drawbacks of the printf() function. The puts(
) function writes a line of output on the screen. It terminates the line with a newline
character ('\n'). It returns an EOF (-1) if an error occurs and returns a positive number on
success.
The strings can also be written by calling the putchar() function repeatedly to print a
sequence of single characters.
6. Write a C program to read and display 3 x 3 matrix.
#include <stdio.h>
int main ( )
{
int i, j, mat[3][3];
printf ( "Enter the elements of the matrix: \n" );
for ( i=0; i<3; i++ )
{
for ( j=0; j<3; j++ )
{
scanf ( "%d", &mat[ i ][ j ] );
}
}
printf ( "The elements of the matrix are: \n " );
for ( i=0; i<3; i++)
{
printf ("\n");
for ( j=0; j<3; j++ )
{
char s[100];
int i, vowels=0, consonants=0, len=0;
printf ( "Enter the string: " );
gets ( s );
len = strlen(s);
for ( i=0; i<len; i++ )
{
if ( s[ i ] >= ‘A’ && s[ i ] <= ‘Z’ ) || ( s[ i ] >= ‘a’ && s[ i ] <= ‘z’ )
{
if ( s[ i ] == ‘a’ || s[ i ] == ‘e’ || s[ i ] == ‘i’ || s[ i ] == ‘o’ || s[ i ] == ‘u’ || s[ i ] == ‘A’ || s[ i ]
== ‘E’ || s[ i ] == ‘I’ || s[ i ] == ‘O’ || s[ i ] == ‘U’ )
vowels++;
else
consonants++;
}
}
printf (“ Vowels in the string is = %d”, vowels);
printf (“ Consonants in the string is = %d”, consonants);
return 0;
}
11. Write a C program to find the sum of diagonal elements of a matrix.
##include<stdio.h>
int main( )
{
int r, c, i, j, a[10][10], sum;
printf ( "Enter the order of the matrix \n" );
scanf ( "%d %d", &r, &c );
if ( r != c)
{
row++;
}
for(i=0;i<7;i++)
{
printf("\n");
for (j=0; j<=i; j++)
printf ( "\t %d", arr[i][j]);
}
return 0;
}
13. Write a program to print following pattern.
#include <stdio.h>
int main ( )
{
int i, p;
char str[ ] = "HELLO";
printf ( "\n" );
for ( i=0; i<5; i++)
{
p = i+1;
printf ( "\n % -5 . * s", p, str );
}
printf ( "\n" );
for ( i=4; i>=0; i-- )
{
p = i+1;
printf ( "\n % -5 . * s", p, str );
}
return 0;
}