Skip to content

Instantly share code, notes, and snippets.

@DragonOsman
Created November 29, 2021 17:23
Show Gist options
  • Select an option

  • Save DragonOsman/a7a9a8b12016b1c4e8a44b11bd84859c to your computer and use it in GitHub Desktop.

Select an option

Save DragonOsman/a7a9a8b12016b1c4e8a44b11bd84859c to your computer and use it in GitHub Desktop.
// Osman Zakir
// 11 / 29 / 2021
// Beginning C++20: From Novice to Professional by Ivor Horton and Peter Van Weert
// Chapter 6 Example Code 7
// Using smart pointers
import <iostream>;
import <format>;
import <memory>;
import <vector>;
#include <cctype>
int main()
{
std::vector<std::shared_ptr<std::vector<double>>> records;
std::size_t day{ 1 };
while (true)
{
auto day_records{ std::make_shared<std::vector<double>>() };
records.push_back(day_records);
std::cout << "Enter temperators for day " << day++
<< " separated by spaces. Enter 1000 to end:\n";
while (true)
{
double t{};
std::cin >> t;
if (t == 1000.0)
{
break;
}
day_records->push_back(t);
}
std::cin.ignore(32767, '\n');
std::cout << "Enter another day's temperatures (Y or N)? ";
char answer;
std::cin >> answer;
if (std::toupper(answer) != 'Y')
{
break;
}
}
std::cin.ignore(32767, '\n');
day = 1;
for (auto record : records)
{
double total{};
std::size_t count{};
std::cout << std::format("\nTemperatures for day {}:\n", day++);
for (auto temp : *record)
{
total += temp;
std::cout << std::format("{:6.2f}", temp);
if (++count % 5 == 0)
{
std::cout << std::endl;
}
std::cout << std::format("\nAverage temperature: {:.2f}", total / count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment