18
CHAPTER 6 - ARRAYS 1 noth/Ch6-Arrays

Chapter 6 - Arrays

Embed Size (px)

DESCRIPTION

csc425

Citation preview

Page 1: Chapter 6 - Arrays

noth/Ch6-Arrays 1

CHAPTER 6 - ARRAYS

Page 2: Chapter 6 - Arrays

noth/Ch6-Arrays 2

WHAT IS AN ARRAY An array is a collection of data storage

locations, each of which holds the same type of data.

Array allows you to store a list of values and to easily access members of the list.

An array stores many values in memory using one name, and individual values are identified by number.

Page 3: Chapter 6 - Arrays

noth/Ch6-Arrays 3

DECLARATION OF AN ARRAY

Array size and element type must be declared. All elements must be the same type, eg, all int or all char. Write the element type name, the name of the array variable, then the size enclosed in square brackets ("[]"). Ex:

int scores[100]; // 100 ints, scores[0] to scores[99]

char name[40]; // 40 chars, name[0] to name[39]

Syntax : type array_name[size]

Page 4: Chapter 6 - Arrays

noth/Ch6-Arrays 4

ARRAYS AND LOOPS Loops and arrays go together. This

example sets all array elements to zero. float height[1000]; . . . for (int i=0; i<1000; i++) { height[i] = 0.0; }

Page 5: Chapter 6 - Arrays

noth/Ch6-Arrays 5

EXAMPLE 1#include <iostream>Void main (){

Int student_mark[5]; //declare an array called student_mark which holds 5 integer values.

Int I;For (i=0;i<5;i++){ cout<<“Student[“<<I,,”] “;

cin>>student_mark[i]; //user is prompt for a value, and that value is stored at the current offset which is same as the loop counter. (0,1,2,3,4)

}}

Page 6: Chapter 6 - Arrays

noth/Ch6-Arrays 6

EXAMPLE 2 :

Adding all the elements in an array. File ExampleArray.docx

Page 7: Chapter 6 - Arrays

noth/Ch6-Arrays 7

EXAMPLE 3 :

Example3 program is to illustrate why array is use. Array will readi the values into memory first before doing any computation on them. (Input all the data first then process them all at 1 time)

The example program will printi the input values last to first.

File Example 3 Array.docx

Page 8: Chapter 6 - Arrays

noth/Ch6-Arrays 8

ARRAY MEMORY DIAGRAMS

The diagram is one typical way to represent the memory used by an array.

Each box represents the amount of memory needed to hold one array element.

The actual array variable, a in this example, is a pointer to the memory for all of its elements.

Page 9: Chapter 6 - Arrays

noth/Ch6-Arrays 9

ARRAY INITIALIZATION

An array can be initialized in the declaration by writing a comma-separated list of values enclosed in braces following an equal sign.

int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

Altho this looks like an assignment, assignment statements with arrays are not allowed, and this syntax is legal only in a declaration.

Page 10: Chapter 6 - Arrays

noth/Ch6-Arrays 10

Size can be set from initial value list If the size is omitted, the compiler uses

the number of values. For example, // is the same as the statement below:int

days[] = {31,28,31,30,31,30,31,31,30,31,30,31};

Initialization of character arrays Character arrays can be initialized on the

right by writing a double-quoted string. char greeting[100] = "Hello"; //

Remaining characters zero.char goodbye[] = "Adios"; // Array size is 6 (final zero on strings).

Page 11: Chapter 6 - Arrays

noth/Ch6-Arrays 11

PASSING ARRAY PARAMETERS When an array is passed as a

parameter, only the memory address of the array is passed (not all the values). An array as a parameter is declared similarly to an array as a variable, but no bounds are specified. The function doesn't know how much space is allocated for an array. See the example below.

Example -- Function to add numbers in an array. File Example2 Array.docx

Page 12: Chapter 6 - Arrays

noth/Ch6-Arrays 12

INCLUDE FILES

If you are using the c-string and character functions, you must have :

  #include <cstring> #include <cctype> or for old compilers #include <string.h> #include <ctype.h>

Page 13: Chapter 6 - Arrays

noth/Ch6-Arrays 13

COMMON <CSTRING> FUNCTIONS Here are a number of useful functions work with

character arrays (C-strings). The types of the parameters are cs=char[] or char*, c=char/int. Where true appears, zero is false and non-zero is true.

Type Method Description

#include <cstring> (or older <string.h>)

int strlen(const cs) Returns the length of the c-string cs.

char* strcpy(cs1, const cs2) Copies cs2 to cs1. Returns the first parameter (usually ignored).

char* strcat(cs1, const cs2)

Concatenates cs2 on the end of cs1. Returns the first parameter (usually ignored).

int strcmp(const cs1, const cs2)

Returns one of three kinds of values: negative if cs1 < cs2, zero if cs1 == cs2, or positive if cs1 > cs2.

char* strchr(const cs, c) Returns a pointer to the first occurrence of c in cs or NULL if it isn't in the string.

char* strstr(const cs1, const cs2)

Returns a pointer to the first occurrence of cs2 in cs1 or NULL if it isn't in

Page 14: Chapter 6 - Arrays

noth/Ch6-Arrays 14

OTHER STRING MANIPULATION Strcpy function - Copies the C string pointed by source into the array

pointed by destination, including the terminating null character.

Strcat function –Concatenate strings Appends a copy of the source string to the destination

string. The terminating null character in destination is overwritten by the first character of source, and a new null-character is appended at the end of the new string formed by the concatenation of both in destination.

Strlen function – Get string length Returns the length of str. The length of a C string is

determined by the terminating null-character: A C string is as long as the amount of characters between the beginning of the string and the terminating null character.

Page 15: Chapter 6 - Arrays

noth/Ch6-Arrays 15

EXAMPLE OF STRCMP FUNCTION Syntax: int strcmp ( const char * str1, const char * str2 );

<cstring>

Compare two strings Compares the C string str1 to the C string str2.

This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminanting null-character is reached.

 

Page 16: Chapter 6 - Arrays

noth/Ch6-Arrays 16

RETURN VALUE

Returns an integral value indicating the relationship between the strings:A zero value indicates that both strings are equal.A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

Page 17: Chapter 6 - Arrays

noth/Ch6-Arrays 17

EXAMPLE:1 2 3 4 5 6 7 8 9

10 11 12 13 14 15

/* strcmp example */ #include <stdio.h> #include <string.h> int main () { char szKey[] = "apple"; char szInput[80]; do { printf ("Guess my favourite fruit? "); gets (szInput); } while (strcmp (szKey,szInput) != 0); puts ("Correct answer!"); return 0; }

OUTPUT:Guess my favourite fruit? OrangeGuess my favourite fruit? Apple

Correct answer!

Page 18: Chapter 6 - Arrays

noth/Ch6-Arrays 18

THAT’S ALL WITH ARRAY