Learning C/C++ Step-By-Step - Page 7
07. Step-by-Step C/C++ --- C Programming - Arrays
1. Introduction to arrays | |||
2. About Arrays | |||
3. Array Elements | |||
4. Passing Arrays to Functions | |||
5. Types of Arrays
|
1. Introduction to arrays
A variable can hold a constant value. Only a single constant value and it is not possible to hold more than one value in a variable.
The following example demonstrates the scope a variable.
int main() |
|
Output: 1005 |
The above program is able to display only 1005, but not all the values (i,e. 1001, 1008, 1005 ).
Can we substitute the following program in place of the above program.
int main() |
|
Output: Nothing, The above program displays a list of errors, because of the approach is wrong. |
Let’s continue with the following program to get 0 errors program.
int main() | /* 3 values to be insert */ /* First location to insert 1001 */ /* Next location to insert 1008 */ /* and Next location to insert 1005 */ /* Prints the value of 2nd location */ |
|
Output: Nothing |
The above program displays a list of errors, because of the approach is wrong.
Depending on the above program, the variable sno can hold more than one student number. It’s easy, by using multi-location technique, is also known as arrays.
2. About Arrays
Arrays contain a number of data items of the same type. This type can be a simple data type, a structure, or a class. The items in an array are called elements. Number accesses elements; this number is called an index. Elements can be initialized to specific values when the array is defined.
Arrays can have multiple dimensions.
A two-dimensional array is an array of array. The address of an array can be used as an argument to a function; the array itself is not copied. Arrays can be used as member data in classes. Care must be taken to prevent data from being placed in memory outside an array.
/* The following program reads 4 persons age and displays it */
/* 47_arrays.c */ |
Like other variables in C, an array must be defined before it can be used to store information. And, like other definitions, an array definition specifies a variable type and a name.
But it includes another feature: a size. The size specifies how many data items the array will contain. It immediately follows the name, and is surrounded by square brackets.
3. Array Elements
The items in an array are called elements. Single Dimensional array accepts values to either row wise or column wise. It can store only one set of values.
The first array element is 0, second is 1 and so on.
An array value can be initialized directly at design time.
Initialization of arrays is as follows..
4. Passing Arrays to Functions
Arrays can be used as arguments to functions.In a function declaration, the data type and sizes of the array represent array arguments.
void display(float [DISPLAY][MONTHS]);
When the function is called, only the name of the array is used as an argument.display(sales);
Program to accept and print array of 10 elements
/* 48_ ele10.c */ |
Returning array of values from functions is also possible but we must be clear with the concept of pointers. Please look in to the pointers topics for more info.
5. Classification of Arrays
Arrays are of two types.
1. Single dimensional Arrays 2. Multi Dimensional Arrays |
1. Single Dimensional Arrays
A single dimensional array is a collection of elements in a row or a column fashion.
A single dimensional array can accept the following operations.
1. Append element 2. Insert element 3. Delete element 4. Replace element 5. Search element 6. Deletion of array 7. Sorting of an array |
The following program is able to perform all the tasks described above.
/* ARRAY FUNCTIONS */ /* Read Array elements */
/* Display array elements */
/* Delete array element */
/* Insert array element */
/* To append element to an existing array */
/* Sorting a list of elements of an array in descending order */
/* Sorting a list of elements of an array in ascending order */
/* To find the smallest and biggest of an existing array */
/* Search for an element in an array */
/* Main program */
/* To demonstrate simple array operations */
#include<stdio.h> |
2. Double Dimensional Arrays
A double dimensional array is a collection of elements in row and column fashion.
A Multi dimensional array can accept the following operations.
A multi dimensional array is commonly used in the areas of matrices to understand whole tasks in an easiest approach.
MATRIX FUNCTIONS
/* 50_menumat.c */
/* Read the values for a MATRIX */
/* Write the values of a MATRIX */
/* To find the TRANSPOSE for a MATRIX of ANY ORDER */
/* To ADD two MATRICES
( possible,only if they are of EQUAL ORDER ) */
/* To MULTIPLY MATRICES of ANY ORDER
( provided they follow the MATRIX MULTIPLICATION RULE ) */
/* To SUBTRACT two MATRICES
( possible,only if they are of EQUAL ORDER ) */
/* A MENU driven program to perform MATRIX operations */
#include <stdio.h> |