ARRAYS IN PROGRAMMING

An array in programming is a collection of elements, all of the same type, stored in contiguous memory locations. Arrays allow you to store multiple values in a single variable, making it easier to manage and manipulate data.

Declaring an Array in C

To declare an array in C, you need to specify the type of its elements and the number of elements it can hold. Here are some examples:

// Declare an array of integers

int numbers[5];

// Declare an array of floats

float temperatures[10];

// Declare an array of characters (a string)

char name[20];

Simple Programs to Manipulate Data in an Array

Example 1: Initializing and Printing an Array

int main() {

int numbers[5] = {1, 2, 3, 4, 5}; // Initialize the array

// Print the array elements

for (int i = 0; i < 5; i++) {

     printf(“Element %d: %d\n”, i, numbers[i]);

}

return 0;

}

Example 2: Calculating the Sum of Array Elements

int main() {

int numbers[5] = {1, 2, 3, 4, 5};

int sum = 0;

// Calculate the sum of the array elements

for (int i = 0; i < 5; i++) {

      sum += numbers[i];

}

printf(“Sum of array elements: %d\n”, sum);

return 0;

}

Example 3: Finding the Maximum Element in an Array

int main() {

int numbers[5] = {1, 2, 3, 4, 5};

int max = numbers[0]; // Assume the first element is the maximum

// Find the maximum element

for (int i = 1; i < 5; i++) {

     if (numbers[i] > max) {

           max = numbers[i];

     }

}

printf(“Maximum element: %d\n”, max);

return 0;

}

Example 4: Reversing an Array – This program reverses the elements of an array.

int main() {

int numbers[5] = {1, 2, 3, 4, 5};

int temp;

int n = 5;

// Reverse the array

for (int i = 0; i < n / 2; i++) {

       temp = numbers[i];

       numbers[i] = numbers[n – 1 – i];

       numbers[n – 1 – i] = temp;

}

// Print the reversed array

for (int i = 0; i < n; i++) {

printf(“%d “, numbers[i]);

}

printf(“\n”);

return 0;

}

Example 5: Sorting an Array (Bubble Sort)

Watch our Youtube video on sorting

 

int main() {

int numbers[5] = {5, 2, 9, 1, 5};

int n = 5;

int temp;

// Bubble sort algorithm

for (int i = 0; i < n – 1; i++) {

     for (int j = 0; j < n – i – 1; j++) {

         if (numbers[j] > numbers[j + 1]) {

                temp = numbers[j];

                numbers[j] = numbers[j + 1];

                numbers[j + 1] = temp;

           }

     }

}

// Print the sorted array

for (int i = 0; i < n; i++) {

     printf(“%d “, numbers[i]);

}

printf(“\n”);

return 0;

}

Example 6: Finding the Minimum Element in an Array

int main() {

int numbers[5] = {3, 7, 2, 8, 1};

int min = numbers[0]; // Assume the first element is the minimum

// Find the minimum element

for (int i = 1; i < 5; i++) {

      if (numbers[i] < min) {

            min = numbers[i];

       }

}

printf(“Minimum element: %d\n”, min);

return 0;

}

Example 7: Counting the Frequency of Elements in an Array

int main() {

int numbers[10] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};

int frequency[10] = {0}; // Initialize frequency array to zero

int n = 10;

// Count the frequency of each element

for (int i = 0; i < n; i++) {

        frequency[numbers[i]]++;

}

// Print the frequency of each element

for (int i = 0; i < n; i++) {

     if (frequency[i] != 0) {

             printf(“Element %d: %d times\n”, i, frequency[i]);

     }

}

return 0;

}

 

NOW SEE ANOTHER VIDEO ON INSERTION SORTING

 

 

Active Learning, July 2024