Top Banner

of 91

Lec19 21 Arrays

Apr 14, 2018

Download

Documents

Asad Haneef
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
  • 7/30/2019 Lec19 21 Arrays

    1/91

    Ternary/Conditional Operator (?:)

    Syntax

    (condition) ? (if_true) : (if_false)

    Example

    int largest = ((a > b) ? a : b);

  • 7/30/2019 Lec19 21 Arrays

    2/91

    Ternary/Conditional Operator (?:)

    int x=0;

    (x==0) ? (x=20) : (x=30);

    cout

  • 7/30/2019 Lec19 21 Arrays

    3/91

    Ternary Operator Vs If Statement

    The ternary operator is an expression

    whereas

    the if structure is a statement.

    A ternary operator expression's result can beassigned to a variable, the results of an if

    statement cannot.

  • 7/30/2019 Lec19 21 Arrays

    4/91

    Ternary Operator Vs If Statement

    The advantage that the if statement has is

    that you can have multiple clauses, where

    as with the ternary operator you can haveonly two.

  • 7/30/2019 Lec19 21 Arrays

    5/91

    Arrays offer a convenient

    means ofgrouping together

    several related variables

    Arrays

  • 7/30/2019 Lec19 21 Arrays

    6/91

    List of marks

    List of names

  • 7/30/2019 Lec19 21 Arrays

    7/91

    Declaring an array

    typearray_name[size];

    allocates memory for sizevariables

    index of first element is 0

    index of last element is size-1

    sizemust be a constant

  • 7/30/2019 Lec19 21 Arrays

    8/91

    Declaring an array- Example

    Example: int list[10]; allocates memory for 10 integer variables index of first element is 0

    index of last element is 9

    C++ does not perform any bounds checking on arrays

    list[1]

    list[0]

    list[9]

  • 7/30/2019 Lec19 21 Arrays

    9/91

    Arrays

    One- dimensional arrays

    type var_name[size];

    int sample[10];

    double d[30];

    char ch[100];

  • 7/30/2019 Lec19 21 Arrays

    10/91

    Initializing Arrays

    Arrays can be initialized at the time they are

    declared.

    Examples:double taxrate[3] ={0.15, 0.25, 0.3};

    int num[10] ={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

  • 7/30/2019 Lec19 21 Arrays

    11/91

  • 7/30/2019 Lec19 21 Arrays

    12/91

    C++ does not bound check arrays

    int intArray[5]={1, 2, 3, 4, 5};

    for (int i=0; i

  • 7/30/2019 Lec19 21 Arrays

    13/91

    Lecture 20

  • 7/30/2019 Lec19 21 Arrays

    14/91

    Assigning values to an array

    Exampleint list[10];

    int count, i;

    cin >> count;for(int i=0; i> list[i];

    for loops are often used to assign values to an array

  • 7/30/2019 Lec19 21 Arrays

    15/91

    Assigning values to an arrayExample

    int list[10];

    int count, i;

    cin >> count;

    for(int i=0; i> list[i];

    What if count >9?

  • 7/30/2019 Lec19 21 Arrays

    16/91

    Assigning values to an array

    int main() {// this reserves 10 integer elements

    int sample[10];

    int t;

    // load the array

    for(t=0; t

  • 7/30/2019 Lec19 21 Arrays

    17/91

    Arrays

    Output is:

    0 1 2 3 4 5 6 7 8 9

  • 7/30/2019 Lec19 21 Arrays

    18/91

    Summarize Arrays

    An array is an indexed data structure

    An element of an array is accessed using

    the array name and an index

    An array stores a collection of variables

    All variables stored in an array are of the

    same data type

    An individual variable within an array is

    called an elementof the array

  • 7/30/2019 Lec19 21 Arrays

    19/91

    Summarize Arrays

    In C++ all arrays are stored in contiguous memory

    locations

    An index describes theposition of an element within

    an array.

    In C++ all arrays have zeroas the index of their first

    element.

    The name of the array is the address of the first

    element. The index is the offset

  • 7/30/2019 Lec19 21 Arrays

    20/91

    Arrays

    Total size of an array in bytes?

    Total bytes = (number of bytes in type) x (number ofelements)

    e.g. int sample[10];

    sizeof operator

  • 7/30/2019 Lec19 21 Arrays

    21/91

    Arrays

    int a[10];

    cout

  • 7/30/2019 Lec19 21 Arrays

    22/91

    Arrays

    40

    4

    4

    808

    8

  • 7/30/2019 Lec19 21 Arrays

    23/91

    Arrays Review

    Arrays used for grouping of related variables.int a1; int a[5];

    int a2;

    int a3;int a4;

    int a5;

    Individual elements of array accessed byindices.

    Easier to use in loops.

  • 7/30/2019 Lec19 21 Arrays

    24/91

    Arrays Review

    int a[5] = {10, 20, 30, 40, 50};a[0] is 10

    a[1] is 20

    a[2] is 30

    a[3] is 40

    a[4] is 50

    No boundary checking of arrays because of

    efficiency reasons. Error checking can slow down the program

    execution.

  • 7/30/2019 Lec19 21 Arrays

    25/91

    A variable is a named memory location that may

    be assigned a value.

    10

    20

    30

    40

    50

    a[0]

    a[1]

    a[2]

    a[3]

    a[4]

    0x0012F578

    0x0012F57C

    0x0012F580

    0x0012F584

    0x0012F588

  • 7/30/2019 Lec19 21 Arrays

    26/91

    What is wrong here?

    const int x;

    A

  • 7/30/2019 Lec19 21 Arrays

    27/91

    Arraysint myArray[3];

    myArray[0]=0;

    myArray[1]=1;

    myArray[2]=2;

    ORint myArray[3] = {0,1,2};

    OR

    int myArray[] = {0,1,2};

    int myArray[3]={}; //intializes all elements to 0int myArray[]={};//Error:cant allocate an array of constant size 0

  • 7/30/2019 Lec19 21 Arrays

    28/91

    Arrays

    int main()

    {

    int i, min_value, max_value;

    int list[10];

    for(i=0; i

  • 7/30/2019 Lec19 21 Arrays

    29/91

    Arrays

    // find minimum value

    min_value = list[0];

    for(i=0; ilist[i]){

    min_value = list[i];

    }

    }cout

  • 7/30/2019 Lec19 21 Arrays

    30/91

    Arrays// find maximum value

    max_value = list[0];

    for(i=0; i

  • 7/30/2019 Lec19 21 Arrays

    31/91

    Arrays

    int a[10], b[10];

    // ...

    a = b; // errorillegal.

    /*The name of the array is the address of the first

    element. The index is the offset

    So how do we make the contents of one array

    same as the other?*/

  • 7/30/2019 Lec19 21 Arrays

    32/91

    Arrays

    int a[5]={1,2,3,4,5}, b[5];

    for (int i=0; i

  • 7/30/2019 Lec19 21 Arrays

    33/91

    Arrays

    Finding size of an array

    int a[]= {1,2,4,5,7,9,12};

    cout

  • 7/30/2019 Lec19 21 Arrays

    34/91

    Lecture 21

  • 7/30/2019 Lec19 21 Arrays

    35/91

    Strings

    One dimensional arrays are used to create

    character strings

    In C++, a string is defined as a character

    array that is terminated by a null

    A null is specified using \0 and is zero

    Because of the null terminator, it is

    necessary to declare a character array to be

    one characterlonger than the largest stringit will hold.

    Ch A

  • 7/30/2019 Lec19 21 Arrays

    36/91

    Character Arrays

    char word[6] = fruit; //word has size 6

    char list[6] = {f,r,u,i,t, \0};

    //list of characters, not a string

    Not necessary to add null terminator at end of string

    constants.

    Ch A

  • 7/30/2019 Lec19 21 Arrays

    37/91

    Character Arrays

    char word[] = fruit;//word has size 6

  • 7/30/2019 Lec19 21 Arrays

    38/91

    What is wrong with the following?

    char name[5]="class";

    int MyArray[];

  • 7/30/2019 Lec19 21 Arrays

    39/91

    'a' and "a" are different

  • 7/30/2019 Lec19 21 Arrays

    40/91

    Null String

    ""

    Only contains null terminator.

    R di t i f k b d

  • 7/30/2019 Lec19 21 Arrays

    41/91

    Reading strings from keyboard

    The easiest way to read a string entered from

    the keyboard is to make a characterarrayint main()

    {

    char str[80];

    cout > str; // read string from keyboard

    cout

  • 7/30/2019 Lec19 21 Arrays

    42/91

    Reading strings from keyboard

    There is a problem with previous program, if the string has

    whitespace characters

    // Using gets() to read a string from the keyboard. #include

    int main() {

    char str[80];

    cout

  • 7/30/2019 Lec19 21 Arrays

    43/91

    When you send output to cout, it does not

    necessarily get printed immediately. Rather, it

    may wait in a buffer until some event, e.g.

    buffer full enough, reading from input etc.

    Forcing all buffered output to actually be

    printed is known as "flushing" the stream. Aflush can be forced by inserting flush into the

    stream, or inserting endl.

    cout

  • 7/30/2019 Lec19 21 Arrays

    44/91

    HomeWork (hard copy due in

    next class) Get string input (of length no greater than

    10) from keyboard and store in a char array

    usinggets function provided by C++stdio.h

    library

    Print the inverted string

  • 7/30/2019 Lec19 21 Arrays

    45/91

    HomeWork (hard copy due in

    next class) Example:

    Input string: my str Output: rts ym

  • 7/30/2019 Lec19 21 Arrays

    46/91

    Sorting an array

    Lots of applications require sorting

    Algorithm for sorting

    Sorting an array

  • 7/30/2019 Lec19 21 Arrays

    47/91

    Sorting an array

    double dArray[10]={34, 5.6, 0.9, 345.7, 54.1, 23.5, 2.5,

    6.78, 12.4, 13.9};

    Sorting an array

  • 7/30/2019 Lec19 21 Arrays

    48/91

    Sorting an arrayint main(void){ const int size=10;

    int i, j;

    double dArray[size]={34, 5.6, 0.9, 345.7, 54.1, 23.5,2.5, 6.78, 12.4, 13.9};

    double temp;

    for (i=0; i

  • 7/30/2019 Lec19 21 Arrays

    49/91

    Self Test: Sorting an array

    Modify the previous program so that it does not

    do in-place sorting, but instead stores the sortedlist in another array.

  • 7/30/2019 Lec19 21 Arrays

    50/91

    Lecture 22

    Self Test: Sorting an array

  • 7/30/2019 Lec19 21 Arrays

    51/91

    Self Test: Sorting an array

    // Using the bubble sort to order an array.

    int main()

    {

    int nums[10];

    int a, b, t;

    int size;

    size = 10; // number of elements to sort// give the array some random initial values

    for(t=0; t

  • 7/30/2019 Lec19 21 Arrays

    52/91

    Self Test: Sorting an array(Simulate)

    // This is the bubble sort.

    for(a=1; a=a; b--) {

    if(nums[b-1] > nums[b]) { // if out of order

    // exchange elements

    t = nums[b-1];

    nums[b-1] = nums[b];nums[b] = t;

    }

    } // This is the end of the bubble sort.

  • 7/30/2019 Lec19 21 Arrays

    53/91

    Bubble Sort Simulation

  • 7/30/2019 Lec19 21 Arrays

    54/91

    Two Dimensional Arrays

    C++ supports multi-dimensional arrays

    typearray_name[row_size][column_size]

    int matrix[3][4];

    row[0]

    row[1]

    row[2]

  • 7/30/2019 Lec19 21 Arrays

    55/91

    Two dimensional arrays

    int array[2][3] = {{1,2,3},

    {4,5,6}};

    int E[][2]={

    1,2,

    3,4

    };

  • 7/30/2019 Lec19 21 Arrays

    56/91

    Accessing Array Elements

    int matrix[3][4];

    matrix has 12 integer elements

    matrix[0][0] element in first row, first column

    matrix[2][3] element in last row, last column

  • 7/30/2019 Lec19 21 Arrays

    57/91

    Two Dimensional Arrays

    int jimmy[3][5];

    int main() Two Dimensional Arrays

  • 7/30/2019 Lec19 21 Arrays

    58/91

    int main()

    {

    const int rows=3, cols=4;

    int i, j, num[rows][cols];

    for(i=0; i

  • 7/30/2019 Lec19 21 Arrays

    59/91

    Output:

    1 2 3 45 6 7 8

    9 10 11 12

    int main()Two Dimensional Arrays

  • 7/30/2019 Lec19 21 Arrays

    60/91

    int main()

    {

    const int rows=2, cols=6;

    int i, j, num[rows][cols];

    for(i=0; i

  • 7/30/2019 Lec19 21 Arrays

    61/91

    Output:

    1 2 3 4 5 67 8 9 10 11 12

    Self Test: Arrays

  • 7/30/2019 Lec19 21 Arrays

    62/91

    Self Test: Arrays

    Print the transpose of a 2-D array.

    Self Test: Arrays

  • 7/30/2019 Lec19 21 Arrays

    63/91

    Self Test: Arrays

    Print the transpose of a 2-D array

    for(int i=0; i

  • 7/30/2019 Lec19 21 Arrays

    64/91

    Practice Exercise!

    Write down the code to multiply two

    matrices of sizes NxM and MxQ

    respectively.

    Arrays

  • 7/30/2019 Lec19 21 Arrays

    65/91

    Arrays

    int a[10] = {1,2,3,4,0,3,4,6,7,8};

    int x=3; y=1;

    cout

  • 7/30/2019 Lec19 21 Arrays

    66/91

    Arrays

    int a[10] = {1,2,3,4,0,3,4,6,7,8};

    int x=3; y=1;

    cout

  • 7/30/2019 Lec19 21 Arrays

    67/91

    The name of the array is the address

    of the first element. It is a constant.

    int a[5]={13, 5, 6, 34, 6};cout

  • 7/30/2019 Lec19 21 Arrays

    68/91

    The name of the array is the address

    of the first element. It is a constant.

    5

    346

    0x0012FEC4

  • 7/30/2019 Lec19 21 Arrays

    69/91

    char str[]="fruit";

    cout

  • 7/30/2019 Lec19 21 Arrays

    70/91

    r

    it

    fruit

  • 7/30/2019 Lec19 21 Arrays

    71/91

    int twoD[4][2]={

    1, 10,

    2, 13,

    3, 34,

    4, 15

    };

    Picture of array

    twoD[1][1] is

    twoD[2][0] is

  • 7/30/2019 Lec19 21 Arrays

    72/91

    int twoD[4][2]={

    1, 10,

    2, 13,

    3, 34,

    4, 15

    };

    Picture of array

    twoD[1][1] is 13

    twoD[2][0] is 3

  • 7/30/2019 Lec19 21 Arrays

    73/91

    Arrays of strings

    char strings[6][80]={

    "one","two",

    {'t','h','r','e','e',\0},

    "four",

    "five",""

    };

    Picture of array

    cout

  • 7/30/2019 Lec19 21 Arrays

    74/91

    Arrays of strings

    char strings[6][80]={

    "one","two",

    {'t','h','r','e','e', '\0'},

    "four",

    "five",""

    };

    Picture of array

    cout

  • 7/30/2019 Lec19 21 Arrays

    75/91

    Arrays of strings

    char strings[6][80]={

    "one","two",

    {'t','h','r','e','e,\0},

    "four",

    "five",""

    };

    int i=0;

    while(strings[i][0]){

    cout

  • 7/30/2019 Lec19 21 Arrays

    76/91

    char str[]="This is CS";

    int i=0;while(str[i]){

    cout

  • 7/30/2019 Lec19 21 Arrays

    77/91

    T

    h

    is

    i

    s

    C

    S

  • 7/30/2019 Lec19 21 Arrays

    78/91

    What if I insert a zero?

    char str[]="This is 0CS";int i=0;

    while(str[i]){

    cout

  • 7/30/2019 Lec19 21 Arrays

    79/91

    char str[80];

    do{

    gets(str);

    cout

  • 7/30/2019 Lec19 21 Arrays

    80/91

    S g y

    strcpy() strcat() strlen() strcmp()

    #include strcpy(to, from)

    int main()

    {char str[80];

    strcpy(str, "hello");

    cout

  • 7/30/2019 Lec19 21 Arrays

    81/91

    g y

    strcpy() strcat() strlen() strcmp()

    #include strcpy(to, from)

    int main()

    {char str[80];

    strcpy(str, "hello");

    cout

  • 7/30/2019 Lec19 21 Arrays

    82/91

    g y ( )

    int main()

    {char s1[20], s2[10];

    strcpy(s1, "hello");

    strcpy(s2, " there");

    strcat(s1, s2);

    cout

  • 7/30/2019 Lec19 21 Arrays

    83/91

    Output is:hello there

    String Library Functions (strcmp)

  • 7/30/2019 Lec19 21 Arrays

    84/91

    g y ( p)

    strcmp(s1,s2)compares two strings andreturns 0

    if they are equal. If s1 is greater than s2 (dictionaryorder) then a positive number is returned; if it is

    less than s2 then a negative number is returned.

    cout

  • 7/30/2019 Lec19 21 Arrays

    85/91

    strlen(s) returns the length of s (not counting

    null terminator)

    int main()

    {

    char str[80];cout

  • 7/30/2019 Lec19 21 Arrays

    86/91

    char words[3];

    strcpy (words,

    Now, here is a long string.);

    What is wrong here?

  • 7/30/2019 Lec19 21 Arrays

    87/91

    char words[3];

    strcpy (words,

    Now, here is a long string.);

    Will continue to fill whatever memory follows

    last indexed variable ofwords, even thoughthis memory may be used for something else.

    What is wrong here?

  • 7/30/2019 Lec19 21 Arrays

    88/91

    What is wrong with following?

    char str1[80]=i am a string;

    char str2[80]=i am a string;

    if (str1==str2)cout

  • 7/30/2019 Lec19 21 Arrays

    89/91

    What is wrong with following?

    char str1[80]=i am a string;

    char str2[80]=i am a string;

    if (str1==str2)cout

  • 7/30/2019 Lec19 21 Arrays

    90/91

    What is wrong with following?

    char str1[80]="cs";char str2[80];

    str2=str1;

  • 7/30/2019 Lec19 21 Arrays

    91/91

    What is wrong with following?

    char str1[80]="cs";char str2[80];

    str2=str1; //Error

    Use strcpy(str2, str1);