You are on page 1of 4

Birla Institute of Technology & Science, Pilani, Hyderabad Campus

Computer Programming [CS F111]


2021-22 First Semester
Lab Sheet 7

Topics to be covered:
1. Arrays

1. Arrays

● Arrays are data structures consisting of related data items of the same type.
● A group of contiguous memory locations that all have the same type.
● We refer to a particular location or element in an array by (1) array’s name and
(2) position number of the particular element in the array.

1.1 Array indexing

● The first element in every array is the zeroth element.


● An array name, like other identifiers, can contain only letters, digits, and
underscores and cannot begin with a digit.
● The position number within square brackets is called an index.

The following definition reserves 10 elements for integer array A, which has indices in
the range 0-9.

Index Index Index Index Index Index Index Index Index Index
=0 =1 =2 =3 =4 =5 =6 =7 =8 =9

The following definition reserves 100 elements for character array C and 50 elements
for double array D:

● Like other variables, uninitialized array elements contain garbage values.


● An index of an array must be an integer or an integer expression. For example,
o The statement A[5] = 40; assigns 40 the index 5 of array A.
Index Index Index Index Index Index Index Index Index Index
=0 =1 =2 =3 =4 =5 =6 =7 =8 =9
40

1.2 Array initialization

● If there are fewer initializers than elements in the array, the remaining elements
are initialized to zero.
● Example: int A[10] = {0}; //initializes entire array to zeros.
Index Index Index Index Index Index Index Index Index Index
=0 =1 =2 =3 =4 =5 =6 =7 =8 =9
0 0 0 0 0 0 0 0 0 0

● The array definition: int B[4] = {11, 12, 13, 15, 45}; causes a syntax error because
there are five initializers and only four array elements.
● If the array size is omitted from a definition with an initializer list, the number of
elements in the array will be the number of elements in the initializer list.
● Example: int n[] = {100, 3, 40, 59, 6, -7}; will create a six element array initialized
with the values indicated.
Index=0 Index=1 Index=2 Index=3 Index=4 Index=5
100 3 40 59 6 -7

2. Character array and String representation

● We can store strings in character arrays.


● The only string processing we have seen so far is only to output a string using
“printf” function.
● A string such as “hello” is an array of individual characters in C.
● Example: char string[] = “hello”;
● The size of the array string is determined by the compiler based on the length of
the string.
● The string “hello” contains five characters plus a special string-terminating
character called the null character ‘\0’.
● All strings in C end with this character.
● You can write the previous array expression in this way as well:
o char string[] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\o’};
● As the string is an array of characters, we can access individual characters in a
string directly using array indices.
● We can also assign a string to a character array using “scanf” function as follows:
char string[10];
scanf(“%9s”, string);

● The statement above creates a character array capable of storing string of at


most 9 characters and a terminating null characters.
● Exercise: Check what happens if you omit writing “9” in the “scanf” and give input
a string of length more than 10!

Some important points:


● In C, it is possible to have an array of all types except void and functions. See
this for details.

● In C, arrays and pointers are different. They seem similar because array name
gives address of first element and array elements are accessed using pointer
arithmetic. See array vs pointer in C for details.

● Arrays are always passed as pointers to functions. See this for details.

● There is no index out of bounds checking in C/C++

● Like other variables, arrays can be allocated memory in any of the three
segments, data, heap, and stack. Dynamically allocated arrays are allocated
memory on heap, static or global arrays are allocated memory on the data
segment and local arrays are allocated memory on the stack segment.

Pointer Array

It is declared as -: It is declared as -:

*var_name; data_type var_name[size];

It is used to store the address of It is used to store the multiple variable


different variables of the same data of same data type
type.

We can generate a pointer to the array. We can generate a array of pointer

It is designed to store the address of It is designed to store the value of


variable variable.

A pointer variable can store the A array can store the number of
address of only one variable at a time. elements the same size as the size of
the array variable.
Exercise problems:

1. Write a program to find the sum of integers in an array.


2. Write a program with function that takes and array of integers as input and
checks if a given integer (provided by the user) is present in the array or not. if
present the function should return the index else it should return -1.
3. Write a program that takes a paragraph as input and computes the following: (1)
number of lines (2) number of words (3) number of characters in the paragraph.
4. Write a program that abbreviates a name, for example Vangipurapu Venkata Sai
Laxman becomes V V S Laxman.
5. Say y=f(x) is stored in an array of y and x, find out dy/dx

You might also like