{{{ Q: explain this codes and make a comments for each code: }}}} //template.h #
ID: 3576382 • Letter: #
Question
{{{ Q: explain this codes and make a comments for each code: }}}} //template.h #ifndef TEMPLATE_H #define TEMPLATE_H template<class bubble> void bubble_sort(bubble a[], int n) { int i, j; for (i = 0; i<n - 1; i++) { for (j = i + 1; j<n; j++) { if (a[i]>a[j]) { bubble element; element = a[i]; a[i] = a[j]; a[j] = element; } } } } #endif.....……............
//main.cpp #include<iostream> using namespace std; #include"template.h" int main() { //declare and assign three arrays of different data type to test for tempate function int IntArray[9]; float FloatArray[7]; char CharArray[8]; int i; //call sorting for int array and print array before and after sorting printf("Insert Data items in original order >"); for (i = 0; i < 9; i++) cin >> IntArray[i]; bubble_sort(IntArray, 9); printf(" Data items in ascending order >"); for (i = 0; i < 9; i++) printf("%d ", IntArray[i]); //call sorting for float array and print array before and after sorting printf(" Insert Data items in original order >"); for (i = 0; i < 7; i++) cin >> FloatArray[i]; bubble_sort(FloatArray, 7); printf(" Data items in ascending order >"); for (i = 0; i < 7; i++) printf("%.1f ", FloatArray[i]); //call sorting for char array and print array before and after sorting printf(" Insert Data items in original order >"); for (i = 0; i < 8; i++) cin >> CharArray[i]; bubble_sort(CharArray, 8); printf(" Data items in original order >"); for (i = 0; i < 8; i++) printf("%c ", CharArray[i]); } {{{ Q: explain this codes and make a comments for each code: }}}} //template.h #ifndef TEMPLATE_H #define TEMPLATE_H template<class bubble> void bubble_sort(bubble a[], int n) { int i, j; for (i = 0; i<n - 1; i++) { for (j = i + 1; j<n; j++) { if (a[i]>a[j]) { bubble element; element = a[i]; a[i] = a[j]; a[j] = element; } } } } #endif
.....……............
//main.cpp #include<iostream> using namespace std; #include"template.h" int main() { //declare and assign three arrays of different data type to test for tempate function int IntArray[9]; float FloatArray[7]; char CharArray[8]; int i; //call sorting for int array and print array before and after sorting printf("Insert Data items in original order >"); for (i = 0; i < 9; i++) cin >> IntArray[i]; bubble_sort(IntArray, 9); printf(" Data items in ascending order >"); for (i = 0; i < 9; i++) printf("%d ", IntArray[i]); //call sorting for float array and print array before and after sorting printf(" Insert Data items in original order >"); for (i = 0; i < 7; i++) cin >> FloatArray[i]; bubble_sort(FloatArray, 7); printf(" Data items in ascending order >"); for (i = 0; i < 7; i++) printf("%.1f ", FloatArray[i]); //call sorting for char array and print array before and after sorting printf(" Insert Data items in original order >"); for (i = 0; i < 8; i++) cin >> CharArray[i]; bubble_sort(CharArray, 8); printf(" Data items in original order >"); for (i = 0; i < 8; i++) printf("%c ", CharArray[i]); } {{{ Q: explain this codes and make a comments for each code: }}}} //template.h #ifndef TEMPLATE_H #define TEMPLATE_H template<class bubble> void bubble_sort(bubble a[], int n) { int i, j; for (i = 0; i<n - 1; i++) { for (j = i + 1; j<n; j++) { if (a[i]>a[j]) { bubble element; element = a[i]; a[i] = a[j]; a[j] = element; } } } } #endif
.....……............
//main.cpp #include<iostream> using namespace std; #include"template.h" int main() { //declare and assign three arrays of different data type to test for tempate function int IntArray[9]; float FloatArray[7]; char CharArray[8]; int i; //call sorting for int array and print array before and after sorting printf("Insert Data items in original order >"); for (i = 0; i < 9; i++) cin >> IntArray[i]; bubble_sort(IntArray, 9); printf(" Data items in ascending order >"); for (i = 0; i < 9; i++) printf("%d ", IntArray[i]); //call sorting for float array and print array before and after sorting printf(" Insert Data items in original order >"); for (i = 0; i < 7; i++) cin >> FloatArray[i]; bubble_sort(FloatArray, 7); printf(" Data items in ascending order >"); for (i = 0; i < 7; i++) printf("%.1f ", FloatArray[i]); //call sorting for char array and print array before and after sorting printf(" Insert Data items in original order >"); for (i = 0; i < 8; i++) cin >> CharArray[i]; bubble_sort(CharArray, 8); printf(" Data items in original order >"); for (i = 0; i < 8; i++) printf("%c ", CharArray[i]); }
Explanation / Answer
/* Implicit template instantiation : the complete template definition is in the header file
which is included by main.cpp - so there are no problems during compiling
and linking.
*/
//template.h
#ifndef TEMPLATE_H
#define TEMPLATE_H
// we have written a function called bubble_sort for Bubble sort and we have added a template tag before the function so that, the parameter will be of the data type Name(i.e bubble)
template<class bubble>
void bubble_sort(bubble a[], int n) // this function accepts two parameters where a is the array containing any type of values and n is the size of array
{
int i, j; // declaring two local variables
for (i = 0; i<n - 1; i++) // looping the items in the array from starting
{
for (j = i + 1; j<n; j++) // looping the items in the array from next to the outer for loop
{
// swapping the values
if (a[i]>a[j]) // comparing the two elements to Sort data in ascending order
{
bubble element; // declaring temporary variable of type array type
element = a[i]; // assigning greater value to temporary variable
a[i] = a[j]; // assigning lower value to a[i]
a[j] = element; // assigning temporary variable value to a[j]
}
}
}
}
#endif
.....……............
//main.cpp
#include<iostream>
using namespace std;
#include"template.h" // including the header file
int main()
{
//declare and assign three arrays of different data type to test for tempate function
int IntArray[9];
float FloatArray[7];
char CharArray[8];
int i;
//call sorting for int array and print array before and after sorting
printf("Insert Data items in original order >");
for (i = 0; i < 9; i++)
cin >> IntArray[i];
bubble_sort(IntArray, 9);
printf(" Data items in ascending order >");
for (i = 0; i < 9; i++)
printf("%d ", IntArray[i]);
//call sorting for float array and print array before and after sorting
printf(" Insert Data items in original order >");
for (i = 0; i < 7; i++)
cin >> FloatArray[i];
bubble_sort(FloatArray, 7);
printf(" Data items in ascending order >");
for (i = 0; i < 7; i++)
printf("%.1f ", FloatArray[i]);
//call sorting for char array and print array before and after sorting
printf(" Insert Data items in original order >");
for (i = 0; i < 8; i++)
cin >> CharArray[i];
bubble_sort(CharArray, 8);
printf(" Data items in original order >");
for (i = 0; i < 8; i++)
printf("%c ", CharArray[i]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.