0% found this document useful (0 votes)
8 views12 pages

Informatics 2 - 4,5,6

Uploaded by

wskwarek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Informatics 2 - 4,5,6

Uploaded by

wskwarek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lesson 4, 5, 6

One-dimensional arrays - repetition


and supplementation of knowledge.

Tablice jednowymiarowe – powtórzenie i


uzupełnienie wiadomości
The Agenda
1. Definition of array - repetition
2. Initializing arrays – repetition
3. Accessing the values of an array – repetition
4. Examples
5. Pointers and arrays
6. Declaring variables of pointer types
7. Dynamically Allocated Arrays
8. Examples
Array - deffinition
0 1 2 3 4
billy
int
Example
int billy [5];

type name [elements];


where:
type is a valid type (like int, float...),
name is a valid identifier and the
elements field (which is always enclosed in square brackets []), specifies how
many of these elements the array has to contain.
Initializing arrays
When declaring a regular array of local scope (within a function, for example), if
we do not specify otherwise, its elements will not be initialized to any value by
default, so their content will be undetermined until we store some value in them.
The elements of global and static arrays, on the other hand, are automatically
initialized with their default values, which for all fundamental types this means they
are filled with zeros.

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

0 1 2 3 4
billy 16 2 77 40 12071
Initializing arrays
The amount of values between curly braces { } must not be larger than the number
of elements that we declare for the array between square brackets [ ].
For example, in the example of array billy we have declared that it has 5 elements
and in the list of initial values within braces { } we have specified 5 values, one for
each element.
When an initialization of values is provided for an array, C++ allows the possibility
of leaving the square brackets empty [ ]. In this case, the compiler will assume a
size for the array that matches the number of values included between braces { }:
int billy [] = { 16, 2, 77, 40, 12071 };
After this declaration, array billy would be 5 ints long, since we have provided 5
initialization values.
Accessing the values of an array.
In any point of a program in which an array is visible, we can access the value of
any of its elements individually as if it was a normal variable, thus being able to
both read and modify its value. The format is as simple as:
name[index]
Example
For example, to store the value 75 in the third element of billy, we could write the
following statement:
billy[2] = 75; billy[0] billy[1] billy[2] billy[3] billy[4]
billy

and, for example, to pass the value of the third element of billy to a variable called
a, we could write:
a = billy[2];
Examples
1. Write a C++ program that reads in 10 integers from the user and stores them
in an array. The program should then compute and output the sum and
average of the elements in the array.
2. Write a C++ program that reads in 10 integers from the user and stores them
in an array. The program should then compute and output the sum and
average of the elements in the array.
3. Write a C++ program that reads in an array of 10 integers from the user and
outputs the largest and smallest values in the array.
Pointers and arrays
The concept of array is very much bound to the one of pointer.
In fact, the identifier of an array is equivalent to the address of its first element, as
a pointer is equivalent to the address of the first element that it points to, so in fact
they are the same concept.
int numbers [20];
int * p;

It is possible to do
p = numbers;

but it is not possible to do


numbers = p;
Because numbers is an array, so it operates as a constant pointer, and we cannot
assign values to constants.
Declaring variables of pointer types
Example:
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int numbers[5];
5. int * p;
6. p = numbers; *p = 10;
7. p++; *p = 20;
8. p = &numbers[2]; *p = 30;
9. p = numbers + 3; *p = 40;
10. p = numbers; *(p+4) = 50;
11. for (int n=0; n<5; n++)
12. cout << numbers[n] << ", ";
13. return 0;
14.}
OUTPUT:
10, 20, 30, 40, 50,
Dynamically Allocated Arrays
1. #include <iostream>
2. using namespace std;
3. int main(){
4. int size;
5. cout << "Enter the size of the array: ";
6. cin >> size;
7. // Dynamically allocate memory for an array of 'size' integers
8. int* arr = new int[size];
9. // Input values for the array
10. for (int i = 0; i < size; ++i) {
11. cout << "Enter element " << i << ": ";
12. cin >> arr[i];
13. }
14. // Output the elements of the array
15. cout << "Array elements: ";
16. for (int i = 0; i < size; ++i)
17. cout << arr[i] << " ";
18. cout << endl;
19. // Don't forget to deallocate the dynamically allocated memory
20. delete[] arr;
21. return 0;
22. }
Problems
1. Declare an array of ten floating-point numbers and a pointer variable 'ptr'.
Assign values to the elements of the array and use the pointer 'ptr' to
display the elements in reverse order.

2. Create an integer array of size 6 and initialize it with some random values.
Declare two pointers 'ptr1' and 'ptr2', and make 'ptr1' point to the first
element of the array and 'ptr2' point to the last element of the array. Then,
using these pointers, calculate and display the sum of the elements they
point to.
Homework
Declare a five-element integer array with numbers 101, 102, 103, 104, 105, and
a pointer variable 'p'. Display the elements of the array using the variable 'p'.

You might also like