Programming 1 - L6 - Arrays

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Programming 1 - Lecture

Arrays & Strings

Dr. Mick Mc Keever


Todays Lecture
• When to use an Array.

• How to use an Array.

• When to use a String.

• How to use a String.

• What is the difference between a String and a Character.


Arrays
• Suppose we want to store 10 student numbers
for processing in code?
• An array is an efficient way to hold all the
values with one variable storing 10 values or
any number of values.
Arrays
• Arrays are used to store multiple values in a single variable,
instead of declaring separate variables for each value.
• To create an array, define the data type (like int) and specify
the name of the array followed by square brackets [].
• To insert values to it, use a comma-separated list, inside curly
braces:

int studentNum[] = {25, 50, 75, 100};


How does an array store data?
• An array is a list of elements of the same type, identified by a pair of square
brackets [ ].
• It creates space in the memory to save a list of items of the same type (int,
double , char, float

Name of each space in the array Each section of the array is called
An ELEMENT

Student Student Student Student Student Student Student Student Student Student
Num[0] Num[1] Num[2] Num[3] Num[4] Num[5] Num[6] Num[7] Num[8] Num[9]
13 201 55 6 17 89 25 67 351 29

The value stored in that space in memory


Named studentNum[4]
Access Elements of Array
• To access an array element, refer to its index number.
• Array indexes start with 0: [0] is the first element. [1] is the
second element, etc.
• This statement accesses the value of the first element [0] in
myNumbers:

int myNumbers[] = {25, 50, 75, 100};


printf("%d", myNumbers[0]);

// Outputs 25
Change an Array Element
To change the value of a specific element, refer to the index
number:

int myNumbers[] = {25, 50, 75, 100};


myNumbers[0] = 33;

printf("%d", myNumbers[0]);

// Now outputs 33 instead of 25


Change an Array Element
// Declare an array of four integers:
int myNumbers[4];

// Add elements
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;
Loop Through an Array
• You can loop through the array elements with the for loop.
• The following example outputs all elements in
the myNumbers array:

int myNumbers[] = {25, 50, 75, 100};


int i;

for (i = 0; i < 4; i++) {


printf("%d\n", myNumbers[i]);
}
Data type review
Data Type Size Description
int 4 bytes Stores whole numbers, without decimals
float 4 bytes Stores fractional numbers, containing one or more
decimals. Sufficient for storing 7 decimal digits
double 8 bytes Stores fractional numbers, containing one or more
decimals. Sufficient for storing 15 decimal digits
char 1 byte Stores a single character/letter/number, or ASCII values
Strings & Chars
• char is a data type that holds one character e.g. letter, number, space
• String is a string of characters together.
• String is a string or an array of characters terminated by \0 (which is backslash
zero)

char message1='h'; //were storing one character


char message3[]={'h','e','l','l','o','\0'};//if we type in char
form we enter the \0 to denote it’s a string
char message2[]="hello"; // For a char array ,instead of typing
each char followed individually all followed by \0. C allows you store
a string of characters together or a string
String versus Char and %s Vs %c
Todays Lecture
• When to use an Array.

• How to use an Array.

• When to use a String.

• How to use a String.

• What is the difference between a String and a Character.


Thank You
Dr. Mick Mc Keever

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