Arrays

What are Arrays?

An array is a fixed size sequenced collection of elements of the same data type.

Examples:

  • List of Temperatures
  • List of employees salary
  • List of cost of products
  • Test scores of students
  • etc.

Types of Arrays

  • One Dimensional Array
  • Two Dimensional Array
  • Multi Dimensional Array

One Dimensional Arrays

Declaration

type array-name[size];



                        int status[10]; 
char message[15];
float marks[15];
                    

Note:

  • Arrays must be declared before they can be used so that compiler can allocate space for them in memory.
  • The size of the array tell how many element can be stored in that array.
  • Saving data morethan the size of the array will result in unexpected results.
  • Strings are stored as array of charachters.

One Dimensional Arrays

Initialization

type array-name[size] = { list of values };


Initialization can be done at compile time or at run time

If the array is not initialized then it will have garbage values.

                        int status[3] = { 1, 2, 3 }; 
char message[5] = {'a','e','i','o','u'};
float marks[4] = { 4.5, 5.6, 7.1, 3.3 };
                    

Note:

Size can be ommited. It will be computed from the number of elements in the initialization.

                        int status[] = { 1, 2, 3 };                 // size is 3
char message[] = {'a','e','i','o','u'};     // size is 5
float marks[] = { 4.5, 5.6, 7.1, 3.3 };     // size is 4
                    

Note:

Partial Initialization

Compile time initialization can be partial. That is the number of initializers can be less than the declared size. In such cases the remaining elements are initialized with zero for numeric type and to NULL ($ \0 $) for char type

                        int status[3] = { 1 };                 // 1 , 0 , 0
char message[5] = {'a','e' };           // 'a','e','\0','\0','\0'
float marks[4] = { 4.5, 5.6, 7.1 };     // 4.5 , 5.6 , 7.1 , 0
                    

Problem Statement #1

Write a program to record the hourly temperatures using arrays and find the mean temperature.

Problem Statement #2

Write a program to compute the sum of elements of two one dimensional arrays and store it in a new one dimensional array.

Home Work

An election is contested by five candidates. The candidates are numbered 1 to 5 and the voting is done by marking the candidate number in ballot paper. WAP to read the ballot paper and count the votes. The program should declare the winner of maximum votes. If the number is outside the range then it should take it as spoilt ballot and also count the number of such spoilt ballot.

Two Dimensional Arrays

Declaration

type array-name[row-size][column-size];



                        int students[5][5]; 
char message[15][10];
float marks[15][2];
                    

Visualization

How to Identify if its One or Two Dimensional Array?

Array to store the marks of five students - One Dimensional

                        int marks[5];
                    

Array to store the marks of five students in five subjects- Two Dimensional

                        int marks[5][5];
// 5 students x 5 subjects     or     5 Subjects x 5 Students
                        
                    

To store votes of ten candidates - One Dimensional

                        int votes[10];
                    

To store votes of 10 parties in 5 states - Two Dimensional

                        int votes[10][5];     // 10 Parties x 5 States
int votes[5][10];     // 5 States x 10 Parties
                        
                    

Two Dimensional Arrays

Initialization

type array-name[row][column] = { list of values };


                        int status[3][2] = { 1, 1, 2, 2, 3, 3 }; 
int status[3][2] = { {1, 1}, {2, 2}, {3, 3} }; 
int status[3][2] = { 
                        {1, 1}, 
                        {2, 2}, 
                        {3, 3} 
                    }; 
                    

The array initialization is done row by row.
Partial initialization is possible.
The value inside the sqare brackets are called Array Subscripts. They should be integer.

Memory Layout of Arrays

Problem Statement #3

WAP to find the transpose of a 3x3 matrix.

Problem Statement #4

WAP to do matrix addition.

Home Work

WAP to do matrix multiplication.

Problem Statement #5

Write a program in C to count a total number of duplicate elements in an array.

Problem Statement #6

Write a program in C to print all unique elements in an array.

Problem Statement #7

Write a program in C to find the maximum and minimum element in an array.

Problem Statement #8

Write a program in C to sort elements of array in ascending order.