Skip to main content

Posts

Showing posts from November, 2021

Function Template for Selection Sort using C++

  Problem Statement: Write a function template for selection sort that inputs, sorts and outputs an integer array and a float array. Note :- Scroll horizontally to see the full line of code. #include <iostream> using namespace std ; template < class T > void selectionSort ( T a [], int n ){     for ( int i = 0 ; i < n ; i ++){         T min = 10000 ;         int min_index = i ;         for ( int j = i ; j < n ; j ++){             if ( a [ j ]< min ){                 min = a [ j ];                 min_index = j ;             }         }         T temp = a [ i ];         a [ i ]= a [ min_index ];         a [ min_index ]= temp ;     } } int main (){   ...

File Handling using C++ Part-II | File Reading and Writing

  Problem Statement:  Write a C++ program to create a file “mydata.txt”, accept the name and age of 10 persons from the console and add them to the file (each record must be on a new line). Read the contents of the file back in two variables (name and age) and display them on the console. Use following ways to read & write data and identify the difference:      1. Extraction & insertion operators     2.  get() & put() functions of stream classes Note :- Scroll horizontally to see the full line of code. #include <iostream> #include <fstream> #include <string.h> using namespace std ; int main () {     string name ;     int age ;     ofstream f1 ;     f1 . open ( "mydata.txt" );     cout << "Enter the details below" << endl ;     for ( int i = 0 ; i < 10 ; i ++){         cout << i + 1 << ".Name: " ;  ...

File Handling using C++ | File Reading and Writing

  Problem Statement: Write a C++ program that creates an output file, writes information to it, closes the file, open it again as an input file and read the information from the file. Note:- Scroll horizontally to see the full line of code. #include <iostream> #include <fstream> #include <bits/stdc++.h> #include <string.h> using namespace std ; int main (){     int roll_no ;     string name , department , college_name ;     ofstream f1 ;     f1 . open ( "File.txt" );     // cout<<"File Created: File.txt"<<endl;     if ( f1 ){         cout << endl ;         cout << "Enter the details:" << endl ;         cout << "Name: " ;         getline ( cin , name );         cout << "College Name: " ;         getline ( cin , college_name );   ...