Tag Archives: for loop

Java || Compute The Sum From A String Of Integers & Display Each Number Individually

Here is a simple program, which was presented in a Java course. This program was used to introduce the input/output mechanisms which are available in Java. This assignment was modeled after Exercise 2.30, taken from the textbook “Java How to Program” (early objects) (9th Edition) (Deitel). It is the same exercise in both the 8th and 9th editions.

Our class was asked to make a program which prompts the user to enter a non-negative integer into the system. The program was supposed to then extract the digits from the inputted number, displaying them each individually, separated by a white space (” “). After the digits are displayed, the program was then supposed to display the sum of those digits to the screen. So for example, if the user inputted the number “39465,” the program would output the numbers 3 9 4 6 5 individually, and then the sum of 27.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

How To Get String Input
If/Else Statements
Do/While Loops
For Loops
Methods (A.K.A "Functions") - What Are They?
ParseInt - Convert String To Integer
Substring
Try/Catch


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

Welcome to My Programming Notes' Java Program.

Enter a non negative integer: 0

The digits are: 0 and the sum is 0

Do you have more data for input? (Y/N): y

------------------------------------------------
Enter a non negative integer: 39465

The digits are: 3 9 4 6 5 and the sum is 27

Do you have more data for input? (Y/N): y

------------------------------------------------
Enter a non negative integer: H3ll0 W0rld

H3ll0 W0rld is not a number...
Please enter digits only!

Do you have more data for input? (Y/N): y

------------------------------------------------
Enter a non negative integer: -98

-98 is a negative number...
Please enter positive digits only!

Do you have more data for input? (Y/N): y

------------------------------------------------
Enter a non negative integer: 19.87

19.87 is a decimal number...
Please enter positive whole numbers only!

Do you have more data for input? (Y/N): n

------------------------------------------------

BYE!

C++ || Char Array – Determine If A String Is A Number Or Not

The following is another intermediate homework assignment which was presented in a C++ programming course. This program was assigned to introduce more practice using and manipulating character arrays.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Character Arrays
Cin.getline
Strlen - Get The Length Of A Char Array
Isalpha
Isspace

This program first prompts the user to input a line of text. After it obtains data from the user, using a for loop, it then displays the the string to the screen one letter (char) at a time. If the current character at that specific array index is a letter, a “flag” is set, indicating that the current word which is being displayed is not a number. If the “flag” is not set, the current word is indeed a number.

This program has the ability to intake multiple words at a time, so for example, if the user input was “Hello World 2012” the program would display the output:

Hello is NOT a number!
World is NOT a number!
2012 is a number..

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
(Note: the code was compiled three separate times to display different output)

====== RUN 1 ======

Enter some text to see if its a number or not: My Programming Notes

My is NOT a number! There are 2 letters in that word...
Programming is NOT a number! There are 11 letters in that word...
Notes is NOT a number! There are 5 letters in that word...

====== RUN 2 ======

Enter some text to see if its a number or not: May 30th 2012

May is NOT a number! There are 3 letters in that word...
30th is NOT a number! There are 2 letters in that word...
2012 is a number..

====== RUN 3 ======

Enter some text to see if its a number or not: 5 31 2012

5 is a number..
31 is a number..
2012 is a number..

C++ || FizzBuzz – Tackling The Fizz Buzz Test In C++

What is Fizz Buzz?

Simply put, a “Fizz-Buzz test” is a programming interview question designed to help filter out potential job prospects – those who can’t seem to program if their life depended on it.

An example of a typical Fizz-Buzz question is the following:

Write a program which prints the numbers from 1 to 100. But for multiples of three, print the word “Fizz” instead of the number, and for the multiples of five, print the word “Buzz”. For numbers which are multiples of both three and five, print the word “FizzBuzz”.

This seems easy enough, and many should be able to complete a program which carries out a solution in a few minutes. Though, after doing a little research, apparently that is not the case. This page will present one way to carry out a solution to this Fizz-Buzz problem.

There is a small “catch” that some may encounter when trying to solve this problem, and that is the fact that the conditional statement for the number divisible by 15 should come before each sequential conditional statement. Consider this pseudocode:

The portion that may make this problem tricky for some is the fact that the conditional statement for the number divisible by 15 must be checked -before- the conditional statements which checks for numbers divisible by 3 and 5. If the conditional statements are placed in any other order, the end result will not be correct, which is what can make the problem difficult for many.

======= THE FIZZ BUZZ TEST =======

So building upon the pseudocode found from above, utilizing the modulus operator, here is a simple solution to the Fizz Buzz Test

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

1
2
FIZZ
4
BUZZ
FIZZ
7
8
FIZZ
BUZZ
11
FIZZ
13
14
FIZZ BUZZ!!
16
17
FIZZ
19
BUZZ
FIZZ
22
23
FIZZ
BUZZ
26
FIZZ
28
29
FIZZ BUZZ!!
31
32
FIZZ
34
BUZZ
FIZZ
37
38
FIZZ
BUZZ
41
FIZZ
43
44
FIZZ BUZZ!!
46
47
FIZZ
49
BUZZ
FIZZ
52
53
FIZZ
BUZZ
56
FIZZ
58
59
FIZZ BUZZ!!
61
62
FIZZ
64
BUZZ
FIZZ
67
68
FIZZ
BUZZ
71
FIZZ
73
74
FIZZ BUZZ!!
76
77
FIZZ
79
BUZZ
FIZZ
82
83
FIZZ
BUZZ
86
FIZZ
88
89
FIZZ BUZZ!!
91
92
FIZZ
94
BUZZ
FIZZ
97
98
FIZZ
BUZZ

C++ || Class – Roman Numeral To Integer & Integer To Roman Numeral Conversion


 

Click Here For Updated Version Of Program


The following is another homework assignment which was presented in a C++ Data Structures course. This program was assigned in order to practice the use of the class data structure, which is very similar to the struct data structure.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Header Files - How To Use Them
Class - What Is It?
Do/While Loop
Passing a Value By Reference
Roman Numerals - How Do You Convert To Decimal?
Online Roman Numeral Converter - Check For Correct Results

This is an interactive program in which the user has the option of selecting from 7 modes of operation. Of those modes, the user has the option of entering in roman numerals for conversion, entering in decimal numbers for conversion, displaying the recently entered number, and finally converting between roman or decimal values.

A sample of the menu is as followed:
(Where the user would enter numbers 1-7 to select a choice)


This program was implemented into 3 different files (two .cpp files, and one header file .h). So the code for this program will be broken up into 3 sections, the main file (.cpp), the header file (.h), and the implementation of the functions within the header file (.cpp).

======== FILE #1 – Menu.cpp ========

NOTE: On some compilers, you may have to add #include < cstdlib> in order for the code to compile.



======== FILE #2 – ClassRomanType.h ========

Remember, you need to name the header file the same as the #include from the Menu.cpp file. This file contains the function declarations, but no implementation of those functions takes place here.


======== FILE #3 – RomanType.cpp ========

This is the function implementation file for the ClassRomanType.h class. This file can be named anything you wish as long as you #include “ClassRomanType.h”


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 9

You selected choice #9 which will:
Error, you entered an invalid command!
Please try again...
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 5

You selected choice #5 which will: Print the current Decimal number

The current Roman to Decimal Value is: 0
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 6

You selected choice #6 which will: Print the current Roman Numeral

The current Decimal to Roman Value is: Currently Undefined
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 1

You selected choice #1 which will: Get Decimal Number

Enter a Decimal Number: 1987
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 5

You selected choice #5 which will: Print the current Decimal number

The current Roman to Decimal Value is: 1987
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 3

You selected choice #3 which will: Convert from Decimal to Roman
Running....
Complete!
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 6

You selected choice #6 which will: Print the current Roman Numeral

The current Decimal to Roman Value is: MCMLXXXVII
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 2

You selected choice #2 which will: Get Roman Numeral

Enter a Roman Numeral: mMxiI
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 6

You selected choice #6 which will: Print the current Roman Numeral

The current Decimal to Roman Value is: mMxiI
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 4

You selected choice #4 which will: Convert from Roman to Decimal
Running....
Complete!
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 5

You selected choice #5 which will: Print the current Decimal number

The current Roman to Decimal Value is: 2012
--------------------------------------------------------------------
From the following menu:

1. Enter a Decimal number
2. Enter a Roman Numeral
3. Convert from Decimal to Roman
4. Convert from Roman to Decimal
5. Print the current Decimal number
6. Print the current Roman Numeral
7. Quit

Please enter a selection: 7

You selected choice #7 which will: Quit
--------------------------------------------------------------------
Bye!

C++ || Printing Various Patterns Using Nested Loops

This page will demonstrate various programs which illustrates the use of nested loops to print selected patterns to the screen.

REQUIRED KNOWLEDGE FOR THIS PAGE

While Loops
For Loops
Using Nested Loops

The following are famous homework assignments which are usually presented in an entry level programming course.

There are a total of ten (10) different patterns on this page, which is broken up into sections. This page will list:

(4) methods of printing a triangle
(4) methods of printing an upside down triangle
(1) method which prints a square
(1) method which prints a giant letter 'X'

======= PRINTING A TRIANGLE =======

This program prints a triangle shape to the screen.


In the above example, the user has a choice of entering the number of rows which will be displayed to the screen

SAMPLE OUTPUT:

Enter a number: 9
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *

======= PRINTING A TRIANGLE WITH NUMBERS =======

The following program uses the same concept as above, but this time instead of using stars “*”, numbers will be printed to the screen.


SAMPLE OUTPUT:

Enter a number: 9
9
8 8
7 7 7
6 6 6 6
5 5 5 5 5
4 4 4 4 4 4
3 3 3 3 3 3 3
2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1

======= PRINTING A TRIANGLE WITH NUMBERS IN-ORDER =======

The following program uses the same concept as above, but this time instead of using stars “*”, numbers will be printed to the screen in-order.


SAMPLE OUTPUT:

Enter a number: 9
9
8 9
7 8 9
6 7 8 9
5 6 7 8 9
4 5 6 7 8 9
3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9

======= PRINTING A TRIANGLE WITH NUMBERS USING MULTIPLICATION =======

This example demonstrates another triangle, this time printing a multiplication table.


SAMPLE OUTPUT:

Enter a number: 9
9
9 18
9 18 27
9 18 27 36
9 18 27 36 45
9 18 27 36 45 54
9 18 27 36 45 54 63
9 18 27 36 45 54 63 72
9 18 27 36 45 54 63 72 81

======= PRINTING AN UPSIDE-DOWN TRIANGLE =======

This program is similar to the first one, this time printing the triangle upside-down.


SAMPLE OUTPUT:

Enter a number: 9
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*

======= PRINTING AN UPSIDE-DOWN TRIANGLE WITH NUMBERS =======

This program is similar to the second one, this time printing the triangle upside-down.


SAMPLE OUTPUT:

Enter a number: 9
9 9 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8
7 7 7 7 7 7 7
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

======= PRINTING AN UPSIDE-DOWN TRIANGLE WITH NUMBERS IN-ORDER =======

The following program uses the same concept as above, but this time instead of using stars “*”, numbers will be printed to the screen in-order.


SAMPLE OUTPUT:

Enter a number: 9
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9
4 5 6 7 8 9
5 6 7 8 9
6 7 8 9
7 8 9
8 9
9

===== PRINTING AN UPSIDE-DOWN TRIANGLE WITH NUMBERS USING MULTIPLICATION =====

This program is similar to the third one, this time printing the triangle upside-down.


SAMPLE OUTPUT:

Enter a number: 9
9 18 27 36 45 54 63 72 81
9 18 27 36 45 54 63 72
9 18 27 36 45 54 63
9 18 27 36 45 54
9 18 27 36 45
9 18 27 36
9 18 27
9 18
9

======= PRINTING A SQUARE =======

This program prints a square to the screen.


SAMPLE OUTPUT:

======= PRINTING THE LETTER “X” =======

The final program for this page will print a giant letter “X” to the screen.


SAMPLE OUTPUT:

And there you have it. Simple shapes made possible in C++.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

C++ || Savings Account Balance – Calculate The Balance Of A Savings Account At The End Of A Period

Here is another actual homework assignment which was presented in an intro to programming class. The following program was a question taken from the book “Starting Out with C++: Early Objects (7th Edition),” chapter 5, problem #16.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

For Loops - How To Use Them
Assignment Operators - What Are They?
Setprecision - What Is It?
Interest Rate

This program first prompts the user to enter the annual interest rate, starting balance, and the number of months which has passed since the account was established. Upon obtaining the information, a loop is then used to iterate through each month, performing the following:

• Ask the user for the amount deposited into the account during the month (positive values only). This amount is added to the balance.

• Ask the user for the amount withdrawn from the account during the month (positive values only). This is subtracted from the balance.

• Calculate the monthly interest. The monthly interest rate is the annual interest rate divided by 12. After each loop iteration, the monthly interest rate is multiplied by the current balance, which is then added to the total balance.

After the last iteration, the program displays the ending balance, the total amount of deposits, the total amount of withdrawals, and the total interest earned.

If a negative balance is calculated at any point, an erroneous message is displayed to the screen indicating that the account has been closed, and then the program terminates.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

Enter the annual interest rate on the account (e.g .04): .07
Enter the starting balance: $5000
How many months have passed since the account was established? 3

Month #1
Total deposits for this month: $500
Total withdrawal for this month: $120

Month #2
Total deposits for this month: $750
Total withdrawal for this month: $200

Month #3
Total deposits for this month: $500
Total withdrawal for this month: $100

Ending balance:................. $ 6433.47
Amount of deposits:............. $ 1750.00
Amount of withdrawals:.......... $ 420.00
Amount of interest earned:...... $ 103.47

C++ || “One Size Fits All” – BubbleSort Which Works For Integer, Float, & Char Arrays

Here is another sorting algorithm, which sorts data that is contained in an array. The difference between this method, and the previous methods of sorting data is that this method works with multiple data types, all in one function. So for example, if you wanted to sort an integer and a character array within the same program, the code demonstrated on this page has the ability to sort both data types, eliminating the need to make two separate sorting functions for the two different data types.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Integer Arrays
Character Arrays
BubbleSort
Pointers
Function Pointers - What Are They?
Memcpy
Sizeof
Sizet
Malloc


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

Notice, the same function declaration is being used for all 3 different data types, with the only difference between each function call are the parameters which are being sent out.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
(Note: The function works for all three data types)


Original values in the int array:
44 91 43 22 20 100 77 80 84 60 47 91 51 81

The sorted values in the int array:
20 22 43 44 47 51 60 77 80 81 84 91 91 100
---------------------------------------------------------------------------------------
Original values in the float array:
49.5 30.5 67.5 50.5 29.5 89.5 78.5 80.5 54.5 7.5 54.5 38.5 56.5 70.5

The sorted values in the float array:
7.5 29.5 30.5 38.5 49.5 50.5 54.5 54.5 56.5 67.5 70.5 78.5 80.5 89.5
---------------------------------------------------------------------------------------
Original values in the char array:
This Is Random Text Brought To You By My Programming Notes

The sorted values in the char array:
Brought By Is My Notes Programming Random Text This To You

C++ || Stack Based Postfix Evaluation (Single Digit)

This page consists of another homework assignment which was presented in a C++ Data Structures course. While the previously discussed program dealt with converting Infix expressions to Postfix, this program will demonstrate exactly how to evaluate them.

NOTE: Want to convert & evaluate multi digit, decimal, and negative numbers? Click here!

REQUIRED KNOWLEDGE FOR THIS PROGRAM

What Is Postfix?
How To Convert Infix To Postfix Equations
Stack Data Structure
Cin.getline
How To Evaluate Postfix Expressions
The Order Of Operations
#include "ClassStackType.h"

The title of this page is called – “Stack Based Postfix Evaluation (Single Digit).” Why “single digit?” The program demonstrated on this page has the ability to evaluate a postfix equation, but it only has the ability to evaluate single digit values. What do I mean by that? Consider the infix equation: 5+2. When that expression is converted to postfix, it will come out to be: 52+, and the answer will be 7 (5+2=7). But what if we have an equation like 12+2? When that expression is converted to postfix, it will come out to be: 122+. The postfix conversion is correct, but when you try to evaluate the expression, we do not know if the math operation should be 12+2 or 1+22, it can be read either way.

Question: So why is this program being displayed if it only works for single digits?
Answer: Because it demonstrates the process of evaluating postfix equations very well.

Want to convert & evaluate multi digit, decimal, and negative numbers? Click here!

Before we get into things, here is a helpful algorithm for evaluating a postfix expression in pseudo code:

Once you understand the process of converting from infix to postfix, adding the ability to evaluate multiple digits within this program should be doable.

======= POSTFIX EVALUATION =======

This program uses a custom template.h class. To obtain the code for that class, click here.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

Want to convert & evaluate multi digit, decimal, and negative numbers? Click here!

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
(Note: the code was compile three separate times to display different output)

====== RUN 1 ======

==== Postfix Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root

Sample Postfix Equation: 45^14*232+$2-/12%24*/*

Please enter a postfix expression: 1 2 + 5 6 + /
The postfix expression = 1 2 + 5 6 + /

Calculations:
1+2 = 3
5+6 = 11
3/11 = 0.272727
Final answer = 0.272727

====== RUN 2 ======

==== Postfix Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root

Sample Postfix Equation: 45^14*232+$2-/12%24*/*

Please enter a postfix expression: 35*76^+
The postfix expression = 35*76^+

Calculations:
3*5 = 15
7^6 = 117649
15+117649 = 117664
Final answer = 117664

====== RUN 3 ======

==== Postfix Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root

Sample Postfix Equation: 45^14*232+$2-/12%24*/*

Please enter a postfix expression: 45^4*32+$2-/12%24*/*
The postfix expression = 45^4*32+$2-/12%24*/*

Calculations:
4^5 = 1024
1024*4 = 4096
3+2 = 5
√5 = 2.23607
2.23607-2 = 0.236068
4096/0.236068 = 17350.9
1%2 = 1
2*4 = 8
1/8 = 0.125
17350.9*0.125 = 2168.87
Final answer = 2168.87

C++ || Stack Based Infix To Postfix Conversion (Single Digit)

This page consists of another homework assignment which was presented in a C++ Data Structures course. No matter which institution you attend, it seems every instructor assigns a program similar to this at one time or another.

Want to evaluate a postfix expression? Click here.

Want to convert & evaluate multi digit, decimal, and negative numbers? Click here!

REQUIRED KNOWLEDGE FOR THIS PROGRAM

What Is Infix?
What Is Postfix?
Stack Data Structure
Cin.getline
How To Convert To Postfix
The Order Of Operations
#include "ClassStackType.h"

The program demonstrated on this page has the ability to convert a normal infix equation to postfix equation, so for example, if the user enters the infix equation of (1*2)+3, the program will display the postfix result of 12*3+.

Before we get into things, here is a helpful algorithm for converting from infix to postfix in pseudo code:

======= INFIX TO POSTFIX CONVERSION =======

This program uses a custom template.h class. To obtain the code for that class, click here.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

Want to convert & evaluate multi digit, decimal, and negative numbers? Click here!

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Want to evaluate a postfix expression? Click here for sample code.

Once compiled, you should get this as your output
(Note: the code was compile three separate times to display different output)

====== RUN 1 ======

==== Infix to Postfix Conversion ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root

Sample Infix Equation: (((4^5)*14)/($(23+2)-2))*(1%2)/(2*4)

Please enter an infix expression: ((a+b)+c)/(d^e)
The Infix expression = ((a+b)+c)/(d^e)
The Postfix expression = ab+c+de^/

====== RUN 2 ======

==== Infix to Postfix Conversion ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root

Sample Infix Equation: (((4^5)*14)/($(23+2)-2))*(1%2)/(2*4)

Please enter an infix expression: (3*5)+(7^6)
The Infix expression = (3*5)+(7^6)
The Postfix expression = 35*76^+

====== RUN 3 ======

==== Infix to Postfix Conversion ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root

Sample Infix Equation: (((4^5)*14)/($(23+2)-2))*(1%2)/(2*4)

Please enter an infix expression: (((4^5)*14)/($(23+2)-2))*(1%2)/(2*4)
The Infix expression = (((4^5)*14)/($(23+2)-2))*(1%2)/(2*4)
The Postfix expression = 45^14*232+$2-/12%24*/*

C++ || 8 Different Ways To Reverse A String/Character Array In C++

This page will consist of 8 different ways to reverse a character array, and a string literal (std::string).

REQUIRED KNOWLEDGE FOR THE PROGRAMS

Character Arrays
String Literals
Cin.getline - Use For Char Arrays
Getline - Use For std::string
Length
Strlen
Strcpy
While Loops
For Loops
Recursion - What is it?
#include < algorithm>
#include < stack>

The methods on this page will be broken up into sections. This page will list:


(3) methods which reverses string literals (std::string)
(4) methods which reverses character arrays
(1) method which utilizes the stack to "reverse" a character sequence

Some methods listed on this page demonstrates the use of reversing a character sequence without the use of strlen/length.

======= REVERSE AN STD::STRING =======


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM

======= REVERSE A CHARACTER ARRAY =======

The following will demonstrate (4) methods which reverses a character array.


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM


======= REVERSE A CHARACTER SEQUENCE USING A STACK =======

The following will demonstrate (1) method which reverses a character sequence using the STL stack.


SAMPLE OUTPUT


Enter your name: My Programming Notes

Your name reversed is: setoN gnimmargorP yM

C++ || Char Array – Palindrome Checker Using A Character Array, ToUpper, Strlen, Strcpy, & Strcmp

The following is a palindrome checking program, which demonstrates more use of char array’s, ToUpper, Strlen, & Strcmp.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Character Arrays
Cin.getline
How to convert text in a char array from lower to uppercase
How to reverse a character array
Palindrome - What is it?
Strlen
Strcpy
Strcmp
While Loops
For Loops
Constant Variables
Setw

Using a constant value, by default, this program first asks the user to enter 5 words and/or sentences that they want to compare for similarity. If the text which was entered into the program is a palindrome, the program will prompt a message to the user via cout. This program determines similarity by using the strcmp function, to compare two arrays together. This program also demonstrates how to reverse a character array, aswell as demonstrates how to convert all the text which was placed into the char array from lower to UPPERCASE.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

Click here to see how cin.getline works.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
(Note: The code was compiled 2 seperate times to demonstrate different output)

====== RUN 1 ======

Welcome to the Palindrome Check System!
Please enter 5 word(s) to check for similarity:
#1: SteP oN nO PEts
#2: My ProGramminG NoTeS
#3: RaTs liVE ON No eViL StaR
#4: ABLe wAs I ErE I sAw ElBa
#5: LiVE Non Evil

------------------------------------------------------------
This is what you entered into the system:
Text #1: SteP oN nO PEts
Text #2: My ProGramminG NoTeS
Text #3: RaTs liVE ON No eViL StaR
Text #4: ABLe wAs I ErE I sAw ElBa
Text #5: LiVE Non Evil

------------------------------------------------------------
Here are the palindrome's:
Text #1: SteP oN nO PEts is a palindrome
Text #3: RaTs liVE ON No eViL StaR is a palindrome
Text #4: ABLe wAs I ErE I sAw ElBa is a palindrome
Text #5: LiVE Non Evil is a palindrome

====== RUN 2 ======

Welcome to the Palindrome Check System!
Please enter 5 word(s) to check for similarity:
#1: today Is Great
#2: Tomorrow is Foriegn
#3: Sunday Brunch
#4: Hello SkiPper
#5: Mayday Ship DowN!

------------------------------------------------------------
This is what you entered into the system:
Text #1: today Is Great
Text #2: Tomorrow is Foriegn
Text #3: Sunday Brunch
Text #4: Hello SkiPper
Text #5: Mayday Ship DowN!

------------------------------------------------------------
Here are the palindrome's:
There were no palindrome's found in the current list!

C++ || Char Array – Convert Text Contained In A Character Array From Lower To UPPERCASE

This program demonstrates how to switch text which is contained in a char array from lower to UPPERCASE. This program also demonstrates how to convert all of the text contained in a char array to lower/UPPERCASE.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Character Arrays
Cin.getline
Islower
Isupper
Tolower
Toupper
Strlen
While Loops
For Loops
Constant Variables
Setw

Using a constant integer value, this program first asks the user to enter in 3 lines of text they wish to convert from lower to UPPERCASE. Upon obtaining the information from the user, the program then converts all the text which was placed into the character array from lower to uppercase in the following order:

(1) Switches the text from lower to UPPERCASE
(2) Converts all the text to UPPERCASE
(3) Converts all the text to lowercase

After each conversion is complete, the program displays the updated information to the screen via cout.

NOTE: On some compilers, you may have to add #include < cstdlib>, #include < cctype>, and #include < cstring> in order for the code to compile.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

Click here to see how cin.getline works.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

Please enter 3 line(s) of text you wish to convert from lower to UPPERCASE:
#1: I StriKe hiM a heAVy bloW.
#2: When cAn the neRve ShinE?
#3: My Programming Notes.

------------------------------------------------------------
This is what you entered into the system:
Text #1: I StriKe hiM a heAVy bloW.
Text #2: When cAn the neRve ShinE?
Text #3: My Programming Notes.

------------------------------------------------------------
This is the information switched from lower to UPPERCASE:
Text #1: i sTRIkE HIm A HEavY BLOw.
Text #2: wHEN CaN THE NErVE sHINe?
Text #3: mY pROGRAMMING nOTES.

------------------------------------------------------------
This is the information converted to all UPPERCASE:
Text #1: I STRIKE HIM A HEAVY BLOW.
Text #2: WHEN CAN THE NERVE SHINE?
Text #3: MY PROGRAMMING NOTES.

------------------------------------------------------------
This is the information converted to all lowercase:
Text #1: i strike him a heavy blow.
Text #2: when can the nerve shine?
Text #3: my programming notes.

C++ || Snippet – How To Do Simple Math Using Integer Arrays

This page will consist of simple programs which demonstrate the process of doing simple math with numbers that are stored in an integer array.

REQUIRED KNOWLEDGE FOR THIS SNIPPET

Integer Arrays
For Loops
Assignment Operators - Simple Math Operations
Setw

Note: In all of the examples on this page, a random number generator was used to place numbers into the array. If you do not know how to obtain data from the user, or if you do not know how to insert data into an array, click here for a demonstration.

===== ADDITION =====

The first code snippet will demonstrate how to add numbers together which are stored in an integer array. This example uses the “+=” assignment operator.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

SAMPLE OUTPUT

Original array values
64 85 44 31 35 2 94 67 12 80 97 10
--------------------------------------------------------
The sum of the items in the array is: 621

===== SUBTRACTION =====

The second code snippet will demonstrate how to subtract numbers which are stored in an integer array. This example uses the “-=” assignment operator.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

SAMPLE OUTPUT

Original array values
10 43 77 10 2 17 87 67 6 95 57 18
--------------------------------------------------------
The difference of the items in the array is: -489

===== MULTIPLICATION =====

The third code snippet will demonstrate how to multiply numbers which are stored in an integer array. This example uses the “*=” assignment operator.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

SAMPLE OUTPUT

Original array values
33 36 52 28 4 99 97 17 42 81 83 33
--------------------------------------------------------
The product of the items in the array is: 1803759104

===== DIVISION =====

The fourth code snippet will demonstrate how to divide numbers which are stored in an integer array. This example uses the “/=” assignment operator.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

SAMPLE OUTPUT

Original array values
75 38 59 14 53 42 29 88 92 27 69 16
--------------------------------------------------------
The quotient of the items in the array is: 2.72677e-020

C++ || Snippet – How To Find The Highest & Lowest Numbers Contained In An Integer Array

This page will consist of a simple demonstration for finding the highest and lowest numbers contained in an integer array.

REQUIRED KNOWLEDGE FOR THIS SNIPPET

Integer Arrays
For Loops
Setw

Finding the highest/lowest values in an array can be found in one or two ways. The first way would be via a sort, which would render the highest/lowest numbers contained in the array because the values would be sorted in order from highest to lowest. But a sort may not always be practical, especially when you want to keep the array values in the same order that they originally came in.

The second method of finding the highest/lowest values is by traversing through the array, checking each value it contains one by one to determine if the current number which is being compared truly is a target value or not. That method will be displayed below.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

Original array values
10 14 1 94 29 25 7 95 11 17 6 71 100 59
--------------------------------------------------------
These are the highest and lowest array values:
Highest: 100
Lowest: 1