Arrays and Strings
  • Array Concepts
    • Array Declaration
    • Array Initialization
    • Accessing elements of Array
  • Types of Array
    • One-Dimensional(1-D) Array
    • Multi-Dimensional Array
      • Two-Dimensional(2-D) Array
      • Three-Dimensional(3-D) Array
  • Passing Array to a function as a function argument
    • Passing 1-D Array to a function as a function argument
    • Passing 2-dArray to a function as a function argument
  • Sorting of an array elements
  • Searching
  • Matrix problem
    • Matrix Addition
    • Matrix Transpose
    • Matrix Multiplication
  • Character Array
  • String Handling
    • String Manipulation String
Assignment Set
  1. In what way does an array differ from an ordinary variable?
  2. Explain how array elements are stored in memory.
  3. Differentiate between sorting & searching of array elements.
Board Exam Theory Questions
  1. 2D arrays can be initilized in both the two ways; the compiler time initilization and the run time initilization.

    Compiler Time initilicaiton We can initilize the element of the 2D array during compilation time by

    int a[2][2] = {{1,2},{3,4}};

    Run Time initilizationWe can initilize the element of the 2D array during run time by using loop.

    for(i=0;i <2;i++)
    {
    for(j=0;j <2;j++)
    {
    scanf("%d",&a[i][j]);
    }
    }
  2. Yes, we can pass whole array element from function by using the name of the array. Passing arrays to function is passing by reference.
    Since name of the array itself represent the memory address of first element of an array, the address of rest of array element can be calculated by using the array name and the index value of the element.

    //Passing array to function
    #include< stdio.h>

    void Array(int x[5]); //Function prototype

    void main()
    {
    int a[5] = {1,2,3,4,5},i;
    Array(a); //Calling function
    }

    void Array(int a[5]) //Called Function
    {
    int i;
    for(i=0;i <5;i++)
    {
    printf("%d\t",a[i]);
    }
    }
  3. When we use scanf() to read string, we use the "%s" format specifier without using the "&" to access the variable address because string name itself acts as a pointer to the first variable location. That is the name of the string possesses the memory address of the first element of the string. Therefore, &(address-of) operator is not required while using scanf to scan a string.

    scanf(“%s”,str);

    In the above code, name of the string (str) returns the memory address of the first element of the string (str) i.e. &str[0].

    Next Question String are array of characters and to make the end of the string, C uses the null character (\0) and it is inserted by the compiler automatically at the end of the string. The main drawback of scanf() to use to input string using %s format specifier is that the function terminates as soon as it finds a white space because a null character gets automatically appended on encountering a white space to mark the end of the string.

    Therefore when we input string: "Hello World", only the word "Hello" will be stored into the string. Hence, we cannot input string with white space using scanf() and %s format specifier.

    To overcome this problem we can use following functions:
    gets(str);
    fgets(str, MAX_LIMIT, stdin);
    scanf("%[^\n]s",str);
  4. How two dimensional arrays are created in C programming? [2] [074]
  5. What are overflow and underflow errors in context of array? [2] [073]
  6. What is a string? [2] [2072]
  7. Write down the significance of array in C. [2] [2068]
  8. Describe the limitations of using getchar and scanf functions for reading strings. [2] [2067]
  9. What are the rules that govern the passing of arrays to user defined functions? [3] [2068]
  10. Write a syntax in C-programming with example of stcpy. [2] [2068]
  11. Describe how compiler manages according to the number of initializes and size of an array given by a user in case of 1D array. [3] [072]
  12. How are one dimensional and two dimensional arrays created in C? Explain with examples. [2+2] [2070]

Array

Array is a collective name given to a group of similar quantities. An ordinary variable is able to store one value at a time but an array is able to store more than one value at a time. Declaring the data type, name and maximum number of values to be stored in the array is known as dimensioning the array.

Single Dimensional array

Collective name given to a group of similar quantities of same data type with only one subscript (index).



Two Dimensional array

Collective name given to a group of similar quantities of same data type with two subscript (index).



Multiplication of matrix

Suppose the two matrix are of size m*n and p*q then the product of matrix will be size m*q.
The matrix multiplication is possible only when number of column of 1st matrix is equal to number of rows of 2nd matrix.

Passing array to function

Like passing argument to a function, we can also pass array to a function. While passing array to a function, in calling function name of the array is only required. In called function, name of the array with empty square bracket is required and size is optional.



Passing Multidimensional Arrays to a Function

To pass multidimensional arrays to a function, only the name of the array is passed to the function (similar to one-dimensional arrays). While passing two-dimensional arrays, it is not mandatory to specify the number of rows in the array. However, the number of columns should always be specified.



Note: In C programming, you can pass arrays to functions, however, you cannot return arrays from functions.

String

String are array of character enclosed in the double quotation mark. To make the end of the string, C uses the null character (\0) and it is inserted by the compiler automatically at the end of the string.

Declaration and Initilization
char a[5] = {"abcde"};
char d[] = {"abcde"};
char c[5] = "abcde";
char b[6] = { 'a', 'b', 'c', 'd', 'e','\0' };

Assigning Values to Strings

Arrays and strings do not support the assignment operator once it is declared. For example.
char a[50];
a = "abcde"; // //Error!!

Solution: Use the strcpy() function to copy the string instead.

Read String from the user

The scanf() function is use to reads the sequence of characters until it encounters whitespace (space, newline, tab, etc).

Even though Aamir Khan was entered in the above program, only Aamir was stored in the name string. It's because there was a space after Aamir.

How to read a line of text?

You can use the gets() function to read a line of string. And you can use puts() to display the string.


Passing string to a Function

Strings can be passed to a function in a similar way as arrays like..


String Handling Function

Name Syntax Function & Purpose
strlen() length = strlen(str1) This function is used to determine the lenght of the string except null character.
strrev() strrev(str1) This function reverse the string and store in the same string i.e. str1
strlwr() strlwr(str1) This function is used to convert upper case string to lower case string.
strupr() strupr(str1) This function is used to convert lower case string to upper case string.
strcat() strcat(str1,str2) This function is used to concatinate (combine) the two string i.e. str1 and str2.
strcpy() strcpy(str1,str2) This function is use to copy the source string (str2) to destination string (str1).
strcmp() if(strcmp(str1,str2) == 0)
if(strcmp(str1,str2) > 0)
if(strcmp(str1,str2) < 0)
This function is used to compare two string and return the ASCII value as below.
if(str1 == str2) returns value 0
if(str1 > str2) returns value > 0 i.e. positive value (+)
if(str1 < str2) returns value < 0 i.e. negative value (-)
Programming Set
  1. Write a C program to store N numbers in a one dimensional array and calculate its average with the help of the function.
  2. Write a C program to convert a binary number to decimal with the help of the function int todecimal(int bits[20], int length) where bits is the character array to represent bits of binary numbers and length is the number of bits in the binary number.
  3. Write a program to evaluate transpose of n by n matrix with the help of function void transpose(int matrix[][20], int n) where matrix is the matrix to be transformed and n is the dimension of matrix.
  4. Write a C program for matrix addition with the help of function add(int a[][20], int b[][20], int n, int m) where a and b are matrix to be added and n and m are dimension of a and b.
  5. Write a C program to determine determinant of a square matrix with the help of function int determinant(int a[][], n) where a is the matrix whose determinant is to be found and n is dimension of square matrix.
  6. Write a program to arrange the numbers (array) in ascending order using bubble sort.

INTEGER ARRAY

Single Dimensional Array

  1. WAP to initilize 5 values in array and display the contents of array.
  2. WAP to input 5 numbers and store them in array. Find the sum of all elements of array and print the sum and average.
  3. WAP to input 5 numbers and store them in array. Find the product of all elements of array and print the product.
  4. WAP to enter five numbers and find the product of even number and sum of odd numbers.
  5. WAP to enter five numbers and find the largest number in array.
  6. WAP to enter five numbers and find the smallest number in array.
  7. WAP to enter five numbers, store them in array and display them in ascending order.
  8. WAP to enter n number into one dimensional array and sort them in descending order.
  9. Write a program in C to find the second largest number in an array of n numbers. Read the value of n and the elements of the array from the user. [6] [2069]

Multidimensional Array

  1. WAP to initilize 4 value in two dimensional array and display the value in matrix form.
  2. WAP to input 3x3 matrix and find the sum of all the elements of the given matrix and print the sum.
  3. Write a C program to read two 3x3 matrices from user, add corresponding elements of two matrix and display the result in matrix form. [6] [2070] [073]
  4. Write a program to read square matrix of size NxN and find sum of both diagonals. [4] [074]
  5. WAP to multiply two 3x3 matrix.

INTEGER ARRAY AND FUNCTION

Passing single dimensional array to function

  1. WAP to initilize five values in array and pass them in user defined function. Find the sum of all elements of array and print the sum in main function.
  2. Write a program using a function that returns the largest number from an array of numbers that is passed to the function. [5] [070]
  3. Write a program that passes an array to a function and print the smallest element in 1D array.
  4. Read the ‘n’ number of array and sort them in ascending order, use three functions to read, sort and display the array.

Passing multidimensional array to function

  1. WAP to enter 3x3 matrix, pass it into a user defined function. Find the sum of all elements of the given matrix and print the sum in main function.
  2. Write a C program to display largest elements of a 2D array.
  3. Write a C program to display smallest elements of a 2D array.
  4. WAP to find the transpose of n*n matrix. Your program must include the processing block for finding the transpose of a given m*n matrix. Also display the matrix. [4]
  5. Write a C program to read two 3x3 matrices from user, add corresponding elements of two matrix and display the result in matrix form.
  6. Write a program to multiply two 3x3 matrix. Two matrix are input from main() function and pass to a user defined function. The result is displayed from main() function. [6] [2068]

STRING ARRAY

  1. WAP to input your name and print it.
  2. WAP to input the name of five person and display them.
  3. WAP to input name of five persons and display them in alphabetical order. [4] [2071]

USING STRING HANDLING FUNCTION

  1. WAP to input a string and find its length.
  2. WAP to input a string and copy to another string.
  3. WAP to input two string and combine into one.
  4. WAP to input a string and change into lower case.
  5. WAP to input a string and change into upper case.
  6. WAP to input a string and reverse it.
  7. WAP to input a string and find if it is palindrome or not.
  8. WAP to input two string and compare if it is same or not.

WITHOUT USING STRING HANDLING FUNCTION

  1. WAP to input a string and find its length.
  2. WAP to input a string and copy to another string.
  3. WAP to input two string and combine into one.
  4. WAP to input a string and change into lower case.
  5. WAP to input a string and change into upper case.
  6. WAP to input a string and reverse it.
  7. WAP to input a string and find if it is palindrome or not.
  8. WAP to input two string and compare if it is same or not.

STRING ARRAY AND FUNCTION

  1. WAP to read 5 names of students and sort them alphabetically. The process must be done by the user defined function. [4]
  2. Write a program in C to concatenate two string without using string handling function. Where calculating length of string in program is done using user defined function. [6]
  3. Write a program to read a word from a main function, pass it into a function that will convert all of its characters into upper case if the first character is in lower case and into lower case if the first character is in upper case. Display the converted string from main function. [5] [2072]
  4. Write a C program to read a string and display its reverse. Use user defined function to count number of characters in it and to reverse it. [8] [2068] [2071]
  5. Write a C program to read two strings in main and compare them using user defined function. Display appropriate message from main. [4] [2073]
  6. Write a C program to check whether the given string is palindrome or not. Palindrome should be checked by user defined function. [5] [2074] [Board]
  7. Write a program to read a string and check whether it consists of Alphabet or not. Use user defined function to accomplish the task. [5] [2073]
  8. Write a complete program to insert a string into another string in the location specified by the user. Read the string in main function, pass them to another function along with inserting position and returns the resulting string. [8] [2067]
  9. Write a C program to enter the strings until the user enter “end” and display the list of string in alphabetical order using two dimensional array of character using function. [5] [Board]