Chapter 7 Working With Arrays Learning Objectives: Understand the Purpose of 1-D arrays and vectors Understand the differences between arrays and vectors Be able to declare 1-D arrays Be able to use 1-D arrays Going out of bounds with an array Swapping Values #define 1
What are Vectors and Arrays? Vectors and arrays are both single variables that can hold a list of values/objects. They have a name and index. Example of an array: double a[5]; //declare an array called a that can hold 5 double values for (int i = 0; i<5; i++) //this will read in 5 numbers, the first { //will be stored as a[0], 2 nd as a[1], etc printf("enter Number %i: ", i); scanf("%lf", &a[i]); } double sum=a[2] + a[4]; //this will add the 3 rd and 5 th //number Why 3 rd and 5 th, not 2 nd and 4 th? Array Index Starts with 0 2
Using Arrays Array elements can be used the same way any other variable is used: Example using the array called a: for (int n=0; n<5; n++) //This loop sums all values in a { } sum=sum+a[n]; The difference is that each time you use it, you need to specify which value in the array you are referring to. You do this with the index (value in square brackets). Why would you want to use arrays or vectors? You want to store and use a lot of values. You want to be able to loop through a series of values. 3
What are the main differences between an array and a vector? 1. You can change the size of a vector within the program. Once an array is declared, you can t change it s size. Some compilers allow you to use a variable size to start with, but once set you can t change it. 2. Vectors have functions associated with them, arrays don t. 3. Vectors can hold any data type, including other class objects, but arrays are limited to the original C data types. Once you declare a vector it works just like an array: a[ i ] could be an element from either a vector or from an array We will start with arrays then we ll cover vectors. 4
Declaring Arrays Syntax: type name[size]; Examples: int b[4]; What type of values does b hold? int How many values can you store in b? How do you refer to the 1 st element? 4 b[0] double r [1025]; How do you refer to the last element? r[1024] char letters[256]; What is the name of this array? letters How do you print the 8 th element? printf( %c, letters[7]); 5
Initializing Arrays Initializing arrays with values: int b[4]={1,3,2,-6}; //array where b[0] is 1, b[1] is 3, etc. int x[ ] = {1,3,2,-6}; //does the same thing with auto sizing Or, you can loop through each value and set it. For Example: int y[4]; for(int i=0; i<4; i++) { y[i] = -1; } What does this loop do? Sets all values in y to -1 Remember you can use an array element just like any other variable!! in expressions, printfs, scanfs, assignments, conditions, etc 6
Going out of bounds with an Array Example: int k = 4; int a[2]; printf( k = %i\n",k); a[0] = 9; Output: a[1] = 10; What happened to k? a[2] = 6; a[3] = 7; printf( k = %i\n",k); lgarriso@kec107 /cygdrive/h/cs101 $./scanfarray k = 4 k = 7 When we assigned a value to a[3], we overwrote the value in k s memory slot. Bottom Line: If you go out of bounds of your array, unpredictable things may happen. 7 Many times you ll get a Segmentation Fault Core Dump
Swapping Values in an Array Example: //This code swaps the values of a[0] and a[1] int a[2]; a[0] = 9; a[1] = 10; int temp = a[0]; a[0] = a[1]; a[1] = temp; Why did I need the variable temp? Because a[0] = a[1] overwrites a[0]. If I want to save the value of a[0], I need to store it somewhere. 8
Summary of 1-D Arrays Array = Variable that can hold more than one value Example declaration of a 1-D array: int x[4]; //declare an array called x that can hold 4 integer values indices 0 1 x memory slots 2 3 3 What would happen if you wrote the command: x[2] = 3; The index can be a number or a variable or an expression. Is this ok?: x[ i + 1] = 5; Yes, if i is an integer and has a value 9
#define Preprocessor Directive Notice that when using arrays, we usually also use loops and we loop through the size(s). Therefore, we find the size when we declare the array and every time we loop through it. If we want to change the size of the array in our program, we have to change it in multiple places. It is very easy to miss some. Instead, we can use #define for our array size(s). When you use #define, you create a name that represents a value that can be used throughout the program. It is like declaring a variable that can be used throughout the program, but can t be changed within the program. Whenever you use the name, the value you defined it with will be substituted. It is good for constants (like Pi) and for array sizes. 10
#define Preprocessor Directive syntax: #define name value Note that there is no semicolon. This goes under your #includes. Example (Note the convention is to make the name all-caps): #include <stdio.h> #define SIZE 7 int main() { } double a[size]; for(int i = 0; i < SIZE; i++) { } return 0; //other code goes here Note: you can have as many #defines as you want. For a 2-D array, you might want to have one for ROWS and one for COLUMNS. You could now change SIZE in just one place (at the top) 11and the array size and loop condition will also be changed.