//============================================================================
// Name : test2.cpp
// Author : Emily
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include
using namespace std;
#include
double sqtest(double a);
void drwbx(int size);
#include
void convertToUppercase( char *);
void printCharacters ( const char *);
void mystery1( char *, const char * );
#include
#include
#include
void bubble( int [], const int, int (*)( int, int ) );
int ascending( int, int );
int descending( int, int );
void Time::setTime( int h, int m, int s);
int main ()
{
int integer1, integer2, sum, leg1, leg2, num1, num2, bxht, a, *aPtr;
double hypotenuse2;
cout << "This is a simple program.\n First, it will add integers inputed from the user . . .\n";
cout << "Enter first integer here:\n";
cin >> integer1;
cout << "Enter second integer here: \n";
cin >> integer2;
sum = integer1 + integer2;
cout << "Sum is " << sum;
cout << ".\n\n\n";
cout << "Next, use user inputs again to simulate calculating\n THE HYPOTENUSE OF A TRIANGLE\n"; // this section of the program requires the incorporation of the math functions part of the library
cout << "Enter the length of leg one here: ";
cin >> leg1;
cout << "Enter the length of leg two here: ";
cin >> leg2;
hypotenuse2 = sqtest((double)(leg1*leg1) + (leg2*leg2));
cout << "The length of the hypotenuse squared is " << hypotenuse2;
cout << ". \n(The square root calculation requires use of a math function library and I don't know that yet.)\n\n\n";
cout << "Next, use a boolean tool to MAKE DECISIONS\n\n";
cout << "The user will be prompted to enter two numbers.\nThis program will indicate their relationship to each other.\n\n";
cout << "Enter the first number here: ";
cin >> num1;
cout << "Enter the second number here: ";
cin >> num2;
if ( num1 == num2 )
cout << num1 << " is equal to " << num2 << ".\n";
if ( num1 != num2 )
cout << num1 << " is not equal to " << num2 << ".\n";
if ( num1 < num2 )
cout << num1 << " is less than " << num2 << ".\n";
if ( num1 > num2 )
cout << num1 << "is greater than " << num2 << ".\n";
if ( num1 <= num2 )
cout << num1 << " is less than or equal to " << num2 << ".\n";
if ( num1 >= num2 )
cout << num1 << " is greater than or equal to " << num2 << ".\n";
cout << "\n\n\nNext, draw a picture of a rectangle using the user's inputs of dimension . . .\n";
cout << "(For now, the user will control the vertical dimension only)\n\n";
cout << "Enter a number between one and ten for the desired height of the box: ";
cin >> bxht;
drwbx(bxht);
cout << "Try it again and enter another dimension:";
cin >> bxht;
drwbx(bxht);
cout << "Try it again and enter another dimension:";
cin >> bxht;
drwbx(bxht);
cout << "This is the start of the POINTERS practice section.";
a = 7;
aPtr = &a;
cout << "The address of a is " << &a << "\nThe value of aPtr is " << aPtr;
cout << "\n\nThe value of a is " << "\nThe value of *Ptr is " << *aPtr;
cout << "\n\nShowing that * and & are inverses of " << "each other. \n*&aPtr = " << *&aPtr << endl;
cout << "\n\nHere, the program will convert lowercase letters into uppercase letters using a non-constant pointer to non-constant data";
char string[] = "characters and $32.98";
cout << "\nThe string before conversion is: " << string;
convertToUppercase( string );
cout << "\nThe string after conversion is: " << string <
cout << "\n\n\nNext, the program will print a string1 one character at a time using a non-constant pointer to constant data\n";
char string1[] = "print characters of a string1";
cout << "the string1 is:\n";
printCharacters( string1 );
cout << "\n\n\nHere, the program will do something . . . the purpose of the exercise is to see what exactly";
cout << "\nThe result will be communicated at the conclusion of this exercise.";
char string2[ 80 ], string3[ 80 ];
cout << "\nEnter two strings: ";
cin >> string2 >> string3;
mystery1( string2, string3 );
cout << string2;
cout << "\nThe result of this section of program is to combine two strings together: one right after the other.";
cout << "\n(Strings may be separated by a space or by hitting the return key.)";
cout << "\n\n\nHere the program will sort number inputs in ascending and descending orders.";
const int arraySize = 10;
int order,
counter,
b[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
cout << "enter 1 to sort in ascending order,\n" << "enter 2 to sort in descending order: ";
cin >> order;
cout << "\nData items in original order\n";
for ( counter = 0; counter < arraySize; counter++ )
cout << setw( 4 ) << b[ counter ];
if ( order == 1 ) {
bubble( b, arraySize, ascending );
cout << "\nData items in ascending order\n";
}
else {
bubble( b, arraySize, descending );
cout << "\nData items in descending order\n";
}
for ( counter = 0; counter < arraySize; counter++ )
cout << setw( 4 ) << b[ counter ];
cout << endl;
cout << "Next the program will convert military to standard time.";
cout << "\nthe initial military time is ";
t.printStandard();
t.setTime( 13, 27, 6 );
cout << "\n\nMilitary time after setTime is ";
t.printMilitary();
cout << "\nStandard time after setTime is ";
t.printStandard();
t.setTime( 99, 99, 99 );
cout << "\n\nAfter attempting invalid settings:" << "\nMilitary time: ";
t.printMilitary();
cout << "\nStandard time: ";
t.printStandard();
return 0;
}
double sqtest(double a){
double b = sqrt(a);
drwbx ((int) b);
return b;
}
void drwbx(int size){
cout << "***********\n";
for(int i=0;i
cout << "* *\n";
}
cout << "***********\n";
}
void convertToUppercase( char *sPtr )
{
while ( *sPtr != '\0' ) {
if ( *sPtr >= 'a' && *sPtr <= 'z' )
*sPtr = toupper( *sPtr ); //convert to uppercase
++sPtr; //move sPtr to next character
}
}
void printCharacters( const char *dPtr )
{
for ( ; *dPtr != '\0'; dPtr++ )
cout << *dPtr;
}
void mystery1( char *s2, const char *s3 )
{
while ( *s2 != '\0' )
++s2;
for( ; *s2 = *s3; s2++, s3++ )
;
}
void bubble( int work[], const int size,
int (*compare) ( int, int ) )
{
void swap( int *, int * );
for ( int pass = 1; pass < size; pass ++ )
for ( int count = 0; count < size - 1; count++ )
if ( (*compare)( work[ count ], work[ count + 1 ] ) )
swap( &work[ count ], &work[ count + 1] );
}
void swap( int *element1Ptr, int *element2Ptr )
{
int temp;
temp = *element1Ptr;
*element1Ptr;
*element2Ptr = temp;
}
int ascending( int a, int b )
{
return b < a;
}
int descending( int a, int b )
{
return b > a;
}
class Time {
public:
Time();
void setTime( int, int, int );
void printMilitary();
void printStandard();
private:
int hour;
int minute;
int second;
};
time.h::Time() { hour = minute = second = 0; }
void Time::setTime( int h, int m, int s )
{
hour = (h >= 0 && h < 24 ) ? h : 0;
second = (s >= 0 && s < 60 ) ? s : 0;
}
void Time ::printMilitary()
{
cout << ( hour < 10 ? "0" : "" ) << hour
<< ":" << ( minute < 10 ? "0" : "" ) << minute;
}
void Time::printStandard()
{
cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
<< ":" << (minute < 10 ? "0" : "" ) << minute
<< ":" << ( second < 10 ? "0" : "" ) << second
<< ( hour < 12 ? " AM" : " PM" );
}