Unit-3 Questions and Answeres
Unit-3 Questions and Answeres
An Array is a collection of related data items which will share a common name. It is a finite
collection of values of similar data type stored in adjacent memory locations that is accessed
using one common name. Each value of the array is called as an element. Elements are accessed
using index, which is a number representing the position of the element. By finite we mean that,
there are specific number of elements in an array and by similar we mean that, all elements in the
array are of same type.
Declaring an array An array is declared just like an ordinary variable but with the addition of number of
elements that the array should contain. A particular value is indicated by writing a number called 'index or
subscript' number in brackets for after the array name.
Syntax:
The type specifies data type of element that will be contained in an array such as int, float, char, and
double. The size indicates the maximum number of elements that can be stored inside the array.
E.g. The following declaration declares n array of 20 elements where each element is of int type.
int marks[20];
Array can be of any variable type. The ability to use a single name to represent a collection to refer to an
item by specifying the item number enables us to develop efficient programs.
ONE-DIMENSIONAL ARRAY
A list of items can be given one variable name using only one subscript and such a variable is
called a single subscripted variable or one dimensional array.
For Example:
If we want to represent a set of 5 numbers 35, 40, 20, 57, 19 by an array variable 'number' then
we may define the variable number as follows:
number[0]
number[1]
number[2]
number[3]
number[4]
number[0] = 35;
number[1] = 40;
number[2] = 20;
number[3] = 57;
number[4] = 19;
An array contains multiple elements. But the total array is accessed using one name. So to access
array elements you have to use index or subscript and array name. The index is nothing but the
number of the element. It identifies the position of the element in the array.
Example:-
#include<stdio.h>
void main()
{
int a[5],i;
clrscr();
printf("Enter 5 elements into an array:\n");
for(i=0;i<=4;i++)
scanf("%d",&a[i]);
printf("The 5 elements are:\n");
for(i=0;i<=4;i++)
printf("%d\n",a[i]);
}
TWO-DIMENSIONAL ARRAY
A list of items can be given one variable name using two subscripts and such a variable is called
a double subscripted variable or two-dimensional array. So to access twodimension array you
have to have one row index and one column index.
Declaration of Two-dimensional array The general form of Two-dimensional arrays is Syntax:
Initialization of 2D Array
int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} };
OR
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
Example :
#include <stdio.h>
main()
{
int a[2][2],i,j;
printf("Enter 4 elements into array: ");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n\t");
}
2. What is a format string? How do you write format strings for data entry and
display?
A collection of characters is called as string. In the absence of string data type to store strings in
C we have to simulate string using an array of characters. Any group of characters (except
double quote sign) defined between double quotations marks is a constant string.
Character strings are often used to build meaningful and readable programs. The common
operations performed on character strings are:
For example, to store a name, which may be up to 20 characters, you would declare a string as
follows:
char name[20];
char names[10][20]; /* to store 10 names where each name can contain 20 characters */
Character arrays may be initialized when they are declared. C permits a character array to be
initialized in either of the following two forms:
Input/output of Strings
You can read a string from keyboard. C has provided a conversion character %s for inputting
and outputting string. You can read a string using scanf() & print using printf().
The following code snippet is to read name of the user and display the same.
char name[30];
printf("Enter your name: ");
scanf("%s",name);
printf("Welcome %s",name);
In order to read and write string of characters the functions gets() and puts() are used gets()
function reads the string and puts() function takes the string as argument and writes on the screen
gets()
This function reads a sequence of characters entered through the keyword and is useful for
interactive programming. Since a string has no predetermined length gets() needs a way to know
to stop. It reads character until it reaches a new line character. To terminate input at the keyword,
press the ‘Enter’ key.
puts()
The function puts() writes a string argument onto the screen. The puts() can only output a string
of characters. It takes less space and runs faster than printf().
Example:
#include <stdio.h>
void main()
{
char ch[30];
printf("Enter String: ");
gets(ch);
printf("\nEntered String: ");
puts(ch);
}
3. What is the advantage of using arrays? Give syntax for declaration, accessing and
printing one - dimensional array?
An Array is a collection of related data items which will share a common name. It is a finite
collection of values of similar data type stored in adjacent memory locations that is accessed
using one common name. Each value of the array is called as an element. Elements are accessed
using index, which is a number representing the position of the element. By finite we mean that,
there are specific number of elements in an array and by similar we mean that, all elements in the
array are of same type
Below are some advantages of the array:
• In an array, accessing an element is very easy by using the index number.
• The search process can be applied to an array easily.
• 2D Array is used to represent matrices.
• For any reason a user wishes to store multiple values of similar type then the Array can be
used and utilized efficiently.
Disadvantages of Arrays
Array size is fixed: The array is static, which means its size is always fixed. The memory
which is allocated to it cannot be increased or decreased.
Refer Q-No-1
6. Write the syntax for declaring two - dimensional array write a program to access
and print the array elements.
Refer Q.No-1
7. Write a program to find max and min elements from the array.
In C programming, character strings are terminated by NULL Byte. The representation for
NULL Byte is '\0' or '0' (zero) or NULL. This way we wouldn't be able to use NULL character in
ours' strings. C doesn’t have explicit “string data type” This requires to use some character to
terminate the character arrays so the same could be treated as strings.
The fact is NULL character doesn’t have any graphic printable symbol associated with it and
consequently not required and this is why it’s used as a string terminator.
Recall that character strings and string literals are each terminated by NULL byte. String literal,
however, doesn’t have visible NULL terminator. Compiler provides every string literal a NULL
terminator while compiling the program. But character arrays may or may not contain NUL
terminator. Character arrays which contain terminating NULL byte are strings.
For example:
The following declaration and initialization create a string consisting of the word "Hello". To
hold the null character at the end of the array, the size of the character array containing the
string is one more than the number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization then you can write the above statement as follows −
char greeting[] = "Hello";
Following is the memory presentation of the above defined string in C
Both the arrays and structures are classified as structured data types as they provide a mechanism
that enables us to access and manipulate data in a relatively easy manner. But they differ in a
number of ways listed in the table below:
Arrays Structures
11. Write a C program to check whether the given string is palindrome or not?
12. Write C program to read an array of names and to sort them in alphabetical order
13. Discuss difference between structure and unions
It is possible to pass some values from the command line to your C programs when they are
executed. These values are called command line arguments and many times they are important
for your program especially when you want to control your program from outside instead of hard
coding those values inside the code.
The command line arguments are handled using main() function arguments where argc refers to
the number of arguments passed, and argv[] is a pointer array which points to each argument
passed to the program.
Example:-
#include <stdio.h>
int main(int argc, char *argv)
{
printf( "You have entered %d arguments:" , argc);