Write a for loop that can be used to enter values of time in minutes in the foll
ID: 3581974 • Letter: W
Question
Write a for loop that can be used to enter values of time in minutes in the following array and display the values after converting to hours: int time [100] Write a code fragment declaring the following object as of stream object and opening it as a text output file: coupons. txt. Also check if the file opened correctly or not. C++ Standard Template Library (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?Explanation / Answer
(a)
.........
//Array to hold time in minutes
int time[100];
//Array indexing variable
int i;
//Iterate over array
for(i=0; i<100; i++)
{
//Reading time in minutes
cout << endl << " Enter time in minutes: ";
//Storing in array
cin >> time[i];
}
cout << endl << endl;
//Iterating over array
for(i=0; i<100; i++)
{
//Converting time into hours and printing to console
cout << " " << time[i] << " minutes = " << time[i] * 0.0166667 << " hours " << endl << endl;
}
..........
_________________________________________________________________________________________________________________________
(b)
//Creating an object of ofstream class
ofstream fout;
//Opening file coupons.txt as a text file
fout.open("coupons.txt", ios::out);
//Checking if file opened correctly or not...
if(fout.is_open())
{
//File opened correctly
cout << " File Opening successful ...";
}
else
{
//Problem in opening file
cout << " Error!! Opening file ...";
}
//Closing file
fout.close();
_________________________________________________________________________________________________________________________
(c)
Four header files needed to construct strings and to create and manipulate vectors:
(1) <iostream>:
-> This header file includes input / output library.
-> All the methods related to input and output operations are included in this header file.
(2) <cstdlib.h>:
-> This was originally in C library <stdlib.h>.
-> It provides different type of utilities like Memory management(malloc, calloc, free, etc..) , String to numeric conversion (atof, atoi, etc...)
(3) <string>:
-> This header file includes string library.
-> It provides methods (getline, operator<< , operator>> etc) for working with strings
(4) <vector>
-> This header file is a part of containers library.
-> It provides methods (push_back(), at(), etc) for working with vectors
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.