CS3251 Programming in C
UNIT - 2
ARRAYS AND STRINGS
1. Define Array and types of Array?
Array is a collection of similar type of values
All values are stored in continuous memory locations
All values share a common name
Linear data structure. The elements are organized in a sequential order.
i)One Dimensional Array
ii)Two Dimensional Array
2. Name any two library functions for handling string
strlen() – finds the length of a string. It returns an integer value. It counts the no. of
characters except null character & returns the count
strlen(str)
strcpy() – copies the source string into destination string. So, the source string should
be enough to store the destination string.
strcpy(source,destination)
3. Declare a float array of size 5 and assign 5 values to it
Declaration : float price[5];
Initialization : float price[5]={2.50,1.25,25.5,5.75,40.0}; (or)
float price[]={1.2,3.4,6.5,7.8,9.8};
4. Give an example for initialization of string array?
String is a character array. Collection of one or more characters- enclosed with in
double quotes.
Declaration : char name[10];
Initialization : char name[10]=”India”;
car name[10]={‘I’,’n’,’d’,’i’,’a’};
The char array is terminated by ‘\0’
5. How a character array is declared
Declaration : char name[n];
This array can store n-1 characters.
Initialization : char name[10]=”India”;
car name[10]={‘I’,’n’,’d’,’i’,’a’};
The char array is terminated by ‘\0’
6. Write example code to declare two dimensional array?
Two dimensional array is an array with two subscript values. First subscript specifies
the row & second subscript specifies the column. Used to process matrix operations.
Declaration : datatype array_name [r][c];
int matrixA[10][10];
This matrixA can store 100 elements in a row major order.
7. What is mean & median of a list of elements?
Mean :Average of the N elements can be computed by
sum of N elements/N
Ex: 2,1,3,4,5
Mean = (2+1+3+4+5)/5 = 3
Median: Middle element of a list. To find the median, the list must be sorted first.
If N is odd then Median= (N+1)/2
Ex: 1,2,3,4,5
Median= 6/2 . The element at 3rd position is the median
If N is even then then Median is the average of
Median= (a[(N+1)/2]+a[(N-1)/2])/2
Ex: 1,2,3,4,5,6
Median=(a[3]+a[2])/2
=4+3/2
=3.5
8. Define Searching?
Searching is a process of finding the position of a given element in a list. The
searching is successful if the element is found. There are two types of searching.
Linear Search
Binary Search
[Link] Sorting?
Sorting is a process of arranging the elements either in ascending order or descending
order.
[Link] the following elements using selection sort method. 23,55,16,78,2?
Step1: Find smallest element in the list & exchange the element with first element of the
list
2,55,16,78,23
Step2: Find second smallest value & exchange it with the second element of the list
2,16,55,78,23
Step 3: Continue the process until all the elements are arranged in the order
2,16,23,78,55
Step 4: 2,16,23,55,78
[Link] out the features of an Array?
1) An array holds elements that have the same data type.
2) Array elements are stored in subsequent memory locations.
3) Two-dimensional array elements are stored row by row in subsequent memory locations.
4) Array name represents the address of the starting element.
5) Array size should be mentioned in the declaration. Array size must be a constant
expression and not a variable.
[Link] to create a two dimentional array?
Two Dimensional Arrays can be thought of as an array of arrays or as a matrix
consisting of rows and columns.
Syntax: data_type array_name[i][j];
Example: int A[10][20];
[Link] an example of an two dimensional array?
Syntax: data_type array_name[i][j];
Example: int A[2][3] = {3, 2, 1, 8, 9, 10};
[Link] is the use of ‘\0’ and ‘%s’?
\0' - is referred to as NULL character or NULL terminator It is the character
equivalent of integer 0(zero) as it refers to nothing In C language it is generally used to mark
an end of a string.
%s – It can print the string using %s format specifier in printf function. It will print
the string from the given starting address to the null '\0' character. String name itself the
starting address of the string. So, if we give string name it will print the entire string.
[Link] are the different ways of Initializing an Array?
There are two different kinds of ways to Initializing an Array.
[Link] Time Initialization
[Link] Initialization
[Link] between one dimentional and two dimentional array?
[Link] address operator used in scanf() statement to read an array?why?
The arguments of the scanf() function are the pointers types, we must provide either
an address of a variable or a pointer (which contains the address of the variable). Therefore, if
we are using a pointer in scanf(), we don't use address of (&) operator, because pointer
contains the address itself
[Link] is the role of strrev()?
The strrev() function is a built-in function in C and is defined in string.h header file. The
strrev() function is used to reverse the given string.
Syntax: char *strrev(char *str);
Example: char A[10]= “Reverse”;
strrev(char *A);
Output: esreveR
[Link] the meaning of String?
A string is a sequence of characters terminated with a null character \0 .
For example: char c[] = "c string";
When the compiler encounters a sequence of characters enclosed in the double
quotation marks, it appends a null character \0 at the end by default.
[Link] to initialize a string?Give an Example?
C does not have a String type to easily create string variables.
Instead of,must use the char type and create an array of characters to make a string:
char greetings[] = "Hello World!";
Note that you have to use double quotes ( " " ).
[Link] between Linear Search and Binary Search?
[Link] any 2 Methods of Sorting?
Insertion Sort
Merge Sort
Selection Sort
Quick Sort
[Link] out any four functions that performed on character strings or string operations?
strcat()
strcpy()
strcmp()
strlen()
[Link] the Purpose and Prototype of strcpy()?
The function prototype of strcpy() is: char* strcpy(char* destination, const char*
source);
The strcpy() function copies the string pointed by source (including the null character)
to the destination.
The strcpy() function also returns the copied string.
[Link] is the Starting index of an Array?
Array index starts from 0 as initially i is 0 which means the first element of the array.
A two-dimensional array in C will follow zero-based indexing, like all other arrays in
C.
PART-B
[Link] the need for array variables. Describe the following with respect to arrays:
i)Declaration of array
ii)accessing an array element.
[Link] a C Program to reorder a one dimensional array of numbers in Descending order?
[Link] a C Program to perform the following matrix operations:
i)Addition
ii)Subtraction
iii)Scaling
iv)Multiplication
[Link] a C Program to calculate mean median for an array of elements?
[Link] a C program for Determinent and Transpose of a Matrix?
[Link] will you initialize a two Dimensional Array?
[Link] about the String Arrays and its manipulation in detail?
8. Write a C program to sort the n numbers using selection sort?
9. Develop a C program to search an element from the array?
10. Describe the following functions with examples. (4+4+4+4)
(i) strlen() (ii) strcpy() (iii)strcat() (iv)strcmp()
11. Write a C program to find whether the given string is palindrome or not without using
string functions.?
12. Discuss about the following :
(i).Advantages and disadvantages of linear and binary search.
(ii).Discuss briefly runtime initialization of a two dimensional array.
13. Explain about the following:
(i).String and character array. (6)
(ii).Initialising a string variables.(4)
(iii).String input and output (6)
[Link] a C Program to compare 2 strings without using the function strcmp()?