Arrays in C PDF
Arrays in C PDF
■ Introduction to Arrays
■ Declaring & initializing an array
■ Multidimensional Arrays
■ String and Character Arrays
Introduction to Arrays
■ Arrays are a series of elements of the same data type placed
consecutively in memory that can be individually referenced by adding
an index to a unique name.
■ Using an array we can store five values of type int with a single
identifier without having to declare five different variables with a
different identifier.
Declaring & Initializing an Array
■ Arrays are useful when you store related data items, such as grades
received by the students, sine values of a series of angles, etc.
■ Here int is the data type, arr is the name of the array and 10 is the
size of array. It means array arr can only contain 10 elements of int
type.
■ Index of an array starts from 0 to size-1 i.e first element of arr array
will be stored at arr[0] address and last element will occupy arr[9].
Declaring & Initializing an Array
■ In the first approach, the value of each element of the array is listed
within two curly brackets ({ }) and a comma ( ,) is used to separate one
value from another. For example: The following array holds marks for
five students in a subject.
Declaring & Initializing an Array
■ In the second approach elements of the array can be initialized one at
a time. This approach makes use of the format:
Sample Run:
Example 2: Computing the
average marks
Sample Run:
Example 3: Computing the
average marks using a Function
Sample Run:
Assignment 2.1
1) Write down a C Program that accepts 10 integer values from the
keyboard, stores them in an array called Numbers, and then
calculates the sum of only even numbers.
3) Rewrite the programs you have written in (1) and (2) adding a
function that accepts an array as a formal parameter to each of the
programs.
Multidimensional Arrays
■ The type of arrays that we have discussed so far are called one-
dimensional (or single dimensional) arrays, as they only possess one
index and store only one type of data.
■ The array “marks” above can only hold marks of 5 students in one
subject.
Multidimensional Arrays
■ This array can be extended to store marks of students in many
subjects using a two-dimensional array. Such an array can be declared
in the following form:
int marks[100][5];
Multidimensional Arrays
■ Initializing marks of the first student can be performed as follows:
marks[0][0] = 55;
marks[0][1] = 33;
marks[0][2] = 86;
marks[0][3] = 81;
marks[0][4] = 67;
■ Similarly, we can define arrays with n dimensions and such arrays are
called n-dimensional or multidimensional arrays.
Multidimensional Arrays
■ Two-dimensional arrays are initialized in the same way. The array
“scores” below declares and initializes a two-dimensional array of type
int which holds the scores of three students in five different tests.
Multidimensional Arrays
Program 2.4 shows a
program which
computes and displays
the average score
attained by the
students in all tests.
OUTPUT:
Assignment 2.2
■ Rewrite program 2.4 so that it displays the highest test
score instead of the average. Use a for loop in your
program.
char name[20]={'J','o','s','e','p','h','i','n','e'};
char name[20]=“Josephine”;
String and Character Arrays
■
OUTPUT:
String Input and Output
■ Input function scanf( ) can be used with %s format specifier to read a
string input from the terminal.
■ But there is one problem with scanf( ) function, it terminates its input
on first white space it encounters.
■ Therefore if you try to read an input string "Hello World" using scanf( )
function, it will only read Hello and terminate after encountering white
spaces (See Program 2.6 on the next slide).
String Input and Output
SAMPLE RUN:
String Input and Output
SAMPLE RUN:
String Handling Functions
■ C language supports a large number of string handling functions that
■ Table 2.1 ( on the next slide) shows some of the most commonly used
OUTPUT
Example 2: strlen()
OUTPUT
Example 2: strcpy()
OUTPUT
Reading List
■ https://www.tutorialspoint.com/cprogramming/c_arrays.htm
■ https://www.programiz.com/c-programming/c-arrays
■ https://www.javatpoint.com/c-array
■ https://www.studytonight.com/c/arrays-in-c.php