Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ENGR3200 Final Exam v2 PROBLEM 1 (a) Write a for loop that can be used to enter

ID: 2266195 • Letter: E

Question

ENGR3200 Final Exam v2 PROBLEM 1 (a) Write a for loop that can be used to enter values of time in minutes in the following display the values after converting to hours: int t ine 100] array and IS points (b) Write a code fragment declaring the following object as ofstream object and opening it as a text output file: coupons.txt. Also check if the file opened correctly or not [S points] (c) C+ Standard Template Lbrary (STL) provides a vector class. Which 4 header files need to be included in your program to construct strings and to create and manipulate vector objects? [5 points] 12/15/2016 GHSCSE - FDU

Explanation / Answer

a) solution:

The following code first reads 100 min values and after reading it displays. If you want simultaneous display write the code lines in second for loop in the first for loop it works.......

int main()
{

int time[100];
int min,hrs,temp,i;

// To read 100 values in an array   
for(i=0;i<100;i++)
{
cin >> time[i]);
}
  
// To display in hrs:min format   
for(i=0;i<100;i++)
{
temp = time[i];
hrs = temp / 60;
min = temp % 60;
cout << hrs << " : " << min ;

}

return 0;
}

------------------------------------------------------------------------------------------------------------------------------------------------------

b )Solution:

#include <iostream>

#include <fstream> // you have to include this for file operations //

using namespace std;

int main()
{

//for creating the file
ofstream file("coupons.txt");

//To check it is opened correctly or not
if( !file.is_open() )
{
cout << "Unable to open the file" << endl;
}
else
{
// Writing into the file

file << "first sentence into the text file" << endl;
file << "second sentence into the file" << endl;
// Closing the file   
file.close();
cout << "Successfully opened and written into the file" << endl;
}
  

return 0;
}

-------------------------------------------------------------------------------------------------------------------------

Note : // indicates comments //

--------------------------------------------------------------------------------------------------------------------------

c) solution :

#include <iostream> // standard stream objects cout,cin...many

#include <cstdlib> //General purpose utilities like sorting and searching etc...

#include<vector> // std: vector container

#include<string> //Basic string operations

------------------------------------------------------------------------------------------------------------------------

d) solution :

A char array is just that - an array of characters:

eg : "prices.txt" each charcter is saved in a continous location.

"prices.txt" as a string you need not worry about size.....it allocates c++ manages it.

C++ strings can contain embedded characters, know their length without counting, are faster than heap-allocated char arrays for short texts and protect you from buffer overruns.

---------------------------------------------------------------------------------------------------------------------------------------------

e) Solution :

•Solutions to the “phantom” Enter key problem

–Do not mix cin with getline() inputs in the same program

–Follow the cin input with the call to cin.ignore()

–Accept the Enter key into a character variable and then ignore it

----------------------------------------------------------------------------------------------------------------------------