Top Banner
INTRODUCTION TO ARRAY C++
13

5 ONE DIMENTIONAL ARRAY in C++

Nov 18, 2015

Download

Documents

jelay

xx
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • INTRODUCTION TO ARRAY C++

  • Outline

    IntroductionArraysDeclaring ArraysExamples Using Arrays

    Using looping statementCharactersAssign

    **

  • DEFINITION ARRAYA collection of objects of the same type stored contiguously in memory under one nameMay be type of any kind of variableMay even be collection of arrays!

    For ease of access to any member of arrayFor passing to functions as a group

    **

  • ArrayGroup of consecutive memory

    locations Same name and typeTo refer to an element, specifyArray namePosition numberFormat:

    arrayname[position number]First element at position 0n element array named c:c[0], c[1]...c[n1]

    **

  • Array elements are like normal variables

    c[0] = 3;cout

  • Array # of elements Size of Array Definition ElementSize--------------------------------------------------------------------------char letters[25]; 251 byte25 bytesshort rings[100];1002 bytes200 bytesint miles[84]; 844 bytes336 bytesfloat temp[12]; 12 4 bytes48 bytesdouble distance 1000 8 bytes8000 bytes[1000];**

  • DECLARING ARRAYSWhen declaring arrays, specifyNameType of arrayNumber of elementsarrayType arrayName[numberOfElements];Examples:int c[10]; float myArray[3284];Declaring multiple arrays of same typeFormat similar to regular variablesExample:int b[ 100 ], x[ 27 ]; **

  • EXAMPLES USING ARRAYSInitializersint n[5] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements become 0int n[5] = { 0 } All elements 0If too many a syntax error is produced syntax errorC/C++ arrays have no bounds checkingIf size omitted, initializers determine itint n[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array**

  • SAMPLE PROGRAM USING ARRAY#include using namespace std; int main() { double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28}; cout
  • CREATING AN ARRAY USING LOOPING STATEMENT# include using namespace std;int main (){ int arrayVar [9]; for (int Var = 0; Var
  • ARRAY USING CHARACTERS#include using namespace std;int main(){

    char Color[] = { 'B', 'l', 'a', 'c', 'k' };

    char Country[] = "Philippines";cout

  • TRY THIS!Create a program that will ask the user to input value of length and width of a rectangle and display (compute) the area of a rectangle using array.Array type = floatArray name = rectangleArray element = 3rectangle[0] = lengthrectangle[1] = widthrectangle [2] = area

    **

  • ASSIGN (FINALS)Create a program that ask three input numbers from the user and will determine the highest number using array.

    How will you determine the highest? ( thats part of your assignment) try using if else.

    **