Skip to main content

Describe array and string functions and apply them

 .

Array

An array is a variable that can hold multiple value of similar datatype in continuous memory location. The value are accessed by index. The duffel  index of array start from zero and increase continuously by one. 
Syntax :
    <datatype> <variable name> [size];
Example :
    int roll[20];
    float salary[20];
    char name[10];

Types of array
Thera are two types of array which are given bellow :
  • One Dimensional array 
  • Multi Dimensional array

One Dimensional array

One dimensional array can represent in a single row. It is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value. 
Syntax :
    <data type> <variable name> [size];
Example 
  Write a program to enter to number in any array and find largest among them.
#include<stdio.h>
#include<conoi.h>
        void main( )
            { int i, num[10], l;
                    clrscr( );
            printf("Enter 10 number into array: ");
                for(i=0, i<10; i++)
            { scanf("%d", &num[i]); }
                l = num[0];
            for (i=0; i<10; i++)
                { if(num[i]>l)
                    {l = num[i]; } }
            printf("Largest = %d", l);
                    getch( );
                        }

Write a program to enter the n term number into array & sort them in ascending other. 
#include<stdio.h>
#include<conio.h>
          void main( )
                { int n, i, j, num[50], temp;
                        clrscr( );
                printf("Enter nth number: ");
                scanf("%d", &n);
            printf(" Enter nth value into array: ");
                for(i=0; i<n; i++)
            { scanf("%d", &num[i];}
                    /* sorting Loop */
            for (i=0; i<n; i++)
                { for (j=0; j<n; j++)
                {if(num[i]<num[i])
                    { temp = num[i];
                        num[i] = num[i];
                        num[j]= temp; }}}
            printf(" Starting value: ");
                for(i=0; i<n; i++)
            printf("%d\t", num[i]);
                    getch( );
                        }
                        

Multi Dimensional array 

An array which having two or more then two dimensional which specifies row, column and height. In two dimensional array referred to as matrix which contain two dimension row & column.

Syntax
<datatype> <array name> [size] [size] [size]

Example
Write a program to enter 2*2 matrix and calculate sum.
#include<stdio.h>
#include<conid.h>
        void main( )
            { int i, j, s[2][2];
                int a[2][2] = { (2,3), (5,6) };
                int b[2][2] = { (2,4), (5,8) };
                    clrscr( );
        // Matrix adding Loop
            for(i=0; i<2; i++)
                { for(j=0; j<2; j++)
                    { s[i][j] = a[i][j] + b[i][j]; } }
            printf("resultant matrix\n");
                for(i=0; i<2; i++)
                    { for(j=0; j<2; j++)
            { printf("%d\t", s[i][j]); }
                printf("\n"); }
                    getch( );
                        }


Write a program to enter 3*3 matrix and calculate multiplaction.
#include<stdio.h>
#include<conio.h>
          void main( )
                { int i, j, k, a[3][3], b[3][3], c[3][3], s=0;
                        clrscr( );
                printf("Enter first number  matrix 3*3: ")'
                        for(i=0; i<3; i++)
                { scanf("%d", &a[i][j]); } }
                printf("Enter second matrix 3*3: ");
                        for (i=0; i<3; i++)
                        { for (j=0; j<3; j++)
                { scanf("%d", &b[i][j]); }
            // Matrix multiplication
                        for(i=0; i<3; i++)
                        { for(j=0; j<3; j++)
                                s=0;
                { for(k=0; k<3; k++)
                    { s = s + a[i][j] * b[k][j];
                            c[i][j] = s; } }
            printf("Result matrix\n");
                    for(i=0; i<3; i++)
                        { for (j=0; j<3; j++)
            { printf("%d\t", c[i][j]); }
                printf("\n"); }
                    getch( );
                        }


String 

Array of characters arrange one after another in continues memory is called string. A string is always terminated by a NULL character.

String Function

    Function which are used to manipulate string functions. These are following

1. Strlen( ):
    This function used to determine length of string. It return value which denote length of string in which number of character present in string. It not include NULL character.
Syntax :
    ien = strlen(string);

2. Strcpy( ): 
      This function is used to copy one string to another string. The function accept two string as a parameter and copies the second string into first string include NULL character.
    Syntax :
        strcpy(s1, s2);

3. Strcat( ):
    This function is used to concatenates two string i.e. appends one string at the end of other string. The function accept two string as a parameters and store the contents of second string at the end of first string. 
    Syntax :
        strcat(s1, s2);

4. Strcmp( ):
    This function is used to compare two string to find whether string are same or not.
    Syntax :
        stremp(s1, s2);

5. Strlwr( ): 
    This function is used to convert given string into revers other.
    Syntax :
         strlwr(s1);

6. Strupr( ):
    This function is used to convert lower case into upper case.
    Syntax :
        strupr(s1);

7. Strrev( ):
    This function is used to convert given string into revers other.
    Syntax :
        strrev(s1);



Comments

Popular posts from this blog

BCA mathematics -I (Unit I Set)

 .

BCA Mathematics -I (Unit I Complex Number)

 .

BCA Mathematics -I (Unit I Natural Number)

.