Traversing in array using c

                                         

traversing an element in an array means to print and show all the index of the array.This also can be defined as visiting the array elements




algorithm for traversing an array

step 1. Start

step 2. Set i=LB

step 3. repeat for LB to UB

step 4. apply process to arr[i]

step 5. End of the loap

step 6. stop

VARIABLES

1. i = Loop counter or counter variable for the for loop.

2. arr =array name.

3.LB= Lower bound. ( The index value or simply index of the first element of an array is called its )

4.UB= Upper bound. [ The index of the last element is called its upper bound ]

implementation of the traversing

#include<stdio.h>

int main()

{

    int arr[50];

    int n,i,size;

    printf("enter the size of array");

    scanf("%d",&n);

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

    {

        printf("enter the array elements");

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

    }

    size = sizeof(arr)/sizeof(arr[0]);

    printf("the array elements of the index");

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

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

}

output:

enter the size of array5

enter the array elements7

enter the array elements8

enter the array elements4

enter the array elements3

enter the array elements5

the array elements of the index

 arr[0]=7

 arr[1]=8

 arr[2]=4

 arr[3]=3

 arr[4]=5

Process returned 0 (0x0)   execution time : 12.810 s

Press any key to continue.

Implementation in C++ :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

 
#include <iostream>
using namespace std;
 
int main()
{
    int i, size;
    int arr[] = {53, 99, -11, 5, 102};    //declaring and initializing array "arr"
     
    size = sizeof(arr)/sizeof(arr[0]);
     
    cout << "The array elements are: ";
     
    for(i = 0; i < size; i++)
        cout << "\n" << "arr[" << i << "]= " << arr[i];
     
    return 0;
}
  



Post a Comment

0 Comments