Tag Archives: for loop

C++ || Simple Multi Digit, Decimal & Negative Number Infix To Postfix Conversion & Evaluation

The following is sample code which demonstrates the implementation of a multi digit, decimal, and negative number infix to postfix converter and evaluator using C++.

The program demonstrated on this page has the ability to convert and evaluate a single digit, multi digit, decimal number, and/or negative number infix equation. So for example, if the the infix equation of (19.87 * -2) was entered into the program, the converted postfix expression of 19.87 -2 * would display to the screen, as well as the final evaluated answer of -39.74.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

How To Convert Infix To Postfix
How To Evaluate A Postfix Expression


1. Overview

The program demonstrated on this page is different from a previous implementation of the same type in that this version does not use a Finite State Machine during the conversion process, which simplifies the implemetation!

This program has the following flow of control:

• Get an infix expression from the user
• Convert the infix expression to postfix & isolate all of the math operators, multi digit, decimal, negative and single digit numbers that are found in the postfix expression
• Evaluate the postfix expression by breaking the infix string into tokens found from the above step
• Display the evaluated answer to the screen

The above steps are implemented below.


2. Infix To Posfix Conversion & Evaluation


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.

The following is sample output.

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

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
- || Negative Number
Sample Infix Equation: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: 12/3*9

The Infix expression = 12/3*9
The Postfix expression = 12 3 / 9 *

Calculations:
12/3 = 4
4*9 = 36

Final answer = 36

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

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
- || Negative Number
Sample Infix Equation: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: -150.89996 - 87.56643

The Infix expression = -150.89996 - 87.56643
The Postfix expression = -150.89996 87.56643 -

Calculations:
-150.9-87.5664 = -238.466

Final answer = -238.466

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

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
- || Negative Number
Sample Infix Equation: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

The Infix expression = ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))
The Postfix expression = -4 5 ^ s 1.4 * 23 2 + $ -2.8 - / 1 2 % c 7.28 .1987 * 23 t ^ / *

Calculations:
-4^5 = -1024
sin(-1024) = 0.158533
0.158533*1.4 = 0.221947
23+2 = 25
√25 = 5
5--2.8 = 7.8
0.221947/7.8 = 0.0284547
1%2 = 1
cos(1) = 0.540302
7.28*0.1987 = 1.44654
tan(23) = 1.58815
1.44654^1.58815 = 1.79733
0.540302/1.79733 = 0.300614
0.0284547*0.300614 = 0.00855389

Final answer = 0.00855389

====== RUN 4 ======

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
- || Negative Number
Sample Infix Equation: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: (1987 + 1991) * -1

The Infix expression = (1987 + 1991) * -1
The Postfix expression = 1987 1991 + -1 *

Calculations:
1987+1991 = 3978
3978*-1 = -3978

Final answer = -3978

====== RUN 5 ======

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
- || Negative Number
Sample Infix Equation: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: (1+(2*((3+(4*5))*6)))

The Infix expression = (1+(2*((3+(4*5))*6)))
The Postfix expression = 1 2 3 4 5 * + 6 * * +

Calculations:
4*5 = 20
3+20 = 23
23*6 = 138
2*138 = 276
1+276 = 277

Final answer = 277

C++ || Multi Digit, Decimal & Negative Number Infix To Postfix Conversion & Evaluation


 

Click Here For Updated Version Of Program


The following is sample code which demonstrates the implementation of a multi digit, decimal, and negative number infix to postfix converter and evaluator using a Finite State Machine

REQUIRED KNOWLEDGE FOR THIS PROGRAM

How To Convert Infix To Postfix
How To Evaluate A Postfix Expression
What Is A Finite State Machine?

Using a Finite State Machine, the program demonstrated on this page has the ability to convert and evaluate a single digit, multi digit, decimal number, and/or negative number infix equation. So for example, if the the infix equation of (19.87 * -2) was entered into the program, the converted postfix expression of 19.87 ~2* would display to the screen, as well as the final evaluated answer of -39.74.

NOTE: In this program, negative numbers are represented by the “~” symbol on the postfix string. This is used to differentiate between a negative number and a subtraction symbol.

This program has the following flow of control:

• Get an infix expression from the user
• Convert the infix expression to postfix
• Use a Finite State Machine to isolate all of the math operators, multi digit, decimal, negative and single digit numbers that are found in the postfix expression
• Evaluate the postfix expression using the tokens found from the above step
• Display the evaluated answer to the screen

The above steps are implemented 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.

The following is sample output.

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

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
~ || Negative Number

Sample Infix Equation: ((s(~4^5)*1.4)/($(23+2)-~2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: 12/3*9

The Infix expression = 12/3*9
The Postfix expression = 12 3 /9*

Calculations:
12/3 = 4
4*9 = 36

Final answer = 36

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

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
~ || Negative Number

Sample Infix Equation: ((s(~4^5)*1.4)/($(23+2)-~2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: -150.89996 - 87.56643

The Infix expression = -150.89996 - 87.56643
The Postfix expression = ~150.89996 87.56643-

Calculations:
-150.9-87.5664 = -238.466

Final answer = -238.466

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

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
~ || Negative Number

Sample Infix Equation: ((s(~4^5)*1.4)/($(23+2)-~2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: ((s(~4^5)*1.4)/($(23+2)-~2.8))*(c(1%2)/(7.28*.1987)^(t23))

The Infix expression = ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))
The Postfix expression = ~4 5^ s1.4* 23 2+ $~2.8-/ 1 2% c7.28 .1987* 23t^/*

Calculations:
-4^5 = -1024
sin(-1024) = 0.158533
0.158533*1.4 = 0.221947
23+2 = 25
√25 = 5
5--2.8 = 7.8
0.221947/7.8 = 0.0284547
1%2 = 1
cos(1) = 0.540302
7.28*0.1987 = 1.44654
tan(23) = 1.58815
1.44654^1.58815 = 1.79733
0.540302/1.79733 = 0.300614
0.0284547*0.300614 = 0.00855389

Final answer = 0.00855389

====== RUN 4 ======

==== Infix To Postfix Conversion & Evaluation ====

Math Operators:
+ || Addition
- || Subtraction
* || Multiplication
/ || Division
% || Modulus
^ || Power
$ || Square Root
s || Sine
c || Cosine
t || Tangent
- || Negative Number
Sample Infix Equation: ((s(-4^5)*1.4)/($(23+2)--2.8))*(c(1%2)/(7.28*.1987)^(t23))

Please enter an Infix expression: (1987 + 1991) * -1

The Infix expression = (1987 + 1991) * -1
The Postfix expression = 1987 1991+ ~1*

Calculations:
1987+1991 = 3978
3978*-1 = -3978

Final answer = -3978

Python || Find The Average Using A List – Omit Highest And Lowest Scores

This page will consist of a program which calculates the average of a specific amount of numbers using a list.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Lists
For Loops
Arithmetic Operators
Basic Math - How To Find The Average

The following program is fairly simple, and was used to introduce the list concept. This program prompts the user to enter the total amount of numbers they wish to find the average for, then displays the answer to the screen. Using a sort, this program also has the ability to find the average of a list of numbers, omitting the highest and lowest valued items.


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.

The following is sample output.

How many items do you want to place into the list?: 5

Enter item #1: 7
Enter item #2: 7
Enter item #3: 4
Enter item #4: 8
Enter item #5: 7

The current items inside the list are:
Item #1: 7
Item #2: 7
Item #3: 4
Item #4: 8
Item #5: 7

The average of the 5 numbers is 6.60

The average adjusted score omitting the highest and lowest result is 7.00

Python || Snippet – How To Input Numbers Into A List & Display Its Contents Back To User

This snippet demonstrates how to place numbers into a list. It also shows how to display the contents of the list back to the user via stdout.

REQUIRED KNOWLEDGE FOR THIS SNIPPET

Python Lists
For Loops


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

How many items do you want to place into the list?: 3

Enter item #1: 6
Enter item #2: 4
Enter item #3: 2008

The current items inside the list are:
Item #1: 6
Item #2: 4
Item #3: 2008

Java || 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
The "Random" Class
For Loops
Assignment Operators - Simple Math Operations
Custom Setw/Setfill In Java

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

Welcome to My Programming Notes' Java Program.

Original array values:
22 26 41 89 35 90 15 99 85 5 95 86
--------------------------------------------------
The sum of the items in the array is: 688

===== 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

Welcome to My Programming Notes' Java Program.

Original array values:
99 92 91 26 1 52 98 62 51 22 64 65
--------------------------------------------------
The difference of the items in the array is: -723

===== 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

Welcome to My Programming Notes' Java Program.

Original array values:
95 63 32 19 93 83 71 35 32 37 66 95
--------------------------------------------------
The product of the items in the array is: 494770176

===== 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

Welcome to My Programming Notes' Java Program.

Original array values:
28 85 90 52 1 64 93 85 4 22 4 28
--------------------------------------------------
The quotient of the items in the array is: 1.8005063061510687E-17

Java || 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
Custom Setw/Setfill In Java

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 obviously 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, literally 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

Welcome to My Programming Notes' Java Program.

Original array values:
36 35 46 86 86 58 44 38 79 52 27 78 65 79
------------------------------------------------------------
These are the highest and lowest array values:
Highest: 86
Lowest: 27

Java || Snippet – How To Input Numbers Into An Integer Array & Display Its Contents Back To User

This snippet demonstrates how to place numbers into an integer array. It also shows how to display the contents of the array back to the user via stdout.

REQUIRED KNOWLEDGE FOR THIS SNIPPET

Integer Arrays
For Loops
Final Variables


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.

How many items do you want to place into the array?: 5

Enter item #1: 12
Enter item #2: 43
Enter item #3: 5
Enter item #4: 643
Enter item #5: 2321

The current items inside the array are:
Item #1: 12
Item #2: 43
Item #3: 5
Item #4: 643
Item #5: 2321

C++ || Convert Numbers To Words Using A Switch Statement

This program demonstrates more practice using arrays and switch statements.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Integer Arrays
Cin.get
Isdigit
For loops
While Loops
Switch Statements - How To Use

Using “cin.get(),” this program first asks the user to enter in a number (one at a time) that they wish to translate into words. If the text which was entered into the system is a number, the program will save the user input into an integer array. If the text is not a number, the input is discarded. After integer data is obtained, a for loop is used to traverse the integer array, passing the data to a switch statement, which translates the number to text.

This program is very simple, so it does not have the ability to display any number prefixes. As a result, if the number “1858” was entered into the system, the program would output the converted text: “One Eight Five Eight.”


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 four separate times to display different output

======= Run #1 =======

Enter number: 77331

Seven Seven Three Three One

======= Run #2 =======

Enter number: 234-43-1275

Two Three Four Four Three One Two Seven Five

======= Run #3 =======

Enter number: 1(800) 123-5678

One Eight Zero Zero One Two Three Five Six Seven Eight

======= Run #4 =======

Enter number: This 34 Is 24 A 5 Number 28

Three Four Two Four Five Two Eight

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

The following is a palindromic number checking program, which demonstrates more use of character array’s, Strlen, & Strcmp.

Want sample code for a palindrome checker which works for numbers and words? Click here.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Character Arrays
How to reverse a character array
Palindrome - What is it?
Strlen
Strcpy
Strcmp
Isdigit
Atoi - Convert a char array to a number
Do/While Loops
For Loops

This program first asks the user to enter a number that they wish to compare for similarity. If the number which was entered into the system 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. Using a for loop, this program also demonstrates how to reverse a character array, aswell as demonstrates how to determine if the text contained in a character array is a number or not.

This program will repeatedly prompt the user for input until an “exit code” is obtained. The designated exit code in this program is the number 0 (zero). So the program will not stop asking for user input until the number 0 is entered into the program.


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 a positive integer, or (0) to exit: L33T

*** error: "L33T" is not an integer

Enter a positive integer, or (0) to exit: -728

*** error: -728 must be greater than zero

Enter a positive integer, or (0) to exit: 1858

1858 is NOT a Palindrome!

Enter a positive integer, or (0) to exit: 7337

7337 is a Palindrome..

Enter a positive integer, or (0) to exit: 0

Exiting program...

BYE!

Java || Searching An Integer Array For A Target Value

Here is another actual homework assignment which was presented in an intro to programming class which was used to introduce more practice using integer arrays.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Integer Arrays
For Loops
Methods (A.K.A "Functions") - What Are They?
Final Variables
If/Else Statements

This is a small and simple program which demonstrates how to search for a target value which is stored in an integer array. This program first prompts the user to enter five values into an int array. After the user enters all the values into the system, it then displays a prompt asking the user for a search value. Once it has a search value, the program searches through the array looking for the target value; and wherever the value is found, the program display’s the current array index in which that target value is located. After it displays all the locations where the target value resides, it display’s the total number of occurrences the search value was found within the array.


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 3 separate times to display the different outputs its able to produce

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

Welcome to My Programming Notes' Java Program.

Please enter 5 integer values:
#1: 25
#2: 25
#3: 25
#4: 25
#5: 25
Please enter a search value: 25

25 was found at array index #0
25 was found at array index #1
25 was found at array index #2
25 was found at array index #3
25 was found at array index #4

The total occurrences of value 25 within the array is: 5

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

Welcome to My Programming Notes' Java Program.

Please enter 5 integer values:
#1: 8
#2: 19
#3: 97
#4: 56
#5: 8
Please enter a search value: 8

8 was found at array index #0
8 was found at array index #4

The total occurrences of value 8 within the array is: 2

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

Welcome to My Programming Notes' Java Program.

Please enter 5 integer values:
#1: 78
#2: 65
#3: 3
#4: 45
#5: 89
Please enter a search value: 12

The total occurrences of value 12 within the array is: 0

Java || Find The Prime, Perfect & All Known Divisors Of A Number Using A For, While & Do/While Loop

This program was designed to better understand how to use different loops which are available in Java, as well as more practice using objects with classes.

This program first asks the user to enter a non negative number. After it obtains a non negative integer from the user, the program determines if the user obtained number is a prime number or not, aswell as determining if the user obtained number is a perfect number or not. After it obtains its results, the program will display to the screen if the user inputted number is prime/perfect number or not. The program will also display a list of all the possible divisors of the user obtained number via stdout.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Class Objects - How TO Use
Constructors - What Are They?
Do/While Loop
While Loop
For Loop
Modulus
Basic Math - Prime Numbers
Basic Math - Perfect Numbers
Basic Math - Divisors


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 a number: 41

Input number: 41
41 is a prime number.
41 is not a perfect number.
Divisors of 41 are: 1, and 41

Do you want to input another number?(Y/N): y
------------------------------------------------------------
Enter a number: 496

Input number: 496
496 is not a prime number.
496 is a perfect number.
Divisors of 496 are: 1, 2, 4, 8, 16, 31, 62, 124, 248, and 496

Do you want to input another number?(Y/N): y
------------------------------------------------------------
Enter a number: 1858

Input number: 1858
1858 is not a prime number.
1858 is not a perfect number.
Divisors of 1858 are: 1, 2, 929, and 1858

Do you want to input another number?(Y/N): y
------------------------------------------------------------
Enter a number: -9

Sorry, but the number entered is less than the allowable limit.
Please try again.....

Enter an number: 12

Input number: 12
12 is not a prime number.
12 is not a perfect number.
Divisors of 12 are: 1, 2, 3, 4, 6, and 12

Do you want to input another number?(Y/N): n
------------------------------------------------------------
BYE!

Java || Find The Average Using an Array – Omit Highest And Lowest Scores

This page will consist of two programs which calculates the average of a specific amount of numbers using an array.

REQUIRED KNOWLEDGE FOR BOTH PROGRAMS

Double Data Type
Final Variables
Arrays
For Loops
Assignment Operators
Basic Math - How To Find The Average

====== FIND THE AVERAGE USING AN ARRAY ======

The first program is fairly simple, and it was used to introduce the array concept. The program prompts the user to enter the total amount of numbers they want to find the average for, then the program displays the answer to them via stdout.


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

ARRAY
Notice the array declaration on line #13. The type of array being used in this program is a dynamic array, which has the ability to store up to 100 integer elements in the array. You can change the number of elements its able to store to a higher or lower number if you wish.

FOR LOOP
Lines 27-32 contains a for loop, which is used to actually store the data inside of the array. Without some type of loop, it is virtually impossible for the user to input data into the array; that is, unless you want to add 100 different println statements into your code asking the user to input data. Line 31 uses the assignment operator “+=” which gives us a running total of the data that is being inputted into the array. Note the loop only stores as many elements as the user so desires, so if the user only wants to input 3 numbers into the array, the for loop will only execute 3 times.

Once compiled, you should get this as your output:

Welcome to My Programming Notes' Java Program.

How many numbers do you want to find the average for?: 4
Enter value #1: 21
Enter value #2: 24
Enter value #3: 19
Enter value #4: 17
The average of the 4 numbers is 20.25

====== FIND THE AVERAGE – OMIT HIGHEST AND LOWEST SCORES ======

The second program is really practical in a real world setting. We were asked to create a program for a fictional competition which had 6 judges. The 6 judges each gave a score of the performance for a competitor in a competition, (i.e a score of 1-10), and we were asked to find the average of those scores, omitting the highest/lowest results. The program was to store the scores into an array, display the scores back to the user via stdout, display the highest and lowest scores among the 6 obtained, display the average of the 6 scores, and finally display the average adjusted scores omitting the highest and lowest result.


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

FINAL
A final variable was declared and used to initialize the array (line 7). This was used to initialize the size of the array.

FOR LOOPS
Once again loops were used to traverse the array, as noted on lines 24, 51 and 73. The final variable was also used within the for loops, making it easier to modify the code if its necessary to reduce or increase the number of available judges.

HIGHEST/LOWEST SCORES
This is noted on lines 35-45, and it is really simple to understand the process once you see the code.

OMITTING HIGHEST/LOWEST SCORE
Lines 73-81 highlights this process. The loop basically traverses the array, skipping over the highest/lowest elements.

Once compiled, you should get this as your output

Welcome to My Programming Notes' Java Program.

Judges, enter one score each for
the current competitor: 123 453 -789 2 23345 987

These are the scores from the 6 judges:
The score for judge #1 is: 123.0
The score for judge #2 is: 453.0
The score for judge #3 is: -789.0
The score for judge #4 is: 2.0
The score for judge #5 is: 23345.0
The score for judge #6 is: 987.0

These are the highest and lowest scores:
Highest: 23345.0
Lowest: -789.0
The average score is: 4020.1666666666665
The average adjusted score omitting the highest and lowest result is: 391.25

Java || Snippet – Custom Setw & Setfill Sample Code For Java

This page will consist of a brief implementation of setw/ios::width and setfill/ios::fill in Java.

If you are a C++ programmer, no doubt you have used the setw and setfill commands many times. It makes formatting output like this very clean and simple (see below).

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

But to my amazement, I could not find a very suitable replacement for the setw/setfill functions in Java. Using simple for loops, the methods provided on this page has the ability to mimic both functions which are available in C++.

Included in the sample code are the following:

== SETW ==

(1) right - Justifies string data of size "width," filling the width to the start of the string with whitespace

(2) left - Justifies string data of size "width," filling the width to the end of the string with whitespace

== SETFILL ==

(3) right - Justifies string data of size "width," filling the width to the start of the string with a filler character

(4) left - Justifies string data of size "width," filling the width to the end of the string with a filler character

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

Also, you must understand minimal object oriented programming to use this code!

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

===== DEMONSTRATION HOW TO USE =====

Use of the above snippet is similar to its C++ counterpart. Here is a sample program demonstrating its use.

Once compiled, you should get this as your output

Java || Count The Total Number Of Characters, Vowels, & UPPERCASE Letters Contained In A Sentence Using A ‘For Loop’

Here is a simple program, which demonstrates more practice using the input/output mechanisms which are available in Java.

This program will prompt the user to enter a sentence, then upon entering an “exit code,” will display the total number of uppercase letters, vowels and characters which are contained within that sentence. This program is very similar to an earlier project, this time, utilizing a for loop, strings, and user defined methods.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

How To Get String Input
If/Else Statements
For Loops
Methods (A.K.A "Functions") - What Are They?


QUICK NOTES:
The highlighted portions are areas of interest.

Notice line 22 contains the for loop declaration. The loop will continually loop thru the string, and will not stop doing so until it reaches an exit character, and the defined exit character in this program is a period (“.”). So, the program will not stop looping thru the string until it reaches a period.

Once compiling the above code, you should receive this as your output

Welcome to My Programming Notes' Java Program.

Enter a sentence, ending with a period:
My Programming Notes Is An Awesome Site.

Total number of upper case letters:........7
Total number of vowels:....................14
Total number of characters:................33