code samples
This section contains JavaScript, PHP and C++ code samples written by me, web site author David Glatzl. The purpose of this code sample portfolio is to demonstrate my working knowledge of these programming languages.
JavaScript
Accordion Menu
/* Start JavaScript */
/* End JavaScript */
Added Button Functionality
By using this website you have read and agree to our Terms of Use
/* Start JavaScript */
/* Start JavaScript */
/* End JavaScript */
PHP
An associative array with a while loop and for/each loop.
Project: bulleted list
"General:", "\n . Second rank\n . Glock hand gun and M27 rifle\n . Silver collar insignia \n\n\n" => "First Lieutenant:", "\n . Third rank\n . M27 rifle and Remington shotgun\n . Embroidered green on red badge\n\n\n" => "Sergeant:", "\n . Fourth rank\n . M27 rifle and Marine sword\n . Embroidered gold on red badge\n\n\n" => "Corporal:");
$i = 0;
while( $i < sizeof( $military ) )
$i++;
foreach ( $military as $attribute => $ninja ) {
echo "Attributes of the $ninja \n $attribute";
}
?>
An associative array with two - for/each loops.
Project: categorized information
array(' Role' => 'Leader', ' Weapon' => 'Beretta hand gun', ' Rank' => 'Four star' . "\n\n\n"),
'FIRST LIEUTENANT' => array(' Role' => 'Second in command', ' Weapon' => 'Glock hand gun and M27 rifle', ' Rank' => 'Silver collar Insignia' . "\n\n\n"),
'SERGEANT' => array(' Role' => 'Third in command', ' Weapon' => 'M27 rifle and Remington shotgun', ' Rank' => 'Embroidered green on red badge' . "\n\n\n"),
'CORPORAL' => array(' Role' => 'Fourth in command', ' Weapon' => 'M27 rifle and a Marine sword', ' Rank' => 'Embroidered gold on red badge' . "\n\n\n")
);
foreach ( $military as $ninja => $attribute ) {
echo "$ninja-\n\n";
foreach ( $attribute as $label => $attr ) {
echo "$label: $attr\n";
}
}
?>
Numeric Array
Project: Larger Array with Algebra
Function
Project: This is a function that compares two integers and prints out the greater of the two. Below, there are three separate pairs of integers that are compared by this function.
$num2) {
return $num1;
} else if($num2 > $num1) {
return $num2;
}
}
var_dump( integer_comparison( 2219, 4196 ) );
var_dump( integer_comparison( 23, 22) );
var_dump( integer_comparison(743, 734 ) );
?>
c++
Bubble Sort Algorithm
Project: This C++ algorithm can take any group of ten numbers and put them in descending order from the highest number to the lowest number. On line fifteen in the program below, you can see a group of ten numbers in any random order.
When you look at the output of this program you can see the group of numbers have been put in decsending order starting from the highest number to the lowest number.
When you look at the output of this program you can see the group of numbers have been put in decsending order starting from the highest number to the lowest number.
// Bubblesort Algorithm.cpp
// Author David Glatzl
#include
using namespace std;
const int MAXSIZE = 10;
void bubbleSort(int arr[], int size);
void swap(int& x, int& y);
int main()
{
int nums[] = { 1, 7, 5, 3, 15, 11, 13, 17, 21, 19 };
int k;
cout << "BEFORE SORT: ";
for (k = 0; k < MAXSIZE; k++)
cout << nums[k] << " ";
bubbleSort(nums, MAXSIZE);
cout << endl << endl;
cout << "AFTER SORT: ";
for (k = 0; k < MAXSIZE; k++)
cout << nums[k] << " ";
cout << endl << endl << endl;
system("PAUSE");
return 0;
} // end main()
void bubbleSort(int arr[], int size)
{
int last = size - 2;
int isChanged = 1;
while (last >= 0 && isChanged)
{
isChanged = 0;
for (int k = 0; k <= last; k++)
if (arr[k] < arr[k + 1])
{
swap(arr[k], arr[k + 1]);
isChanged = 1;
}
last--;
}
}// end bubbleSort()
void swap(int& x, int& y)
{
int temp;
temp = x;
x = y;
y = temp;
}// end swap()
Array Sort User
Project: This program sorts an array of numbers in ascending order from the lowest number to the highest number.
Then, after the numbers are sorted, the program will ask the user to enter any number that is within the array.
After that, the index number of the element (the number that the user chose) will output or be displayed in the console window.
Then, after the numbers are sorted, the program will ask the user to enter any number that is within the array.
After that, the index number of the element (the number that the user chose) will output or be displayed in the console window.
// File: ArrayUser.cpp
// a pgm that sorts an array and asks the user to find an element by calling the index number.
// Author: David Glatzl
#include
#include
using namespace std;
const int MAXSIZE = 10;
void sortArr( int arg[], int size );
void swap( int& x, int& y );
int findIndex( int x[] );
int main()
{
int num[10] = { 45, 9, 23, 100, 90, 6, 86, 17, 3, 5 };
int i;
int index;
cout << endl;
cout << "The elements in the array before they are sorted: ";
for( i = 0; i < MAXSIZE; ++i )
cout << num[i] << " ";
sortArr( num, MAXSIZE );
cout << endl << endl;
cout << "After the elements are sorted: ";
for( i = 0; i < MAXSIZE; ++i )
cout << num[i] << " ";
cout << endl << endl;
for( i = 0; i < 1; ++i )
{
cout << "Enter any number within the array: ";
cin >> num[i];
}
index = findIndex( num );
cout << endl;
cout << "The index for the element you entered is: " << num[index] << endl;
cout << endl;
return 0;
}
void sortArr( int arg[], int size )
{
int last = size - 1;
int isChanged = 1;
while (last >= 0 && isChanged)
{
isChanged = 0;
for(int i = 0; i <= last; ++i)
if(arg[i] < arg[i-1])
{
swap(arg[i], arg[i-1]);
isChanged = 1;
}
last--;
}
}
void swap(int& x, int& y)
{
int temp;
temp = x;
x = y;
y = temp;
}
int findIndex( int x[] )
{
int i = 0;
for( i = 0; i < 10; ++i )
{
if( x[i] == 3 )
{
x[i] = 0;
break;
}
if( x[i] == 5 )
{
x[i] = 1;
break;
}
if( x[i] == 6 )
{
x[i] = 2;
break;
}
if( x[i] == 9 )
{
x[i] = 3;
break;
}
if( x[i] == 17 )
{
x[i] = 4;
break;
}
if( x[i] == 23 )
{
x[i] = 5;
break;
}
if( x[i] == 45 )
{
x[i] = 6;
break;
}
if( x[i] == 86 )
{
x[i] = 7;
break;
}
if( x[i] == 90 )
{
x[i] = 8;
break;
}
if( x[i] == 100 )
{
x[i] = 9;
break;
}
}
return 0;
}