Week 9 quiz: Dem’s the lengths

--Originally published at World's Fanciest Cursors

64507089_f91dc66c31_z
Bad sellings graph by Flickr user baboon. Shared under a CreativeCommons (BY) license.

Howdy. Today I’m sharing a cute little program that calculates the distance between two points in a Cartesian plane.  Not much new to see. There’s only a lonely function and the use of squares and roots in a Pythagorean equation.

Without further ado here’s the link to GitHub and here’s the holy noodles:

#include <iostream>
#include <cmath>

float distance (float, float, float, float);

int main ()
{
  float x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  std::cout << "X uno, puh-leeze" << std::endl;
  std::cin >> x1;
  std::cout << "Y uno, puh-leeze" << std::endl;
  std::cin >> y1;
  std::cout << "X dos, puh-leeze" << std::endl;
  std::cin >> x2;
  std::cout << "Y dos, puh-leeze" << std::endl;
  std::cin >> y2;
  std::cout << "\nThe distance is " << distance (x1, y1, x2, y2) << std::endl;
  std::cout << "\nBuh-bye" << std::endl;
}

float distance (float x1, float y1, float x2, float y2)
{
  float delta_x = 0, delta_y = 0, rdistance = 0;
  delta_x = x2 - x1;
  delta_y = y2 - y1;
  rdistance = sqrt(pow(delta_x, 2) + pow(delta_y, 2));
  return rdistance;
}

Week 3 quiz, the one about functions

--Originally published at World&#039;s Fanciest Cursors

800px-computer_mouse_trap
Mousetrap? by Flickr user mllerustad. Shared under a CreativeCommons (BY) license.

Today’s quiz involved a bit of learning.  Besides creating and executing functions we neede to check for correct user input.

The first thing checked is whether the input is actually a number and not some other kind of symbol. To do that I used some code I found that checks for input call failures here. An input call failure usually happens when you try to enter the wrong type of data to a variable, like trying to enter a letter into an “int” variable. This code checks for such a failure and evaluates to true if it exists. We can use this flag to alter the flow of execution to a loop that clears the error, skips the problem data and asks again for input.

The second thing to check is for negative numbers. As we know, negative numbers cannot be used in root operations as that would result in an imaginary number. We need to make sure the user enters positive values. This can be accomplished with a simple if statement that evaluates if the input is greater or equal to zero. If that is false we can set a loop to ask again for input over and over again until an appropriate answer is given. Learn more about operators here.

It works!:

working

Here, enjoy some spaghetti-code:

#include <iostream>
#include <cmath>

double square_root (double);
double cube_root (double);

int main ()
{
  double a;
  std::cout << "Give me a positive number and I'll get its square and cubic roots.\nEnter your number:" << std::endl;   while(1)   {     std::cin >> a;
    while(1) // Loop that cheacks for wrong input types
    {
      if (std::cin.fail()) // Detects a failure of the input function
      {
        std::cin.clear(); // Clears the error
        std::cin.ignore(); // 
20160917_143539
Continue reading "Week 3 quiz, the one about functions"

Integral fun

--Originally published at World&#039;s Fanciest Cursors

It’s time to play with numbers. Here I’ll show you an unnecessarily overcomplicated program that could have been achieved in one third of the lines of code used. To achieve this ugly mess I used control statements (while loops  and if statements), switch cases and functions. Click on each of those phrases to take you to the tutorial I used to learn how to use them.

Today’s featured cursor is the ¯\_(ツ)_/¯ cursor, for when you don’t know what to do and just shrug it of..

cursor-of-the-day
Source

Here’s the code:

#include <iostream>
#include <cmath> // Library for more in depth math, needed for absolute values

int numbercruncher (int, int, int); // prototyping a function

int main()
{
int loopy = 1;
std::cout << “Human, I eat numbers. Feed me.\n”;
while (loopy == 1) // This repeats the process so that you can do it again
{
loopy = 0;
int uno = 0, dos = 0;
char decide;
std::cout << “\nWhat’s your first integer number?” << std::endl;
std::cin >> uno;
std::cout << “What’s your second integer number?” << std::endl;
std::cin >> dos;
std::cout << “This is what I have to say about your numbers:” << std::endl;
std::cout << “Their sum is ” << numbercruncher (uno, dos, 1) << std::endl; // The constant number indicates what part of the function to run
std::cout << “Their difference is ” << numbercruncher (uno, dos, 2) << std::endl;
std::cout << “Their product is ” << numbercruncher (uno, dos, 3) << std::endl;
std::cout << “Their integer division is ” << numbercruncher (uno, dos, 4) << std::endl;
std::cout << “Their division remainder is ” << numbercruncher (uno, dos, 5) << std::endl;
std::cout << “\nWold you like to enter more numbers? (y/n)” << std::endl;
std::cin >> decide;
if (decide == ‘y’)
{
loopy =

Continue reading "Integral fun"

Hello world to you

--Originally published at World&#039;s Fanciest Cursors

Greetings! You may be asking yourself a few things: “Why did you make this blog and then took a year to upload anything? Why are you posting stuff about programming when we want cursors?! Why does my bank statement list the purchase of several hundred pounds of ammonium nitrate from eBay?!”

Don’t worry folks, I’m finally going to upload stuff in a semi-regular basis if we are all lucky, plus you can use what you’ll learn about programming to create you favourite things ever: Colorful, sparkly cursors.

To start us off I’ll share with you the perfect cursor for the occasion: The spinning question-mark box cursor.

boxquestionred

Now I shall show you what a “Hello World!” program looks like in C++:

howdy

I look forward to enlighten your life with the computer personalization of the future.