1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

All about array

Discussion in 'C' started by imported_crmahato, Mar 12, 2010.

  1. imported_crmahato

    imported_crmahato New Member

    Joined:
    Mar 9, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    Arrays in C Programming

    Array in 'C ' is a storage area where u can store similar type of data items(Homogeneous data)

    ex
    If we have to strore roll no of 5 student then we have to declare five variables let say r1,r2,r3,r4 and r5.
    int r1,r2,r3,r4,r5;

    we can replace it by simply writing int roll[5];

    Declaration of array in c.

    data type -name of array -[ size of array];

    e.g

    int arr[5];
    float arr1[3];

    few valid array declaration in 'C' programming.

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

    here
    arr[0]=1;
    arr[1]=2;
    arr[2]=3;
    arr[3]=4;
    arr[4]=5;
    int arr1[5]={1,2,3};
    in arr1
    arr1[0]=1;
    arr1[1]=2;
    arr1[2]=3;
    arr1[3]=0;
    arr1[4]=0;
    arr1[5]=0;

    First program in array.
     
  2. imported_crmahato

    imported_crmahato New Member

    Joined:
    Mar 9, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    First Array Program
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int i=0;
    int  arr[5]={1,2,3,4,5};
    clrscr();
    for(i=0;i<5;i++)
    {
    printf("%d\t",arr[i]);
    }
    getch();
    }
    o/p 1 2 3 4 5.
     
  3. imported_crmahato

    imported_crmahato New Member

    Joined:
    Mar 9, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    character array

    char arr[5]={'a','e','i','o','u'};


    program that shows o/p as sum of 5 user entered no.
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int i;
    int arr[5];
    clrscr();
    for(i=0;i<5;i++)
    {
    printf("please enter %d no ",i+1);
    scanf("%d",arr[i]);
    }
    for(i=0;i<5;i++)
    {
    sum+=arr[i];
    }
    printf("Sum = %d",sum);
    getch();
    }
    o/p please enter 1 no 10
    please enter 2 no 10
    please enter 3 no 10
    please enter 4 no 10
    please enter 5 no 10

    Sum =50.
     
  4. imported_crmahato

    imported_crmahato New Member

    Joined:
    Mar 9, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    Answer these questions

    let the array is arr[5]={10,20,30,40,50};

    Code:
    1. printf("%d",3[arr]);
    
    2.printf("%d",arr[-2]);
    
    3.printf("%d",arr);
    
    4.printf("%u",(arr+3));
    
    5.printf("%d",*(arr+3));
    
    6.printf("%d",arr[6]);
    
    7.printf("%d",arr[1+2]);
    
     
  5. imported_crmahato

    imported_crmahato New Member

    Joined:
    Mar 9, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    TWO DIMENSIONAL ARRAY:):):):
     
  6. imported_crmahato

    imported_crmahato New Member

    Joined:
    Mar 9, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    Declaration

    int arr[2][2];
    int arr[3][3]={1,2,3,4,5,6,7,8,9};
    int arr[3][3]={{1,2,3},
    {4,5,6},
    {7,8,9}};
    int arr[][3]={1,2,3,4,5,6,7,8,9};


    Explanation How members can be accessed in two dimensional array?


    indexing
    arr[3][3] ={1,2,3,4,5,6,7,8,9};

    arr[0][0]=1 , arr[0][1]=2 ,arr[0][2]=3
    arr[1][0]=4 , arr[1][1]=5 ,arr[1][2]=6
    arr[2][0]=7 , arr[2][1]=8 ,arr[2][2]=9
     
  7. imported_crmahato

    imported_crmahato New Member

    Joined:
    Mar 9, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    C program for two dimensional array

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
     int i,j;
     int arr[3][3]={1,2,3,4,5,6,7,8,9};
     clrscr();
     for(i=0;i<3;i++)
     {
     for(j=0;j<3;j++)
    {
     printf("%d\t" arr[i][j]);
    }
    printf("\n");
    }
    getch();
    }
    
    o/p
     1 2 3
     4 5 6
     7 8 9 
    
     
  8. imported_crmahato

    imported_crmahato New Member

    Joined:
    Mar 9, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    C program for two dimensional array

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
     int i,j;
     int arr[3][3]={1,2,3,4,5,6,7,8,9};
     clrscr();
     for(i=0;i<3;i++)
     {
     for(j=0;j<3;j++)
    {
     printf("%d\t" *(*(arr+i)+j));
    }
    printf("\n");
    }
    getch();
    }
    
    o/p
     1 2 3
     4 5 6
     7 8 9
    
     
  9. imported_crmahato

    imported_crmahato New Member

    Joined:
    Mar 9, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    Explanation of two dimensional array

    let array be arr[3][3] ={{1,2,3},
    {4,5,6},
    {7,8,9};

    then structure of array is like

    arr[0][0]=1 arr[0][1]=2 arr[0][2]=3
    arr[1][0]=4 arr[1][1]=5 arr[1][2]=6
    arr[2][0]=7 arr[2][1]=8 arr[2][2]=9


    if you write arr then you are in first row
    (arr+1) then you are in the beginning of second row
    (arr+2) then you are in the beginning of last row
    *(arr) gives you o/p 1
    *(arr+1) gives you o/p 4
    *(arr+2) gives you o/p 7
    *(*(arr+1)+2) gives you o/p 6
     
  10. imported_crmahato

    imported_crmahato New Member

    Joined:
    Mar 9, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
  11. imported_crmahato

    imported_crmahato New Member

    Joined:
    Mar 9, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    String is array of character terminated with a null character

    char str[]={'h' ,'e','l','l','o','\0'};

    char str1[]="chittaranjan";

    equivalent to

    char str1[]={'c','h','i','t','t','a','r','a','n','j','a','n','\0'};


    Standard Library functions to handle strings.

    defined under
    string.h

    1)strlen() returns length of the string

    2)strcpy() copies one sourse string to target string including the terminating null character

    3)strcat() concatenates two string

    4)strcmp() compares two string arguments
     
  12. imported_crmahato

    imported_crmahato New Member

    Joined:
    Mar 9, 2010
    Messages:
    23
    Likes Received:
    0
    Trophy Points:
    0
    small program to understand the functions.

    strlen()
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<conio.h>
    void main()
    {
     char str[32]="hello, world";
     clrscr();
     printf("%d",strlen(str));
     getch();
    
    
    
    o/p : 12

    strcpy()
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<conio.h>
    void main()
    {
    char str1[15],str2[15];
    clrscr();
    printf("Enter first string:");
    scanf("%s",str1);
    strcpy(str2,str1);
    printf("%s",str2);
    getch();
    }
    o/p Enter first string: Chittaranjan
    Chittaranjan
     

Share This Page