Você está na página 1de 3

Brief review of input/ output features that has been used so far The input/output functionality provided by the

standard library in c++ involves stream objects. We can visualize a stream as a sequence of information flowing between an external device and the main memory of the computer. External device -> monitor, keyboard, printer, disk, tape etc. There are two modes of data transfer text mode and binary mode In addition there are two ways in which data can be read from or write to a stream. formatted input/output operation unformatted input/output operation We have already used a standard stream object cin for reading data and cout from writing data. The standard streams are defined as stream class objects within the std namespace. cin is associated with the keyboard cout is associated with the monitor (There are other stream objects cerr, clog etc. (we are not going to discuss them here) The definition of the standard stream objects appear in the header file iostream. We have seen the use of insertion operator >> and extraction operator <<. cin>>i>>x; cout<<i =<<i;

Stream manipulators We have already used stream manipulators (such as endl, setw() to control the formatting of a stream. cout<<i =<<i<<endl; int i=1000; cout<<hex<<uppercase<<i; We will get 3E8 (not 1000) on the screen as output. cout<<endl<<setw(10)<<i; There are many other stream manipulators such as dec, oct, scientific, left, right, setprecision( ) etc.

Input from and output to File(s) Bubble sort There are three types of stream class objects that you can use for working with files. ifstream can be used to read from a file ofstream can be used to write to a file fstream can be used to read and write #include<fstream> // fstream header is required for ifstream object ...................................................... ................................................... const char* filename=c:\yasin\marks.dat; ifstream inpfile(filename); The above statements defines the object inpfile and associates it with the file marks.dat If the file does not exist then the stream object will not work. By any of the following statements we can check whether the file is open or not. A[0] 2 A[1] 5 A[2] 1 #include<fstream> // fstream header is required for ofstream object ................................ ........................... ofstream outfile(data); - the file named data, if it exists, will be opened for writing in the default mode i.e. text mode. Any data that was in the file will be discarded and any data that you write will be its new content - if the file named data does not exist it will be created - even if you dont write to the file after opening it, the original contents will be discarded and the file will be empty A[3] 8 A[4] 4 A[0] 1 A[1] 2 A[2] 4 A[3] 5 A[4] 8 2 5 1 8 4 2 51 15 8 4 2 1 5 8 4 2 1 5 84 48 A[0] 2 A[1] 1 A[2] 5 A[3] 4 A[4] 8 A[0] 1 A[1] 2 A[2] 4 A[3] 5 A[4] 8 21 12 5 4 8 1 2 4 5 8 1 2 5 4 8 1 2 54 45 8 Another logic (bubble sort) The largest number will be placed in the last position if we follow the steps below if A[0]>A[1] interchange A[1]>A[2] interchange A[2]>A[3] interchange A[n-2]>A[n-1] interchange i.e. to fix the n-1 th position A[j]>A[j+1] then interchange, where j=0 to j<n-1 In general to fix the i-th position ( i=n-1 to i>=0 ) if A[j] > A[j+1] then interchange where j=0 to j<i

1 2 4 5 8

1 2 4 5 8

// Program - P36 /* Program to sort an array in ascending order */ #include<iostream> #include<iomanip> using namespace std; void main( ){ double A[10],x; int n,i,j; cout<<" How many numbers (max 10)? "; cin>>n; cout<<" Input the numbers\n "; for(i=0; i<n; i++){cin>>A[i];} for(i=n-1; i>=0; i--){ for(j=0; j<i; j++){ if(A[j]>A[j+1]){ x=A[j]; A[j]=A[j+1]; A[j+1]=x; } } cout<<" i = "<<i<<" A[] --> "; for(j=0; j<n; j++)cout<<setw(5)<<A[j]; cout<<endl; } }

Você também pode gostar