Arrays&strings

                  Arrays 

It is a group of elements with same data type,used to allocate memory in sequential order 

Indexing : property of an array to distinguish all its stored data (because all elements have same name )  . " [ ] "  is called as SUBSCRIPT.

Eg.   arr[5]  ✒ arr[0], arr[1],arr[2],arr[3],arr[4].

Note : Array will always start with 0 index.

                             Array
              Static                    dynamic 

Static array : An array with fixed size  (can't be altered)
                        1. One dimensional
                        2.Two dimensional
                        3. Multi dimensional

Dynamic array: An array whose size can be changed at run time.

One dimensional : Array with single subscript  [ ]

Syntax 
    Data type arrayname [ size] = {value1,value2...n};
Eg ; 
       Int marks [6]={ 100,99,98,97,96,95};

Declaration&Initialisation : 

👉 Incase array is declared but not Intialised it           contains garbage values .
     Eg.  Int marks[6]  
                                Here array is intialised but not      declared so [0],[1]...[4] these values contain            grabage values (dummy values).

👉 Incase array is declared and intialised at least        one value rest will be zero.
       Eug. Int marks[6]={50};
                             [0]=50     rest [1] [2]......[5] will be zero.

👉We can skip index values when initilisation is done perfectly, compiler declares as Compile time.
         Eg: int marks[] = {50,40,30}

Run  time initilisation:
     int marks[3];
            scanf("%d%d%d",&marks[0],&marks[1],&marks[2]);
→In case there are more variables we cant write all so we use looping concept: 
      int marks[i];
         for(i=0;i<10;i++)
    {
        scanf("%d",&marks[i]);
     }
Example:
    Karthik wants to buy 10 items.He want to store the price of all items.Write a program to store 10
values.
Requirements:
  1. Declaration of array size 10
  2. scanf the prices
  3. Display the prices
#include<stdio.h>
void main()
{
int i,item[10];
printf("enter the values");
for(i=0;i<10;i++)
{
    scanf("%d",&item[i]);
}
for(i=0;i<10;i++)
{
    printf("%d,item[%d]",i,item[i]);
}
}
Output:
item[0]=price
.....
....
....
item[9]=price

       
Two- Dimensional arrays:
          Used to represent a tabular data,[] [] rows and columns.
   →Total number of elements = rows*columns 
Syntax:
      Data type  Array name [R.size] [C.size]
     Eg: Matrix of 3*3
           A [3] [3]
Compile time initilisation:
      int A [3] [3]={{1,2,3},{4,5,6},{7,8,9}}
              
Run time initilization:
                    Scanf function is used in any program to initilisation at run time.(use as interative statements).
           

 Strings:
  • It is a collection of characters.
  • A string is called as an array of characters.
  • A string must access by %s access specifier in C.
  • A string always terminated with \0(Null) charatcers.
  • A string always recognized in double quator.
  • A string also consider space as character.
Example: Karthik-contain 7 character
Example: Karthik kamsala-contain 15 character.
Example:char ar[20] -contains 19 characters & 1 Null character
  • All string functions are available in <string.h> header file.
String functions are:

      •  strcat - concatenate two strings.
      • strchr - string scanning operation.
      • strcmp - compare two strings.
      • strcpy - copy a string.
      • strlen - get string length.
      • strncat - concatenate one string with part of another.
      • strncmp - compare parts of two strings.
  • Note:
          strncmp(compares)
  • →compares characters by charactor.
  • →Gives result in 3 cases.
  • Case 1: It first string > second ,result will be "True".
  • Case 2: It first string < second ,result will be "False".
  • Case 3:It first string = second , result will be "0".

Comments