0% found this document useful (0 votes)
20 views

Lecture 6 Arrays

Uploaded by

janithadilsham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture 6 Arrays

Uploaded by

janithadilsham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

11/30/2023

Outline
CSC 1113 • Arrays
Programming Techniques – Declare an array and accessing elements
• Types of array
– 1D Arrays
– Multi-Dimensional Arrays

Mrs.T.D.Gilmini
Senior Lecturer
Department of Computer Science

Learning Outcomes of today’s lesson What is an Array?


• Understand appropriate usage of array data • An array is a collection of same type of
structures elements which are sheltered under a
• Write basic C programs that uses arrays common name
• An array can be visualized as a row in a table,
whose each successive block can be thought
of as memory bytes containing one element
• Look at the figure below :
– An Array of five elements:

How to Define an Array?

• An array is defined as following :


<type-of-array> <name-of-array> [<number of
elements in array>];

• For example, an array of five characters can be


defined as :
char arr[5];
Declare an array, mark, of floating-point type. And its size is 5. Meaning, it
can hold 5 floating-point values

1
11/30/2023

How to Define an Array? Contd… How to Initialize an Array?


• type-of-array: • An array can be initialized in many ways as shown
– It is the type of elements that an array stores
– If array stores character elements then type of array is ‘char’. If
below
array stores integer elements then type of array is ‘int’ • Initialize each element separately
– Besides these native types, if type of elements in array is
structure objects then type of array becomes the structure • For example :
• name-of-array: int nums[10];
– This is the name that is given to array
– It can be any string but it is usually suggested that standards nums[0] = 2;
should be followed while naming arrays nums[1] = 4;
• [number of elements]:
– This value in subscripts [] indicates the number of elements the ………………….
array stores
nums[9] = 24;

How to Initialize an Array? How to Initialize an Array?


• Initialize using a for loop • Initialize an array at the time of declaration
int myNums[10]; For example :
int i = 0; int myNumsArr[] = {1,2,3,4,5};
int n =sizeof(myNums)/sizeof(myNums[0]); char myChars[] = {'c', 'o', 'd', 'e','\0'};
for( i=0 ; i < n ; i++ ) – Strings in C language are nothing but a series of
{
characters followed by a null byte
myNums [i] = i;
// Assign value for each array element – So to store a string, we need an array of characters
} • Initialize a char array with a string
• Note : sizeof is an in-built function char arr[] = "code";
sizeof(array_name) calculates the size of the array in bytes.
sizeof(array_name[index]) calculates the size of one element in the
array.

Accessing Values in an Array Accessing Values in an Array


• Accessing the desired element of an array • Alternatively, a for loop can be used
int arr[10];
int i = 0;
int n =sizeof(arr)/sizeof(arr[0]);
for( i=0 ; i < n; i++ )
{
arr[i] = i; // Initializing each element separately
}
int j = arr[4]; // Accessing the 5th element of integer
array arr and assigning its value to integer 'j‘
printf(“%d” , j);

2
11/30/2023

Array Types
• Single Dimensional Array / One Dimensional
Array
– used to store list of values of same datatype
– store a row of values
– called as one-dimensional arrays, Linear Arrays or
simply 1-D Arrays.
• Multi Dimensional Array
– array of arrays is called as multi dimensional array
– an array created with more than one dimension (size)
• 2 – D array
• 3 – D array
int arr[2][2] = {10,20,30,40};

int arr[3][2][2]={0,1,2,3,4,5,6,7,8,9,3,2}

Example Practice Question


• Write a program to store two-D array consist
of two rows and three columns. Then read
integers to your array and display the array
} values as follows.

3
11/30/2023

#include<stdio.h> int main()


{
/* 2D array declaration*/
int disp[2][3];

/*Counter variables for the loop*/


int i, j;
for(i=0; i<2; i++)
{
for(j=0;j<3;j++)
{
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}

//Displaying array elements


printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2)
{
printf("\n"); }
}
}
return 0;
}

3-D Arrays
• Write C Program to find sum of all elements
of each row of a matrix

Initialize a string array


• Declaring a string is as simple as declaring a
one dimensional array.
• A string can be initialized in different ways

4
11/30/2023

Null character in C

• The Null character is used to end character • char a[10] = "computer";


strings in the C coding language. In other terms,
in C, the Null character represents the conclusion • When this program is run, an array of size 10
of a string, the end of an array, or other concepts. is formed with the string "computer" inside it.
'0' or '\0' or simply NULL represents the The result appears to be this;
conclusion of the character string or NULL byte. • The '\0' character is used to denote the end of
• The primary purpose of using Null character is a string in this case.
using as a string terminator.
• Remember: The memory space for each
character NULL holds is 1 byte.

Display string output Read string input

• “%s” which can be used to directly print and • str gives the base address of this string
read strings

Passing strings to function


String format specifiers
• pass strings to function in a same way we pass
an array to a function • Usually %s format specifier is used to read
string input from terminal
• Scansets in C
– scanset is basically a specifier supported by scanf
family functions
– represented by %[]
– Inside scanset  specify only one character or a
set of characters (Case Sensitive)

5
11/30/2023

The example will store only capital letters to


character array ‘str’, any other character will
not be stored inside character array.

String functions
• C supports a wide range of functions that
manipulate strings under <string.h> library
• To use them, you must include
the <string.h> header file in your program

You might also like