×

10th Class Computer Chapter 04 – Array

10th Class Computer Chapter 04 – Array

Q.1.What is an Array? Explain structure of an array.

Definition:     An array is collection of variables that can store data of same type. Each memory location holds a single value which is called an element of an array. Each array is given a name and element I accessed with a reference to location which is called index or subscript.

General syntax:

                        array name (size of array)

e.g. we have to declare an array named ‘test’ for 10 variable, so

                        test(10)

So when we have to access the element we will use

                        test(1), test(2), test(9) and so on, these are index numbers 1, 2 and 9 and we can access  values in these indexes.

Example: Filling and printing of an array:

Similarly “Scanf” keyword is being use to give input and “printf” to display the values.

#include<stdio.h>

Int main()

{

            int array[3];

            array[0]=23;

            array[1]=2;

            array[2]=31;

            prinf(“%d, %d, %d”, array[0], array[1], array[2]);

}

Array initialization:

Assigning values to an array for the first time is called array initialization. An array can be initialized at the time of its declaration.

Array Declaration:   Int array[5];

Type_of_an_array(space)Name_of_array[size_of_Array];

Array Initialization:

Type_of_an_array Name_of_array[5]={Value1, value2, Value3, Value4, Value5};

Int test[5]= {23, 43, 1, 55, 98};

Q.2. What is Data Structure?

Data structure is a container to store collection of data items in a specific layout. Different data structures are available in C programing language, however in this chapter we will discuss only “ARRAY”.

Q.3. Explain Loop Structure.

If we need to repeat one or more statement, then we use loops, for example, if we want to write Pakistan hundred times on the screen, then instead of writing “printf(“Pakistan”) hundred times, we use loop. Three kind of loop structure are:

  1. For Loop
  2. While Loop
  3. Do While Loop

General Syntax of For Loop: In C programming language, for loop has the following
General Syntax:

            for(initialization;       condition;                   increment/decrement)

                        {

                                                Code to Repeat

                        }

  1. Initialization is the first part to be executed in for loop. In this part we initialize our starting point of variable.
  2. Condition is checked, if condition is true code inside the loop is executed. If it is false then we come out of the loop.
  3. After Executing the Body of the loop, the value of variable is update (increased or Decreased), depending on the logic.

Example:

For (int i=1; i < 4; i=i+1)

            {

                        printf(“This is My First Programming Class.\n”);

            }

Output:

This is My First Programming Class.

This is My First Programming Class.

This is My First Programming Class.

Q. No. 4. What is Nested Loop? Describe its Structure.

Ans: Nested Loop: When we use a loop inside another loop, it is called nested loop structure.

General Structure:

We can observe that code to repeat could:

 for (initialization;     condition;       increment/decrement)

 {

                        for (initialization;      condition;       increment/decrement)

                        {

                                    Code to repeat

                        }

 }

Q. No. 5. Describe the relation between Loop and Array.

Ans: Loop variables can be used as array indexes, so we can use loops to perform different operations on array. If we want to display the whole array, then instead of writing all the elements one by one. Using loops we can easily take input in arrays.

Writing Values:

            int a[10]

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

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

 Reading Values:

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

                        printf(“%d”, a[i]);