Monday, 6 June 2022

Chapter 24 // Drill 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 24 // Drill 8

Read six ints into a Matrix<int,2> m(2,3) and print them out.


.

Sunday, 5 June 2022

Chapter 24 // Drill 7 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 24 // Drill 7

Read ten complex<double>s from cin (yes, cin supports >>  for complex) and put them into a Matrix. Calculate and output the sum of the ten complex numbers.


Complex does have built in I/O support which is nice. Figuring out how to use cin was annoying though. I eventually discovered it here:

You need to input it as (num,num). 

Saturday, 4 June 2022

Chapter 24 // Drill 6 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 24 // Drill 6

Compute a multiplication table for [0,n) * [0, m) and represent it as a 2D Matrix. Take n and from cin and print out the table nicely (assume that m is small enough that the results fit on a line).


.

Friday, 3 June 2022

Chapter 24 // Drill 5 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 24 // Drill 5

Read ten floating-point values from input and put them into a Matrix<double>. Matrix has no push_back() so be careful to handle an attempt to enter a wrong number of doubles. Print out the Matrix.


The accompanying MatixIO.h file provides very simple I/O for 1D and 2D matrices. So
Matrix<double> a(4);
cin >> a;
cout << a;

Will read 4 whitespace-seperate doubles delimited by curly braces. For example with a 1D matrix:
{ 1.2 3.4 5.6 7.8 }

These functions will handle reading in the correct amount of numbers and throw errors if the structure isn't exact.

I also added my own method for fun.

Thursday, 2 June 2022

Chapter 24 // Drill 4 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 24 // Drill 4

Write a program that takes ints from cin and outputs the sqrt() of each int, or "no square root" if sqrt(x) is illegal for some x (i.e., check your sqrt() return values).


So the std::sqrt function is explained here:

I didn't realise it returned floats, doubles or long doubles. It returns some error handles if something goes wrong defined here: