Arrays of Variables

download Arrays of Variables

of 13

Transcript of Arrays of Variables

  • 8/14/2019 Arrays of Variables

    1/13

    Array of VariablesArray of VariablesVikram KulkarniVikram KulkarniMBA - SystemsMBA - Systems

  • 8/14/2019 Arrays of Variables

    2/13

    Arrays of VariablesIn this chapter we will:

    Introduce the array as a structure forstoring large amounts of dataDiscuss common array operationsIntroduce algorithms for searching andsorting arrays

  • 8/14/2019 Arrays of Variables

    3/13

  • 8/14/2019 Arrays of Variables

    4/13

    Array Types1. One Dimensional Array

    2. Two Dimensional Array

    3. Multi Dimensional Arrays3D, 4D and so on

  • 8/14/2019 Arrays of Variables

    5/13

    Example of Multi-DimensionalArray

  • 8/14/2019 Arrays of Variables

    6/13

    Declaration of ArrayIt is the same as Variable

    Declaration The only difference is thenumber of array elements needto be specified in the [ ]

    For two dimensional arrays

    Int count[10];Char name[25];

    Int marks[10][5];

  • 8/14/2019 Arrays of Variables

    7/13

    Array InitializationArrays can be initialized by giving a listof their elementsIf your list contains n elements the

    subscripts will range from 0 to n 1 You do not need to allocate the arrayexplicitly after it is initialized.Int A[5] = {1,2,3,4,5};

    Float S[3] = {1.4,2.6, 8.3};

    Char city[20] = mumbai;Int d[2][3] = {{23,30},{14,76},{5,21}};

    Int arr[] = {5,6,7,0,10,34};

  • 8/14/2019 Arrays of Variables

    8/13

    Array SubscriptsSubscripts are used to access specificarray values.Subscripts starts with 0 and goes up to(n-1).

    Int c[7];counts[0] // first variable in countscounts[1] // second variable in counts

    counts[6] // last variable in countscounts[7] // error trying to access// variable outside countsc0 c6c5c4c3c2c1

  • 8/14/2019 Arrays of Variables

    9/13

    Sample Program#include Void main (){

    Int a[5],i; //single dimension arrayfor (i=0;ia[i];

    }cout

  • 8/14/2019 Arrays of Variables

    10/13

    Sample Program#include

    void main()

    {int A[3][4];

    int i;

    int j;

    for(i=0;i

  • 8/14/2019 Arrays of Variables

    11/13

    Array Operations Copying

    #include Void main (){

    Int a[5],b[5],i;for (i=0;ia[i];

    }b=a; //Copying array

    cout

  • 8/14/2019 Arrays of Variables

    12/13

    Excersies - 1Declare the array of 7 elements.Accept the array elements.Reverse the elements.Show the array.

  • 8/14/2019 Arrays of Variables

    13/13

    Excersies 2Write a program to

    Input student name (max. 20 char)Input mark for 4 subjects.Calculate total marks.Display results as,