Class X Array in C chaper -6




      Chapter 6

      ARRAY IN C




    Excercise

    1. Define array. Why do we use array in computer programs?

    Array is a collection of similar types of data item where elements are contiguous memory location in computer.

    We use array in C program to store multiple values in a single variable, instead of declaring separate variable for each value. For example: to store the marks scored by class X students can be stored in an array.

    3. Write the indices of the first and last element of the following array declaration.

    char city [7] = {‘S’, ‘I’, ‘L’, ‘C’, ‘H’, ‘A’, ‘R,’};

     

    Accessing indices in arrayThe indices of the first elements ‘S’ is O and Last element ‘R’ is 6


    5. Write a C program and declare an integer type array with 7 elements in it. Displays the address of the individual elements in the array

    Program

    #include <stdio.h>

      int element[7]={1,2,3,4,5,6,7};

       int i;

       for(i=0;i<7;i++)

       {

          printf("Address for %d is %d\n",element[i],&element[i]);

       }

         return 0;

        }

     

    6. Write a C program and declare an integer type array, each with capacity 7. Take input only to the first array. Write a loop to copy the elements of the first array to the second one. Display the element of the second array.

    Program

    #include <stdio.h>

    int main()

    {

       int num1[7], num2[7],i;

       //input to num1

       for(i=0;i<7;i++)

       {

           printf("Enter Element %d\n",i+1);

           scanf("%d",&num1[i]);

       }

       // to copy num1 to num2

       for(i=0;i<7;i++)

       {

           num2[i]=num1[i];

       }

       //Display num2

       printf("The elements of Num2 are:\n");

    for(i=0;i<7;i++)

    {

        printf("%d\n",num2[i]);

    }

            return 0;

        }

     

    7. Write a strategy to find the summation of all the even numbers stored in an array. Write a C program for the same. If the array elements are {1,2,4,3,5,6,7,7,8}, the output of the program will be 20.

    Program 1:

    #include <stdio.h>

    int main()

    {

       int num[9]={1,2,4,3,5,6,7,7,8},i,sum=0;

     for(i=0; i<9;i++)

     {

         if(num[i]%2==0)

            sum = sum + num[i];

     }

     printf("%d",sum);     return 0;

        }

    Program 2: Taking from the user

    #include <stdio.h>

    int main()

    {

       int num[9],i,sum=0;

     for(i=0; i<9;i++)

     {

         printf("enter an element %d\n");

         scanf("%d",&num[i]);

     }

     for(i=0; i<9;i++)

     {

         if(num[i]%2==0)

            sum = sum + num[i];

     }

     printf("%d",sum);

     

        return 0;

    }

     

    8. Write a strategy to find the summation of all the even positioned number stored in an array. Write a C program for the same. If the array elements are {1,2,4,3,5,6,7,7,8}, the output of the program will be 18.

    Program1

    #include <stdio.h>

    int n;

       printf("Enter the no of elements\n");

       scanf("%d",&n);

       int num[n],i,sum=0;

       for(i=0;i<n;i++)

       {

           printf("Enter Elemetns\n”);

           scanf("%d",&num[i]);

       }

       for(i=0;i<n;i++)

       {

            if((i+0)%2!=0)

        sum=sum+num[i];

       }

    printf("Sum is %d",sum);

         return 0;

        }

    Program2

    #include <stdio.h>

    int n;

       printf("Enter the no of elements\n");

       scanf("%d",&n);

       int num[n],i,sum=0;

       for(i=0;i<n;i++)

       {

           printf("Enter Elemetn%d\n",i+1);

           scanf("%d",&num[i]);

       }

       for(i=0;i<n;i++)

       {

            if((i+0)%2!=0)

        sum=sum+num[i];

       }

    printf("Sum is %d",sum);

         return 0;

        }

     

     

    9. write the logic to replace only the first occurrence of an element in an array. For example, if the array elements are{1,2,3,4,5,1,2,3} and we want to replace element 3 by 0, the array content becomes {1,2,0,4,5,1,2,3}. Write a complete C program incorporating the logic.

     

    Declare an array as num with size n. take the input to the array. Change the first element of the array as num [2] = 0. Now display the elements of num [n].

     

    Program

    #include <stdio.h>

     

    int main()

    {                                           

        int n;

        printf("Enter the number of elements:\n");

        scanf("%d",&n);

        int num[n],i;

        for(i=0;i<n;i++)

        {

            printf("enter element%d:\n", i+1);

            scanf("%d", &num[i]);

        }

        num[2]=0;

        printf("The Elements of the Array after change\n");

        for(i=0;i<n;i++)

        {

            printf("%d",num[i]);

        }

            return 0;

        }

     

    10. write the logic to replace only the last occurrence of an element in an array. For example, if the array elements are{1,2,3,4,5,1,2,3} and we want to replace element 3 by 0, the array content becomes {1,2,3,4,5,1,2,0}. Write a complete C program incorporating the logic.

    Declare an array as num with size n. take the input to the array. Change the first element of the array as num [n-1] = 0. Now display the elements of num [n].

     

    Program

    #include <stdio.h>

    int main()

    {

        int n;

        printf("Enter the number of elements:\n");

        scanf("%d",&n);

        int num[n],i;

        for(i=0;i<n;i++)

     

        {

     

            printf("enter element%d:\n", i+1);

            scanf("%d", &num[i]);

        }

        num[n-1]=0;

        printf("The Elements of the Array after change\n");

        for(i=0;i<n;i++)

        {

            printf("%d",num[i]);

        }

     

            return 0;

        }

     

    11. Write a C program  to replace all even positioned elements in an integer array by 0. For example, if the array are {1,2,3,9,5,5,7,1,9}, it becomes {1,0,3,0,5,0,7,0,9}.

     

    Program

    #include <stdio.h>

    int main()

    {

        int n;

        printf("Enter the number of elements:\n");

        scanf("%d",&n);

        int num[n],i;

        for(i=0;i<n;i++)

     

        {

            printf("enter element%d:\n", i+1);

            scanf("%d", &num[i]);

        }

        for(i=0;i<n;i++)

        {

            if(i%2==1)

            {

                num[i]=0;

            }

        }

        printf("The Elements of the Array after change\n");

        for(i=0;i<n;i++)

        {

            printf("%d",num[i]);

        }

            return 0;

        }

    12. Write a C program to replace all the odd number in an integer array by 0. For example, if the array elements are {1,2,3,9,5,5,7,1,9}, it becomes {0,2,0,0,0,0,0,0,0}. Write a C program for the same.

    Program

    #include <stdio.h>

    int main()

    {

        int n;

        printf("Enter the number of elements:\n");

        scanf("%d",&n);

        int num[n],i;

        for(i=0;i<n;i++)

     

        {

            printf("enter element%d:\n", i+1);

            scanf("%d", &num[i]);

        }

        for(i=0;i<n;i++)

        {

            if(num[i]%2==1)

            {

                num[i]=0;

            }

        }

        printf("The Elements of the Array after change\n");

        for(i=0;i<n;i++)

        {

            printf("%d",num[i]);

        }

            return 0;

        }

     

    13. Write a C program to store your name and your mother’s name in two different strings. Display them one after another.

    Program

    #include <stdio.h>

    int main()

    {

        char name[20], mother[20];

        printf("Enter your Name\n");

        gets(name);

        printf("Enter Mother's Name\n");

        gets(mother);

        printf("You Entered\n");

        puts(name);

        puts(mother);

     

            return 0;

        }

     

    14. Write a C program to declare 3 integer type arrays to store the mark of 10 students scored in different subjects. Take another integer to store the average marks of the students. The average mark of a student is the average of the mark scored by the student in 3 subjects. This should be calculated and inserted by the program. Then you should display the average marks of the students. The program should also display “PASS” or “Fail” along with the average as per the rule: PASS if average >= 45, Fail otherwise.

     

    #include <stdio.h>

    int main()

    {

       int sub1[10],sub2[10],sub3[10],i,avg;

       for(i=0;i<10;i++)

       {

           printf("Enter student%d marks:\n",i+1);

           printf("Subject 1:\n");

           scanf("%d", &sub1[i]);

           printf("Subject 2:\n");

           scanf("%d", &sub2[i]);

           printf("Subject 3:\n");

           scanf("%d", &sub3[i]);

       }

    for(i=0;i<10;i++)

    {

        avg=(sub1[i]+sub2[i]+sub3[i])/3;

        if(avg>=45)

    {

        printf("Student%d result:\n Average%d\nResult:PASS\n",i+1,avg);

    }

         else

     {

         printf("Student%d result:\n Average%d\nResult:FAIL\n",i+1,avg);

     }

    }

            return 0;

        }

     

    15. Write any two limitations of arrays. Can you store your name and roll number in the same array?

    Any two limitation of arrays are:

    i. Fixed size: We can store only fixed number of elements to an array.

    ii. Homogeneous data: We can store only similar types of data to an array.

    No. We cannot store our name and roll in same array because, an array can store only homogeneous data. As name is a string and roll no is an integer type data so we can’t store them in same array.


    Extra Questions With Answers

    1. An array is a fixed sequence of elements of same data type.

    2. The base address of an array is referred to by the name of the array.

    3. The First element of an array is indices 0

    4. An array is a collection of - same datatype/different data type/ both

    5. Array elements are stored in – scattered memory location/ sequential memory location/ both/ none of them

    6 Array cannot be initialized if the they are – automatic/ static/ external/ none of them

    7. Which of these array elements is referred to be arr[5] -  Sixth/ fifth /fourth

    8. which of these is a valid array declaration- int arr(5);/ float arr[5]; /double[5]arr;

    9. Character are always specified in - double quotes/ braces/single quotes

    10 An array index starts with : - 1 / 0 / 1/ 2

    11. The object of an array is called as – Elements of an array/ Index of an array/ Size of an array/ List of an array

    12.The set of consecutives memory location:- Loop/ Pointer/ Function/ Array

     

     Questions Answers

    1. What type of data can array can only store?

    Array can only store similar type of data.

     

    2. What is fixed size?

    The size of an array is fixed size. For example, in an integer array with size 10, we cannot store 11 integers.

     

    3. What is homogenous data?

    A collection of similar types of data is called homogenous data. For example, collection of integers, collection of characters etc.

     

    4. what is contiguous storage?

    The memory location one after the another is called contiguous storage.

     

    5. How to declared an array in C?

    We can declared an array by providing the type of array followed by array name and within square brackets size of the array. For example, in arr[10];

     

    6. How can we initialized an array in C programing?

    We can initialize an array in C programing in the following ways:

    We can initialized an integer array by writing all the values n braces separated by comma.

    int num[5]  {2,3,4,5,6}

     we can initialized the element of an array by index.

    int num[3];

    num[0]=4;

    num[1]=2;

    num[2]=5;

    We can initialized a character array by writing the values within the single quotes.

    int name[7] = { ‘W’, ‘E’, ‘L’,  ‘C’, ‘O’, ‘M’, ‘E’};

     

    7. How to insert and print array elements?

    We can use loop to insert and print array elements. For example we have an integer array ‘num’ with ‘n’ size we can write the following code segment to insert the elements.

    Inserting array element

    For(int i=0; i<n; i++)

    {

    print(“Enter the %d element\n”,i+1);

    scanf(“%d”, num[i]);

    }

     

    Printing array element

    for(int i=0[ i<n; i++)

    {

    printf(“%d\n”, num[i]);

    }

     

    8. How can an element be searched in an array? Give example

    We can search an element from an array by comparing each array element with the searched element. If any element is matched with the searched element we will display as searched element found, or else we will display as not found.

    Here is a code segment to search an element from an array:

    int item, flag=0;

    printf(“Enter element to be searched\n”);

    scanf(“%d”,&item);

    for(int i=0; i<size; i++)

    {

    if(arr[i]==item)

    flag=1;

    }

    if(flag==1)

    printf(“%d found\n”, item);

    else

    {

    Printf(“%d not found\n”,item);

    }

     5. What are the advantages and disadvantages of an array?

    The advantages of an array are:

    i. We can store multiple values in a same variable.

    ii. It can be declared once and reused multiple times.

    iii. It is easier to declared and use.

    The disadvantages of an array are:

    i. Cannot store multiple types of data at same time.

    ii. The size of the array is fixed once it declared.

    iii. Wastage of memory space if all the elements are not used.

     

    6. Given an array declaration. What type of data can be inserted inside the array?

    Int age[5];

    Only integer can be inserted inside the array.

     

    7. What will be the output of the following program segment?

    int num[5] ={101,  102, 103, 104, 105);

    for(int i=1; i<=5; i++)

    printf(“%d\n”, num[i]);

    102

    103

    104

    105

     

    8. Write the syntax to declared an array.

    data_type array_name [size];

     

    9. What happen when an array is initialized with the number value more than the size of the array.

    Compiler generates an error.

     

    10. What is an element of an array?

    Each data item of an array is called an element.

     

    11. What is array index?

    Each element of an array has a specific memory location this can be accessed its relative location called array index or indices.

     

    12.What is the last index of an array?

    The last index of an array is the size of the array minus 1.

     

    13. What is base address of an array?

    The name of an array is referred as base address of an array.

     

    14. How can you access the nth element of an array?

    We can access the nth element of an array using num[n-1], where num is the name of the array.

     

    15. What is the last character of a character array?

    The last character of a character array is ‘\0’ character. It is called as NULL character.

     

    16. What is the use of Null Character in a character array?

    Null character indicates the end of the valid character of an array.

     

    17. What is the use of string.h header file?

    string.h header file is a dedicated header file that support many library functions for strings.

     

    18. Define gets() and put().

    gets() function is used to take string input from the keyboard and put() function is used to display the string.

     

    19. what is the use of strlen() function?

    strlen() function returns the length of the string, that is number of the characters in the array.

     

    20. What is the limitations of an array?

    The limitations of an array are:

    a. Fixed size: we can store only fixed number of elements inside an array.

    b. Homogenous array: We can store only same type of data inside an array.

    c. Contagious storage:  The elements of an array is stored in contiguous memory location – one after another.

     

    21. What is an Character array?

    An array containing a group of characters is called character array.

     

    22. Write a C program to reverse an character array input by the user.

    [Hint: input Welcome   Output – emocleW]

    #include<stdio.h>

    #include<string.h>

    Int main()

    {

        Char ch[50];

        Int  I, size;

        printf(“Enter a string/word\n”);

        put(ch);

        size = strlen(ch);

        for(i=size-1; i>=0; i--)

        {

          printf(“%c”,ch[i]);

         }

    return 0;

    }

     

    Table of Content