Quiz 3 Solution

Problem 1 and 2

 

#include<iostream>

 

#include<fstream>

 

using namespace std;

 

int main()

 

{

 

      bool correct = true;

 

      int num1;

 

      ifstream infile;

 

       infile.open("c:\\New Folder\\numbers.txt");

 

      infile>>num1;

      

 

      while(correct && num1 < 100)

 

      {

 

            cout<<num1<<endl;

 

            cout<<"continuing...";

 

            if (!num1) correct = false;

 

            infile>>num1;

 

      }

 

cout<<num1;

 

return 0;

 

}

 

Solution 1

3

continuing...4

continuing...5

continuing...6

continuing...0

continuing...17Press any key to continue . . .

 

Solution 2

14

continuing...16

continuing...21

continuing...23

continuing...0

continuing...0Press any key to continue

Problem 3

 

Write a function prototype, header and body (definition) for a function named checkrange.  The function accepts three numbers and returns true if the first number is in the range specified by the last two numbers.  For example, if the function receives the numbers 4  6 and 10, it returns false because 4 is not in the range 6 – 10.  However, if the function receives the numbers 7 2 and 11, the function returns true because 7 is between 2 and 11.

Do not assume that the second and third numbers are in any particular order.

 

 

 

 

 

 

 

Solution 3

 

#include<iostream>

using namespace std;

bool checkrange(int,int,int);

 

int main()

 

{

     int firstNumber, secondNumber, thirdNumber;

     bool answer;

     cout<<"enter 3 numbers "<<endl;

     cin>>firstNumber;

     cout<<"first number= "<<firstNumber<<endl;

     cin>>secondNumber;

     cout<<"second number= "<<secondNumber<<endl;

     cin>>thirdNumber;

     cout<<"third number= "<<thirdNumber<<endl;

     answer=checkrange(firstNumber,secondNumber,thirdNumber);

     if (answer==true)

          cout<<"true"<<endl;

     else cout<<"false"<<endl;

return 0;

}   

 

bool checkrange(int x,int y,int z)

{

     if((x<y&&x<z)||(x>y&&x>z))

          return false;

     else

          return true;

}

 

Problem 4

Write the C++ statements that will print the following:

   9---------

   8--------

    7-------

     6------

      5-----

       4----

        3---

         2--

          1-

           BLAST OFF!!!

 Make sure that you use a loop (preferably nested ones) .

Solution 4

 

#include<iostream>

using namespace std;

 

int main()

 

{

     int i,j;

     int k;

     for (i=9; i>=0; i--)

     {

         

          for(k=9;k-i>=0;k--)

         

               cout<<' ';

               if(i==0)

                    cout<<"BLAST OFF!!!"<<endl;

               else

               {

               cout<<i;

               for(j=0; j<i; j++)

               cout<<'-';

               cout<<endl;

               }

              

     }

 

Problem 5

What is the output of the following program?

#include<iostream>

using namespace std;

const double SCALE = 6.0;

void function1(int, double&);

int main()

{

  int miles=90;

  double inches = 15;

 

  cout<<miles<<"   "<<inches;

  cout<<'\t'<<"are the two numbers\n";

  function1(miles,inches);

  cout<<inches<<"  "<<inches<<endl;

  function1(10,inches);

  cout<<miles<<"  "<<inches<<endl;

}

void function1(int x,double& y)

{

      x++;

    cout<<"Scaling by "<<SCALE<<endl;

      y = y * SCALE;

}

Solution 5

90   15 are the two numbers

Scaling by 6

90  90

Scaling by 6

90  540

Press any key to continue . . .