Computer Programming for Engineers

26
Arrays Arrays

description

Computer Programming for Engineers. Arrays. Arrays of Numbers. Definition of Arrays. An array is a collection of data of the same type. Individual array elements are identified by an integer index. In C, the index begins at zero and is always written inside square brackets. - PowerPoint PPT Presentation

Transcript of Computer Programming for Engineers

Page 1: Computer Programming for Engineers

ArraysArrays

Page 2: Computer Programming for Engineers
Page 3: Computer Programming for Engineers

Definition of Arrays

An array is a collection of data of the same type. Individual array elements are identified by an integer index.

In C, the index begins at zero and is always written inside square brackets.

In this example, 5 different values can be stored using a single variable name, i.e., billy, with the index ranging from [0]----[4].

To refer to a value, we use the variable name with the index. In this example, billy[2] is targeted.

billy [2]

Page 4: Computer Programming for Engineers

Declaration of Arrays

To declare an array, we use type name [elements];

Type is the type of variables such as, int, float, char.

In this course, the number of elements (elements) is a constant.

Exampleint billy [5];

float c [12], x[27];

Page 5: Computer Programming for Engineers

(Array Initialization)Once an array is declared, the value inside

the array is usually not what we want. Hence, Initializing values inside the array is desirable.

Firstly, Declare and Initialize at the same time.

See the examples,

int billy [5] = {16, 2, 77, 40, 12071 };

int n[10] = { 1 };int n[] = {1, 2, 3, 4, 5};int n[5] = { 32, 27, 64,

18, 95, 14 };

{1,0,0,….,0}

n has 5 members

Error!

Page 6: Computer Programming for Engineers

(Array Initialization)2nd Method: Initializing an array can be

performed inside a (for,while) loop. For example,

Example 1:

#include <stdio.h>int i, billy[5];void main(){for ( i = 0; i < 5; i++ )

{ billy[ i ] = 0; /* set element at location i to 0 */

}}

Page 7: Computer Programming for Engineers

Display values stored in billy

Example 2:

#include <stdio.h>int i, billy[5] = {16, 2, 77, 40, 12071 };void main(){for ( i = 0; i < 5; i++ )

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

}}

Page 8: Computer Programming for Engineers

Using values in an ArrayThe format to recall a value stored in an

array: name [index]such as billy[2] = 75;

Suppose the 3rd value in billy is 75and is to assign such a value to the variable a

a= billy[2];

Caution when using arraySince, array does not check the limit of the index. Make sure that the index in an array does not exceed the bound.

Page 9: Computer Programming for Engineers

#include <stdio.h>int i, billy[5] = {16, 2, 77, 40, 12071 };int billy_sum = 0;void main(){for ( i = 0; i < 5; i++ )

{printf("billy [%d] = %d \

n",i,billy[i]);billy_sum = billy_sum + billy[i];

}printf("The sum is: %d \

n",billy_sum);}

Page 10: Computer Programming for Engineers

(Input using an Array)An array can be used in conjunction with sca

nf() to accept many input values

5 1069price[ ] = . ; (“% %”, &[0], &[2])

scanf(“%d ”, &[0]);

scanf(“%d %d %d”, &gr 0 1ades[ ], &grades[ ],2&grades[ ]);

Or put it in a loop to accept many input values of the same type d d d d d0 5 ; ++){

d (“ drade: ” ); scanf(“%d”, &grades[i]

);}The above codes accept 5 integers and store them in the variable grade

Page 11: Computer Programming for Engineers

#include <stdio.h>int i;float billy[5];void main(){for(i = 0; i < 5; ++i)

{printf("Enter a test score: " );scanf("%f", &billy[i]);

}}

Page 12: Computer Programming for Engineers

Special command, rand() and mod%

rand() Is used to construct random integers between

0 and 32767Contained in the standard library

Mod %Mod is used to compute the remainder from

the division between 2 integersa%b is the remainder of dividing a with b

11%9 is 2.

#include <stdlib.h>

9%8 = 125%7 = 4

Page 13: Computer Programming for Engineers

#include <stdio.h> #include <stdlib.h>void main()

{int i,s,r; /* number of visits for each interval */for (i=0;i<20;i++){

s = rand();printf("# random number is: %d #\t",s);r = s%20;printf("* random number between [0,10] is: %d * \

n",r);}

}

Example 5: Creating random numbers

Page 14: Computer Programming for Engineers
Page 15: Computer Programming for Engineers

Arrays of Characters (Strings)An array of characters is called a string. For

example,

char string1[] = “first”;orchar string1[] = { ‘f’, ‘i’, ‘r’, ‘s’, ‘t’,‘\0’};

o where string1 consists of 5 characters, and the special terminating character called, Null Character. So, the total number of chars in string1 is really 6. o string1[0] = ‘f’ and string1[2] = ‘r’In char string1[] = { ‘f’, ‘i’, ‘r’, ‘s’, ‘t’};The char ‘\0’ is automatically added to string1

Page 16: Computer Programming for Engineers

• We can accept string as the input directly from the keyboard using scanf(“%s”,var_name)Example.

char string2[12] scanf( “%s”, string2 );is used to store 11 characters in the var string2

•When using more than 11, string2[12], string2[13] is deleted unintentionally.

Page 17: Computer Programming for Engineers

char string2[12]scanf( “%s”, string2 );

String2 can be used as the address in the input (both with and) without the need of the “&” symbol.

Page 18: Computer Programming for Engineers

#include <stdio.h>

void main(){

char st2[12];int i=12;printf("Please enter a string:");scanf("%s",string2);printf("\n The string is called: %s\n",string2);

}

Terminate with 0

@ string[8]

Page 19: Computer Programming for Engineers

ExerciseWrite a program that reads input from the

keyboard and print them backward on the display, e.g.,

NovemberThe output should be rebmevoNAssume that the input does not exceed 12

characters. Hint.At the end of the string input, the next character is

0.

Page 20: Computer Programming for Engineers

Exercise. Fill in the rest of the codes#include <stdio.h>void main(){

char string2[12];int i=12;printf("Please enter a string:");scanf("%s",string2);// Now print if in reversefor (i;i>=0;i--){

}}

Page 21: Computer Programming for Engineers
Page 22: Computer Programming for Engineers

Multi-Dimensional ArrayMulti-dimensional array is possible. First, an

2-D array can be declared as

This means array of integers with 3 row x 5 column as

So, jimmy [1][3] is indicated at the location below.

int jimmy [3][5]

Page 23: Computer Programming for Engineers

Value Assignment in a 2-D ArrayThere are several ways to declare a 2-

D array. See the example below. int jimmy 3[ ][5 ] = {{1,2,3,4,5},

{2,4,6,8,10}, {3,6,9,12,15}};

int jimmy 3[ ][5 ] = {1,2,3,4,5, 2,4,6,8,10, 3,6,9,12,15};

int jimmy[3][5] = {1,2,3,4,5,2,4,6,8,10,3,6,9,12,15};

Page 24: Computer Programming for Engineers

#define WIDTH 5 #define HEIGHT 3 int jimmy [HEIGHT][WIDTH]; int n,m; int main () { for (n=0;n<HEIGHT;n++) for (m=0;m<WIDTH;m++) { jimmy[n][m]=(n+1)*(m+1); } return 0; }

Results in Array-jimmy

Page 25: Computer Programming for Engineers

Program computing the sum in an arraytotal = 0;for (n= 0; n< HEIGHT; n++ )

for ( m = 1; m <= 3; m++ ){

total += jimmy[ n ][ m ];}

Page 26: Computer Programming for Engineers