C++ Program to Implement Sorted Array

This C++ Program demonstrates the implementation of Sorted Array.

Here is source code of the C++ Program to demonstrate the implementation of Sorted Array. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Implement Sorted Array
  3.  */
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <string>
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     int array[100001] = {0}, value;
  12.     char ch = 'y';
  13.     while (ch == 'Y' || ch == 'y')
  14.     {
  15.         cout<<"Enter an integer to be inserted: ";
  16.         cin>>value;
  17.         array[value] = value;
  18.         cout<<"Do you want to insert more ( Y or N): ";
  19.         cin>>ch;
  20.     }
  21.     for(int i = 0; i < 100001; i++)
  22.     {
  23.         if (array[i] != 0)
  24.             cout<<array[i]<<"  ";
  25.     }
  26.     cout<<endl;
  27.     return 0;
  28. }

$ g++ sorted_array.cpp
$ a.out
Enter an integer to be inserted: 10
Do you want to insert more ( Y or N): Y
Enter an integer to be inserted: 6
Do you want to insert more ( Y or N): Y
Enter an integer to be inserted: 11
Do you want to insert more ( Y or N): Y
Enter an integer to be inserted: 3
Do you want to insert more ( Y or N): Y
Enter an integer to be inserted: 8
Do you want to insert more ( Y or N): Y
Enter an integer to be inserted: 2
Do you want to insert more ( Y or N): Y
Enter an integer to be inserted: 7
Do you want to insert more ( Y or N): Y
Enter an integer to be inserted: 1
Do you want to insert more ( Y or N): N
1  2  3  6  7  8  10  11
 
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.