Write a program which sorts the CGPA array having cgpa of all students in ascending order by using bubble sort and make use of pointers for each and every variable and constant and array
Write a
the CGPA array having cgpa of
all students in ascending order
by using bubble sort and make
use of pointers for each and
every variable and constant and
array
PROGRAM STRUCTURE:
- Include the required header files.
- Start the definition of the function to sort the cgpa of the students.
- Use the bubble sort approach for the purpose of sorting the array of cgpa.
- Display the content of the sorted array.
- Start the definition of the main function.
- Declare the required variables.
- Take number of students from the user.
- Take the cgpa for the students.
- Calls the function to sort the array of the cgpa of the students.
PROGRAM CODE:
#include <iostream> // include the required header files
using namespace std;
void sort(int n, float* ptr) // start the definition of the main function
{
float t; // declare a temporary variable
for (int i = 0; i < n; i++) // outer loop for accessing the iteration for the bubble sort
{
for (int j = i + 1; j < n; j++) // inner loop for performing the task of bubble sort
{
if (*(ptr + j) < *(ptr + i)) // swapping the adjacent element
{
t = *(ptr + i);
*(ptr + i) = *(ptr + j);
*(ptr + j) = t;
}
}
}
for (int i = 0; i < n; i++) // display the array elements using pointer notation
{
cout<< *(ptr + i);
cout<<" ";
}
}
int main() // start the main function definition
{
int n; // declare the required variable
float arr[100];
cout<<"Enter the number of students: ";
cin>>n; // take the number of students from the user
cout<<"Enter the cgpa: ";
for(int i=0; i<n; i++)
{
cin>>arr[i]; // take the cgpa for the students
}
sort(n, arr); // calls the function to sort the cgpa
return 0;
}
Step by step
Solved in 3 steps with 1 images