exericse 1.28 solution

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main() {
	cout << "*********" << "\t" << "   ***   " << "    *    " << "    *    " << endl;
	cout << "*       *" << "\t" << " *     * " << "   ***   " << "   * *   " << endl;
	cout << "*       *" << "\t" << "*       *" << "  *****  " << "  *   *  " << endl;
	cout << "*       *" << "\t" << "*       *" << "    *    " << " *     * " << endl;
	cout << "*       *" << "\t" << "*       *" << "    *    " << "*       *" << endl;
	cout << "*       *" << "\t" << "*       *" << "    *    " << " *     * " << endl;
	cout << "*       *" << "\t" << "*       *" << "    *    " << "  *   *  " << endl;
	cout << "*       *" << "\t" << " *     * " << "    *    " << "   * *   " << endl;
	cout << "*********" << "\t" << "   ***   " << "    *    " << "    *    " << endl;

	return 0;
}

exercise 1.27 solution

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main() {
	int radius;
	
	// prompt user to enter the radius
	cout << "Enter the radius: ";
	cin >> radius;

	int diameter = 2 * radius;
	int circum = 2 * radius * 3.14159;
	int area = 3.14159 * radius * radius;

	// display the results
	cout << "Diameter: " << diameter << endl;
	cout << "Circumference: " << circum << endl;
	cout << "Area: " << area << endl;

	return 0;
}

exercise 1.26 solution

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main() {
	// store user input
	int num1, num2, num3;

	// prompt the user to enter 3 integer
	cout << "Input three different integers: ";
	cin >> num1 >> num2 >> num3;
	
	int sum = num1 + num2 + num3;
	int average = (num1 + num2 + num3) / 3;
	int product = num1 * num2 * num3;
	int largest;	

	if(num1 > average)
		largest = num1;
	else if(num2 > average)
		largest = num2;
	else
		largest = num3;

	int smallest;
	int median = (sum - largest) / 2;
	if(num1 < median)
		smallest = num1;
	else if(num2 < median)
		smallest = num2;
	else
		smallest = num3;

	cout << "Sum is " << sum << endl;
	cout << "Average is " << average << endl;
	cout << "Product is " << product << endl; 	
	cout << "Smallest is " << smallest << endl;		
	cout << "Largest is " << largest << endl;

	return 0;
}

exercise 1.24 solution

#include <iostream>

using std::cout;
using std::cin;

int main() {
	int num1, num2, num3, num4;

	////////////////////////////////////////////////////////////
	// a) Using one output statement with one stream insertion operator
	////////////////////////////////////////////////////////////

	// little introduction
	cout << "Enter four integers: ";
	// read two integer
	cin >> num1 >> num2 >> num3 >> num4;

	cout << num1 << "\t" << num2 << "\t" << num3 << "\t" << num4 << "\n";

	////////////////////////////////////////////////////////////
	// b) Using one output statement with four stream insertion operator
	////////////////////////////////////////////////////////////

	cout << "Enter four integers: ";
	cin >> num1;
	cin >> num2;
	cin >> num3;
	cin >> num4;

	cout << num1 << " " << num2 << " " << num3 << " " << num4 << "\n";

	////////////////////////////////////////////////////////////
	// c) Using four output statement
	////////////////////////////////////////////////////////////

	cout << "Enter four integers: ";
	cout << 1 << " " << 2 << " " << 3 << " " << 4 << "\n";

	return 0;
}

Simple Ant build file to build and run cpp files

I’ve tried to look for a simple ant build file for compiling c++ files however I’ve had no joy. After couple of hours or so I’ve made my own. It is basic for now however I’m going to update it later. Anyhoo, here it is if you are interested.

<project name="builder" default="build" basedir=".">

	<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

	<property name="src.dir" value="src"/>
	<property name="build.dir" value="bin"/>
	<property name="etc.dir" value="etc"/>
	<property name="resultset" value=""/>

	<target name="init">
		<mkdir dir="${build.dir}"/>
		<mkdir dir="${src.dir}"/>
		<mkdir dir="${etc.dir}"/>
	</target>
	
	<target name="build" depends="init">  
		<delete file="main"/>
		<foreach param="param.build" target="do.build">
			<path>  
				<fileset dir="${src.dir}" includes="*.cpp"/>  
			</path>
		</foreach>
	</target> 

	<target name="do.build">
		<propertyregex override="yes"
			property="filename" input="${param.build}"
			regexp=".*/(.*).cpp" replace="\1"/>
		<delete file="${build.dir}/${filename}.o"/>
		<echo message="building ${filename}.cpp"/>
		<exec executable="g++">
			<arg value="-Wall"/>
			<arg value="-c"/>
			<arg value="${src.dir}/${filename}.cpp"/>
			<arg value="-o"/>
			<arg value="${build.dir}/${filename}.o"/>
		</exec>
	</target>

	<target name="make">
		<delete file="${etc.dir}/main.properties"/>
		<echo file="${etc.dir}/main.properties" append="true" message="path="/> 
		<foreach param="param.make" target="do.make">
			<path>  
				<fileset dir="${build.dir}" includes="*.o"/>  
			</path>
		</foreach>
	</target>

	<target name="do.make">
		<propertyregex override="yes"
				property="filename" input="${param.make}"
				regexp=".*/(.*).o" replace="\1"/>
		<echo file="${etc.dir}/main.properties" append="true" message="${build.dir}/${filename}.o "/> 
	</target>

	<target name="run" depends="make">
		<property prefix="make" file="${etc.dir}/main.properties"/>
		<echo message="${make.path}"/>
		<exec command="g++ ${make.path} -o main"/>
		<exec executable="./main"/>
	</target>
</project>