Lab #2 - C++ Basics (Pointers, FileIO)
Question 1 - Outlet Sales Report
Modify Lab 1 Question 2 to use the following functions:-
/*********************************************************************/
/* getSales obtains user input for the outlets */
/* aptr - pointer to an array of double */
/* size - size of array */
/*********************************************************************/
void getSales(double * aptr, int size);
**********************************************************************/
/* calcTotalSales calculates the total sales for all outlets */
/* aptr - pointer to an array of double */
/* size - size of array */
/* returns total as double */
/*********************************************************************/
double calcTotalSales(const double * aptr, int size);
Save your sales report by using the following code snippet:-
#include <fstream>
...
ofstream outfile;
outfile.open("report.txt");
outfile << ... << endl;
....
outfile.close();
cout << "File written successfully" << endl;
Solution:
#include <iostream>
#include <iomanip>
#include <fstream>
void printbar(std::string store, double sales, double sales_max, std::ofstream &outfile)
{
outfile << std::setw(12) << std::left << store << ": \t";
for (int j = 0; j < int(sales * 50.0 / sales_max); j++)
{
outfile << "*";
}
outfile << std::endl;
}
void getSales(double *aptr, int size)
{
for (int i = 0; i < size; i++)
{
// std::cout << "Enter sales for day " << i + 1 << ": ";
std::cin >> aptr[i];
}
}
double calcTotalSales(const double *aptr, int size)
{
double total = 0;
for (int i = 0; i < size; i++)
{
total += aptr[i];
}
return total;
}
int main(int argc, char const *argv[])
{
std::string date = "July 22, 2019", manager = "Tutu Pal";
std::string store_names[5] = {"City Centre", "Park Street", "Rajarhat", "South City", "Dum Dum"};
std::ofstream outfile;
int size = 5, max = 0;
double *aptr = new double[size];
getSales(aptr, size);
for (int i = 0; i < size; i++)
{
if (aptr[i] > aptr[max])
{
max = i;
}
}
outfile.open("report.txt");
outfile << "\nDate:\t" << date << "\nName of Sales Manager:\t" << manager << "\n"
<< std::endl;
for (int i = 0; i < size; i++)
{
printbar(store_names[i], aptr[i], aptr[max], outfile);
}
outfile << "\nThe store with the top sales on " << date << " is " << store_names[max] << std::endl;
outfile << "\nThe total sales is $";
outfile << std::setprecision(2) << std::fixed << calcTotalSales(aptr, size)
<< ".\n"
<< std::endl;
outfile.close();
std::cout << "File written successfully" << std::endl;
return 0;
}
Input:
456
652
64
12
35
File written successfully
Output:
Date: July 22, 2019
Name of Sales Manager: Tutu Pal
City Centre : **********************************
Park Street : **************************************************
Rajarhat : ****
South City :
Dum Dum : **
The store with the top sales on July 22, 2019 is Park Street
The total sales is $1219.00.
Question 2 - Country List
Read a list of countries from the file countries.txt, and sort them in reverse alphabetical order, i.e. Zambia first and Australia last.
#include <fstream>
...
ifstream infile;
int number;
infile.open("numbers.txt");
while (!infile.eof()) {
infile >> number;
cout << number << endl;
}
infile.close();
Handle the situation where the stipulated filename cannot be found.
Solution:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
int main(int argc, char const *argv[])
{
std::ifstream infile;
std::vector<std::string> countries;
std::string country;
int i, j;
infile.open("countries.txt");
if (infile.fail())
{
std::cout << "Error opening file" << std::endl;
return 1;
}
while (std::getline(infile, country))
{
countries.push_back(country);
}
infile.close();
for (int i = 0; i < countries.size(); i++)
{
for (int j = 0; j < countries.size() - i - 1; j++)
{
if (std::lexicographical_compare(countries[j].begin(), countries[j].end(), countries[j + 1].begin(), countries[j + 1].end()))
{
std::swap(countries[j], countries[j + 1]);
}
}
}
for (std::string country : countries)
{
std::cout << country << std::endl;
}
return 0;
}
Input:
United Kingdom
Italy
Brazil
Russia
South Africa
Chile
Portugal
Greece
Kenya
Egypt
Zambia
Malaysia
Japan
Australia
Output:
Zambia
United Kingdom
South Africa
Russia
Portugal
Malaysia
Kenya
Japan
Italy
Greece
Egypt
Chile
Brazil
Australia
Question 3 - Vowels & Consonants
Write a function that returns the number of vowels in a given string. Write another function that returns the number of consonants in a given string.
Use the 2 functions to perform the following:-
- The user is asked to enter a string.
- The program displays the following menu:
- Count the number of vowels in the string
- Count the number of consonants in the string
- Count both the vowels and consonants in the string
- Enter another string
- Exit the program
- The program performs the operation selected by the user and repeats until the user selects E to exit the program.
Solution:
#include <iostream>
int no_of_vowels(std::string str)
{
int count = 0;
for (char c : str)
{
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
count++;
}
return count;
}
int no_of_consonants(std::string str)
{
int count = 0;
for (char c : str)
{
if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'))
count++;
}
return count;
}
int main(int argc, char const *argv[])
{
std::string s;
std::cout << "Enter a string:" << std::endl;
std::getline(std::cin, s);
char ch = '';
while (ch != 'E')
{
std::cout << "nOptions:"
<< "nA) Count the number of vowels in the string"
<< "nB) Count the number of consonants in the stringnC) Count both the vowels and consonants in the string"
<< "nD) Enter another string"
<< "nE) Exit the program"
<< "nEnter your choice: ";
std::cin >> ch;
switch (ch)
{
case 'A':
std::cout << "The number of vowels in the string is: " << no_of_vowels(s) << std::endl;
break;
case 'B':
std::cout << "The number of consonants in the string is: " << no_of_consonants(s) << std::endl;
break;
case 'C':
std::cout << "The number of vowels in the string is: " << no_of_vowels(s) << std::endl;
std::cout << "The number of consonants in the string is: " << no_of_consonants(s) << std::endl;
break;
case 'D':
std::cout << "Enter a string:" << std::endl;
std::cin.ignore();
std::getline(std::cin, s);
break;
case 'E':
std::cout << "Exiting the program..." << std::endl;
break;
default:
std::cout << "Invalid choice. Please try again." << std::endl;
break;
}
}
return 0;
}
Output:
Enter a string:
React component that displays a spinner via spin.js until your component is loaded.
Options:
A) Count the number of vowels in the string
B) Count the number of consonants in the string
C) Count both the vowels and consonants in the string
D) Enter another string
E) Exit the program
Enter your choice: A
The number of vowels in the string is: 25
Options:
A) Count the number of vowels in the string
B) Count the number of consonants in the string
C) Count both the vowels and consonants in the string
D) Enter another string
E) Exit the program
Enter your choice: E
Exiting the program...
0 comments:
Post a Comment