1. Explain 2-dimensional arrays with its syntax.
How 2-dimensional arrays are declared
and initialized. (Nov. / Dec. 2023)
Or
Explain with syntax. How 2-Dimensional arrays are declared and initialized. (June/July
2023)
The two-dimensional array can be defined as an array of arrays. The 2D array is organized as
matrices which can be represented as the collection of rows and columns.
Ex: int a[10][10];
Declaration of Two-dimensional arrays
As we declare the variables before they are used in a program, an array must also be
declared before it is used using the following syntax.
Syntax: data_type array_name [row_size][col_size];
data_type: It can be int, float, char etc.
array_name: It is the name of the array.
row_size: It is the number of rows in the array.
col_size: It is the number of columns in the array.
Semicolon is must at the end.
The size used during declaration of the array is useful to reserve the specified memory
locations.
Initialization of 2-dimensional arrays
As we initialize a variable to the required value, we can initialize the individual elements of
the array during initialization.
Syntax: data_type array_name[row_size][col_size]= {
{a1,a2, ---- ,an},
{b1,b2, ---- ,bn},
..…………...,
{z1,z2, ---- ,zn}
};
Ex: int a[4][3]= {{11,22,33}, {44,55,66},{77,88,99},{10,20,30} };
The array has 4 rows and 3 columns
2. Write a C program to find product of 2 matrices. (Nov. / Dec. 2023)
Or
Write a C program to do the matrix multiplication and validate the rules of
Multiplication. (June/July 2023)
# include <stdio.h>
#include<conio.h>
int main()
{
int a[20][20],b[20][20],c[20][20];
int m,n,p,q,i,j,k;
printf("Enter rows and columns of matrix A\n");
scanf("%d%d",&m,&n);
printf("Enter rows and columns of matrix B\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrix multiplication not possible\n");
return 0;
}
printf("Enter elements of matrix A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter elements of matrix B\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("Product of two matrices is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\n",c[i][j]);
}
}
Return 0;
getch();
}
3. What are 2D arrays? How to access the elements of 2-D array. (Jan./Feb. 2023)
The two-dimensional array can be defined as an array of arrays. The 2D array is organized as
matrices which can be represented as the collection of rows and columns.
Access the elements of 2-D array
To read the 2-dimensional array, we have to use two for loops along with scanf(), where the
outer for loop indicates the number of rows to be read and the inner for loop indicates the
number of columns to be read.
To read the “n” array elements of two dimensional array:
for(i=0;i<m;i++)
{
for(j=0;j<n;i++)
{
scanf(“%d”,&a[i][j]);
}
}
To write/print the 2-dimensional array elements, we have to use two for loops along with
printf(), where the outer for loop indicates the number of rows to be printed and the inner
for loop indicates the number of columns to be printed.
To print the “n” array elements of Two dimensional array:
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
4. Explain with example program how a two dimensional array can be passed to the
functions. (June/July 2023)
Figure shows the three ways of using two-dimensional arrays for inter-function
communication.
Passing a Row
A row of a 2D 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 2D array is passed to the called
function
main ( ) // Calling Function
{
intarr [2] [3] = ( (1, 2, 3), (4, 5, 6));
func (arr [1] ) ;
}
void func (intarr []) // Called Function
{
int i;
Calling function
for (i=0; i<5; i++)
Figure 11.34
printf (" %d", arr [i] * 10) ;
}
Passing the Entire Array
To pass a 2D array to a function, we use the array name as the actual parameter. (The same
we did in case of a 1D array). However, the parameter in the called function must indicate
that the array has two dimensions.
5. Demonstrate with example the functions used to read and write strings. (June/July
2023)
Or
What is string? Explain function of ‘C’ to read and write strings. (Nov. /Dec. 2023)
Or
Demonstrate with example the functions used to read and write strings. (June/July
2023)
Or
Explain the string concept and ways of reading (entering) the strings and display the
string with suitable C programming examples. (Jan./Feb. 2023)
“A string is a sequence of characters enclosed within double quotes”.
or
“String is an array of characters and terminated by NULL character which is denoted by
“\0”.
Reading a String using gets( ): input Function
gets() function is used to read a sequence of characters (string) with spaces in between.
The “gets()” function allows us to read an “entire line” of input including whitespace
characters.
Syntax :gets(string);
Example:
char name[20];
printf(“Enter the name:”);
gets(name);
Printing a String using puts( )
“puts()” function is used for printing the given strings.
Whenever we want to display a sequence of characters stored in memory locations on the
screen, then puts() function can be used.
Syntax: puts(string);
Example: char name[20];
printf(“Enter the name:\n”);
gets(name);
printf(“The entered name is:\n”);
puts(name);
Example:
#include<stdio.h>
#include<string.h>
void main()
{
char str[20];
printf(“Enter a String:\n”);
gets(str);
printf(“Entered string is:\n”);
puts(str);
}
6. Write a C program to find the length of the string without using library function.
(Jan./Feb. 2023)
Or
Write a C program to find the length of the string without using built-in function.
(Jan./Feb. 2023)
#include <stdio.h>
#include <conio.h>
int main()
{
char str[100], i = 0, length;
clrscr();
printf("\n Enter the string :");
gets(str);
while(str[i] t= "\0")
i++;
length = i;
printf("\n The length of the string is %d", length);
getch();
return 0;
}
Output
Enter the string : HELLO
The length of the string is : 5
7. Write a C Program to check whether the given string is Palindrome or not without
using built-in function. (Jan./Feb. 2023)
#include <stdio.h>
#include <conio.h>
int main()
{
char str[100];
int i= 0, j, length = 0;
clrscr();
printf("\n Enter the string: “);
gets(str);
while(str[i] != ‘\o’)
{
length++;
i++;
}
i=O;
j = length - 1;
while(i <= length/2)
{
if(str[i] == str[j])
{
i++;
j--;
}
else
break;
}
if(i>=j)
printf(“\n PALINDROME”);
else
printf(“\n NOT APALINDROME”);
return 0;
}
8. Explain sprintf() and sscanf() functions with examples. (Jan./Feb. 2023)
sprintf() Function:
The sprintf() function is used to format a series of characters and store the result
as a string.
It takes the destination string, a format string, and a variable number of
arguments to format and insert into the destination string.
It works similarly to printf(), but instead of printing to the standard output, it
stores the formatted string in the specified buffer.
Exapmple:
#include <stdio.h>
int main()
{
char buffer[50];
int num = 42;
float pi = 3.14159;
// Format and store in buffer
sprintf(buffer, "The number is %d and the value of pi is %.2f", num, pi);
// Print the result
printf("%s\n", buffer);
return 0;
}
sscanf() Function:
The sscanf() function is used for parsing input strings. It reads data from a string
based on the format specifier provided and stores the extracted values in
variables.
It works similarly to scanf(), but reads from a string instead of standard input.
Example
#include <stdio.h>
int main()
{
char input[] = "The number is 42 and the value of pi is 3.14";
int num;
float pi;
// Parse values from the string
sscanf(input, "The number is %d and the value of pi is %f", &num, &pi);
// Print the parsed values
printf("Parsed values: Number = %d, Pi = %.2f\n", num, pi);
return 0;
}
9. What is scanset? With an example, write ‘C’ Program to illustrate the use of caret
symbol in scanset. (Nov. /Dec. 2023) (June/July 2023)
A scanset is a character set specified inside square brackets [] in a format control string
of functions like scanf() or sscanf(). It allows you to specify a set of characters that are
valid for input. The caret symbol (^) has a special meaning when placed at the beginning
of a scanset; it negates the set, indicating that the characters listed should not be
present in the input.
Here's an example program illustrating the use of the caret symbol in a scanset:
#include <stdio.h>
int main()
{
char input[50];
int age;
printf("Enter your age: ");
// Using caret symbol to specify characters that should not be present
// In this example, it means only digits are allowed in the input for age.
scanf("%[^0-9]%d", input, &age);
if (age > 0)
{
printf("You entered a valid age: %d\n", age);
}
else
{
printf("Invalid input. Please enter a valid age.\n");
}
return 0;
}
10. What is string? Explain the ‘C’ functions used to read and write characters.
i. getchar(): Reading a Character ( Input function)
Whenever we want to read a character from the keyboard and store this character
into a memory location, then getchar() function is used.
Syntax: variable_name = getchar();
variable_name is a valid C name that has been declared as char type.
When this statement is encountered, the computer waits until a key is pressed and
assigns this character as a value to getchar function.
ii. putchar(): Writing a character (output function)
Whenever we want to display a character stored in the memory on the screen,
thenputchar() function is used.
Syntax: putchar(variable_name);
variable_name is type char variable containing a character. This statement displays the
character contained in the variable_name at the terminal.
Example: Write a C program to demonstrate the usage of getchar( ) and putchar( ) functions.
#include<stdio.h>
#include<string.h>
void main()
{
char ch;
printf(“Enter a character:\n”);
ch=getchar();
printf(“The entered character is:\n”);
putchar(ch);
}
Output:
Enter a character: W
The entered character is: W
iii. getch():
Whenever we want to read a character from the keyboard without echo (i.e., typed
character will not be visible on the screen), then we use getch().
The character thus entered will be stored in memory location.
Syntax : ch=getch();
iv. putch():
Whenever we want to display a character stored in the memory on the screen, then
putch() function is used.
Syntax : putch(ch);
Example: Write a C program to demonstrate the usage of getch( ) and putch( ) functions.
#include<stdio.h>
#include<string.h>
void main()
{
char ch;
printf(“Enter a character:\n”);
ch=getch();
printf(“The character is:\n”);
putch(ch);
}
Output:
Enter a character:
The character is: A