0% found this document useful (0 votes)
2 views

C++ array

The document provides an overview of arrays in C++, detailing their characteristics, declaration, and access methods. It emphasizes the importance of array size during declaration and the potential pitfalls when accessing elements out of bounds. Additionally, it discusses passing arrays to functions and the implications of call-by-reference semantics.

Uploaded by

lxiiiii05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C++ array

The document provides an overview of arrays in C++, detailing their characteristics, declaration, and access methods. It emphasizes the importance of array size during declaration and the potential pitfalls when accessing elements out of bounds. Additionally, it discusses passing arrays to functions and the implications of call-by-reference semantics.

Uploaded by

lxiiiii05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

C++ Programming

Arrays

M1 Math
Michail Lampis
[email protected]
Arrays
● Correspond to notion of 1-dimensional vector
● Allow to store many items of same type
● Items are accessed with an int index





















W




















Arrays
● Arrays should be declared only inside functions
● Their size must be given in declaration






































I


Accessing array elements
● Use the [ ] operators to access individual elements
● Numbering starts at 0 and goes up to size-1
























● Allowed to initialize array at declaration







}







● Note: a[i] is an lvalue (it can be referenced)
Out of bounds
● Careful with the following:









K


















K
● No compiler or (predictable) run-time error!
Easy example
● The following loop finds the max in an int array




















































}
Example 2
● Write a loop that calculates the average value
of an int array
Example 2
● Write a loop that calculates the average value
of an int array
int data[size];
double avg;
for(int i=0; i<size; i++)
avg += data[i];
avg = avg/size;
Arrays and Functions
● It is possible to pass an array as a function
parameter
● Semantics are call-by-reference, not call-by-
value
– Reason: array is not copied
– Only reference to array is passed
find_max


_













































}
Arrays and Functions
● Note the prototype


_









● The first parameter has type array, of
unspecified size
– The size cannot specified (it will be ignored)
● Imperative to supply the array size as parameter
– Impossible to recover otherwise inside the function
Why we need the size




















}




































}
Array pitfalls
● Common operators may not compile, or not
work as expected
● Example 1






}










Array pitfalls
● Common operators may not compile, or not
work as expected
● Example 2






}







}








“

”





W







Explanation
● The identifiers a,b in the previous programs are
allowed to be considered as variables
themselves (of type array) BUT
– They cannot be written on (const type)
– Their actual value is NOT the array, but the place in
memory where the array is stored
● This will become more familiar once we talk
about pointers
Returning arrays
● Suppose that I want to write a function that, given n, return
the array [1,2,3,...,n]
● First try









WG
















}
Returning Arrays
● Arrays cannot be returned for two reasons
– Returning an array is forbidden by the C++ rules
(stupid reason)
– The memory where the array is stored is destroyed
when the function terminates!
– Recall: Normally, return copies its operand (with
the = operation). The = operation does not work on
arrays...
● Solution: pointers (next class)
An example
● The sieve of Eratosthenes
● Write a program that decides which of the
integers 1,2,...,n are primes
● For each i
– If i is prime mark all multiples of i as non-prime
Eratosthenes




















I
























“



”


















}
}
Arrays and Strings
● Traditionally in C strings are just char arrays





“

”










}









● 


















● C++ has a string class
– Much nicer, more convenient, less buggy
– Avoid C-type strings if possible
Resizing Arrays?
● Once an array has been declared its value is
fixed
● What if I need more/less space?
● Two solutions:
– Vector class (to be seen later)
– Dynamic memory management (new, delete)
● Second solution is needed also to return arrays

You might also like