You will create 3 files:
"main.cpp" , "printme.h" and "printme.cpp"
In "printme.cpp" include the header files "iostream" and "printme.h" . Also include the line:
using namespace std ;
Write a function
void printme()
that uses "cout" to print "Hello World" to the screen.
The file "printme.h" will contain the declaration of this function "printme" .
In the file "main.cpp" include this file "printme.h" and call the function "printme" . Compile the 2 files "main.cpp" and "printme.cpp" to produce a single executable.
You may compile using the "make" utility or the command:
g++ -o hello.exe main.cpp printme.cpp
After the execution command run the command:
whoami
on the console. Take the source code for the 3 files and paste in a word document.
Take the snapshot of the compilation, execution and "whoami" output and paste in the
same word document. Submit the word document to canvas.
Run the executable to make sure the programming is running properly.
The "float" data type can be used to store numbers with decimal points. Write a program "main.cpp" that prompts the user
for a number ( representing number of miles ) and outputs the equivalent kilometers.
Sample output:
[amittal@hills hw2]$ ./a.out
Please enter the number of miles to convert:3.5
3.5 miles are 5.6 km
[amittal@hills hw2]$
Write a single file program that prints the prime numbers between 2 to 100 .
There are 2 parts that you will write. You can write the 2 parts in a single main
function if you want.
The 1st part prints the prime numbers using 2 nested for loops and the modulus operator and works in the traditional manner. To test if a number x1 is prime try to see if any number starting from 2 and 1 less than x1 divides the number exactly and if it does then it is not a prime number.
The 2nd part uses a different technique( Sieve of Eratosthenes) to print the prime numbers. You can also use 2 nested for loops to write part 2.
Initialize the array to be filled with zeroes .
Create an array of size 101 ( since index in C++ starts from 0 ) .
Then for numbers from 2 to 10 do the following:
Take the number and perform it's multiplication table till the value is less than 100 and mark the array with 1's for each value in the multiplication table but not the number itself.
So for 2
Array Index
4 , 6, 8, 10 . ........................................100
We don't include 2 but the rest of the indexes are marked with 1 .
Similarly for 3
Array Index
6,9,12,15,18 ..........................................99
We mark all the indexes with 1 .
We only need to do from 2 to 10 .
Now we loop through the array and check which values are still 0 ( not marked with 1 ) .
These indexes are the prime numbers. Print these prime numbers.
For both the parts the output should not be on separate lines but on the same line separated with commas. Ex:
2,3,5,7,11,13,17 ...... 97
Make sure the prime numbers from both approaches are the same.
Given an array write a function to find out the repeating number with the longest count. You can use the below code and fill the code for the function "computeLongestSequence" . Your program should output something like:
Longest repeating number is:1 with count:1
Longest repeating number is:3 with count:4
Longest repeating number is:3 with count:4
Longest repeating number is:3 with count:10
The above statements will be written inside the function "computeLongestSequence" .
The first line could have the number 1 or some other number also depending on your code as the first array has all different elements.
#include <iostream>
using namespace std ;
/*
The function "computeLongestSequence" prints the longest sequence of a repeating number. If there is more than 1 repeating sequence then
any number can be the answer. The function should print the following for each invocation of the array and size.
Longest repeating number is: ( repeating number) with count ( count of the number)
*/
void computeLongestSequence( int arrayOfNumbers[] , int size )
{
}
int main()
{
/*
int arr5[] = {1 , 2 , 3, 3 };
computeLongestSequence( arr5 , 4 ) ;
exit(0);
*/
const int SIZE=10 ;
int arr1[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } ;
int arr2[10] = { 1, 2, 3, 3, 3, 3, 7, 7, 9, 10 } ;
int arr3[10] = { 2, 2, 3, 4, 5, 6, 3, 3, 3, 3 } ;
int arr4[10] = { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 } ;
computeLongestSequence( arr1 , SIZE ) ;
computeLongestSequence( arr2 , SIZE ) ;
computeLongestSequence( arr3 , SIZE ) ;
computeLongestSequence( arr4 , SIZE ) ;
return(0) ;
}
You can use the initial code in "main" function to debug your application.
Jagged array.
The following code creates a 2 dimensional array that contains rows of uneven sizes. Fill in the code to first print the contents of the array and then print row sums and print column sums.
#include <iostream>
using namespace std ;
int main()
{
int** TwoDimArray ;
const int ROW_COUNT = 5 ;
TwoDimArray = new int*[ROW_COUNT] ;
for(int i1=0 , i2=1, k1=1 ; i1<ROW_COUNT ; i1++, i2++ )
{
TwoDimArray[i1] = new int[i2] ;
for( int j1=0 ; j1<i2 ; j1++ )
{
TwoDimArray[i1][j1] = k1++ ;
}
} //for
//print contents of the 2 dimensional array
//Print row sums
//Print column sums
return(0) ;
}
Output should be:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
1
5
15
34
65
25
28
28
24
15
Assignment 8
This homework involves creating a "mytime" class that will keep track of the hours, minutes and seconds.
The class
will have 3 integer variables representing hours, minutes and seconds.
Some of the code has been written for you. You have to implement the remaining
functions.
//Add seconds to the current time but if the seconds go over 60 then
//add to the minutes variable and remainder goes into the seconds.
// If the seconds are 50 then adding 20 seconds makes it 70 seconds
// which is 1 minute and 10 seconds. So the value of the seconds variable
// is now 10 and we have to add the 1 minute to the minutes variable.
//However if the minutes are say 59 then the minutes variable should be
//changed to 1 and 1 hour added to the hours variable.
//If the hours variable is 23 then adding 1 sets it to 0 .
void addSeconds( int secondsP ) ;
//Minutes will be incremented by 1 and if greater than 60 then hours will get
//incremented.
void addMinutes( int minutesP ) ;
//If the hours cross 24 then it starts again from 0
//If the hour was 23 and 1 hour got added then it is reset to 0
//If the hour was 23 and we added 2 hours it is reset to 1
void addHours( int hoursP ) ;