Thursday, March 16, 2023

Generic with real life example, write a C++ generic function that will sort an array of integer, float value.

Explain generic with real life example and write a C++ generic function that will sort an array of integer, float value.

Generics are like a toolbox that contains a set of wrenches in different sizes. Just as a toolbox with different sized wrenches can be used to fix various sizes of bolts, a set of generic code can be used to work with different data types.

For example, imagine you have a program that needs to sort a list of items, such as a list of names or a list of numbers. Instead of writing separate sorting algorithms for each data type, you could use a generic sorting algorithm that works with any data type. This would be like using the right-sized wrench from a toolbox to tighten bolts of different sizes.

One way to use generics is through the built-in collections, which are easier to use than arrays and provide type-safety, meaning that they prevent you from adding the wrong type of data to the collection. This can improve the performance of your application by reducing the need for data type conversion.

Another way to use generics is through generic methods, which allow you to write code that works with any data type. You specify the data type that you want to use when you call the method, which minimizes repetitive code and maximizes type safety.

Overall, generics are a powerful tool for minimizing the amount of code you write and improving the performance of your applications by working with generalized code in a data type-specific way.

By using generics, you can write more efficient and flexible code that can be used in many different situations, just as a toolbox with a set of wrenches can be used to fix many different things.


#include <iostream>

// define the template function for sorting
template<typename T>
void mySort(T arr[], int n) {
    for(int i=0; i<n-1; i++) {
        for(int j=i+1; j<n; j++) {
            if(arr[i] > arr[j]) {
                T temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

int main() {
    // create an array of integers
    int intArr[] = {5, 2, 8, 3, 1};
    int intLen = sizeof(intArr)/sizeof(int);

    // sort the integer array using mySort function
    mySort(intArr, intLen);

    // print the sorted integer array
    std::cout << "Sorted integer array: ";
    for(int i=0; i<intLen; i++) {
        std::cout << intArr[i] << " ";
    }
    std::cout << std::endl;

    // create an array of floats
    float floatArr[] = {3.5, 1.2, 9.8, 4.1, 2.0};
    int floatLen = sizeof(floatArr)/sizeof(float);

    // sort the float array using mySort function
    mySort(floatArr, floatLen);

    // print the sorted float array
    std::cout << "Sorted float array: ";
    for(int i=0; i<floatLen; i++) {
        std::cout << floatArr[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

This C++ program demonstrates the use of templates for a generic sorting algorithm that can sort arrays of different types. Here are some notes on the program:

  • The program starts by including the standard input/output library iostream.
  • A function called mySort is defined using a template. The template parameter T allows the function to work with any data type, making it a generic function. The function takes two arguments: an array of type T and an integer n, which represents the length of the array.
  • The mySort function implements a basic sorting algorithm known as "selection sort". The outer loop iterates over the array elements from the first to the second-to-last element. The inner loop iterates over the array elements from the second element to the last element. If the element at the outer loop index is greater than the element at the inner loop index, the two elements are swapped.
  • In the main function, an integer array intArr is created with the values {5, 2, 8, 3, 1} and its length is calculated using the sizeof operator. The mySort function is called with the intArr and its length as arguments, sorting the array in place.
  • The sorted integer array is printed to the console using a for loop that iterates over the elements of the array and prints them out.
  • A float array floatArr is created with the values {3.5, 1.2, 9.8, 4.1, 2.0} and its length is calculated using the sizeof operator. The mySort function is called with the floatArr and its length as arguments, sorting the array in place.
  • The sorted float array is printed to the console using a for loop that iterates over the elements of the array and prints them out.
  • Finally, the program returns 0, indicating successful execution.

No comments:

Post a Comment

Write a c++ program to show the difference between an array and a list.

Array vs List Example Write a c++ program to show the difference between an array and a list. Arrays are useful when you know the number ...