Traversing in a Linear Array

Traversing in a Linear Array

Line 1:   #include<iostream>

Line 2:   #include<conio.h>

Line 3:   using namespace std;

Line 4:   main()


Line 5:        int arr[5]={1,2,3,4,5};    // initialising the elements in an array

Line 6:        int i;                                   // initialising an iteration variable

Line 7:        for(i=0;i<5;i++)             // a loop that goes to repeat the process till the end of the array


Line 8:              cout<<arr[i];        // printing the elements on the screen


Line 9:        getch();



Algorithm for the above program

[Note: LB = Lower Bound: starting value for the iteration,

UB = Upper Bound: ending value for the iteration,

A = name of the array]

Traverse (LB, UB, A)                 // Name of the algorithm and the variable that used in the algorithm

  1. Repeat for K = LB to UB       // This statement is similar to line 7

Apply PROCESS to A[K]  // This statement is similar to line 8

[End of Loop]

  1. Exit                                  // we have to write exit at last as this shows the algorithm have finished.

Complexity: O(n)


PROBLEM 1: Find the number of elements in an array which are greater than 25.

SOLUTION:         Traverse (A, LB, UB, count)

  1. Set count = 0, K = LB        //Setting the value of count and iteration variable
  2. While K<=UB                       //Repeating the operation till it reached UB
  3. if A[K] > 25, then:              //Checking condition for the elements >25
  4. count = count+1.              //counting the elements >25
  5. Set K = K+1                        //Incrementing the iteration variable

[End of while loop]

  1. Exit

Complexity: O(n)


PROBLEM 2: Find out the sum of all the two digit numbers in an array.

SOLUTION:         Traverse (A, N, sum)

  1. Set sum=0                                                 //Setting the value of sum
  2. Repeat K = 0 to N                                   //Repeating the operation
  3. if A[K]>=10 and A[K]<=99, then:      //Checking for two digit number
  4. sum = sum+A[K]                                    //Taking sum of all the two digit numbers

[End of for loop]

  1. Exit

Complexity: O(n)



  1. We can give any name of the algorithm of our wish.
  2. We can use any loop of our own wish; whether for loop of while loop.
  3. Loop can go from LB to UB or 0 to N. There is no difference between these two statements. And N denotes the size of the array.
  4. We have to write all the major variables that used in the algorithm inside the parenthesis just besides the name of the algorithm

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s