Saturday, April 23, 2016

Things different about C++ than other languages especially variations of C


Hello World,

Today I am going to try a different approach to how I have been doing the previous blog posts. Rather than focusing solely on making a program to test out how to implement a specific feature of the language, I am going to talk about C++ in general. For instance, there so many different languages that exist. Not only that, there are many different languages that are different variations of C++. Some of these variations include C, C#, and Objective C. In addition to stating why C++ exists, I shall state two interesting parts of the language that sets it apart especially from these different variations.

I have already stated in a previous post that the language was created by Bjarne Stroustrup. He created C++ from a variation of the language C. The difference between them was that C did not implement object orientation. C++ does. C is a language that produces fast runtime so this was a concept that Stroustrup wished to keep.

In short, one reason why C++ is used so much is because of its transferability between other interpreters and other libraries. C++ is such a common language that it can be accessed easily by other development applications. One quote I found online basically sums up my point. “You rarely find a big program written all in one language, or without using libraries, so easy integration with other languages and libraries was a key design goal.” (www.cantrip.org).

One very great thing you can do in C++ that sets it apart from other languages especially C is the creation of objects. I already stated that object orientation was a main reason C++ was created. There is a function in this language that takes it one step further. Objects can be created by declaring a template. One nice feature about templates is that they relate back to efficiency. For example, imagine creating an array that swaps placement of values every time the user enters new data. Rather than having multiple places for the same code to be written, you could just declare a template or an object to hold all the specified information. Then the user can keep recycling that one line of template when they need to. For reference look at the picture below.


This idea of creating these easy to use objects or templates is not strictly limited to just C++. Java as well as other object oriented languages also has the functionality of making templates however it is quicker to do in C++. If one were to make a template in Java, they could not simply just declare template and be done. They would need to make a class for the object template, reference that object in a different class, then apply some sort of data to that object. Basically the image below sums up the concept.




Although C++ is not very different from other languages especially object oriented languages, there are important features that differentiate it from others. Some of these features include templates and also the transferability of code from different applications and programs. By this I mean that C++ code is not too far off from some of the other major programming languages. Thus doing a program in C++ is mostly easily adjustable when tried in a different language.

I hope you enjoyed this post and please let me know if you have any questions.

Also sometime within the next week, I will be doing another post on a bigger programming task to do in C++. I will post some suggestions soon, but if you happen to have any ideas for programs that are at the intermediate level, please let me know.


Thanks!!!


Sunday, March 20, 2016

How to make methods and writing a binary search program in C++

Hello World,

Today we will be discussing methods. This may seem like a really basic lesson that is fundamental to all programmers, but every language is unique in stating procedures/methods. So let’s first talk about the basics. Methods are used when code is a little more complex than to just throw everything in the main method. In most programs, programmers try to keep their work separate so it is easy to follow and understand. There are locations for calculations to be made, places where one can run the program and places to test instances or catch errors. Basically dividing up your code into different sections is essential for many programs.

If you have been following my blog up until this point, you may have noticed the main method in all my programs. The main method is just int main(). This is slightly different from Java. In Java, aside from deciding whether to make your program methods private, protected, or public, you also have to add void or static for different instances. Methods are real basic in C++. Just like the method int main() you can make your own method by typing int desiredmethod() or any text in there.

Today rather than just writing a simple program to display the purpose and use of a method, I will show you how to successfully run a binary search. For starters, binary search is a type of program that takes a sorted array (an array is a list of numbers) and keeps cutting the list in half until the user finds the output that they are looking for. Arrays are zero based so when the program finds a given number and for example states that that number was found at index of 2, it actually means it was found at the third element in the array (0, 1, 2 and 2 is the index in the example). Although binary searching is a very efficient and fast way of finding specific elements in an array there is a catch. In order for you to find a given element, the array must first be sorted and will not function correctly with an unsorted list. I have a program that uses 2 methods the main method and a new method for displaying what happens in a binary search.

Below is the code for the program


This first part shows how to make a method for starters. Int binarySearch is the method. It takes some parameters which is the array, the size of the array, and a variable called search value. It first starts with low being set to 0. This is the first element of the array. High is set to the length of the list minus 1 to include all elements of the array. There must also be a mid variable. This variable will exist to sway one way or the other depending on the number you are looking for. Say you have an array that consists of 10 elements and the number 22 is the one you are looking for. 22 is located at the second element of the list so it’s at index 1. The program first takes the list and sets the number at index 0 to low and the number at the last element of the list to high. Mid is set to (high+low)/2. So in this case it is 10+0=10/2=5. It then sees that the number you are looking for is lower than the mid so it sets high equal to the number just below the mid and low stays the same. If the desired number where in the 9th spot then it would set low equal to one higher than the mid. Nonetheless, it takes the number at index 4 which is now the high and finds the midpoint again. Low=0 and high=4. (4+0)/2=2 and mid is set to 2. Now since the number 22 is in the 2nd index location, it is at the same spot as mid. The program now knows to return the number in the mid location because it found the correct number and thus returns 22. If it does not find the desired number after it keeps cutting the list in half, then it determines that the number you are looking for does not exist.


This next piece of code is simply making the array and populating it with specific ordered numbers, prompting the user to enter a number and calling the other method in this main method. It is also displaying the number found or not found depending on what the user enters. Below I entered some test outputs.


Hope this makes sense. If you are still having trouble, watch this video because it helped me understand the program a lot better. Video: https://www.youtube.com/watch?v=vohuRrwbTT4


Thanks!

Sunday, March 6, 2016

Using loops in C++

Hello Internet,

It’s been a few weeks since I posted something about C++. Today we will be talking about loops. In C++ there are 3 primary loops. Just like Java, C++ has “for” loops and “while” loops. For loops loop through specific criteria until all instances are met. While loops continue doing code while the condition has not been met. There is another loop in C++ and that is a “do…while” loop. There is a body of code within the do statement and you will do that code until the condition in the while is met. The code in the “do…while” loop will always be executed at least once.

I wrote up a small program to test all of these loops. First the user must enter a number between 1 and 3. 1 means to execute the “for” loop. 2 means to execute the “while” loop. 3 means to execute the “do…while” loop. The “for” loop and the “while” loop do the exact same thing. They count the number of times the program loops through the loop. In other words, you start at 1 and loop through until you get to 10. However the “do…while” loop is a little different. Basically it is an integer counter. It first starts as 0 and as long as the user wishes to continue, it increments by 1 each time.

Note: If you are using C++ shell like me or if you are using a compiler found online, definitely copy and paste all your code into notepad or a word document while you go. I’m telling you this because 3 times during creating this program, the compiler decided to get rid of all my work and revert back to the original page. Luckily because I copied all my code to a word document, each time I lost no code.

Below is the code for the first loop.

 Below is the output of the first loop.


Below is the code for the second loop.


Below is the output for the second loop.


Below is the code for the third loop.


Below is the output for the third loop.


As you can tell until the user says “no” the integer will keep incrementing.

Well that’s the basics of using loops in C++. Hope you enjoyed my example program and I hope this helped you to understand how to use loops. Let me know if anything did not make sense. 

Thanks!

Tuesday, February 16, 2016

3 Basic Conditionals

Hello World,

Today we will be discussing conditionals. These conditionals were pretty easy for me to understand. Basically you have 3 conditionals, or at least 3 that we will cover today. They are “if”, “else if”, and “else”. You may be familiar with these conditionals if you have had any programming experience before in a common programming language.

Conditionals are commonly used to test a comparison. I made a basic program that uses user input to test which number is the largest of 3 numbers. Of course anyone can tell you which number is largest off first glance of 3 numbers. However, this program uses all 3 conditionals in an easy to understand way.

Below is my program.




Basically, it starts by asking the user to enter 3 numbers. The numbers are instance variables as learned in a previous post. Then there are some conditionals. A, b, and c correspond to the first number inputted, the second number inputted, and the third number inputted respectively. The first tests if a is larger than both c and b. If it is, then it is displayed as the largest. Then within each if and if else statement, there is a test to see if the highest number is positive or negative. If it is greater than zero, it is positive. Otherwise it is negative (else). The second conditional asks if c is larger than b and a. If it is, the program figures out if it is negative or positive again. The third conditional asks if b is larger than both a and c, then figures out if it is negative or positive. Then the last else if statement tests if a is larger than b, but c is larger than b, then tests the negative and positive.

This is how a potential output may look.


Hope you enjoyed this lesson. Let me know if anything is unclear.


Thanks!

Tuesday, February 9, 2016

Constants, Variables, and Arithmetic

Hello World,

Today we will be discussing how to declare variables, constants, and basic arithmetic using C++. Fortunately for me, due to my prior experience with Java, today’s post is a simple review. However, one thing that is necessary for your code to execute properly is to make sure everything prints correctly. What I mean by this, at the top of your compiler, verify that this line of code exists: using namespace std;. This is the code that prints any output. I had to add this line of code to my program because it wasn’t already a default in my compiler.

Let’s start with declaring a variable. C++ has many of the same features as Java when it comes to declaring variables. For instance if you are making an instance variable in your main called volume, you just type: int volume;. Now that the instance variable volume is defined you can assign it to a number by saying volume = 1; on the line after. And you can then print that by typing: cout << volume;. Please refer to the picture below to get a better understanding of what I am talking about.



Constants are slightly different that how Java does it. In Java, at the top of the page you can just say int constant; and that is that. C++ has syntax that is slightly different. At the top of the page you must type #define “(constant’s name)” and then add a number. Also semicolons are not necessary in defining constants. Below is how to define a constant properly.


Finally arithmetic is exactly the same in Java as it is for C++. Some of the commands you can do in arithmetic are: addition, subtraction, multiplication, division, mod, incrementing by 1, decrementing by 1, divide and assign, multiply and assign, and mod and assign. Below is a picture taken from wikiversity.org. It shows how to write the arithmetic commands in C++. If you are familiar with Java then this should be an easy review.


Now let’s take what we just learned and apply it to a basic arithmetic function. I defined 3 constants, LENGTH, WIDTH, and HEIGHT, at the top of the page. Then I made an instance variable called volume. I set volume equal to LENGTH*WIDTH*HEIGHT and then I print out the result. Below is a picture of this little program.


I hope you enjoyed this post and please let me know if you have any questions pertaining to some of the information we covered.

Thanks!

Monday, February 1, 2016

Hello World Program

Hello World,

Today I will show you how to do some of the simple features of C++. In particular I will be doing a program involving print statements and possibly implementing a scanner to get user input in the program. This should not difficult to implement even for a beginner. I found this program fun and interesting. 

For this program to work successfully, you will need a working compiler. As I mentioned last week, the compilers that I found are free and require no installation. The compiler that I will be using is called C++ Shell. There is also another compiler on Code Chef’s website which is free and requires no installation as well.

In many ways, from the research I have been doing on the language so far, C++ is similar to the languages I have previously worked with. However, there are minor syntax differences. In order to get a working program, you must have a main method or somewhere to call upon your previous work. You can make a main method by stating: int main() {….// this is where your work is and then finish with }. Just like Java, two slashes marks a comment.


The 1st line is the comment “Example program.” You can tell it’s a comment by the two slashes. The 2nd and 3rd lines include iostream and string is there because you have to tell the program all the components that you will be using to write out your program. This is just like how Java makes the user import rectangles or ellipses if you are making graphics. Iostream is basically the input for telling the program that you will be including declarations of the basic input and outputs your program will have. In other words you will be stating if you are printing out text. The 5th line is just making the main method. Line 7 is how to make a print statement. It starts with std::cout << “…” includes \n and ends with a semicolon. Std stands for standard and cout means character output. \n is just like Java because it allows for your previous text to be on its own line. Line 8 defines the string you will be using and it is called name. Line 9 prompts the user to enter their name and then line 10 retrieves the input you just typed in and prints a line in 11 saying “Hello, (insert name)! Thank you for experiencing this memorable occasion with me!

Lastly, I included a short video of how the program may function. Hope you enjoy it!




The information I got pertaining to this introduction to the language was obtained at www.CPlusPlus.com

Tuesday, January 26, 2016

Introduction Guide to C++

Hello World,

This blog is a beginner’s introductory guide to getting familiar with the language C++. C++ today is used in many different software companies. It is used for writing, testing, and making new programs. I wanted to get familiar with this language because I recently started applying for software developing internships and jobs. On one of the websites for an internship I applied for, I was required to take an assessment for the company to get a general understanding of the skills and knowledge I possess. There were questions on general programming, C++, SQL, and ASP.NET. Because it was optional to attempt any question from languages you do not know, I decided to try the questions for C++ and the outcome was not pretty.

C++ was created in 1983 by a man named Bjarne Stroustrup. Originally the language was modeled after the language C as well as a language called Simula. He wanted to take some of the advantages of each language and make something new. The con with the language C was that it could not implement the ideas behind the object-oriented programming paradigm. Simula implemented object oriented programming but the speed at which programs were compiled was too slow. Therefore the reason this language exists is because Stroustrup wanted to implement object-orientated programming and make the run time of the actions fast just like C.

If you are familiar with any other language ++ means to increment. This allows C++ to take the hidden meaning that it is always incrementing and always getting better. In fact, today there are three different editions of the language. There is C++98 which is the first edition, C++03 which is the second edition, and C++11 which is the third edition.

For this blog, I’m planning on doing some practice problems to better understand the concepts I will cover. These practice problems may come from StackOverFlow.com or CodeChef.com. I may even get a textbook online that can better explain some concepts and provide examples to go along with them. One book I looked at was C++ How to Program (7th Edition). However, I will keep you informed as to where I get the practice problems I do.

Finally the primary compiler that I will be using is called C++ Shell. It is free to use and run and you can find it by doing a search online or by clicking this link C++ Shell. There is also a free compiler on one of the resources I previously listed. The link for it is CodeChef Compiler.

Also, I have no experience with C++ so if you happen to see something incorrect or if you have a better way of explaining something, please leave a message for me in the comment section below.


Thanks!