esc101 s 06 arrays

Upload: sushantmukhija

Post on 03-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 ESc101 S 06 Arrays

    1/292

    Arrays

    Fundamentals of Computing (ESc 101A)Lecture Notes 6

    Raghunath [email protected]

    Indian Institute of Technology, Kanpur

    Raghunath Tewari [email protected] ESc101A

    http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    2/292

    Arrays

    ArraysDefinition and Notation

    Raghunath Tewari [email protected] ESc101A

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    3/292

    Arrays

    ArraysDefinition and Notation

    Arrays are variables that contain a list of values (multiple) ofthe same type.

    Raghunath Tewari [email protected] ESc101A

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    4/292

    Arrays

    ArraysDefinition and Notation

    Arrays are variables that contain a list of values (multiple) ofthe same type.

    For example, considerint ar[10];

    Raghunath Tewari [email protected] ESc101A

    A

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    5/292

    Arrays

    ArraysDefinition and Notation

    Arrays are variables that contain a list of values (multiple) ofthe same type.

    For example, considerint ar[10];

    The above statement, declares an array ar of type int, andallocates memory to store 10 different values.

    - Name: ar- Type: int- Size: 10

    Raghunath Tewari [email protected] ESc101A

    A

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    6/292

    Arrays

    ArraysDefinition and Notation

    Arrays are variables that contain a list of values (multiple) ofthe same type.

    For example, considerint ar[10];

    The above statement, declares an array ar of type int, andallocates memory to store 10 different values.

    - Name: ar- Type: int- Size: 10

    Question: If the array ar is storing 10 values, how do we referto them individually?

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    7/292

    Arrays

    ArraysDefinition and Notation

    Arrays are variables that contain a list of values (multiple) ofthe same type.

    For example, considerint ar[10];

    The above statement, declares an array ar of type int, andallocates memory to store 10 different values.

    - Name: ar- Type: int- Size: 10

    Question: If the array ar is storing 10 values, how do we referto them individually?

    The 10 values are referred by ar[0], ar[1], ar[2],ar[3], ar[4], ar[5], ar[6], ar[7], ar[8] and ar[9].

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://goforward/http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    8/292

    Arrays

    ArraysDefinition and Notation

    Arrays are variables that contain a list of values (multiple) ofthe same type.

    For example, considerint ar[10];

    The above statement, declares an array ar of type int, andallocates memory to store 10 different values.

    - Name: ar- Type: int- Size: 10

    Question: If the array ar is storing 10 values, how do we referto them individually?

    The 10 values are referred by ar[0], ar[1], ar[2],ar[3], ar[4], ar[5], ar[6], ar[7], ar[8] and ar[9].

    Note that the indices run from0 to 9.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    9/292

    Arrays

    ArraysDeclaration and Initializaton

    Declaration:int ar[10];

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://goforward/http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    10/292

    Arrays

    ArraysDeclaration and Initializaton

    Declaration:int ar[10];

    Arrays can be initialized in two ways:

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://goforward/http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    11/292

    y

    ArraysDeclaration and Initializaton

    Declaration:int ar[10];

    Arrays can be initialized in two ways:

    - ar[0] = 5, ar[1]=8, ar[2]=-3;

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    12/292

    y

    ArraysDeclaration and Initializaton

    Declaration:int ar[10];

    Arrays can be initialized in two ways:

    - ar[0] = 5, ar[1]=8, ar[2]=-3;In this case, the array must declared at an earlier step andshould have at the capability to hold at least 3 values.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://goforward/http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    13/292

    ArraysDeclaration and Initializaton

    Declaration:int ar[10];

    Arrays can be initialized in two ways:

    - ar[0] = 5, ar[1]=8, ar[2]=-3;In this case, the array must declared at an earlier step andshould have at the capability to hold at least 3 values.

    - int ar[] = {5,8,-3};

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    14/292

    ArraysDeclaration and Initializaton

    Declaration:int ar[10];

    Arrays can be initialized in two ways:

    - ar[0] = 5, ar[1]=8, ar[2]=-3;In this case, the array must declared at an earlier step andshould have at the capability to hold at least 3 values.

    - int ar[] = {5,8,-3};In this case, declaration and initialization happens in the same

    statement. However, the array has a size 3.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://goforward/http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    15/292

    ArraysDeclaration and Initializaton

    Declaration:int ar[10];

    Arrays can be initialized in two ways:

    - ar[0] = 5, ar[1]=8, ar[2]=-3;In this case, the array must declared at an earlier step andshould have at the capability to hold at least 3 values.

    - int ar[] = {5,8,-3};In this case, declaration and initialization happens in the same

    statement. However, the array has a size 3.In C, arrayscannotbe declared with a variable size. Exampleint var_arr[x];

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    16/292

    ArraysDeclaration and Initializaton

    Declaration:int ar[10];

    Arrays can be initialized in two ways:

    - ar[0] = 5, ar[1]=8, ar[2]=-3;In this case, the array must declared at an earlier step andshould have at the capability to hold at least 3 values.

    - int ar[] = {5,8,-3};In this case, declaration and initialization happens in the same

    statement. However, the array has a size 3.In C, arrayscannotbe declared with a variable size. Exampleint var_arr[x]; THIS IS INCORRECT

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    17/292

    ArraysBasic Facts and Importance of Arrays

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    18/292

    ArraysBasic Facts and Importance of Arrays

    You mustnevertryaccessing an array outsideits bounds.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    19/292

    ArraysBasic Facts and Importance of Arrays

    You mustnevertryaccessing an array outsideits bounds.

    If an array has size 100,

    you can only use theindices from 0 to 99.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    20/292

    ArraysBasic Facts and Importance of Arrays

    You mustnevertryaccessing an array outsideits bounds.

    If an array has size 100,

    you can only use theindices from 0 to 99.

    Use of arrays make it possible to store lots of values in aprogram.(Think of writing a program that takes 100 numbers andarranges them in ascending order. Without arrays, you needto declare 100 separate integers. )

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    21/292

    ArraysBasic Facts and Importance of Arrays

    You mustnevertryaccessing an array outsideits bounds.

    If an array has size 100,

    you can only use theindices from 0 to 99.

    Use of arrays make it possible to store lots of values in aprogram.(Think of writing a program that takes 100 numbers andarranges them in ascending order. Without arrays, you needto declare 100 separate integers. )

    Arrays makes it easier to process the values stored in onearray variable.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    22/292

    Motivation for using ArrayExample: Computing Maximum I

    Maximum numberGet the number of students in class from the user (say n) as input.Get marks of every student and display the maximum.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    23/292

    Motivation for using ArrayExample: Computing Maximum I

    Maximum numberGet the number of students in class from the user (say n) as input.Get marks of every student and display the maximum.

    1 int main(){

    2 int n, a, max=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)

    9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12 return 0;

    13 }

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    24/292

    Motivation for using ArrayExample: Computing Maximum I

    Maximum numberGet the number of students in class from the user (say n) as input.Get marks of every student and display the maximum.

    1 int main(){

    2 int n, a, max=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12 return 0;

    13 }

    Table : Variable Table

    n i a max

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    25/292

    Motivation for using ArrayExample: Computing Maximum I

    Maximum numberGet the number of students in class from the user (say n) as input.Get marks of every student and display the maximum.

    1 int main(){

    2 int n, a, max=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12 return 0;

    13 }

    Table : Variable Table

    n i a max

    Before the loop 5 0

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    26/292

    Motivation for using ArrayExample: Computing Maximum I

    Maximum numberGet the number of students in class from the user (say n) as input.Get marks of every student and display the maximum.

    1 int main(){

    2 int n, a, max=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12 return 0;

    13 }

    Table : Variable Table

    n i a max

    Before the loop 5 0

    After 1st iteration 5 0 4 4

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    27/292

    Motivation for using ArrayExample: Computing Maximum I

    Maximum numberGet the number of students in class from the user (say n) as input.Get marks of every student and display the maximum.

    1 int main(){

    2 int n, a, max=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12 return 0;

    13 }

    Table : Variable Table

    n i a max

    Before the loop 5 0

    After 1st iteration 5 0 4 4After 2nd iteration 5 1 2 4

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    28/292

    Motivation for using ArrayExample: Computing Maximum I

    Maximum numberGet the number of students in class from the user (say n) as input.Get marks of every student and display the maximum.

    1 int main(){

    2 int n, a, max=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12 return 0;

    13 }

    Table : Variable Table

    n i a max

    Before the loop 5 0

    After 1st iteration 5 0 4 4After 2nd iteration 5 1 2 4

    After 3rd iteration 5 2 13 13

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    29/292

    Motivation for using ArrayExample: Computing Maximum I

    Maximum numberGet the number of students in class from the user (say n) as input.Get marks of every student and display the maximum.

    1 int main(){

    2 int n, a, max=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12 return 0;

    13 }

    Table : Variable Table

    n i a max

    Before the loop 5 0

    After 1st iteration 5 0 4 4After 2nd iteration 5 1 2 4

    After 3rd iteration 5 2 13 13

    After 4th iteration 5 3 4 13

    Raghunath Tewari [email protected] ESc101A

    Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    30/292

    Motivation for using ArrayExample: Computing Maximum I

    Maximum numberGet the number of students in class from the user (say n) as input.Get marks of every student and display the maximum.

    1 int main(){

    2 int n, a, max=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12 return 0;

    13 }

    Table : Variable Table

    n i a max

    Before the loop 5 0

    After 1st iteration 5 0 4 4After 2nd iteration 5 1 2 4

    After 3rd iteration 5 2 13 13

    After 4th iteration 5 3 4 13

    After 5th iteration 5 4 12 13

    Raghunath Tewari [email protected] ESc101A

    Arrays

    M i i f i A

    http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    31/292

    Motivation for using ArrayExample: Computing Maximum II

    1 int main(){2 int n, a, max=0, i;

    3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)

    9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12

    return 0;13 }

    Raghunath Tewari [email protected] ESc101A

    Arrays

    M i i f i A

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    32/292

    Motivation for using ArrayExample: Computing Maximum II

    1 int main(){2 int n, a, max=0, i;

    3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)

    9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12

    return 0;13 }

    The for loop (Line 5 10)

    Raghunath Tewari [email protected] ESc101A

    Arrays

    M ti ti f i A

    http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    33/292

    Motivation for using ArrayExample: Computing Maximum II

    1 int main(){2 int n, a, max=0, i;

    3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)

    9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12

    return 0;13 }

    The for loop (Line 5 10)

    - reads a number,

    Raghunath Tewari [email protected] ESc101A

    Arrays

    M ti ti f i A

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    34/292

    Motivation for using ArrayExample: Computing Maximum II

    1 int main(){2 int n, a, max=0, i;

    3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)

    9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12 return 0;

    13 }

    The for loop (Line 5 10)

    - reads a number,

    - if greater than previousmaximum,

    Raghunath Tewari [email protected] ESc101A

    Arrays

    M ti ti f i A

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    35/292

    Motivation for using ArrayExample: Computing Maximum II

    1 int main(){2 int n, a, max=0, i;

    3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)

    9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12 return 0;

    13 }

    The for loop (Line 5 10)

    - reads a number,

    - if greater than previousmaximum,

    - it replaces it.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Motivation for using Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    36/292

    Motivation for using ArrayExample: Computing Maximum II

    1 int main(){2 int n, a, max=0, i;

    3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)

    9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12 return 0;

    13 }

    The for loop (Line 5 10)

    - reads a number,

    - if greater than previousmaximum,

    - it replaces it.

    Note: When the program readsan input from the user, into thevariablea, the previous value

    stored in a is lost.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Motivation for using Array

    http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    37/292

    Motivation for using ArrayExample: Computing Maximum II

    1 int main(){2 int n, a, max=0, i;

    3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0;i max)

    9 max = a;

    10 }

    11 printf("Max mark is %d\n",max);

    12 return 0;

    13 }

    The for loop (Line 5 10)

    - reads a number,

    - if greater than previousmaximum,

    - it replaces it.

    Note: When the program readsan input from the user, into thevariablea, the previous value

    stored in a is lost.

    Now what if we want to write a program that computes howmany students got the maximum marks?

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Motivation for using Array

    http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    38/292

    Motivation for using ArrayExample: Frequency of Maximum Number I

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    39/292

    Motivation for using ArrayExample: Frequency of Maximum Number I

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    40/292

    Motivation for using ArrayExample: Frequency of Maximum Number I

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    41/292

    Motivation for using ArrayExample: Frequency of Maximum Number I

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    42/292

    Motivation for using ArrayExample: Frequency of Maximum Number I

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    43/292

    Motivation for using ArrayExample: Frequency of Maximum Number I

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    44/292

    Motivation for using ArrayExample: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    45/292

    ot at o o us g ayExample: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    46/292

    g yExample: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    47/292

    g yExample: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    48/292

    g yExample: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    49/292

    g yExample: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    50/292

    Example: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    51/292

    Example: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    52/292

    Example: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    53/292

    Example: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    54/292

    Example: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    55/292

    Example: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    56/292

    Example: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    57/292

    Example: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    58/292

    Example: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    59/292

    Example: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    60/292

    Example: Frequency of Maximum Number II

    1 int main(){

    2 int n, a, max=0, mark[500], count=0, i;3 printf("How many students? ");

    4 scanf("%d",&n);

    5 for(i=0; i max)

    10 max = a;

    11 }

    12 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    61/292

    Lab Exam:

    12th Feb (Wednesday), 2pm 6pm: Sections A1, A2, A3, A7,A8, A9.13th Feb (Thursday), 2pm 6pm: Sections A4, A5, A6, A10,A11.

    Do not use Feedback Form for posting question.

    If you have questions, send email.

    Programs discussed in class uploaded on Lecture Notes page.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Factorial of a NumberProblem Statement and C Code

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    62/292

    Problem Statement and C Code

    Factorial

    The factorial of a non-negativenumber is defined as follows:

    0! = 1

    n! = n (n 1)! for n 1.

    Note factorial is a fast growingfunction.

    1 #include

    2

    3 int main(){

    4 int n, f=1, i;

    5 printf("Enter number ");6 scanf("%d",&n);

    7

    8 for (i=1; i

  • 8/12/2019 ESc101 S 06 Arrays

    63/292

    Datatypes to store integers we have seen int and longint.

    The largest datatype in C to store integers is lont long int.

    Uses 64 bits. Integers between263 to 263 1.What if we want to manipulate integers beyond this range?

    Read the digits of the numbers as characters and store thedigits of the number in an array.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Handling Very Large IntegersC Program: Reading a Large Number

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    64/292

    C Program: Reading a Large Number

    1 int main(){

    2 char ch, arr[100];

    3 int n=0, i;

    4 do{

    5 scanf("%c",&ch);

    6 arr[n] = ch;

    7 n++;

    8 } while((ch >= 0) && (ch

  • 8/12/2019 ESc101 S 06 Arrays

    65/292

    C Program: Reading a Large Number

    1 int main(){

    2 char ch, arr[100];

    3 int n=0, i;

    4 do{

    5 scanf("%c",&ch);

    6 arr[n] = ch;

    7 n++;

    8 } while((ch >= 0) && (ch

  • 8/12/2019 ESc101 S 06 Arrays

    66/292

    g g g

    1 int main(){

    2 char ch;

    3 int n=0, i, arr_int[100];

    4 do{

    5 scanf("%c",&ch);

    6 arr_int[n] = ch - 0;

    7

    n++;8 } while((ch >= 0) && (ch

  • 8/12/2019 ESc101 S 06 Arrays

    67/292

    g g g

    1 int main(){

    2 char ch;

    3 int n=0, i, arr_int[100];

    4 do{

    5 scanf("%c",&ch);

    6 arr_int[n] = ch - 0;

    7

    n++;8 } while((ch >= 0) && (ch

  • 8/12/2019 ESc101 S 06 Arrays

    68/292

    g g g

    1 int main(){

    2 char ch;

    3 int n=0, i, arr_int[100];

    4 do{

    5 scanf("%c",&ch);

    6 arr_int[n] = ch - 0;

    7

    n++;8 } while((ch >= 0) && (ch

  • 8/12/2019 ESc101 S 06 Arrays

    69/292

    1 int main(){

    2 char ch;

    3 int n=0, i, arr_int[100];

    4 do{

    5 scanf("%c",&ch);

    6 arr_int[n] = ch - 0;

    7

    n++;8 } while((ch >= 0) && (ch

  • 8/12/2019 ESc101 S 06 Arrays

    70/292

    1 int main(){

    2 char ch;3 int n=0, i, arr_int[100], digitsum=0;

    4 do{

    5 scanf("%c",&ch);

    6 arr_int[n] = ch - 0;

    7 n++;

    8 } while((ch >= 0) && (ch

  • 8/12/2019 ESc101 S 06 Arrays

    71/292

    ProblemGenerate all prime numbers up to a given upper limit.

    A Greek mathematician named Eratosthenescame up with a

    simple idea to solve the above problem.His idea was as follows:

    - On a piece of paper, write down all the integers starting from2 till the upper bound.

    - Starting from 2 strike off all multiples of 2, except 2.

    - Next, find the first number that has not been struck and strikeoff all its multiples, except the number.- Continue until you cannot strike out any more numbers.- The numbers that have not been struck, are PRIMES.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Generating Prime NumbersSieve of Eratosthenes Example

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    72/292

    2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 20

    21 22 23 24 25 26 27 28 29 30

    31 32 33 34 35 36 37 38 39 40

    41 42 43 44 45 46 47 48 49 5051 52 53 54 55 56 57 58 59 60

    61 62 63 64 65 66 67 68 69 70

    71 72 73 74 75 76 77 78 79 80

    81 82 83 84 85 86 87 88 89 90

    91 92 93 94 95 96 97 98 99 100

    Table : Listing all numbers from 2 to 100. Initially unmarked.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Generating Prime NumbersSieve of Eratosthenes Example

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    73/292

    2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 20

    21 22 23 24 25 26 27 28 29 30

    31 32 33 34 35 36 37 38 39 40

    41 42 43 44 45 46 47 48 49 5051 52 53 54 55 56 57 58 59 60

    61 62 63 64 65 66 67 68 69 70

    71 72 73 74 75 76 77 78 79 80

    81 82 83 84 85 86 87 88 89 90

    91 92 93 94 95 96 97 98 99 100

    Table : Marking all unmarked multiples of 2 starting from 4.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Generating Prime NumbersSieve of Eratosthenes Example

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    74/292

    2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 20

    21 22 23 24 25 26 27 28 29 30

    31 32 33 34 35 36 37 38 39 40

    41 42 43 44 45 46 47 48 49 5051 52 53 54 55 56 57 58 59 60

    61 62 63 64 65 66 67 68 69 70

    71 72 73 74 75 76 77 78 79 80

    81 82 83 84 85 86 87 88 89 90

    91 92 93 94 95 96 97 98 99 100

    Table : Marking all unmarked multiples of 3 starting from 6.

    Raghunath Tewari [email protected] ESc101A Arrays

    Generating Prime NumbersSieve of Eratosthenes Example

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    75/292

    2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 20

    21 22 23 24 25 26 27 28 29 30

    31 32 33 34 35 36 37 38 39 40

    41 42 43 44 45 46 47 48 49 5051 52 53 54 55 56 57 58 59 60

    61 62 63 64 65 66 67 68 69 70

    71 72 73 74 75 76 77 78 79 80

    81 82 83 84 85 86 87 88 89 90

    91 92 93 94 95 96 97 98 99 100

    Table : Marking all unmarked multiples of 5 starting from 10.

    Raghunath Tewari [email protected] ESc101A Arrays

    Generating Prime NumbersSieve of Eratosthenes Example

    http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    76/292

    2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 20

    21 22 23 24 25 26 27 28 29 30

    31 32 33 34 35 36 37 38 39 40

    41 42 43 44 45 46 47 48 49 5051 52 53 54 55 56 57 58 59 60

    61 62 63 64 65 66 67 68 69 70

    71 72 73 74 75 76 77 78 79 80

    81 82 83 84 85 86 87 88 89 90

    91 92 93 94 95 96 97 98 99 100

    Table : Marking all unmarked multiples of 7 starting from 14.

    Raghunath Tewari [email protected] ESc101A Arrays

    Generating Prime NumbersSieve of Eratosthenes Example

    http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    77/292

    2 3 4 5 6 7 8 9 10

    11 12 13 14 15 16 17 18 19 20

    21 22 23 24 25 26 27 28 29 30

    31 32 33 34 35 36 37 38 39 40

    41 42 43 44 45 46 47 48 49 50

    51 52 53 54 55 56 57 58 59 6061 62 63 64 65 66 67 68 69 70

    71 72 73 74 75 76 77 78 79 80

    81 82 83 84 85 86 87 88 89 90

    91 92 93 94 95 96 97 98 99 100

    Table : No more numbers can be marked. Algorithm terminates.

    Primes up to 100 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,

    89, 97.

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Generating Prime NumbersSieve of Eratosthenes Program

    http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    78/292

    1 int main(){

    2 int comp[10000], n, i, j=2, k;3 comp[0]=0, comp[1]=0;

    4 printf("Enter upper bound: ");

    5 scanf("%d",&n);

    6 for (i=2; i

  • 8/12/2019 ESc101 S 06 Arrays

    79/292

    1 int main(){

    2 int comp[10000], n, i, j=2, k;3 comp[0]=0, comp[1]=0;

    4 printf("Enter upper bound: ");

    5 scanf("%d",&n);

    6 for (i=2; i

  • 8/12/2019 ESc101 S 06 Arrays

    80/292

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Recap

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    81/292

    Using arrays to store and manipulate very large integers.

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Recap

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    82/292

    Using arrays to store and manipulate very large integers.

    Generating prime numbers using Sieve of Eratosthenesmethod.

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Recap

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    83/292

    Using arrays to store and manipulate very large integers.

    Generating prime numbers using Sieve of Eratosthenesmethod.

    AnnouncementsNew contest on Programming Gymhttp://www2.cse.iitk.ac.in:81/newonj/contests.php?contestID=14

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Recap

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    84/292

    Using arrays to store and manipulate very large integers.

    Generating prime numbers using Sieve of Eratosthenesmethod.

    AnnouncementsNew contest on Programming Gymhttp://www2.cse.iitk.ac.in:81/newonj/contests.php?contestID=14

    Lab Exam timings 2 pm 5 pm

    - 12th Feb (Wednesday): Sections A1, A2, A3, A7, A8, A9

    - 13th Feb (Thursday): Sections A4, A5, A6, A10, A11

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Warm Up ExampleMaximum Element in an Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    85/292

    Maximum Element in an Array

    Given an array containing n integers, find the maximum number.

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Warm Up ExampleMaximum Element in an Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    86/292

    Maximum Element in an Array

    Given an array containing n integers, find the maximum number.

    We initially set max to be thefirst element in the array.

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Warm Up ExampleMaximum Element in an Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    87/292

    Maximum Element in an Array

    Given an array containing n integers, find the maximum number.

    We initially set max to be thefirst element in the array.

    For every number betweenthe second and the lastelement of the array,

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Warm Up ExampleMaximum Element in an Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    88/292

    Maximum Element in an Array

    Given an array containing n integers, find the maximum number.

    We initially set max to be thefirst element in the array.

    For every number betweenthe second and the lastelement of the array,

    - if the number is largerthan the current max,

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Warm Up ExampleMaximum Element in an Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    89/292

    Maximum Element in an Array

    Given an array containing n integers, find the maximum number.

    We initially set max to be thefirst element in the array.

    For every number betweenthe second and the lastelement of the array,

    - if the number is largerthan the current max,

    - we assign that number tomax.

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Warm Up ExampleMaximum Element in an Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    90/292

    Maximum Element in an Array

    Given an array containing n integers, find the maximum number.

    We initially set max to be thefirst element in the array.

    For every number betweenthe second and the lastelement of the array,

    - if the number is largerthan the current max,

    - we assign that number tomax.

    Finally,max will contain themaximum element in thearray.

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Warm Up ExampleMaximum Element in an Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    91/292

    Maximum Element in an Array

    Given an array containing n integers, find the maximum number.

    We initially set max to be thefirst element in the array.

    For every number betweenthe second and the lastelement of the array,

    - if the number is largerthan the current max,

    - we assign that number tomax.

    Finally,max will contain themaximum element in thearray.

    1 max = ar[0];

    2 for (i=1; i max)

    4 max = ar[i];

    5 }

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Sorting a List of NumbersProblem Description

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    92/292

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Sorting a List of NumbersProblem Description

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    93/292

    Sorting Problem

    Given a list of integers (say in an array), arrange them indescending order.

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Sorting a List of NumbersProblem Description

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    94/292

    Sorting Problem

    Given a list of integers (say in an array), arrange them indescending order.

    Example:

    - Input: 5 2 9 4 5 8

    Raghunath Tewari rtewari@cse iitk ac in ESc101A Arrays

    Sorting a List of NumbersProblem Description

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    95/292

    Sorting Problem

    Given a list of integers (say in an array), arrange them indescending order.

    Example:

    - Input: 5 2 9 4 5 8- Output: 9 8 5 5 4 2

    Raghunath Tewari rte ari@cse iitk ac in ESc101A Arrays

    Sorting a List of NumbersProblem Description

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    96/292

    Sorting Problem

    Given a list of integers (say in an array), arrange them indescending order.

    Example:

    - Input: 5 2 9 4 5 8- Output: 9 8 5 5 4 2

    Sorting is an extremely important problem in computerscience.

    Raghunath Tewari t i@ iitk i ESc101A Arrays

    Sorting a List of NumbersProblem Description

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    97/292

    Sorting Problem

    Given a list of integers (say in an array), arrange them indescending order.

    Example:

    - Input: 5 2 9 4 5 8- Output: 9 8 5 5 4 2

    Sorting is an extremely important problem in computerscience.

    A common problem in everyday life.Example:

    Raghunath Tewari t i@ iitk i ESc101A Arrays

    Sorting a List of NumbersProblem Description

    http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    98/292

    Sorting Problem

    Given a list of integers (say in an array), arrange them indescending order.

    Example:

    - Input: 5 2 9 4 5 8- Output: 9 8 5 5 4 2

    Sorting is an extremely important problem in computerscience.

    A common problem in everyday life.Example:

    - Contact list on your phone.

    Ragh nath Te ari t i@ iitk i ESc101A Arrays

    Sorting a List of NumbersProblem Description

    S i P bl

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    99/292

    Sorting Problem

    Given a list of integers (say in an array), arrange them indescending order.

    Example:

    - Input: 5 2 9 4 5 8- Output: 9 8 5 5 4 2

    Sorting is an extremely important problem in computerscience.

    A common problem in everyday life.Example:

    - Contact list on your phone.- Ordering marks before assignment of grades.

    Raghunath Tewari

    t i@ iitk i ESc101A

    Arrays

    Sorting a List of NumbersSelection Sort: Algorithm

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    100/292

    Raghunath Tewari

    t i@ iitk i ESc101A

    Arrays

    Sorting a List of NumbersSelection Sort: Algorithm

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    101/292

    1 Select the largest element in your array and swap it with thefirst element of the array.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Sorting a List of NumbersSelection Sort: Algorithm

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    102/292

    1 Select the largest element in your array and swap it with thefirst element of the array.

    2 Consider the subarray from the second element to the last, asyour current array and repeat Step 1.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Sorting a List of NumbersSelection Sort: Algorithm

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    103/292

    1 Select the largest element in your array and swap it with thefirst element of the array.

    2 Consider the subarray from the second element to the last, asyour current array and repeat Step 1.

    3 Stop when the array has only one element.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Sorting a List of NumbersSelection Sort: Example

    5 2 9 4 5 8 Initial Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    104/292

    5 2 9 4 5 8 Initial Array

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Sorting a List of NumbersSelection Sort: Example

    5 2 9 4 5 8 Initial Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    105/292

    5 2 9 4 5 8 Initial Array

    0 5 2 9 4 5 8 Array considered (Max element 9)

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Sorting a List of NumbersSelection Sort: Example

    5 2 9 4 5 8 Initial Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    106/292

    y

    0 5 2 9 4 5 8 Array considered (Max element 9)9 2 5 4 5 8 After swapping

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Sorting a List of NumbersSelection Sort: Example

    5 2 9 4 5 8 Initial Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    107/292

    y

    0 5 2 9 4 5 8 Array considered (Max element 9)9 2 5 4 5 8 After swapping

    1 9 2 5 4 5 8 Array considered (Max element 8)

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Sorting a List of NumbersSelection Sort: Example

    5 2 9 4 5 8 Initial Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    108/292

    y

    0 5 2 9 4 5 8 Array considered (Max element 9)9 2 5 4 5 8 After swapping

    1 9 2 5 4 5 8 Array considered (Max element 8)

    9 8 5 4 5 2 After swapping

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Sorting a List of NumbersSelection Sort: Example

    5 2 9 4 5 8 Initial Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    109/292

    0 5 2 9 4 5 8 Array considered (Max element 9)9 2 5 4 5 8 After swapping

    1 9 2 5 4 5 8 Array considered (Max element 8)

    9 8 5 4 5 2 After swapping

    2 9 8 5 4 5 2 Array considered (Max element 5)9 8 5 4 5 2 After swapping (stays the same)

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Sorting a List of NumbersSelection Sort: Example

    5 2 9 4 5 8 Initial Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    110/292

    0 5 2 9 4 5 8 Array considered (Max element 9)9 2 5 4 5 8 After swapping

    1 9 2 5 4 5 8 Array considered (Max element 8)

    9 8 5 4 5 2 After swapping

    2 9 8 5 4 5 2 Array considered (Max element 5)9 8 5 4 5 2 After swapping (stays the same)

    3 9 8 5 4 5 2 Array considered (Max element 5)

    9 8 5 5 4 2 After swapping

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Sorting a List of NumbersSelection Sort: Example

    5 2 9 4 5 8 Initial Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    111/292

    0 5 2 9 4 5 8 Array considered (Max element 9)9 2 5 4 5 8 After swapping

    1 9 2 5 4 5 8 Array considered (Max element 8)

    9 8 5 4 5 2 After swapping

    2 9 8 5 4 5 2 Array considered (Max element 5)9 8 5 4 5 2 After swapping (stays the same)

    3 9 8 5 4 5 2 Array considered (Max element 5)

    9 8 5 5 4 2 After swapping

    4 9 8 5 5 4 2 Array considered (Max element 4)

    9 8 5 5 4 2 After swapping (stays the same)

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Sorting a List of NumbersSelection Sort: Example

    5 2 9 4 5 8 Initial Array

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    112/292

    0 5 2 9 4 5 8 Array considered (Max element 9)9 2 5 4 5 8 After swapping

    1 9 2 5 4 5 8 Array considered (Max element 8)

    9 8 5 4 5 2 After swapping

    2 9 8 5 4 5 2 Array considered (Max element 5)9 8 5 4 5 2 After swapping (stays the same)

    3 9 8 5 4 5 2 Array considered (Max element 5)

    9 8 5 5 4 2 After swapping

    4 9 8 5 5 4 2 Array considered (Max element 4)

    9 8 5 5 4 2 After swapping (stays the same)

    9 8 5 5 4 2 Array is sorted

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Sorting a List of Numbers

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    113/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Sorting a List of Numbers

    1 int main(){

    2 int ar[]={2,6,1,9,2,7,9,-2},

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    114/292

    i, j, n=8, max, temp;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    115/292

    i, j, n=8, max, temp;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    116/292

    i, j, n=8, max, temp;

    3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    117/292

    i, j, n 8, max, temp;

    3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    118/292

    i, j, n 8, max, temp;

    3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    119/292

    i, j, n=8, max, temp;

    3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    120/292

    i, j, n=8, max, temp;

    3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    121/292

    i, j, n=8, max, temp;

    3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    122/292

    i, j, n=8, max, temp;

    3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    123/292

    i, j, n=8, max, temp;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    124/292

    i, j, n=8, max, temp;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    125/292

    i, j, n=8, max, temp;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    126/292

    i, j, n=8, max, temp;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    127/292

    i, j, n=8, max, temp;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    128/292

    i, j, n=8, max, temp;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    129/292

    i, j, n=8, max, temp;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    130/292

    i, j, n=8, max, temp;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    131/292

    i, j, n 8, max, temp;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    132/292

    i, j, n 8, max, temp;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    133/292

    i, j, n 8, max, temp;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    134/292

    , j, , , p;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    135/292

    , j, , , p;3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    136/292

    j p3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    137/292

    3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    138/292

    3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    139/292

    3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    140/292

    3

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    141/292

    Dot Product

    The dot productof two vectors a= (a1, a2, . . . , an) andb= (b1, b2, . . . , bn) is defined as

    a b= a1b1+a2b2+ . . .+anbn.

    To compute dot product, the two vectors must have the samesize.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Dot Product of Two VectorsC Code

    1 #include

    2 int main(){

    3 int a[100],b[100],size, i, dot_prod=0;4 printf("Enter size of vector: ");

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    142/292

    4 printf( Enter size of vector: );

    5 scanf("%d",&size);

    6 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    143/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Announcements

    Lab Exam Schedule:12th Feb (Wednesday)

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    144/292

    12th Feb (Wednesday)

    - A1 CSE Lab, Ground Floor, CSE Building- A2, A3 CC Labs, First Floor, CC Building- A7, A8, A9 New Core Labs, Second Floor, New Core Lab

    Building13th Feb (Thursday)

    - A4, A5 CC Labs, First Floor, CC Building- A6, A10, A11 New Core Labs, Second Floor, New Core Lab

    Building

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsExample: Dot Product using Functions

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    145/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsExample: Dot Product using Functions

    1 #include

    2 int dot_product(int[], int[], int);

    3

    4 int dot product(int a[] int b[] int

    The function dot product

    takes as as argument twointeger arrays a and b and

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    146/292

    4 int dot_product(int a[], int b[], int

    size){

    5 int sum=0, i;

    6 for(i=0;i

  • 8/12/2019 ESc101 S 06 Arrays

    147/292

    4 int dot_product(int a[], int b[], int

    size){

    5 int sum=0, i;

    6 for(i=0;i

  • 8/12/2019 ESc101 S 06 Arrays

    148/292

    4 int dot_product(int a[], int b[], int

    size){

    5 int sum=0, i;

    6 for(i=0;i

  • 8/12/2019 ESc101 S 06 Arrays

    149/292

    _p ( [], [],

    size){

    5 int sum=0, i;

    6 for(i=0;i

  • 8/12/2019 ESc101 S 06 Arrays

    150/292

    When specifying an array argument to a function, it is notnecessary to give the size.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsHow Arrays are Passed to Functions

    When specifying an array argument to a function, it is not

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    151/292

    When specifying an array argument to a function, it is notnecessary to give the size.Reason: Does not create a copy of the array in the functionbox. It only keeps a copy ofthe name of the array.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsHow Arrays are Passed to Functions

    When specifying an array argument to a function, it is not

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    152/292

    When specifying an array argument to a function, it is notnecessary to give the size.Reason: Does not create a copy of the array in the functionbox. It only keeps a copy ofthe name of the array.

    If an array is modified in a function call, the actual array ismodified as well.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsHow Arrays are Passed to Functions

    When specifying an array argument to a function, it is not

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    153/292

    p y g y g ,necessary to give the size.Reason: Does not create a copy of the array in the functionbox. It only keeps a copy ofthe name of the array.

    If an array is modified in a function call, the actual array ismodified as well.

    When we study Pointers in the second half of the course,we will discuss this in more detail.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls Example

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    154/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls Example

    1 void swap1(int a, int b){

    2 int temp;

    3 temp = a;4 a = b;

    b t

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    155/292

    5 b = temp;

    6 } // swapping a and b

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls Example

    1 void swap1(int a, int b){

    2 int temp;

    3 temp = a;4 a = b;

    5 b = temp;

    1 void swap2(int ar[]){

    2 int temp;

    3 temp = ar[0];4 ar[0] = ar[1];

    5 ar[1] = temp;

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    156/292

    5 b = temp;

    6 } // swapping a and b

    5 ar[1] = temp;

    6 } // swapping ar[0] and ar[1]

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls Example

    1 void swap1(int a, int b){

    2 int temp;

    3 temp = a;4 a = b;

    5 b = temp;

    1 void swap2(int ar[]){

    2 int temp;

    3 temp = ar[0];4 ar[0] = ar[1];

    5 ar[1] = temp;

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    157/292

    5 b = temp;

    6 } // swapping a and b

    5 ar[1] = temp;

    6 } // swapping ar[0] and ar[1]

    1 int main(){

    2 int ar[]={5,2}, a=10, b=15;

    3 printf ("a=%d b=%d\n",a,b);

    4 swap1(a,b);

    5 printf ("a=%d b=%d\n",a,b);

    6

    7 printf ("ar[0]=%d ar[1]=%d\n",ar[0],ar[1]);

    8 swap2(ar);9 printf ("ar[0]=%d ar[1]=%d\n",ar[0],ar[1]);

    10 return 0;

    11 }

    Output

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls Example

    1 void swap1(int a, int b){

    2 int temp;

    3 temp = a;4 a = b;

    5 b = temp;

    1 void swap2(int ar[]){

    2 int temp;

    3 temp = ar[0];4 ar[0] = ar[1];

    5 ar[1] = temp;

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    158/292

    5 b = temp;

    6 } // swapping a and b

    5 ar[1] = temp;

    6 } // swapping ar[0] and ar[1]

    1 int main(){

    2 int ar[]={5,2}, a=10, b=15;

    3 printf ("a=%d b=%d\n",a,b);

    4 swap1(a,b);

    5 printf ("a=%d b=%d\n",a,b);

    6

    7 printf ("ar[0]=%d ar[1]=%d\n",ar[0],ar[1]);

    8 swap2(ar);9 printf ("ar[0]=%d ar[1]=%d\n",ar[0],ar[1]);

    10 return 0;

    11 }

    Output

    $ ./a.out

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls Example

    1 void swap1(int a, int b){

    2 int temp;

    3 temp = a;4 a = b;

    5 b = temp;

    1 void swap2(int ar[]){

    2 int temp;

    3 temp = ar[0];4 ar[0] = ar[1];

    5 ar[1] = temp;

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    159/292

    5 b temp;

    6 } // swapping a and b

    5 ar[1] temp;

    6 } // swapping ar[0] and ar[1]

    1 int main(){

    2 int ar[]={5,2}, a=10, b=15;

    3 printf ("a=%d b=%d\n",a,b);

    4 swap1(a,b);

    5 printf ("a=%d b=%d\n",a,b);

    6

    7 printf ("ar[0]=%d ar[1]=%d\n",ar[0],ar[1]);

    8 swap2(ar);9 printf ("ar[0]=%d ar[1]=%d\n",ar[0],ar[1]);

    10 return 0;

    11 }

    Output

    $ ./a.out

    a=10 b=15

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls Example

    1 void swap1(int a, int b){

    2 int temp;

    3 temp = a;4 a = b;

    5 b = temp;

    1 void swap2(int ar[]){

    2 int temp;

    3 temp = ar[0];4 ar[0] = ar[1];

    5 ar[1] = temp;

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    160/292

    5 b temp;

    6 } // swapping a and b

    5 ar[1] temp;

    6 } // swapping ar[0] and ar[1]

    1 int main(){

    2 int ar[]={5,2}, a=10, b=15;

    3 printf ("a=%d b=%d\n",a,b);

    4 swap1(a,b);

    5 printf ("a=%d b=%d\n",a,b);

    6

    7 printf ("ar[0]=%d ar[1]=%d\n",ar[0],ar[1]);

    8 swap2(ar);9 printf ("ar[0]=%d ar[1]=%d\n",ar[0],ar[1]);

    10 return 0;

    11 }

    Output

    $ ./a.out

    a=10 b=15

    a=10 b=15

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls Example

    1 void swap1(int a, int b){

    2 int temp;

    3 temp = a;4 a = b;

    5 b = temp;

    1 void swap2(int ar[]){

    2 int temp;

    3 temp = ar[0];4 ar[0] = ar[1];

    5 ar[1] = temp;

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    161/292

    p;

    6 } // swapping a and b

    [ ] p;

    6 } // swapping ar[0] and ar[1]

    1 int main(){

    2 int ar[]={5,2}, a=10, b=15;3 printf ("a=%d b=%d\n",a,b);

    4 swap1(a,b);

    5 printf ("a=%d b=%d\n",a,b);

    6

    7 printf ("ar[0]=%d ar[1]=%d\n",ar[0],ar[1]);

    8

    swap2(ar);9 printf ("ar[0]=%d ar[1]=%d\n",ar[0],ar[1]);

    10 return 0;

    11 }

    Output

    $ ./a.out

    a=10 b=15

    a=10 b=15

    ar[0]=5 ar[1]=2

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls Example

    1 void swap1(int a, int b){

    2 int temp;

    3 temp = a;4 a = b;

    5 b = temp;

    1 void swap2(int ar[]){

    2 int temp;

    3 temp = ar[0];4 ar[0] = ar[1];

    5 ar[1] = temp;

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    162/292

    p;

    6 } // swapping a and b

    p;

    6 } // swapping ar[0] and ar[1]

    1 int main(){

    2 int ar[]={5,2}, a=10, b=15;3 printf ("a=%d b=%d\n",a,b);

    4 swap1(a,b);

    5 printf ("a=%d b=%d\n",a,b);

    6

    7 printf ("ar[0]=%d ar[1]=%d\n",ar[0],ar[1]);

    8

    swap2(ar);9 printf ("ar[0]=%d ar[1]=%d\n",ar[0],ar[1]);

    10 return 0;

    11 }

    Output

    $ ./a.out

    a=10 b=15

    a=10 b=15

    ar[0]=5 ar[1]=2

    ar[0]=2 ar[1]=5

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    163/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls

    When a basic datatype (such as int, char, float, etc) is

    passed to a function,

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    164/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls

    When a basic datatype (such as int, char, float, etc) is

    passed to a function,- a copy of thevalueis created in the memory space for thatfunction

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    165/292

    function,

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls

    When a basic datatype (such as int, char, float, etc) ispassed to a function,

    - a copy of thevalueis created in the memory space for thatfunction,

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    166/292

    function,- after the function completes its execution, the values are lost.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls

    When a basic datatype (such as int, char, float, etc) ispassed to a function,

    - a copy of thevalueis created in the memory space for thatfunction,

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    167/292

    ,- after the function completes its execution, the values are lost.

    When an array is passed to a function,

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls

    When a basic datatype (such as int, char, float, etc) ispassed to a function,

    - a copy of thevalueis created in the memory space for thatfunction,

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    168/292

    ,- after the function completes its execution, the values are lost.

    When an array is passed to a function,

    - a copy of thename of the arrayis created in the memory spacefor that function,

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls

    When a basic datatype (such as int, char, float, etc) ispassed to a function,

    - a copy of thevalueis created in the memory space for thatfunction,

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    169/292

    - after the function completes its execution, the values are lost.

    When an array is passed to a function,

    - a copy of thename of the arrayis created in the memory spacefor that function,- whatever manipulation is done with thisarray name, is

    happening to the original array,

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls

    When a basic datatype (such as int, char, float, etc) ispassed to a function,

    - a copy of thevalueis created in the memory space for thatfunction,

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    170/292

    - after the function completes its execution, the values are lost.

    When an array is passed to a function,

    - a copy of thename of the arrayis created in the memory spacefor that function,- whatever manipulation is done with thisarray name, is

    happening to the original array,- after the functions completes its execution, the array might

    have potentially changed.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Arrays as Arguments to FunctionsUnderstanding Array Manipulation in Function Calls

    When a basic datatype (such as int, char, float, etc) ispassed to a function,

    - a copy of thevalueis created in the memory space for thatfunction,

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    171/292

    - after the function completes its execution, the values are lost.

    When an array is passed to a function,

    - a copy of thename of the arrayis created in the memory spacefor that function,- whatever manipulation is done with thisarray name, is

    happening to the original array,- after the functions completes its execution, the array might

    have potentially changed.

    Note: Name of a variable is NOT THE SAME ASthecontents of that variable.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional Arrays

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    172/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional Arrays

    Arrays to store higher dimensional data such as matrices.

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    173/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional Arrays

    Arrays to store higher dimensional data such as matrices.

    Declaration:

    int mat[3][4];

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    174/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional Arrays

    Arrays to store higher dimensional data such as matrices.

    Declaration:

    int mat[3][4];

    The cells of the array mat are:

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    175/292

    mat[0][0] mat[0][1] mat[0][2] mat[0][3]

    mat[1][0] mat[1][1] mat[1][2] mat[1][3]

    mat[2][0] mat[2][1] mat[2][2] mat[2][3]

    Note that both indices start at 0.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional Arrays

    Arrays to store higher dimensional data such as matrices.

    Declaration:

    int mat[3][4];

    The cells of the array mat are:

    [ ][ ] [ ][ ] [ ][ ] [ ][ ]

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    176/292

    mat[0][0] mat[0][1] mat[0][2] mat[0][3]

    mat[1][0] mat[1][1] mat[1][2] mat[1][3]

    mat[2][0] mat[2][1] mat[2][2] mat[2][3]

    Note that both indices start at 0.

    Think of matas an array of arrays.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional Arrays

    Arrays to store higher dimensional data such as matrices.

    Declaration:

    int mat[3][4];

    The cells of the array mat are:

    [0][0] [0][1] [0][2] [0][3]

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    177/292

    mat[0][0] mat[0][1] mat[0][2] mat[0][3]

    mat[1][0] mat[1][1] mat[1][2] mat[1][3]

    mat[2][0] mat[2][1] mat[2][2] mat[2][3]

    Note that both indices start at 0.

    Think of matas an array of arrays.

    - The individual elements ofmatare mat[0], mat[1], mat[2],and

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional Arrays

    Arrays to store higher dimensional data such as matrices.

    Declaration:

    int mat[3][4];

    The cells of the array mat are:

    t[0][0] t[0][1] t[0][2] t[0][3]

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    178/292

    mat[0][0] mat[0][1] mat[0][2] mat[0][3]

    mat[1][0] mat[1][1] mat[1][2] mat[1][3]

    mat[2][0] mat[2][1] mat[2][2] mat[2][3]

    Note that both indices start at 0.

    Think of matas an array of arrays.

    - The individual elements ofmatare mat[0], mat[1], mat[2],and

    - each element is an integer array. For example, mat[1] is thearray of integers

    mat[1][0], mat[1][1], mat[1][2], mat[1][3]

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional ArraysTranspose of a Matrix: Definition

    Transpose of a Matrix

    Let m11 m12 . . . m1mm21 m22 . . . m2m

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    179/292

    M=

    21 22 2m

    ......

    ...mn1 mn2 . . . mnm

    be an nm matrix. Then the transpose ofM is the m n matrix defined as

    MT =

    m11 m21 . . . mn1m12 m22 . . . mn2

    ......

    ...m1m m2m . . . mnm

    .

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional ArraysTranspose of a Matrix: Definition

    Transpose of a Matrix

    Let m11 m12 . . . m1mm21 m22 . . . m2m

    For example if

    5 3 2

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    180/292

    M=

    ......

    ...mn1 mn2 . . . mnm

    be an nm matrix. Then the transpose ofM is the m n matrix defined as

    MT =

    m11 m21 . . . mn1m12 m22 . . . mn2

    ......

    ...m1m m2m . . . mnm

    .

    M=

    5 3 28 4 1

    then,

    MT =

    5 83 4

    2 1

    .

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional ArraysTranspose of a Matrix: C Code

    1 int main(){

    2 int m[100][100], row, col, i, j;

    3 printf("Enter number of rows: ");

    4

    scanf("%d",&row);5 printf("Enter number of columns: ");

    6 scanf("%d",&col);

    7 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    181/292

    8 for (j=0; j

  • 8/12/2019 ESc101 S 06 Arrays

    182/292

    8 for (j=0; j

  • 8/12/2019 ESc101 S 06 Arrays

    183/292

    8 for (j=0; j

  • 8/12/2019 ESc101 S 06 Arrays

    184/292

    8 for (j=0; j

  • 8/12/2019 ESc101 S 06 Arrays

    185/292

    8 for (j=0; j

  • 8/12/2019 ESc101 S 06 Arrays

    186/292

    8 for (j=0; j

  • 8/12/2019 ESc101 S 06 Arrays

    187/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Recap

    Passing arrays as arguments to functions.

    http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    188/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Recap

    Passing arrays as arguments to functions.In case of the basic datatypes such as int, float, etc, thevalues are copied in the function space.

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    189/292

    p p

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Recap

    Passing arrays as arguments to functions.In case of the basic datatypes such as int, float, etc, thevalues are copied in the function space.

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    190/292

    In case of array, only the name is copied.

    Example: Dot product, Swapping of elements

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Recap

    Passing arrays as arguments to functions.In case of the basic datatypes such as int, float, etc, thevalues are copied in the function space.

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    191/292

    In case of array, only the name is copied.

    Example: Dot product, Swapping of elementsMultidimensional Arrays.

    Example: Transpose of a matrix.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional ArraysColumn Average: Problem Description

    Computing Column Average

    Write a program that implements a function to do the following:- takes as input a two-dimensional array of integers (together

    with number of rows and columns of the array),

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    192/292

    - computes the average of each column, and returns theaverages.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional ArraysColumn Average: Problem Description

    Computing Column Average

    Write a program that implements a function to do the following:- takes as input a two-dimensional array of integers (together

    with number of rows and columns of the array),

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    193/292

    - computes the average of each column, and returns theaverages.

    For example, if the array is as follows:

    M=

    5 83 4

    2 1

    ,

    then the values returned are

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional ArraysColumn Average: Problem Description

    Computing Column Average

    Write a program that implements a function to do the following:- takes as input a two-dimensional array of integers (together

    with number of rows and columns of the array),

    h f h l d h

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    194/292

    - computes the average of each column, and returns theaverages.

    For example, if the array is as follows:

    M=

    5 83 4

    2 1

    ,

    then the values returned are 3.33 4.33

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional ArraysColumn Average: Returning Multiple Values

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    195/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional ArraysColumn Average: Returning Multiple Values

    Observe that the number of values that we need to return isequal to the number of columns.

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    196/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional ArraysColumn Average: Returning Multiple Values

    Observe that the number of values that we need to return isequal to the number of columns.

    When we discussed functions we noted that a function canreturn only one value.

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    197/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional ArraysColumn Average: Returning Multiple Values

    Observe that the number of values that we need to return isequal to the number of columns.

    When we discussed functions we noted that a function canreturn only one value.

    How do we solve the problem?

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    198/292

    How do we solve the problem?

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional ArraysColumn Average: Returning Multiple Values

    Observe that the number of values that we need to return isequal to the number of columns.

    When we discussed functions we noted that a function canreturn only one value.

    How do we solve the problem?

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    199/292

    How do we solve the problem?

    Answer: We exploit the fact that when an array is modifiedin a function, the modification happens in the actual array inthe parent function.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional ArraysColumn Average: Returning Multiple Values

    Observe that the number of values that we need to return isequal to the number of columns.

    When we discussed functions we noted that a function canreturn only one value.

    How do we solve the problem?

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    200/292

    How do we solve the problem?

    Answer: We exploit the fact that when an array is modifiedin a function, the modification happens in the actual array inthe parent function.

    Solution:

    - Pass an extra array as an argument to the function.

    - This array will store the averages of all the columns.void column_average(int a[][10], int r, int c, float avg[]);

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Multi-Dimensional ArraysColumn Average: C Code

    1 void column_average(int a[][10],int r, int c, float avg[]){

    2 int i,j,sum;

    3

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    201/292

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    202/292

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    203/292

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    204/292

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    205/292

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    206/292

    4 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    207/292

    9 }

    10 int main(){

    11 int m[10][10],i,j;12 float avg[10];

    13

    14 for (i=0;i

  • 8/12/2019 ESc101 S 06 Arrays

    208/292

    (between 0 and n 1),- else, it displays1.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Searching in an ArrayProblem Description

    Searching in an Array

    Write a program that searches for a given element in an array ofsize n and,

    - if the element is found, it displays the index of the element(between 0 and n 1)

    http://find/http://goback/
  • 8/12/2019 ESc101 S 06 Arrays

    209/292

    (between 0 and n 1),- else, it displays1.

    For example, if the array is{5, 0,2, 8, 3, 8} and element to besearched for

    - is 8, then the program displays 3,

    - is 10, then the program displays1.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Searching in an ArrayC Program

    1 int array_search(int arr[], int size, int elt){

    2 int i;

    3 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    210/292

    10 int a1[100], i, n, search;

    11 printf("Enter size of array: ");

    12 scanf("%d",&n);13 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    211/292

    10 int a1[100], i, n, search;

    11 printf("Enter size of array: ");

    12 scanf("%d",&n);13 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    212/292

    10 int a1[100], i, n, search;

    11 printf("Enter size of array: ");

    12 scanf("%d",&n);13 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    213/292

    10 int a1[100], i, n, search;

    11 printf("Enter size of array: ");

    12 scanf("%d",&n);13 for (i=0; i

  • 8/12/2019 ESc101 S 06 Arrays

    214/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Tips for Lab ExamGeneral Instructions

    Questions are ordered in increasing order of difficulty.

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    215/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Tips for Lab ExamGeneral Instructions

    Questions are ordered in increasing order of difficulty.

    Read all instructions and the questions carefully.

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    216/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Tips for Lab ExamGeneral Instructions

    Questions are ordered in increasing order of difficulty.

    Read all instructions and the questions carefully.

    Approaching a problem:- Make sure your program compiles correctly and gives thecorrect output.

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    217/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Tips for Lab ExamGeneral Instructions

    Questions are ordered in increasing order of difficulty.

    Read all instructions and the questions carefully.

    Approaching a problem:- Make sure your program compiles correctly and gives thecorrect output.

    - Dont try to write the entire program in one attempt.

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    218/292

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Tips for Lab ExamGeneral Instructions

    Questions are ordered in increasing order of difficulty.

    Read all instructions and the questions carefully.

    Approaching a problem:- Make sure your program compiles correctly and gives thecorrect output.

    - Dont try to write the entire program in one attempt.- Write short segments of code and keep compiling whenever

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    219/292

    possible to ensure correctness.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Tips for Lab ExamGeneral Instructions

    Questions are ordered in increasing order of difficulty.

    Read all instructions and the questions carefully.

    Approaching a problem:- Make sure your program compiles correctly and gives thecorrect output.

    - Dont try to write the entire program in one attempt.- Write short segments of code and keep compiling whenever

    bl

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    220/292

    possible to ensure correctness.

    - If your program is not doing what it should,

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Tips for Lab ExamGeneral Instructions

    Questions are ordered in increasing order of difficulty.

    Read all instructions and the questions carefully.

    Approaching a problem:- Make sure your program compiles correctly and gives thecorrect output.

    - Dont try to write the entire program in one attempt.- Write short segments of code and keep compiling whenever

    ibl

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    221/292

    possible to ensure correctness.

    - If your program is not doing what it should,- insert dummy printf statements to test where it is going

    wrong,

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Tips for Lab ExamGeneral Instructions

    Questions are ordered in increasing order of difficulty.

    Read all instructions and the questions carefully.

    Approaching a problem:- Make sure your program compiles correctly and gives thecorrect output.

    - Dont try to write the entire program in one attempt.- Write short segments of code and keep compiling whenever

    ibl t t

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    222/292

    possible to ensure correctness.

    - If your program is not doing what it should,- insert dummy printf statements to test where it is going

    wrong,- make a variable table (especially when using loops).

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Tips for Lab ExamGeneral Instructions

    Questions are ordered in increasing order of difficulty.

    Read all instructions and the questions carefully.Approaching a problem:

    - Make sure your program compiles correctly and gives thecorrect output.

    - Dont try to write the entire program in one attempt.- Write short segments of code and keep compiling whenever

    ibl t t

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    223/292

    possible to ensure correctness.

    - If your program is not doing what it should,- insert dummy printf statements to test where it is going

    wrong,- make a variable table (especially when using loops).

    - Use functions to divide your program into smaller components.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Tips for Lab ExamGeneral Instructions

    Questions are ordered in increasing order of difficulty.

    Read all instructions and the questions carefully.Approaching a problem:

    - Make sure your program compiles correctly and gives thecorrect output.

    - Dont try to write the entire program in one attempt.- Write short segments of code and keep compiling whenever

    possible to ensure correctness

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    224/292

    possible to ensure correctness.

    - If your program is not doing what it should,- insert dummy printf statements to test where it is going

    wrong,- make a variable table (especially when using loops).

    - Use functions to divide your program into smaller components.

    - Important: Write comments to explain your code.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Tips for Lab ExamGeneral Instructions

    Questions are ordered in increasing order of difficulty.

    Read all instructions and the questions carefully.Approaching a problem:

    - Make sure your program compiles correctly and gives thecorrect output.

    - Dont try to write the entire program in one attempt.- Write short segments of code and keep compiling whenever

    possible to ensure correctness

    http://find/
  • 8/12/2019 ESc101 S 06 Arrays

    225/292

    possible to ensure correctness.

    - If your program is not doing what it should,- insert dummy printf statements to test where it is going

    wrong,- make a variable table (especially when using loops).

    - Use functions to divide your program into smaller components.

    - Important: Write comments to explain your code.- Give meaningful variable names whenever possible.

    Raghunath Tewari [email protected] ESc101A

    Arrays

    Tips for Lab ExamGeneral Instructions

    Questions are ordered in increasing order of difficulty.

    Read all instructions and the questions carefully.Approaching a problem:

    - Make sure your program compiles correctly and gives thecorrect output.

    - Dont try to write the entire program in one attempt.- Write short segments of code and keep compiling whenever

    possible to ensure correctness

    http://find/
  • 8/12/20