Unit 4Arrays and Strings
Follows IGNOU MCS-201, Unit 4
A single variable holds a single value, but real programs work with collections: the marks of a whole class, the prices on an invoice, the letters of a name. An array stores many values of the same type under one name, and a string is simply an array of characters with a special end marker. This unit covers how to declare, fill and process arrays, how to search and sort them, how two-dimensional arrays model tables, and how C stores and manipulates text.
In this unit you will learn to
- Declare one-dimensional arrays and access their elements with subscripts.
- Initialise arrays and understand what uninitialised elements contain.
- Process arrays with loops to find totals, averages and maximum values.
- Search an array with linear search and sort it with selection sort.
- Declare, initialise and process two-dimensional arrays.
- Store text as null-terminated strings, and read and print strings safely.
- Format string output and work with arrays of strings.
- Use the standard string functions, and write your own versions of the common ones.
Sections in this unit
- 4.1Introduction to Arrays and Array DeclarationLearn what an array is, why it is needed, how to declare one in C, and how to size it with a symbolic constant.8 min
- 4.2Array Initialisation and SubscriptsInitialise arrays in their declaration, understand default and garbage values, and access elements safely with subscripts.8 min
- 4.3Processing Arrays: Maximum, Average and Linear SearchProcess arrays with loops to find a maximum, compute averages across parallel arrays, and locate a value with linear search.9 min
- 4.4Selection Sort and Passing Arrays to FunctionsSort an array in ascending order with selection sort, trace its passes, and understand how an array is passed to a function.8 min
- 4.5Multi-Dimensional ArraysDeclare and initialise two-dimensional arrays, understand row-major storage, and process tables with nested loops.7 min
- 4.6Strings: Declaration, Initialisation and Input/OutputUnderstand C strings as null-terminated character arrays, declare and initialise them, and read and print them safely with scanf and fgets.11 min
- 4.7Formatting Strings and Arrays of StringsControl how strings are printed with width and precision in printf, and store several strings in a two-dimensional character array.9 min
- 4.8Standard String FunctionsUse the standard string.h functions strlen, strcpy, strcat, strcmp and related functions correctly and safely.7 min
- 4.9Non-Standard String Functions and Writing Your OwnRecognise the non-standard string functions strlwr, strupr, strrev, strset and stricmp, and write portable versions of common string operations.8 min