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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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

Expert Solution
Step 1

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.
Step 2

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; 

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Randomized Select Algorithm
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education