/*
Name - Aniket Sable
Roll No. - 59
Class - SE.IT
Batch - S3

PROBLEM STATEMENT :- Consider a student database of SEIT class (at least 15 records). Database contains different Fields of every student like Roll No, Name and SGPA.(array of structure)
a. Design a roll call list, arrange list of students according to roll numbers in ascending order (Use Bubble Sort)
b. Arrange list of students alphabetically. (Use Insertion sort)
c. Arrange list of students to find out first ten toppers from a class. (Use Quick sort)
d. Search students according to SGPA. If more than one student having same SGPA, then print list of all students having same SGPA.
e. Search a particular student according to name using binary search without recursion. (all the student records having the presence of search key should be displayed)
*/

#include <cstring>
#include <iostream>
using namespace std;

struct student
{
    char student_name[20];
    float SGPA;
    int Roll_no;
};
int n;
class student_data
{
private:
    struct student s1[15];

public:
    void viewdata()
    {
        cout << "\n\n   Student Name        SGPA        Roll No.\n-----------------------------------------------------\n";
        for (int i = 0; i < n; i++)
        {
            cout << "    " << i + 1 << ". " << s1[i].student_name;
            cout << "\t\t";
            cout << s1[i].SGPA;
            cout << "\t\t";
            cout << s1[i].Roll_no << endl;
        }
    }

    void add()
    {
        cout << "\n Enter no. of Student details to be added (max 15) : ";
        cin >> n;
        cout << "\n Filling data such as Name,    SGPA,    Roll no.\n ------------------------------------------------------";
        for (int i = 0; i < n; i++)
        {
            cout << "\n\n# Details of Student no. " << i + 1 << " - \n\n  Enter Name of Student : ";
            cin >> s1[i].student_name;
            cout << "\n  Enter Student SGPA : ";
            cin >> s1[i].SGPA;
            cout << "\n  Enter Student Roll no. : ";
            cin >> s1[i].Roll_no;
        }
        cout << "---------------- Addition Done Successfully ! ----------------";
        student_data ::viewdata();
    }
    void search_sgpa(float r) // linear search on SGPA
    {
        cout << "\n\n< Searching in SGPA using Linear Search...>\n-----------------------------------------------";
        int t = 0;
        for (int i = 0; i < n; i++)
        {
            if (s1[i].SGPA == r)
            {
                cout << "\n\n#" << t << " Details of Student with SGPA " << s1[i].SGPA;
                cout << "\n\n Student     Name           SGPA           Roll_No.\n";
                cout << "    " << i + 1 << ".       " << s1[i].student_name;
                cout << "    \t   " << s1[i].SGPA << "   \t    " << s1[i].Roll_no;
                cout << "\n--------------------------------------------------------------";
                t++;
                continue;
            }
        }
        if (t == 0)
        {
            cout << "\n Student '" << r << "' not found or Input correct SGPA";
        }
    }

    void sort_names() // insertion sort for sorting names
    {
        for (int k = n - 1; k > 0; k--)
        {
            struct student temp = s1[k];
            int j = k - 1;
            while (j >= 0 && strcmp(temp.student_name, s1[j].student_name) < 0)
            {
                s1[j + 1] = s1[j];
                j = j - 1;
            }
            s1[j + 1] = temp;
        }
    }

    void sort_SGPA(int l, int k) // sorting SGPA using quick sorting
    {
        int r = k - 1;
        if (l >= r)
            return;
        int i = l;
        int j = r + 1;
        struct student prec;
        int p = s1[l].SGPA; // Select pivot element
        prec = s1[l];       // temporarily storing pivot record prec
        while (1)
        {
            do
            {
                i++;
            } while (s1[i].SGPA < p && i <= r);
            do
            {
                j--;
            } while (s1[j].SGPA > p && j >= l);
            if (i >= j)
                break;
            struct student temp;
            temp = s1[j];
            s1[j] = s1[i];
            s1[i] = temp;
        }
        s1[l] = s1[j];
        s1[j] = prec;
        sort_SGPA(l, j - 1); // left list
        sort_SGPA(j + 1, r); // right list
    }

    void sort_RollNo() // sorting roll no. in ascending order using bubble sort
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n - 1; j++)
            {
                if ((s1[j].Roll_no) < (s1[j + 1].Roll_no)) // Swapping
                {
                    struct student temp;
                    temp = s1[j];
                    s1[j] = s1[j + 1];
                    s1[j + 1] = temp;
                }
            }
        }
        student_data ::viewdata();
    }

    void search_name()
    {
        cout << "\n Enter student name to be searched : ";
        char search[10];
        cin >> search;
        cout << "\n< Searching name using Binary Search...>";
        int lower = 0, upper, mid;
        upper = n - 1;
        mid = (lower + upper) / 2;
        student_data ::sort_names();
        while (lower <= upper)
        {
            if (strcmp(s1[mid].student_name, search) < 0)
            {
                lower = mid + 1;
            }
            else if (strcmp(s1[mid].student_name, search) == 0)
            {
                cout << "\n\n# Details of Student with name " << s1[mid].student_name << " -";
                cout << "\n\n Student        Name         SGPA        Roll\n    No.                                           No.\n--------------------------------------------------------------\n";
                cout << "    1.        " << s1[mid].student_name;
                cout << "   \t  " << s1[mid].SGPA << "   \t  " << s1[mid].Roll_no;
                cout << "\n--------------------------------------------------------------";
                break;
            }
            else
            {
                upper = mid - 1;
                mid = (lower + upper) / 2;
            }
        }
        if (lower > upper)
        {
            cout << "\n Student '" << search << "' details not found or Input correct name";
        }
    }
};

int main()
{
    student_data std;
    float r;
    cout << "--------------------------------------------------------------------\n\t\t----- SE IT Student Database -----\n--------------------------------------------------------------------\n";
    char stopApp;
    stopApp = 'Y';
    while (stopApp == 'Y')
    {
        cout << "\n\nSelect action from following : \n";
        cout << "\n1.ADD RECORDS\t2.SEARCH SGPA\t3.SORT NAME \t4.SORT SGPA(Toppers)\t5.SORT ROLL NO.\t 6. SEARCH NAME\t7. EXIT\n";
        cout << "Enter choice (1/2/3/4/5/6/7): ";
        int choice;
        cin >> choice;
        switch (choice)
        {
        case 1:
            std.add();
            break;
        case 2:
            cout << "\n Enter Student SGPA to be searched : ";
            cin >> r;
            std.search_sgpa(r);
            break;
        case 3:
            cout << "\n< Sorting name alphabetically using Insertion Sort...>";
            std.sort_names();
            std.viewdata();
            break;
        case 4:
            cout << "\n< Sorting top 10 SGPA using Quick Sort...>";
            std.sort_SGPA(0, 10);
            std.viewdata();
            break;
        case 5:
            cout << "\n< Sorting Roll No. wise using Bubble Sort...>";
            std.sort_RollNo();
            break;
        case 6:
            std.search_name();
            break;
        case 7:
            cout << " >Exited successful<\n --| END OF CODE |--";
            return 0;
        default:
            cout << "\n Invalid choice !";
        }
        cout << "\n\nDo you want to continue (Y/N) ? : ";
        cin >> stopApp;
        if (stopApp == 'N' | 'n')
        {
            cout << " ----------------| END OF CODE |----------------\n";
        }
    }
    return 0;
}

/*
base) rmdstic@rmdstic-OptiPlex-3020:~$ g++ experiment2.cpp
(base) rmdstic@rmdstic-OptiPlex-3020:~$ ./a.out

--------------------------------------------------------------------
                ----- SE IT Student Database -----
--------------------------------------------------------------------


Select action from following :

1.ADD RECORDS   2.SEARCH SGPA   3.SORT NAME     4.SORT SGPA(Toppers)    5.SORT ROLL NO.  6. SEARCH NAME 7. EXIT
Enter choice (1/2/3/4/5/6/7): 1

Enter no. of Student details to be added (max 15) : 2

Filling data such as Name,    SGPA,    Roll no.
------------------------------------------------------

# Details of Student no. 1 -

Enter Name of Student : Aniket

Enter Student SGPA : 9.1

Enter Student Roll no. : 59


# Details of Student no. 2 -

Enter Name of Student : Shantanu

Enter Student SGPA : 9.3

Enter Student Roll no. : 58
---------------- Addition Done Successfully ! ----------------

Student Name        SGPA        Roll No.
-----------------------------------------------------
    1. Aniket           9.1             59
    2. Shantanu         9.3             58


Do you want to continue (Y/N) ? : Y
----------------| END OF CODE |----------------


Select action from following :

1.ADD RECORDS   2.SEARCH SGPA   3.SORT NAME     4.SORT SGPA(Toppers)    5.SORT ROLL NO.  6. SEARCH NAME 7. EXIT
Enter choice (1/2/3/4/5/6/7): 2

Enter Student SGPA to be searched : 9.3


< Searching in SGPA using Linear Search...>
-----------------------------------------------

#0 Details of Student with SGPA 9.3

Student     Name           SGPA           Roll_No.
    2.       Shantanu              9.3              58
--------------------------------------------------------------

Do you want to continue (Y/N) ? : Y
----------------| END OF CODE |----------------


Select action from following :

1.ADD RECORDS   2.SEARCH SGPA   3.SORT NAME     4.SORT SGPA(Toppers)    5.SORT ROLL NO.  6. SEARCH NAME 7. EXIT
Enter choice (1/2/3/4/5/6/7): 7
>Exited successful<
--| END OF CODE |--

*/ 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}