Unit4ArrayandStringpptx__2024_09_30_13_15_25

Marwadi University Department of FOT 01CE0101 - Computer Programming Unit - 4 Array and String

Arrays

  • Definition: An array is a collection of data items of the same type, accessed with a common name.

    • Data Structure: Holds a finite sequential collection of homogeneous data.

Characteristics of Arrays

  • Collection: An array acts as a container for a collection of data.

  • Finite: The number of data items in an array is determined before use and is always finite.

  • Sequential: Data in the array is stored sequentially in memory.

  • Homogeneous: All data items in an array must share the same data type.

Examples of Arrays

  • List of customers and their phone numbers.

  • Table of daily rainfall data.

  • List of employees in an organization.

  • Test scores of a class of students.

Declaration of Arrays

  • Syntax: type arrayName[arraySize];

  • Examples:

    • int group[10];

    • float height[50];

    • char name[15];

Initialization of Arrays

  • After declaring an array, its elements must be initialized.

  • Static Initialization: Initialize during declaration.

  • Dynamic Initialization: Initialize elements during program execution.

Static Initialization

  • Defined using curly braces with values separated by commas.

    • Example: int marks[5] = {90, 86, 89, 76, 91};

Dynamic Initialization

  • Assign values to an array element during execution using the following syntax:

    • Syntax: array_name[index] = some_value;

    • Example: scanf("%d", &arr[i]);

Advantages and Disadvantages of Arrays

  • Advantages:

    • Less lines of code for multiple elements.

    • Random access using array index.

    • Easy access and traversal of elements.

    • Simplified sorting.

  • Disadvantages:

    • Costly insertion and deletion due to memory management.

    • Fixed size defined at declaration; not dynamic like linked lists.

One-Dimensional Arrays

  • Syntax: datatype array_name[size];

  • Example: Declaration and access examples provided.

Accessing Elements of an Array

  • Specify array name followed by an index inside square brackets. Index starts at 0.

  • Example:

    • int a[5];

    • Elements: a[0], a[1], ... a[4]

Two-Dimensional Arrays in C

  • A simplest form of multidimensional array, defined as an array of arrays.

  • Organized as matrices (rows and columns) for bulk data processing.

Declaration of Two-Dimensional Arrays

  • Syntax: datatype array_name[rows][columns];

  • Example: Can be declared with the type and size of rows and columns.

Initializing Two-Dimensional Arrays

  • Two methods: conventional and better method using nested braces.

    • Example: int x[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};

Matrix Operations Using Arrays

  • Definition: A matrix is a grid-like storage format that organizes data in rows and columns.

  • Basic operations include addition, subtraction, and multiplication along with user-defined orders.

Matrix Multiplication Algorithm

  1. Start Program.

  2. Enter rows and columns of matrices.

  3. Input elements for both matrices.

  4. Perform multiplication and store in a third matrix.

  5. Print final matrix.

Strings in C

  • Definition: Sequence of characters treated as a single data item, enclosed in double quotes. Example: "You are the best."

Declaration of Strings

  • Strings are declared as character arrays.

  • Syntax: char string-name[size];

Initializing String Variables

  • Strings are null-terminated; anything after is ignored.

    • Example: char str[20] = "Initial value";

Common String Handling Functions

  • Functions included in string.h:

    • strlen(): Returns length, excluding null character.

    • strcpy(): Copies source string to destination.

    • strcmp(): Compares two strings.

    • strcat(): Concatenates two strings.

    • strrev(): Reverses the string.

    • strlwr(): Converts string to lowercase.

    • strupr(): Converts string to uppercase.

    • strstr(): Returns pointer to a substring.

Example Program for String Functions

  • Demonstrates the use of several string functions in C like length, reverse, concatenation, and comparison.

Conclusion

  • Understanding arrays and strings is fundamental in programming with C, enabling structured data storage and manipulation.

robot