Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming for Engineers

Similar presentations


Presentation on theme: "Computer Programming for Engineers"— Presentation transcript:

1 Computer Programming for Engineers
Arrays

2 Arrays of Numbers

3 Definition of Arrays billy [2]
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]

4 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. Example int billy [5]; float c [12], x[27];

5 (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, }; 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!

6 (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 */ }

7 Example 2: Display values stored in billy #include <stdio.h>
int i, billy[5] = {16, 2, 77, 40, }; void main() { for ( i = 0; i < 5; i++ ) printf("billy [%d] = %d \n",i, billy[i]); }

8 Using values in an Array
The format to recall a value stored in an array: name [index] such as billy[2] = 75; Suppose the 3rd value in billy is 75 and is to assign such a value to the variable a a= billy[2]; Caution when using array Since, array does not check the limit of the index. Make sure that the index in an array does not exceed the bound.

9 Example 3: Using an array to find the sum
#include <stdio.h> int i, billy[5] = {16, 2, 77, 40, }; 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);

10 (Input using an Array) An array can be used in conjunction with scanf() to accept many input values price[5] = 10.69; scanf(“%d %lf”, &grades[0], &price[2]) scanf(“%d”, &code[0]); scanf(“%d %d %d”, &grades[0], &grades[1], &grades[2]); Or put it in a loop to accept many input values of the same type for(i = 0; i <5; ++i) { printf(“Enter a grade: ” ); scanf(“%d”, &grades[i]); } The above codes accept 5 integers and store them in the variable grade

11 Example 4: Using an array for many input values
#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]); }

12 Special command, rand() and mod%
Is used to construct random integers between 0 and 32767 Contained in the standard library Mod % Mod is used to compute the remainder from the division between 2 integers a%b is the remainder of dividing a with b 11%9 is 2. #include <stdlib.h> 9%8 = 1 25%7 = 4

13 Example 5: Creating random numbers
#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); }

14 Arrays of Characters

15 Arrays of Characters (Strings)
An array of characters is called a string. For example, char string1[] = “first”; or char string1[] = { ‘f’, ‘i’, ‘r’, ‘s’, ‘t’,‘\0’}; 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. string1[0] = ‘f’ and string1[2] = ‘r’ In char string1[] = { ‘f’, ‘i’, ‘r’, ‘s’, ‘t’}; The char ‘\0’ is automatically added to string1

16 String for input 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.

17 String Input 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.

18 Example of using String
#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]

19 Exercise Write a program that reads input from the keyboard and print them backward on the display, e.g., November The output should be rebmevoN Assume that the input does not exceed 12 characters. Hint. At the end of the string input, the next character is 0.

20 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 reverse for (i;i>=0;i--) }

21 Multi-Dimensional Arrays

22 Multi-Dimensional Array
Multi-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]

23 Value Assignment in a 2-D Array
There 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};

24 2-D Array Results in Array-jimmy #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

25 Program computing the sum in an array
total = 0; for (n= 0; n< HEIGHT; n++ ) for ( m = 1; m <= 3; m++ ) { total += jimmy[ n ][ m ]; }

26 End


Download ppt "Computer Programming for Engineers"

Similar presentations


Ads by Google