Solution 1 and 2 Lectures

 

Lecture 1 F

 

#include <iostream>

using namespace std;

int main()

{

     double a,b,c,avg;

     int d,e,f,sum;

     cout<<"Enter 3 decimal numbers"<<endl;

     cin>>a>>b>>c;

     cout<<"The 3 decimal numbers = "<<a<<" "<<b<<" "<<c<<endl;   

     d=static_cast<int>(a);

     e=static_cast<int>(b);

     f=static_cast<int>(c);

     cout<<"The 3 whole numbers = "<<d<<" "<<e<<" "<<f<<endl;

     sum=d+e+f;                                    

     avg=(d+e+f)/3;

     cout<<"The sum = "<<sum<<endl;

     cout<<"The average = "<<avg<<endl;

     return 0;

}

 

/*This is lecture 2 Exercise 1 program*/

#include <iostream>

using namespace std;

int main()

{

            int a = 45;

 

            int b = 17;

 

            double z = 18.15;

 

            char ch = 'j';

 

            char ch1 = 'n';

            cout<< "Input"<<endl;

            cin >> ch; //input = Nt

            cout<<"a= "<<a<<" "<<"b= "<<b<<" "<<"ch= "<<ch

                        <<" "<<"ch1= "<<ch1<<" "<<"z= "<<z<<" "<<endl;

 

            cin >> ch1;

            cout<<"a= "<<a<<" "<<"b= "<<b<<" "<<"ch= "<<ch

                        <<" "<<"ch1= "<<ch1<<" "<<"z= "<<z<<" "<<endl;

 

            cout<< "Input"<<endl;

            cin >> a;  //input = 27                            

            cout<<"a= "<<a<<" "<<"b= "<<b<<" "<<"ch= "<<ch

                        <<" "<<"ch1= "<<ch1<<" "<<"z= "<<z<<" "<<endl;

 

            cout<< "Input"<<endl;

            cin >> z; //input = 9

            cout<<"a= "<<a<<" "<<"b= "<<b<<" "<<"ch= "<<ch

                        <<" "<<"ch1= "<<ch1<<" "<<"z= "<<z<<" "<<endl;

 

            cout<< "Input"<<endl;

            cin >> a >> z; //input = 4 18.4

 

            cout<<"a= "<<a<<" "<<"b= "<<b<<" "<<"ch= "<<ch

                        <<" "<<"ch1= "<<ch1<<" "<<"z= "<<z<<" "<<endl;

 

            cout<< "Input"<<endl;

            cin >> a >> z; //input = 13

                                   //input = 9

            cout<<"a= "<<a<<" "<<"b= "<<b<<" "<<"ch= "<<ch

                        <<" "<<"ch1= "<<ch1<<" "<<"z= "<<z<<" "<<endl;

 

            cout<< "Input"<<endl;

            cin>>z>>ch1>>a; //input = 15.5A4

            cout<<"a= "<<a<<" "<<"b= "<<b<<" "<<"ch= "<<ch

                        <<" "<<"ch1= "<<ch1<<" "<<"z= "<<z<<" "<<endl;

 

            cout<< "Input"<<endl;

            cin>>z>>ch1>>a;  //input = 19.3B

                                          //input = 26

            cout<<"a= "<<a<<" "<<"b= "<<b<<" "<<"ch= "<<ch

                        <<" "<<"ch1= "<<ch1<<" "<<"z= "<<z<<" "<<endl;

                                        

            cout<< "Input"<<endl;

            cin>>a>>z; //input = 15.65

            cout<<"a= "<<a<<" "<<"b= "<<b<<" "<<"ch= "<<ch

                        <<" "<<"ch1= "<<ch1<<" "<<"z= "<<z<<" "<<endl;

 

            cout<< "Input"<<endl;

            cin>>ch>>ch1; //input = CD

            cout<<"a= "<<a<<" "<<"b= "<<b<<" "<<"ch= "<<ch

                        <<" "<<"ch1= "<<ch1<<" "<<"z= "<<z<<" "<<endl;

 

            cout<< "Input"<<endl;

            cin>>a>>b; //input = 48.62

            cout<<"a= "<<a<<" "<<"b= "<<b<<" "<<"ch= "<<ch

                        <<" "<<"ch1= "<<ch1<<" "<<"z= "<<z<<" "<<endl;

 

            cout<< "Input failure"<<endl;

            cin>>a>>b; //input = 15 11

            cout<<"a= "<<a<<" "<<"b= "<<b<<" "<<"ch= "<<ch

                        <<" "<<"ch1= "<<ch1<<" "<<"z= "<<z<<" "<<endl;

 

            cout<<"The end"<<endl;

            return 0;

}

 

Input

Nt

a= 45 b= 17 ch= N ch1= n z= 18.15

a= 45 b= 17 ch= N ch1= t z= 18.15

Input

27

a= 27 b= 17 ch= N ch1= t z= 18.15

Input

9

a= 27 b= 17 ch= N ch1= t z= 9

Input

4 18.4

a= 4 b= 17 ch= N ch1= t z= 18.4

Input

13

9.6

a= 13 b= 17 ch= N ch1= t z= 9.6

Input

15.5A4

a= 4 b= 17 ch= N ch1= A z= 15.5

Input

19.3B

26

a= 26 b= 17 ch= N ch1= B z= 19.3

Input

15.65

a= 15 b= 17 ch= N ch1= B z= 0.65

Input

CD

a= 15 b= 17 ch= C ch1= D z= 0.65

Input

48,62

a= 48 b= 17 ch= C ch1= D z= 0.65

Input failure

a= 48 b= 17 ch= C ch1= D z= 0.65

The end

Press any key to continue . . .

 

 

 

 

 

 

/*This is lecture 2 Exercise 2 program*/

#include <iostream>

using namespace std;

int main()

{

      char ch;

 

    int a;

 

    cout<<"Enter a string: ";

 

    cin.get(ch);

 

    cout<<endl;

 

    cout<< "after first cin.get(ch) ch= "<<ch

 

        <<endl;

 

 

 

    cin.get(ch);

 

    cout<< "after second cin.get(ch) ch= "<<ch

 

        <<endl;

 

    a = ch;

 

    cout<< "after second cin.get(ch) a= "<<a<<endl;

 

 

 

    cin.get(ch);

 

    cout<< "after third cin.get(ch) ch= "<<ch<<'\n';

 

 

 

    cin.putback(ch);

      cin.get(ch);

 

cout<< "after putback(ch) and fourth cin.get(ch) ch= "<<ch                                                 <<endl;

 

 

    ch = cin.peek();

           

      cout<< "after peek() ch= "<<ch

 

            <<endl;

 

      cin.get(ch);

 

    cout<< "after fifth cin.get(ch) ch= "<<ch<<endl;

 

      cin.get(ch);

 

    cout<< "after sixth cin.get(ch) ch= "<<ch<<endl;

                                              

                                                           

      return 0;

}

 

Enter a string: a bcde

 

after first cin.get(ch) ch= a

after second cin.get(ch) ch=

after second cin.get(ch) a= 32

after third cin.get(ch) ch= b

after putback(ch) and fourth cin.get(ch) ch= b

after peek() ch= c

after fifth cin.get(ch) ch= c

after sixth cin.get(ch) ch= d

Press any key to continue . . .

 

 

 

 

 

 

/* Lecture 2 Exercise 3

   Data input for this program is Harry Potter

   What are the outputs */

#include<iostream>

#include<string>

using namespace std;

int main()

{

      int a;

      string name, throw_Away;

    char ch;

    cout << "What is your full name ? ";

    cin >> name>>throw_Away;

    cin.get(ch);

    cout<<"the cin name is "<<name<< endl;

   

      cout<<"the cin throw_Away is "<<throw_Away<< endl;

   

      a=ch;

    cout<<"the cin ch is "<<a<<endl;

   

      string full_Name;

      cout << "What is your full name ? ";

 

      getline(cin, full_Name);

    cout <<"the getline true name is "<<full_Name;

 

    cout<<endl;

    return 0;

 

}

 

What is your full name ? Harry Potter

the cin name is Harry

the cin throw_Away is Potter

the cin ch is 10

What is your full name ? Harry Potter

the getline true name is Harry Potter

Press any key to continue . . .

 

Lecture 2 Exercise 4

 

// Revised Program to calculate the average test score.

 

#include<iostream>

#include<fstream>

#include<iomanip>

#include<string>

 

using namespace std;

 

int main()

{

      ifstream indata;

      ofstream outdata;

 

      string name; // line 10

      //string lastName; //line 11

     

      double test1,test2,test3,test4,test5;

      double average;

 

      indata.open("c:\\New Folder\\test.txt");

      outdata.open("c:\\New Folder\\testavg.txt");

 

outdata<<fixed<<showpoint;

outdata<<setprecision(2);

 

cout<<"processing data" <<endl;

 

getline(indata, name); //line 19

outdata<<"Student name: "<<name<<endl; //line 20

 

indata>>test1>>test2>>test3>>test4>>test5;

outdata<<"Test scores; " <<setw(6)<<test1 <<setw(6)<<test2 <<setw(6)<<test3 <<setw(6)<<test4 <<setw(6)<<test5

<<endl;

 

average = (test1 + test2 + test3+ test4 + test5) / 5;

 

outdata<<"average test score: " << setw(6)

<<average  << endl;

 

indata.close();

outdata.close();

 

return 0;