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

Intro to Programming Week 6

The document outlines the curriculum for BCSC 1102: Intro to Programming, focusing on arrays, their characteristics, and their use in programming. It covers single and multidimensional arrays, their relationship with pointers, and how to pass arrays to functions. Additionally, it discusses string manipulation and common string operations, providing examples and function definitions.
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)
2 views

Intro to Programming Week 6

The document outlines the curriculum for BCSC 1102: Intro to Programming, focusing on arrays, their characteristics, and their use in programming. It covers single and multidimensional arrays, their relationship with pointers, and how to pass arrays to functions. Additionally, it discusses string manipulation and common string operations, providing examples and function definitions.
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/ 9

BCSC 1102 : Intro To Programming Week 6

Dr. Shem Mbandu Angolo, PhD

The Co-operatetive University of Kenya


September - December 2024

Contents
1 Introduction to Arrays 3
1.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Uses of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Single and Multidimensional Arrays 3


2.1 Single-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . 3
2.1.1 Declaration and Initialization . . . . . . . . . . . . . . . . 3
2.1.2 Accessing Elements . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.1 Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . 4
2.2.2 Declaration and Initialization . . . . . . . . . . . . . . . . 4
2.2.3 Accessing Elements . . . . . . . . . . . . . . . . . . . . . . 4
2.2.4 Higher-Dimensional Arrays . . . . . . . . . . . . . . . . . 4

3 Convergence Between Arrays and Pointers 4


3.1 Relationship Between Arrays and Pointers . . . . . . . . . . . . . 4
3.1.1 Array Name as a Pointer . . . . . . . . . . . . . . . . . . 4
3.1.2 Pointer Arithmetic . . . . . . . . . . . . . . . . . . . . . . 5
3.2 Pointers and Array Indexing . . . . . . . . . . . . . . . . . . . . . 5

4 Arrays in Functions 5
4.1 Passing Arrays to Functions . . . . . . . . . . . . . . . . . . . . . 5
4.1.1 Function Declaration . . . . . . . . . . . . . . . . . . . . . 5
4.1.2 Function Definition . . . . . . . . . . . . . . . . . . . . . . 5
4.1.3 Function Call . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.2 Multidimensional Arrays in Functions . . . . . . . . . . . . . . . 6
4.2.1 Function Declaration . . . . . . . . . . . . . . . . . . . . . 6
4.2.2 Function Definition . . . . . . . . . . . . . . . . . . . . . . 6
4.2.3 Function Call . . . . . . . . . . . . . . . . . . . . . . . . . 6

1
5 String Manipulation and Functions 6
5.1 Definition of Strings . . . . . . . . . . . . . . . . . . . . . . . . . 6
5.2 String Declaration and Initialization . . . . . . . . . . . . . . . . 7
5.3 Common String Operations . . . . . . . . . . . . . . . . . . . . . 7
5.3.1 String Length . . . . . . . . . . . . . . . . . . . . . . . . . 7
5.3.2 String Concatenation . . . . . . . . . . . . . . . . . . . . 7
5.3.3 String Comparison . . . . . . . . . . . . . . . . . . . . . . 7
5.3.4 String Copy . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5.4 String Manipulation Functions . . . . . . . . . . . . . . . . . . . 8
5.4.1 strcpy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.4.2 strcat . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.4.3 strcmp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
5.4.4 strlen . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

6 Conclusion 9

2
1 Introduction to Arrays
1.1 Definition
An array is a data structure consisting of a collection of elements, each identified
by at least one array index or key. Arrays are used to store multiple values in
a single variable, instead of declaring separate variables for each value.

1.2 Characteristics
• Fixed Size: The size of an array is determined at the time of its creation.
• Homogeneous Elements: All elements in an array are of the same data
type.
• Contiguous Memory Location: Elements are stored in contiguous
memory locations.

1.3 Uses of Arrays


Arrays are widely used in programming to:
• Store multiple items of the same type together.
• Implement other data structures like lists, stacks, queues, etc.
• Efficiently manage and manipulate large amounts of data.

2 Single and Multidimensional Arrays


2.1 Single-Dimensional Arrays
A single-dimensional array (or one-dimensional array) is a list of elements, all
of the same type, accessed by a single index.

2.1.1 Declaration and Initialization

1 // Declaration
2 int arr [10];
3

4 // Initialization
5 int arr [5] = {1 , 2 , 3 , 4 , 5};

2.1.2 Accessing Elements


Elements in a single-dimensional array are accessed using the index:
1 int value = arr [2]; // value is 3

3
2.2 Multidimensional Arrays
Multidimensional arrays are arrays of arrays. The most commonly used multi-
dimensional arrays are two-dimensional arrays.

2.2.1 Two-Dimensional Arrays


A two-dimensional array is an array of arrays, where each element is accessed
by two indices.

2.2.2 Declaration and Initialization

1 // Declaration
2 int arr [3][4];
3

4 // Initialization
5 int arr [2][3] = {{1 , 2 , 3} , {4 , 5 , 6}};

2.2.3 Accessing Elements


Elements in a two-dimensional array are accessed using two indices:
1 int value = arr [1][2]; // value is 6

2.2.4 Higher-Dimensional Arrays


Arrays can have more than two dimensions, such as three-dimensional arrays,
though these are less commonly used.

3 Convergence Between Arrays and Pointers


3.1 Relationship Between Arrays and Pointers
In C, the name of an array acts as a pointer to the first element of the array.
This means that the array name can be used to access the elements of the array
through pointer arithmetic.

3.1.1 Array Name as a Pointer

1 int arr [5] = {1 , 2 , 3 , 4 , 5};


2 int * p = arr ; // p now points to arr [0]

4
3.1.2 Pointer Arithmetic

1 int arr [5] = {1 , 2 , 3 , 4 , 5};


2 int * p = arr ;
3 int value = *( p + 2) ; // value is 3

3.2 Pointers and Array Indexing


Both array indexing and pointer arithmetic can be used interchangeably to
access elements of an array.
1 int arr [5] = {1 , 2 , 3 , 4 , 5};
2 int * p = arr ;
3

4 // Using array indexing


5 int value1 = arr [2]; // value1 is 3
6

7 // Using pointer arithmetic


8 int value2 = *( p + 2) ; // value2 is 3

4 Arrays in Functions
4.1 Passing Arrays to Functions
Arrays can be passed to functions by specifying the array name without brackets.
The function receives a pointer to the first element of the array.

4.1.1 Function Declaration

1 void printArray ( int arr [] , int size ) ;

4.1.2 Function Definition

1 void printArray ( int arr [] , int size ) {


2 for ( int i = 0; i < size ; i ++) {
3 printf ("% d " , arr [ i ]) ;
4 }
5 printf ("\ n ") ;
6 }

5
4.1.3 Function Call

1 int main () {
2 int arr [5] = {1 , 2 , 3 , 4 , 5};
3 printArray ( arr , 5) ;
4 return 0;
5 }

4.2 Multidimensional Arrays in Functions


Multidimensional arrays can also be passed to functions. However, the dimen-
sions (except the first) must be specified in the function definition.

4.2.1 Function Declaration

1 void print2DArray ( int arr [][3] , int rows ) ;

4.2.2 Function Definition

1 void print2DArray ( int arr [][3] , int rows ) {


2 for ( int i = 0; i < rows ; i ++) {
3 for ( int j = 0; j < 3; j ++) {
4 printf ("% d " , arr [ i ][ j ]) ;
5 }
6 printf ("\ n ") ;
7 }
8 }

4.2.3 Function Call

1 int main () {
2 int arr [2][3] = {{1 , 2 , 3} , {4 , 5 , 6}};
3 print2DArray ( arr , 2) ;
4 return 0;
5 }

5 String Manipulation and Functions


5.1 Definition of Strings
A string is a sequence of characters. Strings are used to represent text in
programming.

6
5.2 String Declaration and Initialization

1 // Declaration
2 char str [10];
3

4 // Initialization
5 char str [] = " Hello ";

5.3 Common String Operations


5.3.1 String Length
The length of a string can be determined using the strlen function.
1 # include < string .h >
2

3 int len = strlen ( str ) ; // len is 5 for " Hello "

5.3.2 String Concatenation


Strings can be concatenated using the strcat function.
1 # include < string .h >
2

3 char str1 [20] = " Hello ";


4 char str2 [] = " World ";
5 strcat ( str1 , str2 ) ; // str1 is now " Hello World "

5.3.3 String Comparison


Strings can be compared using the strcmp function.
1 # include < string .h >
2

3 int result = strcmp ( str1 , str2 ) ;


4 if ( result == 0) {
5 // str1 and str2 are equal
6 }

5.3.4 String Copy


Strings can be copied using the strcpy function.
1 # include < string .h >
2

3 char str1 [20];

7
4 char str2 [] = " Hello ";
5 strcpy ( str1 , str2 ) ; // str1 is now " Hello "

5.4 String Manipulation Functions


5.4.1 strcpy
Copies the string pointed to by source (including the null character) to the
destination.
1 # include < string .h >
2

3 char dest [20];


4 char src [] = " Sample ";
5 strcpy ( dest , src ) ; // dest now contains " Sample "

5.4.2 strcat
Appends a copy of the source string to the destination string.
1 # include < string .h >
2

3 char dest [20] = " Hello ";


4 char src [] = " World ";
5 strcat ( dest , src ) ; // dest now contains " Hello World "

5.4.3 strcmp
Compares two strings lexicographically.
1 # include < string .h >
2

3 char str1 [] = " Hello ";


4 char str2 [] = " World ";
5 int result = strcmp ( str1 , str2 ) ; // result is negative
as " Hello " is less than " World "

5.4.4 strlen
Computes the length of the string.
1 # include < string .h >
2

3 char str [] = " Hello ";


4 int len = strlen ( str ) ; // len is 5

8
6 Conclusion
Arrays and strings are fundamental data structures in programming. Under-
standing their properties, how to manipulate them, and the functions available
for their manipulation is essential for efficient and effective coding. The conver-
gence between arrays and pointers is particularly important in C programming,
as it provides a deeper understanding of memory management and efficient data
handling.

You might also like