Class Exercises
Fibonacci Sequence
#include <iostream>
using namespace std;
int main()
{
int x1 = 1 , x2 = 1 ;
int temp ;
cout << x1 << endl ;
cout << x2 << endl ;
for( int i1=0 ; i1<10 ; i1++ )
{
cout << ( x1 + x2 ) << endl ;
temp = x1 + x2 ;
x1 = x2 ;
x2 = temp ;
}
return 0;
}
Printing digits of a number
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int number = 675 ;
int num1 = number ;
int powers = 0 ;
int divisor ;
while ( num1 > 0 )
{
num1 = num1 /10 ;
powers++ ;
} //while
powers-- ;
divisor = pow( 10, powers ) ;
num1 = number ;
while ( num1 > 0 )
{
cout << num1 / divisor << endl ;
num1 = num1 % divisor ;
divisor = divisor/10 ;
} //while
return(1) ;
}
Finding the prime numbers by dividing by primes numbers less than the number.
#include <iostream>
using namespace std ;
int main()
{
int array1[100] = {0} ;
int p1 = 2+1 ;
int index=1 ;
array1[0] = 2 ;
while ( p1 < 100 )
{
bool isPrime = true ;
for( int i1=0 ; i1<index ; i1++ )
{
if( p1 % array1[i1] == 0 )
isPrime = false ;
} //for
if( isPrime )
{
array1[index] = p1 ;
index++ ;
}
p1++ ;
} //while
for( int i1=0 ; i1 < index ; i1++ )
{
cout << array1[i1] << endl ;
}
return(0) ;
}
Reverse an Array
#include <iostream>
using namespace std ;
void reverseArray( int arrayOfNumbers[] , int size )
{
int i1 = 0 , j1 = size - 1 ;
for( ; i1 < j1 ; i1++ , j1--)
{
int temp ;
temp = arrayOfNumbers[i1] ;
arrayOfNumbers[i1] = arrayOfNumbers[j1] ;
arrayOfNumbers[j1] = temp ;
} //for
for(int i2=0 ; i2 < size ; i2++)
{
cout << arrayOfNumbers[i2] << endl ;
}
}
int main()
{
int arr5[] = {1 , 2 , 3, 4 , 5 };
reverseArray( arr5 , 5 ) ;
return(0) ;
}
Binary Gap
#include <iostream>
using namespace std ;
//-----------------------------------------------------------
int main()
{
int number = 9 ;
int maxGap = 0 ;
int currentGap = 0 ;
//remove the right hand zeroes if they exist .
//The left hand side will always have a 1
for( ; number > 0 ; number /= 2 )
{
if( number % 2 != 0 )
break ;
}
cout << number << endl ;
for( ; number > 0 ; number /= 2 )
{
cout << number << endl ;
if( number % 2 == 0 )
{
cout << " modulus equals 0 " << endl ;
currentGap++ ;
}
else
{
cout << " modulus does not equal 0 " << endl ;
if( currentGap > maxGap )
maxGap = currentGap ;
currentGap = 0 ;
}
}
cout << endl << endl << maxGap << endl ;
return( 0 ) ;
}
//-----------------------------------------------------------
MaxProfit Codility Problem
https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_profit/
#include <iostream>
using namespace std;
//--------------------------------------------
int solution(int array1[], int size)
{
int minPrice = array1[0] ;
int maxProfit = 0 ;
if( size < 1 )
return 0 ;
for( int i1=1; i1<size ; i1++ )
{
if( array1[i1] <= minPrice ) //reset
{
minPrice = array1[i1] ;
}
else
{
if( ( array1[i1] - minPrice ) > maxProfit )
maxProfit = array1[i1] - minPrice ;
}
} //for
return ( maxProfit ) ;
}
//--------------------------------------------
//Works but not an efficient solution
int solution1(int array1[], int size)
{
int minPrice = array1[0] ;
int maxProfit = 0 ;
if( size < 1 )
return 0 ;
for( int i1=0; i1<size ; i1++ )
{
for( int j1=i1+1; j1<size ; j1++ )
{
if( (array1[j1] - array1[i1] ) > maxProfit )
maxProfit = array1[j1] - array1[i1] ;
} //for
} //for
return ( maxProfit ) ;
}
//--------------------------------------------
int main()
{
const int SIZE = 10 ;
int arr1[] = { 71, 11 ,23, 66, 67 } ;
int result = solution1( arr1 , 5 ) ;
cout << "result:" << result << endl ;
return (1) ;
}
//--------------------------------------------
Longest substring without repeating characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
#include <iostream>
using namespace std;
int main()
{
//const int SIZE = 8 ;
//char arr1[] = { 'a' , 'b' , 'c', 'a' , 'b', 'c' , 'b' , 'b' } ;
const int SIZE = 5 ;
//char arr1[] = { 'a' , 'b' , 'b', 'd' , 'a' } ;
char arr1[] = { 'b' , 'b' , 'b', 'd' , 'b' } ;
//char arr1[] = { 'a' , 'b' , 'b', 'd' , 'b' } ;
//char arr1[] = { 'a' , 'b' , 'b', 'd' , 'b' } ;
bool myset[256] = {false} ;
int maxCount = 0 ;
int currentCount = 0 ;
int start = 0 ;
for( int i1=0 ; i1 < SIZE ; i1++ )
{
// found a duplicate character
if( myset[arr1[i1]] == true )
{
//check max count
if ( ( start - i1 ) > maxCount )
maxCount = start - i1 ;
//Reset start position and take out elements before the
//last duplicate character
int j1=0 ;
for( j1=start ; j1<i1 ; j1++ )
{
if( arr1[j1] == arr1[i1] )
{
start = j1+1 ;
break ;
}
else
myset[ arr1[j1] ] = false ;
} //for
}
else
{
myset[ arr1[i1] ] = true ;
} //if
} //for
cout << "Start:" << start << ":" << endl ;
if ( ( SIZE - start ) > maxCount )
maxCount = SIZE - start ;
cout << maxCount << " " << endl ;
return (1) ;
}
Exercise 1 ( Classes )
First Part
Create a class called "Person" with 3 variables "First Name" , "Last Name" and age. The variables and member functions are all public.
Create a function called "print()" that returns void and prints all the 3 variables values.
You may write all the code in 1 file if you want.
In your main function create an object of the class "Person" using the regular method and then create a person object using the new method. Ex:
Person person1 ;
Person* person2 = new Person() ;
Assign values to the 3 variables and call the function print to print the values out.
Second Part
Move the class declaration to a header file. Write the include guard in the header file. Write the declaration for a constructor and a destructor.
The header file should not have the implementation for the methods.
Make the data variable private and write a constructor
that will take 3 arguments and assign the values to the variables. Inside the constructor write a statements like:
cout << "Inside the constructor." << endl ;
Modify the creation of your objects from Part 1 to use the new constructors to set the variables .
Inside the destructor place the statement:
cout << "inside the destructor" << endl ;
Your program should have 3 files ( Person.h Person.cpp and main.cpp ) ;
The creation of your objects should be in the file main.cpp .
Create an array of Person objects in 2 ways.
One is the regular way such as:
Person personArray[3] ;
and the other is dynamically using new such as:
Person* personArray[3] ;
In both the cases print out the values of the variables in each object in the array.