0% found this document useful (0 votes)
62 views32 pages

Arrays in C PDF

This document provides an introduction to arrays in C programming. It defines arrays as a series of elements of the same data type placed consecutively in memory that can be referenced using an index. The document discusses declaring and initializing arrays, multidimensional arrays, string and character arrays, and common string handling functions. Examples are provided to demonstrate declaring arrays, initializing elements, calculating averages and highest values, and manipulating strings.

Uploaded by

Dj Louderhouse
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)
62 views32 pages

Arrays in C PDF

This document provides an introduction to arrays in C programming. It defines arrays as a series of elements of the same data type placed consecutively in memory that can be referenced using an index. The document discusses declaring and initializing arrays, multidimensional arrays, string and character arrays, and common string handling functions. Examples are provided to demonstrate declaring arrays, initializing elements, calculating averages and highest values, and manipulating strings.

Uploaded by

Dj Louderhouse
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/ 32

ARRAYS IN C

Mr. Luyima Alex Cedric


aluyima@ndejjeuniversity.ac.ug
+256 772 302 775/+256 708045305

Mr. Mugejjera Emmanuel


emugejjera@gmail.com
Phone: +256 703 186 705 / +256 772 959 183
Outline

■ 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.

■ Like any other variable in C an array must be declared before it is


used.

■ The typical declaration of an array is:

■ Number of elements is also called the index.


Declaring & Initializing an Array
■ Please Note: The array name must not be separated from the square
brackets containing the index.

■ When declaring an array, the number of array elements should be a


constant.

■ Arrays are blocks of static memory locations of a given size, the


compiler must be able to determine exactly how many memory
locations to allocate at compilation time.
Declaring & Initializing an Array
int arr[10];

■ 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:

In an array, the index of the first


element is considered as zero rather
than one. Therefore in an array with
“n” elements, the first index is “ 0”
and the last index is “ n-1”.
Example 1: Entering array
elements from Keyboard
Program 2.1 shows a C
program which allows a
user to enter marks for
five students in one
subject and then these
marks are displayed on
the screen.

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.

2) Write down a C Program that accepts 5 float values from the


keyboard, stores them in an array called Heights, and then displays
the highest number among the 5 on the screen. Use a for loop in
your program.

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:

■ An array can be declared to hold marks for 100 students in 5 subjects


as shown below:

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.

■ Write down a C program that stores marks for 5


students in 4 subjects entered through the keyboard.
Your program should then calculate and display the
average marks of each student, and then calculate the
average marks attained in each subject.
String and Character Arrays
■ A string is a sequence of characters that is treated as a single data
item.

■ The C language does not support strings as a data type.

■ A string is actually one-dimensional array of characters in C language.

■ Strings are often used to create meaningful and readable programs.


String and Character Arrays
■ A string is a sequence of characters that is treated as a single data
item.

■ The C language does not support strings as a data type.

■ A string is actually one-dimensional array of characters in C language.


Strings are often used to create meaningful and readable programs.

■ There are different ways to initialize a character array variable:

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

can be used to carry out many of the string manipulations.

■ These functions are packaged in string.h library. Hence, you must

include string.h header file in your program to use these functions.

■ Table 2.1 ( on the next slide) shows some of the most commonly used

string handling functions.


Table 2.1 String Handling
Functions
Function / Method Description
strcat(s1,s2) It is used to concatenate(combine) two strings.
strlen(s1) It is used to show length of a string.
strcpy(s1,s2) It is used to copy one string into another.
strrev(s1,s2) It is used to show reverse of a string.
strcmp(s1,s2) It is used to compare two string.
strchr(s1,ch) Returns a pointer to the first occurrence of character ch
in string s1
strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in
string s1
Example 1: strcat()

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

■ Video Link https://www.youtube.com/watch?v=u8I31l8Pwc4

■ Video Link https://www.youtube.com/watch?v=DQTBPuCdqYI


END
THANK YOU
Contact Details

Mr. Luyima Alex Cedric


Email: aluyima@ndejjeuniversity.ac.ug / alace122@gmail.com
Phone: +256 772 302 775/+256 708 045 305

Mr. Mugejjera Emmanuel


Email: emugejjera@gmail.com
Phone: +256 703 186 705 / +256 772 959 183
Ndejje University in Uganda

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy