Example Programs and Exercises in Cpp

Simple Array Manipulation Examples

Finding the sum of array elements:

int sumArray(int arr[], int size){ 
    int sum = 0;
    for (int i = 0; i < size; i++)
    { 
        sum += arr[i];
    }
    return sum;
}

Calculating the average of array elements:

double averageArray(int arr[], int size) {
    int sum = sumArray(arr, size);
    return static_cast<double>(sum) / size;
}

Array Sorting and Searching Examples

Sorting an array in ascending order using bubble sort:

void bubbleSort(int arr[], int size) {
    for (int i = 0; i < size 1; i++) {
        for (int j = 0; j < size i 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // Swap elements
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j+1] = temp;
            }
        }
    }
}

Searching for a specific element in an array:

int linearSearch (int arr[], int size, int target) {
    for (int i = 0; i < size; i++) { 
        if (arr[i] == target) { 
            return i; // Element found at index i
        }
    }
    return -1; // Element not found
}

Multidimensional Array Examples

Printing elements of a 2D array:

void printMatrix (int matrix[][3], int rows) { 
    for (int i = 0; i < rows; i++) { 
        for (int j = 0; j < 3; j++) { 
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

Finding the maximum value in a 2D array:

int findMaxValue (int matrix[][3], int rows) {
    int maxVal = matrix[0][0];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < 3; j++) {
            if (matrix[i][j] > maxVal)
                maxVal = matrix[i][j];
            }
        }
    }
    return maxVal;
}

Programming Exercises for Practice

  • Write a program that calculates an array's sum and average of elements.
  • Implement a function to check if an array is sorted in ascending order.
  • Write a program to reverse the elements of an array.
  • Implement a binary search algorithm for a specific element in a sorted array.
  • Write a program that transposes a square matrix (rows become columns and vice versa).

These exercises help you practice manipulating, sorting, searching, and working with multidimensional arrays

Conclusion

Arrays are fundamental data structures that play a crucial role in programming. They provide a way to efficiently store and manipulate multiple values of the same type. Understanding arrays is essential for handling large amounts of data, performing calculations, and implementing algorithms. Arrays are used extensively in various domains, such as data processing, scientific computations, and game development.

To master arrays in C++, practising and applying the concepts learned is crucial. Explore more complex examples, solve programming exercises, and work on real-world problems that require array manipulation. Additionally, deepen your understanding of advanced topics, such as dynamic arrays, array algorithms, and library functions available for array operations.

Exploring and practising arrays will enhance your programming skills, improve your problem-solving abilities, and help you gain confidence in working with data structures. Arrays are a powerful tool in C++; mastering them will significantly benefit your programming journey.

Remember, practice makes perfect. Keep coding, experimenting, and challenging yourself to become proficient in working with arrays and unleash their full potential in your C++ programs.


Learn via Video Course

C++ Programming (English) Logo

C++ Programming (English)

2056

18 hrs