Page 62 - DCAP407_DATA_STRUCTURE
P. 62
Unit 3: Arrays
In this example:
1. First, the header files are included using #include directive.
2. Using the #define directive, the array size, SIZE, is set to 20.
3. In the main() function, the function sum and the variables are declared.
4. A for loop is used accept the elements of the array.
5. The next for loop prints the elements of the array.
6. The program calls the sum() function to add all the elements of the array.
The value returned by the sum() function is stored in the variable
Sum_total.
7. The program then prints the sum of the elements of the array.
8. getch() prompts the user to press a key to exit the program.
9. The function sum that accepts two arguments and returns a float value is
defined. The function sum does the following steps:
(a) Initializes an integer variable i.
(b) Initializes a float variable total and assigns 0.0 to it.
(c) Adds the elements of the array using a for loop and the result is
stored in total.
(d) Finally, it returns the value of total.
1. Write a C program to accept a two-dimensional array, print the values
and sum up the elements of the array.
2. Write a C program to perform matrix addition and multiplication.
3.4 Summary
• An array is a set of same data elements grouped together. Arrays can be one-dimensional or
multidimensional.
• The fundamentals of arrays include the concepts of declaring and initializing an array.
• A linear or one-dimensional array is a structured collection of elements (often called as array
elements) that are accessed individually by specifying the position of each element with a single
index value.
• Multidimensional arrays are nothing but "arrays of arrays". Two subscripts are used to refer to the
elements.
• The operations that are performed on an array are adding, sorting, searching, and traversing.
• Adding of elements is done at the end of an array or in the middle of an array.
• Sorting operation arranges the elements of a list in a certain order.
• Searching operation is used for finding an item with specified properties among a collection of
items.
• Traversing an array refers to moving in inward and outward direction to access each element in an
array.
LOVELY PROFESSIONAL UNIVERSITY 55