C ++

Exam 1 | Exam 2 | Exam 3

 
Project 1.0: Character, integer
Project 2.1: Main Calculation (midterm)
Project 2.2: Main Calculation with Class-1 (midterm)
Project 2.3: Main Calculation with different files in class (midterm)
Project 3.0 Sort the array (20)
Project 5.0: Last Project Formula
Project 6.0: Final Project


Hello World
Hello World 2
Addition two integer
Product Two Num
Factorial (long, loop)
Factorial 2
10 Element 10 value (array)1
Ten Element Ten Value2
Enter 2 Num Result Sum, Product, Div, Sub
Print the Larger number from two


Odd & Even Number(If Else)
Desicion Making Numbers relationship(If Else)
Power (pow)
Char-Constnant, vowel and other Characters (srand, unsigned)
Getline
Getline and Array Size
Getline and strlensize and backword string
Getline and strlen array size and backword string of the space
RAND


Maximum & and Minumum Num From Four Num
Random Character with equivalance to it number
Out put 5 char on the screen ([] For)
cin.getline (Enter a sentence)
Array size 10
Project 1
TAX & 3 Num
TAX & 3Num (double)
10 Int array output them on the screen
calculate add, sub, multh, div


Pointer
Pointer 2
Convert up getline
Min Max & calculation of 4 num
rdig
Project 6
Dog Cat Mambal
Rectangle Area1
Rectangle Area 2
Rectangle Starting point


Enter a sitring and Out put the character
returns the smallest of three double-precision, floading point number
Print Array
What is this
Three Price and Tax
base exponent num counter POW
Two string anc combine
Military Standart Time
Class Person (ctrcpy)
Popping and Pussing


Circle Radius and area
Emp. Payroll
function that accepts a structure variable as its argument
Manaher information
Part Information
Emp Information and pay
Employee informaton and pay 2
Student Information and GPA
This program calculates either hourly wages or sales commissio
Enter name, middle and last (conbime the name)


Happy New Year
Test
This is a sentence with 7 tokens
This is base’s Vfunct()
hours worked by 5 employees, and their hourly pay rates.
Increment
Project .inputs from the keyboard into integer array ar[] with a size of 20, sorts & outputs
Project
Draw Rectangle Project


ofstream examplefile (“hello.txt”)
ifstream examplefile(“hello.txt”)
fstream TestFile(“steuff.dat”, ios::out)
datafile.open (“a:\\table.txt”, ios::in)
coppy out.txt to in.txt
String &operator +=(const String &S)
criate a file from a drive outputs the numbers below


Hello World
#include<iostream.h>
void main (void)
{
cout <<“Hello World!”;
}

—————————————————————–Top
Hello World 2
#include<iostream.h>
int main()
{
cout <<“\n\nHello\n New YOrk!\n the world Famous City\n in the world!\n”;
return 0;
}
—————————————————————–Top
Adding Two integer
#include <iostream.h>
int main ()
{
int n1, n2, sum;
cout << “Enter num 1:”;
cin >> n1;
cout << “Enter num 2:”;
cin >> n2;
sum = n1 + n2;
cout << “Sum is : ” << sum <<endl;
return 0;
}
—————————————————————–Top
Product Two Num
#include<iostream.h>
void main (void)
{
int x=0,y=0;
cout << “Enter two numbers:”;
cin >> x;
cin >> y;
cout << ‘\n’ <<x<<‘*'<<y<<‘='<<x*y<<endl;
}
—————————————————————–Top
Factorial (long loop)
#include <ctype.h>
#include <iostream.h>
#include <iomanip.h>
long factorial (long);
int main (void)
{
char loop = ‘n';
long n= 0;
do
{
cout<<“\n Enter a Number : “;
cin>>n;
cout<<‘\n'<<setw(2)<<n<<“!=”<<factorial(n)<<endl;
cout<<“\n Another (y/N? “;
cin>>loop;
}
while (toupper(loop)==’Y’); //while 9tolower(loop)==’y’);
cout<<“\nBye!!”;
return 0;
}
long factorial (long i)
{
// long ans=1;
for (long ans=1; i>1; i–)
ans*=i; // ans=ans*i
return ans;
}
—————————————————————–Top
Factorial 2
#include <ctype.h>
#include <iostream.h>
#include <iomanip.h>
long rfact (long);
int main (void)
{
char loop = ‘n';
long n= 0;
do
{
cout<<“\n Enter a Number : “;
cin>>n;
cout<<‘\n'<<setw(2)<<n<<“!=”<<rfact(n)<<endl;
cout<<“\n Another (y/n)? “;
cin>>loop;
}
while (toupper(loop)==’Y’); //while 9tolower(loop)==’y’);
cout<<“\nBye!!”;
return 0;
}
long rfact (long i)
{
// long ans=1;
if(i<=1)
return 1;
else return i*rfact (i-1);
}
10 Element 10 value (array)1
—————————————————————–Top
#include <iostream.h>
#include <iomanip.h>
int main()
{
int i, n [10];
for (i = 0; i<10; i++) // initialize array
n[i] = 0;
cout << “Element” << setw (13) << “Value” << endl;
for (i = 0; i < 10; i++) //print array
cout << setw (7) << i << setw (13) << n [i] << endl;
return 0;
}
Ten Element Ten Value2
—————————————————————–Top
#include <iostream.h>
#include <iomanip.h>
int main()
{
int i, n [10];
for (i = 0; i<10; i++) // initialize array
n[i] = 0;
cout << “Element” << setw (13) << “Value” << endl;
for (i = 0; i < 10; i++) //print array
cout << setw (7) << i << setw (13) << n [i] << endl;
return 0;
}
// for repetition structure to initialize the elements of ten elenebt integer
// array n to zeros and print the arrays in tabular format
Enter 2 Num Result Sum, Product, Div, Sub
—————————————————————–Top
Write a program that asks the user to enter two numbers, obtain two numbers from the user and print the sum, product, difference, quotient of the two numbers
#include <iostream.h>
int main ()
{
int num1, num2, sum, mult, sub, div;
cout << ” Enter first number: \n”;
cin >> num1;
cout << ” Enter second number: \n”;
cin >> num2;
sum = num1 + num2;
cout << “Sum is……..:” << sum << endl;
mult = num1 * num2;
cout << “Product is….:” << mult << endl;
sub = num1 – num2;
cout << “Difference is.:” << sub << endl;
div = num1 / num2;
cout << “Qutient is….:” << div << endl;
return 0;
}
Print Larger Number from Two
—————————————————————–Top
Write a program that asks the user to enter two integer, optains the number from the user, than prints the larger number followed by the words “IS LARGER” If the numbers equal, print the message “THESE NUMBERS ARE EQUAL.
#include <iostream.h>
int main ()
{
int num1, num2;
cout << ” Enter first number: \n”;
cin >> num1;
cout << ” Enter second number: \n”;
cin >> num2;
if (num1 > num2)
cout << num1 << ” is larger ” << endl;
if (num1 < num2)
cout << num2 << ” is larger ” << endl;
else
cout << “These numbers are Equal” <<endl;
return 0;
}

—————————————————————–Top
Odd & Even Number (If Else)
#include<iostream.h>
void main (void)
{
int x=0;
cout<<“Enter a number”;
cin>> x;
if (x%2!=0)
cout<<‘\n'<<x<<“is an odd number”;
else cout<<‘\n'<<x<<“is an even number”;
}
—————————————————————–Top
Desicion Making Numbers relationship(If Else)

if statement, desigion makes
#include <iostream.h&gt
int main

int n1, n2;
cout << “Enter n1 & n2 and I will tell you \n”
<< “the relationships they satisfie: “;
cin >> n1 >> n2;
if (n1 == n2)
cout << n1 << “is equal to ” << n2 << endl;
if (n1 != n2)
cout << n1 << “is not equal to ” << n2 << endl;
if (n1 < n2)
cout << n1 << ” is less than ” << n2 << endl;
if (n1 > n2)
cout << n1 << ” is greater than ” << n2 << endl;
if (n1 <= n2)
cout << n1 << ” is less than OR equal to ” << n2 << endl;
if (n1 >= n2)
cout << n1 << “is greater OR equal to ” << n2 << endl;
return 0;
}
—————————————————————–Top
Power (pow)
#include <iostream.h>
#include <math.h>
void main (void)
{
int x=0, y=0;
cout << “\n Enter value of x & y: “;
cin >>x>>y;
cout <<x<<‘^'<<y<<‘='<<pow(x,y);
}
—————————————————————–Top
Char-Constnant, vowel and other Characters (srand, unsigned)
#include<time.h>
//typedef int integer
void main( void )
{
char a=’ ‘;
srand(unsigned(time(0)));
for(int i=0; i<10; i++){
a=char(65+rand()%57);
cout << a << ” “;
if(a==’a’||a==’A’||a==’e’||a==’E’||a==’i’||a==’I’
||a==’o’||a==’O’||a==’u’||a==’U’||a==’y’||a==’Y’)
cout << ” = Vowel.\n”;
else if((a!=’a’||a!=’A’||a!=’e’||a!=’E’||a!=’i’||a!=’I’
||a!=’o’||a!=’O’||a!=’u’||a!=’U’||a!=’y’||a!=’Y’)
&& ( (int(a)>90) && (int(a)<97)))
cout << ” = Control Character.\n”;
else cout << ” = consonant.\n”;
}
}
/* char a=’\n';
srand(time(0));
for(int i=0; i<10; i++){
a=char(65+rand()%57);
cout << a << ” “;
}
}
*/
—————————————————————–Top
Getline
#include<iostream.h>
void main (void)
{
const int SIZE = 60;
char ar [SIZE];
cout<<“\n Enter String: “;
cin.getline(ar, SIZE);
cout << ar;
}

—————————————————————–Top
Getline and array Size
// 10/10/02 Thursday
#include <string.h>
#include <iostream.h>
void main (void)
{
const int SIZE = 60;
char ar [SIZE];
cout <<“\n Enter String: “;
cin.getline(ar, SIZE);
cout << ar;
cout << ‘\n’ <<strlen(ar);
cout <<‘\n’ <<sizeof(ar);
}

—————————————————————–Top
Getline and strlen array size and backword string
// 10/10/02 Thursday
#include <string.h>
#include <iostream.h>
void main (void)
{
const int SIZE = 60;
char ar [SIZE];
cout <<“\n Enter String: “;
cin.getline(ar, SIZE);
cout << ar;
//strlen tells the how many string you enter.
cout << ‘\n’ <<strlen(ar);
cout <<‘\n’ <<sizeof(ar)<<‘\n';
for (int i=strlen(ar)-1; i>-1; i–)
cout << ar[i];
cout<<‘\n';
for (i=0; i<strlen(ar); i++)
cout << ar[i];
}

—————————————————————–Top
Getline and strlen array size and backword string of the space
// 10/10/02 Thursday
#include <string.h>
#include <iostream.h>
#include <ctype.h>
void main (void)
{
const int SIZE = 60;
char ar [SIZE];
cout <<“\n Enter String: “;
cin.getline(ar, SIZE);
cout << ar;
//strlen tells the how many string you enter.
cout << ‘\n’ <<strlen(ar);
cout <<‘\n’ <<sizeof(ar)<<‘\n';
for (int i=strlen(ar)-1; i>-1; i–)
cout << ar[i];
cout<<‘\n';
for (i=0; i<strlen(ar); i++)
cout << ar[i];
//isspace is count the spac
int cnt=0;
for (i=0; i<strlen(ar); i++)
if (isspace(ar[i]))
cnt ++;
cout<<“\n #of the space:” << cnt;
}
//int cnt=0;
//for (i=0; i<strlen(ar); i++)
//if (ar[i] ==’ ‘)
//cnt++;
//cout<<“\n #of the space:” << cnt;
//}

—————————————————————–Top
RAND
#include <iostream.h>
#include <iomanip.h>
#include <cstdlib>
int main()
{
for (int i= 1; i <=10; i++)
{
cout<<setw(10) << (1+rand() % 100);
if (i % 99 ==0)
cout<<endl;
}
return 0;
}

—————————————————————–Top
Maximum & and Minumum Num From Four Num
//write a program that takes (4) integers from the keyboard and outputs the largest, the smallest the sum, product, different quation and the remainder of the two numbers and whetter the largest and smallest are odd and even Turgut Tezir
#include <iostream.h>
int maximum (int, int, int, int); //funktion protatype
int minumum(int, int, int, int);
int main ()
{
int a, b, c, d;
cout <<“Enter number one: “;
cin >> a;
cout <<“Enter number second: “;
cin >> b;
cout <<“Enter number third: “;
cin >> c;
cout <<“Enter number fourth: “;
cin >> d;
cout <<“Maximum is:”<< maximum (a, b, c, d)<<endl;
cout <<“Minumum is:”<< minumum (a, b, c, d)<<endl;
return 0;
}
int maximum (int x, int y, int z, int p )
{
int max=x;
if (y> max)
max = y;
if ( z > max)
max = z;
if (p > max)
max =p;
return max;
}
int minumum (int x, int y, int z, int p )
{
int min=x;
if (y < min)
min = y;
if ( z < min)
min = z;
if (p < min)
min =p;
return min;
}

—————————————————————–Top
Random Character with equivalance to it number
// Turgut Tezir Project 1 Write a program that outputs a random character and it’s intiger equivalantbetween A & Z (65-90) and a to z (97-122)
#include<stdlib.h>
#include<iostream.h>
#include<time.h>
void main(void)
{
char loop = ‘n’,ch = ‘n';
srand(unsigned(time(0)));
do
{
cout << “\n Character:” <<“\t\t”<<“Integer\n”;
//ch=char(65 +rand()%57);
//while( ch < ‘a’ && ch >’Z’)
// ch=char (65 +rand()%57);
while ((ch = char(65 + rand()%57)) <‘a’ && ch > ‘Z’);
// () needed for telling what the operand is before the < operator
cout << “\n\t” << ch << “\t\t” << int(ch);
cout << “\nWould you like another run?(y/n) “;
cin >> loop;
}
while(loop==’y’||loop==’Y’);
}

—————————————————————–Top
Out put 5 char on the screen ([] For)
#include <iostream.h>
void main (void)
{
char ch [5]={‘z’,’a’,’b’,’7′,’9′};
for (int i=0; i<5; i++)
cout<<‘\n'<<ch[i]
<<“\t\t”<<i;
}
—————————————————————–Top
cin.getline (Enter a sentence)
#include <iostream.h>
void main (void)
{
const int SIZE=80;
char ch [SIZE];
cout<<“\n Enter a sentence: “;
cin.getline (ch, SIZE);
cout << “\n” <<ch;
}

—————————————————————–Top
Array Size 10
//Turgut Tezir
#include <iostream.h>
int whatIsThis (int[], int);
int main()
{
const int arraySize = 10;
int a[arraySize] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int result = whatIsThis (a, arraySize);
cout << “Result is :” << result << endl;
return 0;
}
int WhatIsThis (int b[], int size)
{
if (size == 1)
return b[0];
else
return b [size -1 ] + whatIsThis (b, size -1);
}

—————————————————————–Top
Project1

//Turgut Tezir project 1
#include<stdlib.h>
#include<iostream.h>
#include<time.h>
void main (void)
{
char loop,ch;
srand (unsigned (time (0)));
do // outer loop
{
cout<<“\n Character: “<<“\t\t”<<“Integer\n”;
do //inner loop
{
ch=char (65 + rand () %57);
//while (char (65 +rand () %57) <‘a’ && > ‘Z’){
}
while(ch < ‘a’ && ch > ‘Z’);// inner while
//ch=char (65 + rand () %57);
cout<< “\n\t” << ch << “\t\t”<< int (ch);
cout<< “\n do again (y/n) “;
cin>>loop;
}
while (loop==’y’ ||loop== ‘Y’); // outer while
}
// inner loop picks a random char. and checks if it is between ‘a’to ‘Z’
// if it is, it prints otherwise go back picks another Rand. char.
—————————————————————–Top
TAX & 3Num (error)
#include <iostream.h>
#include <iomanip.h>
void main()
{
double p1, p2, p3, total;
const double tax = 0.0875;
cout << “\n Enter the first price……..:”;
cin >> p1;
cout << “\n Enter the second price…….:”;
cin >> p2;
cout << “\n Enter the third price……..:”;
cin >> p3;
total=(p1+p2+p3)//* tax;
cout << “\n The total price is………..: ” << setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)
<< total << endl;
}
—————————————————————–Top
TAX & 3 NUM (double)
#include <iostream.h>
#include <iomanip.h>
int main ()
{
double x, y, z,
sum, total,
tax = 0.0875;
cout << “\n Enter First price: “;
cin >> x;
cout << “\n Enter second price: “;
cin >> y;
cout << “\n Enter third price : “;
cin >> z;
sum= (x + y + z);
tax =static_cast< double >(sum) * 0.0875;
total = sum + tax;
total = sum + tax;
cout<<“********************”<<“total :”<<total << endl;
return 0;
}
—————————————————————–Top
10 Int array output them on the screen
// write a program that initializes array or with integer values (20,10,5,39,4,16,19,26,31,20) and outputs them to the screen
# include <iostream.h>
//# include <iomanip>
void main(void)
{
int ar [10]= {20,10,5,39,4,16,19,26,31,20};
for (int i = 0; i<10; i++) // for (int i = 0; i<10; i+=2)
cout<< ar [i] <<‘\n'; //’\t’
//<<“ar [“<<i<< “]= ” << ar[i];
}
—————————————————————–Top
calculate add, sub, multh, div
#include<iostream.h>
#include<iomanip.h>
#include<ctype.h>
class calcul
{
float x, y;
char op;
public:
calcul(){} //member function—no return type
float Add();
float Mult();
float Sub();
float Div();
void calc(float m,char o, float n);
void Error(char *s);
}; //End of class
float calcul::Add()
{
return(x+y);
}
float calcul::Sub()
{
return(x-y);
}
float calcul::Mult()
{
return(x*y);
}
float calcul::Div()
{
return(x/y);
}
void calcul::Error(char*s)
{
cout<<s;
}
void calcul::calc(float m,char o, float n)
{
x=m;
op=o;
y=n;
if(op==’+’)
cout<<‘\n'<<x<<‘+'<<y<<‘=’
<<setprecision(2)
<<setiosflags(ios::showpoint |ios::fixed)
<<Add();
else if(op==’-‘)
cout<<‘\n'<<x<<‘-‘<<y<<‘=’
<<setprecision(2)
<<setiosflags(ios::showpoint |ios::fixed)
<<Sub();
else if(op==’*’)
cout<<‘\n'<<x<<‘*'<<y<<‘=’
<<setprecision(2)
<<setiosflags(ios::showpoint |ios::fixed)
<<Mult();
else if(op==’/’)
{
if(x==0 || y==0)
Error(“No Zero Division”);
else cout<<‘\n'<<x<<‘/'<<y<<‘=’
<<setprecision(2)
<<setiosflags(ios::showpoint |ios::fixed)
<<Div();
}
else Error(“Invalid charater”);
}
void main (void)
{
float a=0, b=0;
char op,loop =’n';
do
{
cout<<“\nEnter Operation(Ex:2*2)”;
cin>>a>>op>>b;
calcul task;
task.calc(a,op,b);
cout<<“\nDo Again(y/n)”;
cin>>loop;
} while(tolower(loop)==’y’);
}
—————————————————————–Top
Ponter
#include<iostream.h>
int main (void)
{
int a;
int *aPtr;
a=7;
aPtr=&a;
cout<<“The address of a is ” <<&a
<< “\nThe value of *aPtr is ” <<aPtr;
cout<<“\n\n The value of a is ” <<a
<< “\nThe value of *aPtr is ” <<*aPtr;
cout<<“\n\nShowing that * and & are inverses of”
<<“each other. \n&*&aptr= “<<&*aPtr
<<“\n*&aptr= ” <<*&aPtr <<endl;
return 0;
}
—————————————————————–Top
Pointer 2
#include<iostream.h>
int main(void)
{
int a,c =200;
int * aPtr;
a = 7;
aPtr = &a;
cout << “\n Address of a:”
<< ‘\n’ << &a
<< “\n Value of aPtr:”
<< ‘\n’ << aPtr;
cout << “\n Value of a:”
<< ‘\n’ << a
<< “\n Value * aPtr points to:”
<< ‘\n’ << * aPtr;
cout << “\n Address of a:”
<< ‘\n’ << &a
<< “\n Address of c:”
<< ‘\n’ << &c
<< “\n Value of aptr:”
<< ‘\n’ << aPtr;
cout << “\n Value of a:”
<< ‘\n’ << a
<< “\n Value of c:”
<< ‘\n’ << c
<< “\n Value * aPtr points to:”

<< ‘\n’ << * aPtr;
return 0;
}
—————————————————————–Top
Convert up and getline
#include<ctype.h>
#include<iostream.h>
void convertUp(char *);
void main(void)
{
const int SIZE=80;
char array[SIZE];
cout <<“\n Enter String: “;
cin.getline(array, SIZE);
cout <<‘\n'<<array;
convertUp(array);
cout<<“\n\n”<<array<<endl;
}
void convertUp(char *ar)
{
while (*ar !=’\0′)
{
if (islower(*ar))
*ar=toupper(*ar);
cout<<‘n'<<ar<<‘\t'<<*ar;
++ar;
}
}
—————————————————————–Top
Min Max & calculation of 4 num
//write a program that takes (4) integers from the keyboard and outputs the largest, the smallest the sum, product, different quation and the remainder of the two numbers and whetter the largest and smallest are odd and even Turgut Tezir
#include <iostream.h>
int maximum (int, int, int, int); // funktion prototype.
int minimum(int, int, int, int); // funktion prototype.
int main()
{
int num1, num2, num3, num4;
int sum, mult, sub, div;
cout <<“Enter number one……: “;
cin >> num1;
cout <<“Enter number second…: “;
cin >> num2;
cout <<“Enter number third….: “;
cin >> num3;
cout <<“Enter number fourth…: “;
cin >> num4;
cout << “\nMaximum is: ” << maximum (num1, num2, num3, num4) << endl;
cout << “\nMinimum is: ” << minimum (num1, num2, num3, num4) << endl;
sum = num1 + num2 + num3 + num4;
cout << “\nSum is……….: ” << sum << endl;
mult = (num1 * num2) * (num3 * num4);
cout << “\nProduct is……: ” << mult << endl;
sub = (num1 – num2 )- (num3 – num4);
cout << “\nDifference is..: ” << sub << endl;
div = (num1 / num2 ) / (num3 / num4);
cout << “Qutient is …….:” << div << endl;
return 0;
}
int maximum (int x, int y, int z, int p)
{
int max = x;
if (y > max)
max = y;
if (z > max)
max = z;
if (p > max)
max = p;
return max;
}
int minimum(int x, int y, int z, int p)
{
int min = x;
if (y < min)
min = y;
if (z < min)
min = z;
if (p < min)
min = p;
return min;
}
—————————————————————–Top
rdig
#include <iostream.h>
void main (void)
{
int num =0, rdig=0;
cout<< “\n Enter # “;
cin>> num;
while (num !=0)
{
rdig=num %10;
cout<<rdig;
num /=10;
}
cout<<‘\n';
}
—————————————————————–Top
Project 6
//Write a program that takes varios integer inputs from the keyboard into integer array ar[] with a size of 20, sorts & outputs them to the screen.
#include <fstream.h>
#include <iomanip.h>
void main()
{
int temp, i, j, number1;
const int arraysize = 20;
int mynumbers[arraysize];
for (i=0; i<arraysize; i++)
{
cout<<“Give number: “<<i<<” “;
cin>>number1;
mynumbers[i] =number1;
cout<<endl;
}
cout<<“Original array”<<endl;
for (i=0; i<arraysize; i++)
cout<<mynumbers[i]<<” “;
for (i=0;i<arraysize-1;i++)
{
for(j= i+1 ; j<arraysize; j++)
{
if (mynumbers[i] > mynumbers[j])
{
temp=mynumbers[i];
mynumbers[i]=mynumbers[j];
mynumbers[j]=temp;
}
}
}
cout<<endl<<endl<<“Sorted array”<<endl;
for (i=0;i<arraysize;i++)
{
cout<<mynumbers[i]<<” “;
cout<<endl;
}
}
—————————————————————–Top
Dog Cat Mambal
//12/3/02
#include<iostream.h>
class Mammal
{
public:
Mammal():itsage(1){cout<<“\n Mammal Coxtructor….\n”;}
virtual~Mammal(){cout<<“\n Mammal Destructor…\n”;}
Mammal(const Mammal &rhs);
virtual void Speak(){cout<<“\n Mammal Speak!”;}
virtual Mammal *Clone(){return new Mammal(*this);}
int GetAge(){return itsage;}
protected:
int itsage;
};
Mammal::Mammal(const Mammal &rhs)
:itsage(rhs.GetAge())
{
cout<<“\n Mammal Copy Constructor……\n”;
}
class Dog: public Mammal
{
public:
Dog(){cout<<“\n Dog Constuctor…\n”;}
virtual ~Dog(){cout<<“\n Dog destructor…..\n”;}
Dog(const Dog &rhs);
void Speak(){cout<<“\n Woof Woof…….\n”;}
};
Dog::Dog(const Dog &rhs):Mammal(rhs)
{
cout<<“\n Dog Copy Construcor…..\n”;
}
class Cat: public Mammal
{
public:
Cat(){cout<<“\n Cat Constructor……\n”;}
~Cat(){cout<<“\n Cat Destructor……\n”;}
Cat(const Cat&);
void Speak(){ cout<<“\n Meow…Meow….!\n”;}
virtual Mammal *Clone(){return new Cat(*this);}
};
Cat::Cat(const Cat &rhs):Mammal(rhs)
{
cout<<“\n Cat Copy Constructor…..\n”;
}
const int NumAnimalTypes=3;
void main(void)
{
int choice=0;
Mammal *Array[NumAnimalTypes];
Mammal *ptr;
for(int i=0;i<NumAnimalTypes;i++)
{
cout<<“\n(1)Dog.\n(2)Cat.\n(3)Mammal:”;
cin>>choice;
switch(choice)
{
case 1:ptr=new Dog;
break;
case 2:ptr=new Cat;
break;
case 3:ptr=new Mammal;
break;
}
Array[i]=ptr;
}
Mammal *other[NumAnimalTypes];
for(i=0;i<NumAnimalTypes;i++)
{
Array[i]->Speak();
other[i]=Array[i]->Clone();
}
for(i=0;i<NumAnimalTypes;i++)
other[i]->Speak();
}
—————————————————————–Top
Rectangle Area
#include <iostream.h>
class Rectangle
{
private:
float l, w;
public:
// no argument constructor
Rectangle();
// two argument constructor
Rectangle(float m, float n);
float get_l()
{return l;}
float get_w()
{return w;}
void set_lw(float m, float n)
{l = m; w=n; }
float Area();
};
Rectangle::Rectangle()
{ l= 1.; w = 1.;}
Rectangle::Rectangle(float m, float n)
{ l = m; w = n; }
float Rectangle::Area()
{return l*w; }
main()
{
Rectangle a(5.,10.),b(3.,5.),c();
cout << “Area of A is ” << a.Area() << endl;
a.set_lw(6.,6.);
cout << “Area of A is ” << a.Area() << endl;
cout << “Area of B is ” << b.Area() << endl;
}
—————————————————————–Top
Rectangle Area 2
#include<iostream.h>
#include<iomanip.h>
class Rectangle
{
public:
Rectangle(double=1.0,double=1.0);
double Perimeter (void);
double Area (void);
void Setwidth(double w);
void Setlength(double l);
double getwidth(void);
double getlength(void);
private:
double length;
double width;
};
Rectangle::Rectangle(double w, double l)
{
Setwidth (w);
Setlength (l);
}
double Rectangle::Perimeter (void)
{
return 2*(width+length);
}
double Rectangle::Area (void)
{
return width*length;
}
void Rectangle::Setwidth (double w)
{
width=w>0.0&&w<20.00?w:1.0;
}
void Rectangle::Setlength (double l)
{
length=l>0.0&&l<20.00?l:1.0;
}
double Rectangle::getwidth (void)
{
return width;
}
double Rectangle::getlength (void)
{
return length;
}
void main (void)
{
Rectangle a, b(4.0, 5.0), c(67.0, 888.0);
cout<<setiosflags(ios::showpoint|ios::fixed)
<<setprecision(1);
cout<<“a:Length = “<<a.getlength()
<<“;Width = “<<a.getwidth()
<<“;perimeter = “<<a.Perimeter()
<<“;Area = “<<a.Area()<<‘\n';
cout<<“b:length = “<<b.getlength()
<<“;width = “<<b.getwidth()
<<“;perimeter = “<< b.Perimeter()
<<“;Area = “<<b.Area()<<‘\n';
cout<<“C:length= “<<c.getlength()
<<“;width = “<<c.getwidth()
<<“;perimeter = “<<c.Perimeter()
<<“;Area = “<<c.Area();
}
—————————————————————–Top
Rectangle Starting Point

#include <stdio.h>

typedef struct point {

int x;

int y;

} DOT;

struct rect {

DOT ul;

DOT ur;

DOT ll;

DOT lr;

};

int main()

{

DOT location; /* One way to declare structures */

DOT *lpt;

struct rect myRect; /* Second way to declare structures */

struct circle { /* Third way */

DOT origin;

int radius;

} myCircle;

location.x = 5;

location.y = 4;

lpt = &location;

printf(“Starting point %d %d\n”,location.x, lpt->y);

return 0;

}

Enter a String Out put the Character

 

//11/12/10

//Turgut Tezir

//Write a program that takes a string of characters from the keyboard

//and outputs for each characters when it is a space, and alfhabet and upper

//characters, a lower case characters, and alpha numeric character, a

// vowel, punctuation, a digit or a consonant.

#include<string.h>

#include<iostream.h>

#include<ctype.h>

void main( void )

{

const int SIZE = 80;

char ar[SIZE];

cout << “\nEnter a String:”;

cin.getline(ar, SIZE);

for(int i=0; i<strlen(ar); i++)

{

cout << “\n——————————–\n”;

cout << ‘\n’ << ar[i] << ” is : “;

if(isspace(ar[i])) cout << ” \n*a: space. “;

if(isalpha(ar[i])) cout << ” \n*an: alphabet. “;

if(isupper(ar[i])) cout << ” \n*an: upper case. “;

if(islower(ar[i])) cout << ” \n*a: lower case. “;

if(isalnum(ar[i])) cout << ” \n*an: alphanumeric. “;

if( ispunct(ar[i])) cout << ” \n*a: punctuation. “;

if( isdigit(ar[i])) cout << ” \n*a: digit. “;

char temp = toupper(ar[i]);

if(temp==’A’ || temp == ‘E’ || temp == ‘I’ || temp == ‘O’

|| temp == ‘U’) cout << ” \n*a: vowel. “;

else if(temp == ‘ ‘) cout << “\n*a: space.”;

else cout << ” \n*a: consonant”;

}

}

returns the smallest of three double-precision, floading point number

//Turgut Tezir

//HW 3.28 Write a program that returns the smallest of three

//double-precision, floading point number

 

#include <iostream.h>

#include <iomanip.h>

void main()

{

double a, b, c, smallest;

cout<<“Enter a number: “;

cin>>a;

cout<<“\nEnter another number: “;

cin>>b;

cout<<“\nEnter another number: “;

cin>>c;

double smallestNo(double , double , double, double );

smallest = smallestNo(a, b, c);

//print smallest

cout<<“The smallest number is “<<setprecision(2)<<smallest;

}

double smallestNo(double a, double b, double c, double smallest)

{

smallest = a;

if (smallest > b)

smallest = b;

if (smallest > c)

smallest = c;

return smallest;

}

Print Array

//Turgut Tezir

//Chp 4.

#include <iostream.h>

#include <iomanip.h>

int main()

{

int i, n [10];

for (i = 0; i<10; i++) // initialize array

n[i]=0;

cout << “Element” << setw (13) << “value” <<endl;

for (i=0; i<10; i++) //print array

cout <<< setw (7) << i <<setw (13) << n [i] <<endl;

return 0;

}

What is this

//Turgut Tezir

//Chp 4.1

#include <iostream.h>

int whatIsThis (int[], int);

int main()

{

const int arraySize = 10;

int a[arraySize] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int result = whatIsThis (a, arraySize);

cout << “Result is :” << result << endl;

return 0;

}

int WhatIsThis (int b[], int size)

{

if (size == 1)

return b[0];

else

return b [size -1 ] + whatIsThis (b, size -1);

}

Three Price and Tax1

#include <iostream.h>

#include <iomanip.h>

void main()

{

double p1, p2, p3, total;

const double tax = 0.0875;

cout << “\n Enter the first price……..:”;

cin >> p1;

cout << “\n Enter the second price…….:”;

cin >> p2;

cout << “\n Enter the third price……..:”;

cin >> p3;

total=(p1+p2+p3)//* tax;

cout << “\n The total price is………..: ” << setprecision(2)

<<setiosflags(ios::fixed | ios::showpoint)

<< total << endl;

}

base exponent num counter POW

#include <iostream.h>

//int power(int, int); // function prototype

int main ()

{

int base,

exponent,

num,

counter;

num = 0;

counter = 1;

while (counter=exponent)

cout<<“\n Enter Base: “;

cin<<base;

cout<<“\n Enter Exponent: “;

cin>>exponent;

num=base*base;

counter=counter+1

cout<<“\nThe number is: “<<num<<endl;

return 0;

}

Two sting Combine

#include<iostream.h>

void mystery1(char *, const char *);

int main()

{

char string1[80], string2[80];

cout<<“Enter two strings:”;

cin>>string1>>string2;

mystery1(string1, string2);

cout<<string1<<endl;

return 0;

}

void mystery1(char *s1, const char *s2)

{

while (*s1 !=’\0′)

++s1;

for (; *s1 = *s2; s1++, s2++)

;

}

Military Time and Standart Time

//11/19/02

//Turgut Tezir

 

// definition of the structure: Structures are aggregate data types built

//using other element of types.

//OOPencapsulates data (atributes) and functions (behavior) into packages

//called class. OOPallows us to hide information or imlementation details

//within the class

#include<iostream.h>

class Time

{

public:

Time();

void setTime(int, int, int);

void printMilitary();

void printStandard();

private:

int hour;

int minute;

int second;

};

Time::Time(){hour=minute=second=0;}

void Time::setTime(int h, int m, int s)

{

hour= (h>=0 && h<24 ) ? h : 0;

minute= (m>=0 && m<24 ) ? m : 0;

second= (s>=0 && s<24 ) ? 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” );

}

int main ()

{

Time t;

cout << “Military time is:”;

t.printMilitary();

cout << “\nStandart time is: “;

t.printStandard();

t.setTime(13, 27, 6);

cout << “\n\nMilitary Time after set time is: “;

t.printMilitary();

cout << “\n Standart Time after set time is: “;

t.printStandard();

t.setTime (99, 99, 99);

cout << “\n\nAfter Atemting invalid setting: “

<< “\n Military time:”;

t.printMilitary();

cout << “\nStandart time:”;

t.printStandard();

cout << endl;

return 0;

}

Class Person (ctrcpy)

//11/19/02

//Turgut Tezir

//importand function signature?

// overloading function?

// definition of the structure: Structures are aggregate data types built

//using other element of types.

#include<iostream.h>

#include<string.h>

class Person

{

char Name[100];

int Age;

public:

Person():Age(0) {Name [0]=0;}

Person (char *name)

{strcpy(Name, name); Age=0;}

Person(char *name, int age)

{

strcpy(Name, name);

Age=age;

}

~Person(){}

void set(char *name)

{

strcpy(Name, name);

}

void set(int age) {Age=age;}

void set(char *name, int age)

{

set(name); set(age);

}

void set(int age, char *name)

{

set(name, age);

}

void Print (void)

{

cout<<‘\n'<<Name<<‘\n'<<Age;

}

};

int main(void)

{

Person John;

John.set(“John”, 23); John.Print();

John.set(45); John.Print();

Person Mary(“Mary”); Mary.Print();

return 0;

}

Popping and pussing

//Turgut Tezir

//11/07/02

#include<iostream.h>

const int MAX=5;

class stack //name of the class

{

protected:

int st [MAX];

int top;

public:

stack() {top=-1;}

void push (int var){st[++top]=var;}

int pop(void) {return st [top–];}

};

class Mystack:public stack

{

public:

void push (int var)

{

if (top>=MAX)

cout<<“\n Error Stack is full!!!”;

else stack::push (var);

}

int pop (void)

{

if (top<0)

{

cout<<“\nERROR Stack is emty!!”;

return -1;

}

else return stack::pop();

}

};

void main(void)

{

Mystack s1;

for (int i=5; i<MAX+5; i++)

s1.push(i);

for (i=5; i<MAX+5; i++)

cout<<“\n Popping: ” <<s1.pop();

}

Circle Radius and area

// This program uses a function to return a structure. This

// is a modification of Program 11-2.

#include <iostream.h>

#include <iomanip.h>

#include <math.h> // For the pow function

// Circle structure declaration

struct Circle

{

float Radius;

float Diameter;

float Area;

};

 

// Function prototype

Circle GetInfo(void);

// Constant definition for Pi

const float Pi = 3.14159;

void main(void)

{

Circle C;

C = GetInfo();

C.Area = Pi * pow(C.Radius, 2.0);

cout << “The radius and area of the circle are:\n”;

cout.precision(2);

cout.setf(ios::fixed | ios::showpoint);

cout << “Radius: ” << C.Radius << endl;

cout << “Area: ” << C.Area << endl;

}

 

// Definition of function GetInfo. This function uses a

// local variable, Round, which is a Circle structure.

// The user enters the diameter of the circle, which is

// stored in Round.Diameter. The function then calculates

// the radius, which is stored in Round.Radius. Round is then

// returned from the function.

Circle GetInfo(void)

{

Circle Round;

cout << “Enter the diameter of a circle: “;

cin >> Round.Diameter;

Round.Radius = Round.Diameter / 2;

return Round;

}

Emp. Payroll

// Write a structures progran that shows employee payroll.

//programmer enter emp#, emp name, how many hours emp work,

// emp. pay rate.

# include<iostream.h>

struct PayPoll;

{

int empNum;

char empName[25];

float workHours;

float payRate;

float crossPay;

};

void main (void)

{

PayRoll Employee;

cout<<“Enter the employee Number”;

cin>>Employee.empNum;

cout<<“Enter employee’s Name….”;

cin.ignore();

cin.getline(Employee.Name,25);

cout<<“Howmany hours employee work”;

cin>>Employee.workHours;

cout<<“Employees hours pay rate….”;

cin>>Employee.payRate;

Employee.crossPay=Employee.workHours*Employee.payRate;

cout<<“This is Epmloyee payRoll DATA:\n”;

cout<<“Name………:”<<Employee.empName<<endl;

cout<<“Number…….:”<<Employee.empNum<<endl;

cout<<“Hours worked.:”<<Employee.workHours<<endl;

cout<<“Hours PayRate:”<<Employee.payPate<<endl;

cout.precision(2);

cout.set(ios::fixed|ios::showpoint);

cout<<“Gross Pay….:”<<Employee.crossPay<<endl;

}

function that accepts a structure variable as its argument

// This program demonstrates a function that accepts

// a structure variable as its argument.

#include <iostream.h>

struct InvItem

{

int PartNum; // Part number

char Description[50]; // Item description

int OnHand; // Units on hand

float Price; // Unit price

};

void ShowItem(InvItem); // Function prototype

void main(void)

{

InvItem Part = {171, “Industrial Widget”, 25, 150.0};

ShowItem(Part);

}

// Definition of function ShowItem. This function accepts

// an argument of the InvItem structure type. The contents

// of the structure is displayed.

void ShowItem(InvItem Piece)

{

cout.precision(2);

cout.setf(ios::fixed | ios::showpoint);

cout << “Part Number: ” << Piece.PartNum << endl;

cout << “Description: ” << Piece.Description << endl;

cout << “Units On Hand: ” << Piece.OnHand << endl;

cout << “Price: $” << Piece.Price << endl;

}

Manaher information

 

// This program shows a structure with two nested structure members.

#include <iostream.h>

struct Date

{

int Month;

int Day;

int Year;

};

struct Place

{

char Address[50];

char City[20];

char State[15];

char Zip[11];

};

struct EmpInfo

{

char Name[50];

int EmpNumber;

Date BirthDate;

Place Residence;

};

void main(void)

{

EmpInfo Manager;

// Ask for the manager’s name and employee number

cout << “Enter the manager’s name: “;

cin.getline(Manager.Name, 50);

cout << “Enter the manager’s employee number: “;

cin >> Manager.EmpNumber;

 

// Get the manager’s birth date

cout << “Now enter the manager’s date-of-birth.\n”;

cout << “Month (up to 2 digits): “;

cin >> Manager.BirthDate.Month;

cout << “Day (up to 2 digits): “;

cin >> Manager.BirthDate.Day;

cout << “Year (2 digits): “;

cin >> Manager.BirthDate.Year;

cin.get(); // Eat the remaining newline character

// Get the manager’s residence information

cout << “Enter the manager’s street address: “;

cin.getline(Manager.Residence.Address, 50);

cout << “City: “;

cin.getline(Manager.Residence.City, 20);

cout << “State: “;

cin.getline(Manager.Residence.State, 15);

cout << “Zip Code: “;

cin.getline(Manager.Residence.Zip, 11);

// Display the information just entered

cout << “\nHere is the manager’s information:\n”;

cout << Manager.Name << endl;

cout << “Employee number ” << Manager.EmpNumber << endl;

cout << “Date of Birth: “;

cout << Manager.BirthDate.Month << “-“;

cout << Manager.BirthDate.Day << “-“;

cout << Manager.BirthDate.Year << endl;

cout << “Place of residence:\n”;

cout << Manager.Residence.Address << endl;

cout << Manager.Residence.City << “, “;

cout << Manager.Residence.State << ” “;

cout << Manager.Residence.Zip << endl;

}

Part information

// This program has a function that uses a structure reference variable

// as its parameter.

#include <iostream.h>

#include <iostream.h>

struct InvItem

{

int PartNum; // Part number

char Description[50]; // Item description

int OnHand; // Units on hand

float Price; // Unit price

};

// Function Prototypes

void GetItem(InvItem&);

void ShowItem(InvItem);

void main(void)

{

InvItem Part;

GetItem(Part);

ShowItem(Part);

}

// Definition of function GetItem. This function uses a structure reference

// variable as its parameter. It asks the user for information to store in the

// structure.

void GetItem(InvItem &Piece)

{

cout << “Enter the part number: “;

cin >> Piece.PartNum;

cout << “Enter the part description: “;

cin.get(); // Eat the remaining newline

cin.getline(Piece.Description, 50);

 

cout << “Enter the quantity on hand: “;

cin >> Piece.OnHand;

cout << “Enter the unit price: “;

cin >> Piece.Price;

}

// Definition of function ShowItem. This function accepts an argument of

// the InvItem structure type. The contents of the structure is displayed.

void ShowItem(InvItem Piece)

{

cout.precision(2);

cout.setf(ios::fixed | ios::showpoint);

cout << “Part Number: ” << Piece.PartNum << endl;

cout << “Description: ” << Piece.Description << endl;

cout << “Units On Hand: ” << Piece.OnHand << endl;

cout << “Price: $” << Piece.Price << endl;

}

Emp Information and pay

This program uses a structure pointer to dynamically allocate a structure

// variable in memory. It is a modification of Program 11-1.

#include <iostream.h>

#include <iomanip.h>

#include <stdlib.h>

struct PayRoll

{

int EmpNumber; // Employee number

char Name[25]; // Employee’s name

float Hours; // Hours worked

float PayRate; // Hourly Payrate

float GrossPay; // Gross Pay

};

 

void main(void)

{

PayRoll *Employee; // Employee is a pointer to a

// PayRoll structure

Employee = new PayRoll; // Dynamically allocate a struct

if (Employee == NULL) // Test the allocated memory

{

cout << “Memory allocation error!\n”;

exit(EXIT_FAILURE);

}

cout << “Enter the employee’s number: “;

cin >> Employee->EmpNumber;

cout << “Enter the employee’s name: “;

cin.ignore(); // To skip the remaining ‘\n’ character

cin.getline(Employee->Name, 25);

 

cout << “How many hours did the employee work? “;

cin >> Employee->Hours;

cout << “What is the employee’s hourly payrate? “;

cin >> Employee->PayRate;

Employee->GrossPay = Employee->Hours * Employee->PayRate;

 

cout << “Here is the employee’s payroll data:\n”;

cout << “Name: ” << Employee->Name << endl;

cout << “Number: ” << Employee->EmpNumber << endl;

cout << “Hours worked: ” << Employee->Hours << endl;

cout << “Hourly Payrate: ” << Employee->PayRate << endl;

cout.precision(2);

cout.setf(ios::fixed | ios::showpoint);

cout << “Gross Pay: $” << Employee->GrossPay << endl;

delete Employee; // Free the allocated memory

}

Employee informaton and pay 2

include <iostream.h>

struct PayRoll

{

int EmpNumber; // Employee number

char Name[25]; // Employee’s name

float Hours; // Hours worked

float PayRate; // Hourly Payrate

float GrossPay; // Gross Pay

};

void main(void)

{

PayRoll Employee; // Employee is a PayRoll structure

cout << “\nEnter the employee’s number: “;

cin >> Employee.EmpNumber;

cout << “Enter the employee’s name: “;

cin.ignore(); // To skip the remaining ‘\n’ character

cin.getline(Employee.Name, 25);

cout << “How many hours did the employee work? “;

cin >> Employee.Hours;

cout << “What is the employee’s hourly payrate? “;

cin >> Employee.PayRate;

Employee.GrossPay = Employee.Hours * Employee.PayRate;

cout << “Here is the employee’s payroll data:\n”;

cout << “Name………….: ” << Employee.Name << endl;

cout << “Number………..: ” << Employee.EmpNumber << endl;

cout << “Hours worked…..: ” << Employee.Hours << endl;

cout << “Hourly Payrate…: ” << Employee.PayRate << endl;

cout.precision(2);

cout.setf(ios::fixed | ios::showpoint);

cout << “Gross Pay……..: $” << Employee.GrossPay << endl;

}

Student Information and GPA

// This program demonstrates a function that uses a

// pointer to a structure variable as a parameter.

#include <iostream.h>

#include <iomanip.h>

struct Student

{

char Name[35]; // Student’s name

int IdNum; // Student ID number

int CrdHrs; // Credit hours enrolled

float Gpa; // Current GPA

};

 

void GetData(Student *); // Function prototype

 

void main(void)

{

Student Freshman;

cout << “Enter the following student data:\n”;

GetData(&Freshman);

cout << “\nHere is the student data you entered:\n”;

cout.precision(2);

// Now display the data stored in Freshman

cout << “Name: ” << Freshman.Name << endl;

cout << “ID Number: ” << Freshman.IdNum << endl;

cout << “Credit Hours: ” << Freshman.CrdHrs << endl;

cout << “GPA: ” << Freshman.Gpa << endl;

}

 

// Definition of function GetData. Uses a pointer to a

// Student structure variable. The user enters student

// information, which is stored in the variable.

void GetData(Student *S)

{

cout << “Student Name: “;

cin.getline(S->Name, 35);

cout << “Student ID Number: “;

cin.ignore(); // Ignore the leftover newline

cin >> S->IdNum;

cout << “Credit Hours Enrolled: “;

cin >> S->CrdHrs;

cout << “Current GPA: “;

cin >> S->Gpa;

}

This program calculates either hourly wages or sales commission

// This program demonstrates a union.

#include <iostream.h>

#include <iomanip.h>

#include <ctype.h> // For toupper

union PaySource

{

short Hours;

float Sales;

};

void main(void)

{

PaySource Employee1;

char PayType;

float PayRate, GrossPay;

cout.precision(2);

cout.setf(ios::showpoint);

cout << “This program calculates either hourly wages or\n”;

cout << “sales commission.\n”;

cout << “Enter H for hourly wages or C for commission: “;

cin >> PayType;

if (toupper(PayType) == ‘H’)

{

cout << “What is the hourly pay rate? “;

cin >> PayRate;

cout << “How many hours were worked? “;

cin >> Employee1.Hours;

GrossPay = Employee1.Hours * PayRate;

cout << “Gross pay: $” << GrossPay << endl;

}

 

else if (toupper(PayType) == ‘C’)

{

cout << “What are the total sales for this employee? “;

cin >> Employee1.Sales;

GrossPay = Employee1.Sales * 0.10;

cout << “Gross pay: $” << GrossPay << endl;

}

 

else

{

cout << PayType << ” is not a valid selection.\n”;

}

}

Enter name, middle and last (conbime the name)

\// This program uses a structure to hold someone’s first,

// middle, and last name.

#include <iostream.h>

#include <string.h>

struct Name

{

char First[15];

char Middle[15];

char Last[15];

char Full[45];

};

void main(void)

{

Name Person;

cout << “Enter your first name: “;

cin >> Person.First;

cout << “Enter your middle name: “;

cin >> Person.Middle;

cout << “Enter your last name: “;

cin >> Person.Last;

strcpy(Person.Full, Person.First);

strcat(Person.Full, ” “);

strcat(Person.Full, Person.Middle);

strcat(Person.Full, ” “);

strcat(Person.Full, Person.Last);

cout << “\nYour full name is ” << Person.Full << endl;

}

Happy New Year

//11/14/02

//turgut tezir

#include<string.h>

#include<iostream.h>

void main(void)

{

char *s1=”Happy New Year”;

char *s2=”Happy New Year”;

char *s3=”Happy Holidays”;

cout<<“\ns1=”<<s1<<“\ns2=”<<s2

<<“\ns3=”<<s3<<“\n strcmp(s1,s2)=”

<<strcmp(s1,s2);

cout<<“\n strcmp(s1,s3)=”<<strcmp(s1,s3)

<<“\n strcmp(s3,s1)=”<<strcmp(s3,s1);

cout<<“\n\n\n strncmp(s1,s3,6)=”

<<strncmp(s1,s3,6)

<<“\n strncmp(s1,s3,7)=”

<<strncmp(s1,s3,7);

cout<<“\n strncmp(s3,s1,7)=”

<<strncmp(s3,s1,7)

<<endl;

}

test

//11/14/02

//turgut tezir

#include<iostream.h>

class Test

{

public:

Test(int=0);

void Print()const;

private:

int x;

};

Test::Test(int a)

{

x=a;

}

void Test::Print(void)const

{

cout<<“\n x=” << x <<endl

<<“this ->x=” <<this ->x<<endl

<<“(*this).x=” <<(*this).x<<endl’

}

int main (void)

{

Test a(12), b;

a.print();

cout<<“\n\n”;

b.print();

return 0;

}

This is a sentence with 7 tokens

//11/14/02

//turgut tezir

#include<string.h>

#include<iostream.h>

void main(void)

{

char string[]=”This is a sentence with 7 tokens”;

char *tokenptr;

cout<<“\nString to be tokenized is : \n”

<<string<<“\n The tokens are:”;

tokenptr=strtok(string, ” “);

while(tokenptr !=NULL)

{

cout<<tokenptr<<‘\n';

tokenptr=strtok(NULL, ” “);

}

}

This is base’s Vfunct()

//11/14/02

//turgut tezir

#include<iostream.h>

class base

{

public:

virtual void vfunc()

{

cout<<“\nThis is base’s Vfunct().”<<endl;

}

};

class drived1:public base

{

public:

void vfunc()

{

cout<<“\nThis is Drived1 vfunc().”<<endl;

}

};

class drived2:public base

{

public:

void vfunc()

{

cout<<“\nThis is Drived2 vfunc().”<<endl;

}

};

void main(void)

{

base *p, b;

drived1 d1;

drived2 d2;

p=&b;

p->vfunc();

p=&d1;

p->vfunc();

p=&d2;

p->vfunc();

}

hours worked by 5 employees, and their hourly pay rates.

// This program stores, in an array of structures,

// the hours worked by 5 employees, and their hourly

// pay rates. (This is a modification of Program 7-12.)

#include <iostream.h>

struct PayInfo

{

int Hours; // Hours Worked

float PayRate; // Hourly Pay Rate

};

void main(void)

{

PayInfo Workers[5]; // Array of 5 structures

cout << “Enter the hours worked by 5 employees and their\n”;

cout << “hourly rates.\n”;

for (int Index = 0; Index < 5; Index++)

{

cout << “Hours worked by employee #” << (Index + 1);

cout << “: “;

cin >> Workers[Index].Hours;

cout << “Hourly pay rate for employee #”;

cout << (Index + 1) << “: “;

cin >> Workers[Index].PayRate;

}

 

cout << “Here is the gross pay for each employee:\n”;

cout.precision(2);

cout.setf(ios::fixed | ios::showpoint);

for (Index = 0; Index < 5; Index++)

{

float Gross;

Gross = Workers[Index].Hours * Workers[Index].PayRate;

cout << “Employee #” << (Index + 1);

cout << “: $” << Gross << endl;

}

}

Increment

//10/31/01

//Turgut Tezir

#include<iostream.h>

class Increment

{

public:

Increment(int c=0, int i=1);

void Addincrement() {count += increment;}

void print();

private:

int count;

int increment;

};

Increment::increment(int c, int i)

{

increment=2;

count=c;

}

void Increment::print()

{

count <<‘\n'<<“cout=”<<count<<“Increment=”<<increment<<endl;

}

void main (void)

{

Increment value(10,5);

count<<“\nDefore Incrementing”;

value.print();

for(int j=1; j<=3; j++)

{

value.addincrement();

cout<<‘\n'<<“After Increment: j”;<<j;

cout<<“\n\n”;

value.print();

}

}

Project .inputs from the keyboard into integer array ar[] with a size of 20, sorts & outputs

//Write a program that takes varios integer inputs from the keyboard into

//integer array ar[] with a size of 20, sorts & outputs them to the screen.

#include <fstream.h>

#include <iomanip.h>

void main()

{

int temp, i, j, number1;

const int arraysize = 20;

int mynumbers[arraysize];

 

for (i=0; i<arraysize; i++)

{

cout<<“Give number: “<<i<<” “;

cin>>number1;

mynumbers[i] =number1;

cout<<endl;

}

cout<<“Original array”<<endl;

 

for (i=0; i<arraysize; i++)

cout<<mynumbers[i]<<” “;

 

for (i=0;i<arraysize-1;i++)

{

for(j= i+1 ; j<arraysize; j++)

{

if (mynumbers[i] > mynumbers[j])

{

temp=mynumbers[i];

mynumbers[i]=mynumbers[j];

mynumbers[j]=temp;

}

}

}

cout<<endl<<endl<<“Sorted array”<<endl;

for (i=0;i<arraysize;i++)

{

cout<<mynumbers[i]<<” “;

cout<<endl;

}

}

 

 

Today…Project

//Project

//wordi.txt for 1000 generated random integer values

//between 7 & 21000

//Output file: wordo.txt for the sorted values

//Screen file: result.txt for all screen output

#include<iostream.h>

#include<stdlib.h>

#include<time.h>

#include<ctype.h>

void InsertSort(char c[], const int SIZE);

void PrintAr(char a[], const int SIZE);

void BubbleSort(char a[], const int SIZE);

int BinarySearch(const char [], char, int, int, int);

int LinearSearch(const char [], char, int);

 

void main( void )

{

long z = 99000000L;

char loop[4];

srand( unsigned(time(0)));

const int SIZE=40;

char a[SIZE], searchkey;

int element;

clock_t start, finish;

float duration;

do

{

for (int i=0; i<SIZE; i++)

{

a[i] = char((65 + rand() % 57));

cout << a[i] << ” “;

if(i%9 ==0) cout << ‘\n';

}

cout << “\n\n”;

/* Measure the duration of a BubbleSort event. */

cout << “\n\nMeasuring the duration of a BubbleSort event.\n\n “;

start = clock();

z = 99000000L;

while( z– );

BubbleSort(a, SIZE);

finish = clock();

duration = (float)(finish – start) / CLOCKS_PER_SEC;

cout << ” seconds: ” << duration << “\n\n”;

PrintAr(a, SIZE);

for ( i=0; i<SIZE; i++)

{

a[i] = char((65 + rand() % 57));

cout << a[i] << ” “;

if(i%9 ==0) cout << ‘\n';

}

 

/* Measure the duration of a SelectionSort event. */

cout << “\n\nMeasuring the duration of a InsertionSort event.\n\n”;

start = clock();

z = 99000000L;

while( z– );

InsertSort(a, SIZE);

finish = clock();

duration = (float)(finish – start) / CLOCKS_PER_SEC;

cout << ” seconds: ” << duration << “\n\n”;

PrintAr(a, SIZE);

/* Measure the duration of a Binary Search event. */

cout << “\n\nMeasurin the duration of a BinarySearch event.\n\n”;

cout << “\nEnter Search Key: “;

cin >> searchkey;

cout << “\n\nTime to do Binary Search: ” << searchkey;

start = clock();

z = 99000000L;

while( z– );

element = BinarySearch(a, searchkey, 0, SIZE-1, SIZE);

finish = clock();

duration = (float)(finish – start) / CLOCKS_PER_SEC;

cout << ” seconds: ” << duration << ” ” << “Element: ” << a[element];

cout << “\n\nMeasurin the duration of a LinearSearch event.\n\n”;

cout << “\n\nTime to do Linear Search: ” << searchkey;

start = clock();

z = 99000000L;

while( z– );

element = LinearSearch(a, searchkey,SIZE);

finish = clock();

duration = (float)(finish – start) / CLOCKS_PER_SEC;

cout << ” seconds: ” << duration << ” ” << “Element: ” << a[element];

cout << “\nDo Another (y/n)?”;

cin >> loop;

}while(tolower(loop[0]) == ‘y’);

cout << “\nBye….”;

}

int BinarySearch(const char a[], char key, int low, int high, int size)

{

int middle=0;

while( low<=high)

{

middle = (low + high) /2;

if(key == a[middle])

return middle;

else if( key < a[middle])

high = middle -1;

else low = middle + 1;

}

return -1;

}

int LinearSearch(const char a[], char key, int size)

{

for( int n=0; n < size; n++)

if(a[n] == key)

return n;

return -1;

}

void BubbleSort(char a[], const int SIZE)

{

int hold=0;

for( int pass=1; pass < SIZE; pass++)

for( int j=0; j< SIZE-1; j++)

if(a[j] > a[j+1])

{

hold = a[j];

a[j] = a[j+1];

a[j+1] = hold;

}

}

void InsertSort(char c[], const int SIZE)

{

int position, insert;

for(int i = 1; i < SIZE; i++)

{

insert = c[i];

for (position = i – 1; position >= 0 && insert < c[position]; position–)

c[position + 1] = c[position];

c[position + 1] = insert;

}

}

void PrintAr(char a[], const int SIZE)

{

for(int j=0; j<SIZE; j++)

{

cout << a[j] << ” “;

if(j%9 ==0) cout << ‘\n';

}

cout << ‘\n';

}

Draw Rectangle Project

//10/31/02

//Turgut Tezir

#include<iostream.h>

class Point

{

int x,y; //private no access

public:

Point(){}

Point(int ix, int iy)

 

:x(ix),y(iy){}

//if you wantto know the value of x we write.

int Getx(){return x;}

//If you want to know value of y we write

int Gety(){return y;}

void Draw(void);

void SetP(int xa, int ya){x=xa;y=ya;}

};

void Point::Draw(void)

{

for(int cy=0; cy<y; cy++)

cout<<‘\n';

for(int cx=0; cx<x; cx++)

cout<<‘\t';

cout<<‘X';

}

 

class Rectangle

{

Point p1, p2;

public:

Rectangle(int x1, int y1,int x2, int y2)

:p1(x1,y1), p2(x2,y2){}

void SetR(int xa, int ya, int xb, int yb)

{

p1.SetP(xa,ya);

p2.SetP(xb,yb);

}

void Draw();

};

void Rectangle::Draw()

{

p1.Draw();

p2.Draw();

}

 

void main (void)

{

Rectangle r(0,1,5,10);

r.Draw();

}

ofstream examplefile (“hello.txt”);

//Turgut Tezir

//11/26/02

#include<fstream.h>

int main (void)

{

ofstream examplefile (“hello.txt”);

if (examplefile)

{

examplefile<<“this is Line.\n”;

examplefile<<“This is another line \n”;

examplefile.close();

}

return 0;

}

ifstream examplefile(“hello.txt”);

//Turgut Tezir

//11/26/02

#include<process.h>

#include<fstream.h>

#include<iostream.h>

int main (void)

{

const int SIZE=256;

char buffer[SIZE];

ifstream examplefile(“hello.txt”);

if(!examplefile)

{

cout<<“\nError opening file.\n”;

exit (1);

}

while (! examplefile.eof())

{

examplefile.getline(buffer, 100);

cout<<buffer<<endl;

}

return 0;

}

/*#include<fstream.h>

int main (void)

{

ofstream examplefile (“c:\PayrollProjects\Payroll.txt”);

examplefile<<“this is Line”;

examplefile<<“This is another line \n”;

examplefile.close();

}

return 0;

}

*/

fstream TestFile(“steuff.dat”, ios::out)

//Turgut Tezir

//12/12/02

#include <fstream.h>

void ShowState(fstream &);

void main (void)

{

fstream TestFile(“steuff.dat”, ios::out);

if(TestFile.fail())

{

cout << “Cannot open the file n”;

return;

}

int Num = 10;

cout <<“Writing to the file \n”;

TestFile << Num;

TestFile;

ShowState (TestFile);

TestFile.close();

TestFile.open(“stuff.dat”, ios::in);

if(TestFile.fail())

{

cout << ” Cannot open the file \n”;

return;

}

cout <<“Reading from the file \n”;

TestFile >> Num;

cout <<” Forcing bad read operation \n”;

TestFile << Num;

//Force an invalid read operation

ShowState(TestFile);

TestFile.close();

//Close the file

}

void ShowState(fstream &File)

{

cout << “File Status :\n”;

cout << ” eof bit:” <<File.eof() << endl;

cout << ” bad bit:” <<File.bad() << endl;

cout << ” good bit:” <<File.good() << endl;

File.clear();

}

datafile.open (“a:\\table.txt”, ios::in)

 

//12/10/02

// Turgut Tezir

// edid the file

#include <fstream.h>

#include <iostream.h>

void main (void)

{

fstream datafile;

char name [40];

datafile.open (“a:\\table.txt”, ios::in);

if (datafile == 0)

{

cout << “File error”;

return;

}

cout << “File open sucsessfully \n”

<<“Now Reading data from file : “;

cout<<“\n\n\n\n”;

for (int count = 0; count < 2; count ++)

{

datafile >> name;

cout << name << endl;

}

datafile.close();

}

coppy out.txt to in.txt

//12/10/02

// Turgut Tezir

// coppy out.txt to in.txt

#include <fstream.h>

#include <ctype.h>

void main (void)

{

ifstream infile;

ofstream outfile (“a:\\in.txt”);

char filename [30], ch, ch2;

cout << “\n Enter File name: “;

cin >> filename;

infile.open (filename);

if (infile == 0)

{

cout <<“\n Error Opening: “

<< filename;

return;

}

while (!infile.eof())

{

infile.get (ch);

ch2 = toupper(ch);

outfile.put(ch2);

}

infile.close();

outfile.close();

cout << “\n Done”;

}

String &operator +=(const String &S)

//Turgut Tezir

//11/26/02

#include<iostream.h>

#include<string.h>

class String

{

private:

char *str;

public:

String (const char*);

~String(){delete str;}

String &operator +=(const String &S);

void Print(){cout<<str;}

};

String::String(const char *cp)

:str(new char[strlen(cp)+1])

{

strcpy(str,cp);

}

String &String::operator +=(const String &S)

{

int len;

len=strlen(str) + strlen(S.str);

char *p = new char[len + 1];

strcpy (p,str) strcat(p,” “);

strcat (p, S.str);

delete str;

str = p;

return *this;

}

void main (void)

{

String S1(“Good”);

String S2(“Morning”);

S1 += S2;

S1.Print();

}

criate a file from a drive outputs the numbers below

//12/10/02

// Turgut Tezir

//This program writes three row of numbers to a file.

//This program criate a file from a drive outputs the numbers below.

#include<fstream.h>

#include<iomanip.h>

void main (void)

{

fstream outfile(“table.txt”, ios::out);

int nums [3][3]={2895,5,837,

34,7,1623,

390,3456,12};

for(int row=0; row<3; row++)

for(int col=0; col<3; col++)

{

outfile<<nums[row][col]<<endl;

}

outfile.close();

}

EXAM 1

Turgut Tezir

CS 503

Review first exam

1. Scope resolution Operator access global variable when a local

varibla of the same name is in scope ( pg 202).

2. // line comments. Doesn end with anything.

3. Function Signature is name of the function and

the types of the argument. Function signature does not

include the return type of the function.

4. cout>>age; operator is wrong.cout << age;

5. cin.getline (): input an entire line of the text into an array.

It takes Three arguments: a character array in which the line

of the text will be stored, a lenght and a delimiter character.

6. <iostream.h> :Contains function prototypes for the standart input

and standart output functions.

7. setw() Right justified.

and setiosflags steam manipulators to specify internal spacing

8. cout << “Hi!\n << setprecision(2) << setiosflags(ios::showpoint( << amr;

9.

#include <iostream.h>

int main()

{

int num=234;

char x=’A';

cout << num << x << endl;

return 0;

}

10.

#include <iostream.h>

#include <iomanip.h>

int main ()

{

double x, y, z,

sum, total,

tax = 0.0875;

 

cout << “\n Enter First price: “;

cin >> x;

cout << “\n Enter second price: “;

cin >> y;

cout << “\n Enter third price : “;

cin >> z;

sum= (x + y + z);

tax =static_cast< double >(sum) * 0.0875;

total = sum + tax;

total = sum + tax;

cout<<“********************”<<“total :”<<total << endl;

return 0;

}

11. Advantage of “Overloaded funktions” C++ enables several functions of the same

name to be defined, as long as these functions have different sets of parameters

(at least as far as their types are concerned) This is called function overloading.

12. Default Argument list has to be difine.

Default Argument : Function calls commonly pass a particular value of an argument.

The programmer can specify that such an argument is a default argument.

13. Overloaded functions are distinguished by their signatures. The compiler encodes

each function identifier with the number and types of its parameters to enable

type-safe linkage.

14.

#include <iostream.h>

int integerPower(int,int); // function prototype

int main()

{

int base, exponent;

cout<<“\n Enter base : “;

cin>> base;

cout<<“\n Enter exponent: “;

cin>> exponent;

 

cout<< integerPower(base, exponent) <<endl;

return 0;

}

int integerPower(int b, int e)

{

int number =1;

 

for (int i =1; i<=e; i++)

 

number= number * b;

return number;

}

15.

#include <iostream.h>

#include <iomanip.h>

#include <cstdlib>

int main()

{

for (int i= 1; i <=10; i++)

{

-1-) cout<<setw(10) << (1+rand() % 2);

-2-) cout<<setw(10) << (1+rand() % 100);

-3-) cout<<setw(10) << (1+rand() % 9);

-4-) cout<<setw(10) << (1000+rand() % 112);

-5-) cout<<setw(10) << (-1+rand() % 3);

-6-) cout<<setw(10) << (-3+rand() % 14);

 

if (i % 2 ==0)

cout<<endl;

}

return 0;

}

 

16. a). x=ceil(-9.15) is -9.0

ceil(X): Rounds X to the smallest integer not less than X

 

b). x=fabs(-8.4) is 8.4

fabs(x) absulute value of x.

c). x=floor(6.5) is 6.0

floor(x): rounds x to the largest integer not greater than x.

True- False:

17. T

18. F

19. T

20. T

21. T

22. F

EXAM 2

Turgut Tezir

CS 503

 

1. Find & correct the errors in each of the following program segments;

a. #include<ctype.h>

b. const arraySize = 10;//ArraySize was declered constant.

c. int b[10]={0};

for (int i=0; i<=10; i++)

b[i]= 0;

d. int a[2] [2]= {{1,2}, {3,4}};

a[1][1]=5;

2. State which of the following are T and which are F

a. T

b. T Arrays occupy space in memory.

c. F int p[100];

d. T

e. T

3. Define

a. Arrays: Array is a consecutive group of memory locations that all have the same

name and same type.

b. Data Type: Data types stored procedures, functions and methods.

There re different Data Types such as char, double, int, float.

c. Subscript: The position number contained within square brackets is called subscript.

d. Searching & Sorting: Searching is a method of locating specific item

of information in a large collection of data.

4 What is the purpose of the scope resolution operator (:)? You can tell the

compiler to use the global identifier rather than the local identifier by prefixing the identifier with ::,

the scope resolution operator. The identifier can be a variable or a function.

If you have nested local scopes, the scope resolution operator does not provide

access to identifiers in the next outermost scope. It provides access to only the global identifiers.

5.

#include<iostream.h>

int Mystery3(const char *, const char *);

int main(void)

{

char string1[80], string2[80];

cout<<“\n Enter two string: “;

cin>>string1>>string2;

cout<<“\n The result is:”

<<Mystery3(string1, string2)<<endl;

return 0;

}

int Mystery3 (const char *s1, const char *s2)

{

int c=0;

for(; *s1 !=’\0′ && *s2 !=’\0′; s1++, s2++)

if (*s1==*s2)

c++;

return c;

}

First String t u r g u t t e z i r

Second String t e z i r t u r g u t

X X

This program compare two strings and count the letter in same position

in this case program give us the result 2

6 How does setw( )behave differently from the other manipulator? :

This parameterized manipulator sets the stream’s internal field width parameter

For example: cout << setw (10); This code uses setw to set the field width to 10 characters.

7. cout <<” \nHi!” << setprecision(2) << setiosflags(ios::showpoint) << amt;

8. How many bytes are allocated & what are the ranges for the following types?

a. char: Length 8 Bits, Range -128 to 127

b. int Length 16 Bits, Range -32,768 to 32,767

c. float Length 32 Bits, Range 3.4×10 to 3.4×10

9 Develop a program that generates 13 random numbers between 5 & 15, anew line

&13 random characters between A & Z?

#include <iostream.h>

#include <iomanip.h>

#include <stdlib.h>

//#include <time.h>

void main (void)

{

char a = ‘ ‘;

srand (time (0))

for (j=1; j<=13; j++)

{

srand(unsigned (time(0));

a=char (rand());

a=char (65+rand()%57)

cout<<a<<” “;

}

for (int i= 1; i <=13; i++)

{

if (i % 14 ==0)

cout<< setw(3)<< (5+rand() % 15)<<“\n”<<endl;

}

10.

#include<iostream.h>

#include<string.h>

#include<ctype.h>

long sum1(int a[], int n);

long sum2(int*a, int n);

void main(void)

{

const int SIZE=10;

int array[SIZE]={20,10,5,39,4,16,19,26,31,20};

long answer1=0, answer2=0;

answer1=sum1(array, SIZE);

answer2=sum2(array, SIZE);

cout<<“\nTotal from Sum: “<<answer1

<<“\nTotal From Sum2: “<<answer2

<<“\nSize of Array: “<<sizeof(int);

}

long sum1(int a[], int n)

{

long total=0;

int i=0;

while(i<n)

total +=a[i];

i++;

}

return total;

}

long sum2(int*a, int n)

{

long total=0;

int i=0;

while (i<n)

{

total=*(a+i);

i++;

}

return total;

}

OR

long sum1(int a[], int n)

{

int x= 0;

for (int i=0; i<n; i++)

x+=a[i];

return x;

}

long sum2(int*a, int n)

{

int y=0;

for(int i=1;i<*a;i++)

y+=n;

return y;

}

Links

file:///C:/Documents%20and%20Settings/cett/Local%20Settings/Temporary%

20Internet%20Files/Content.IE5/X7NFL1OE/281,1,Chapter 8 – Searching and Sorting Arrays

http://www-3.ibm.com/software/data/db2/udb/ad/v7/adg/db2a0/frame3.htm#db2a0158

http://www-3.ibm.com/software/data/db2/udb/ad/v7/adg/db2a0/frame3.htm#PToC_656

http://www.cprogramming.com/tutorial.html

http://cplus.about.com/library/weekly/aa050102h.htm?terms=searching+and+sorting

EXAM 3

Turgut Tezir

Exam 3

1. Why do OOP programmers tend to use OOP for all their programs?

OOP is importand for programmer because OOP encapsulates data (atributes) and function (behaviors)

into packages called clasess. Data and functions of a class are intimately tied together.

Class can be use many times to make many objects of the same class.

2. What does the OOP stand for? What is the tem for non-OOP languages?

OOP (Object Oriented Programming) Structured programming and information system Management.

3.What is a function’s signature?

Function signature is: The portion of a function prototype that includes the name of the function

and the type its arguments is called the functioin signature. Function Signature does not include

the return type of the function.

4. What is refrerence? How do I define a reference?

reference can be used to perform call-by-reference.Pointers enable programmers to simulate call-by-reference

and to create and manipulate dynamic data structures ( data structures that can grow and shrink), such

as linked lists, queues,stack and trees.

5. Why is dynamic memory allocation becoming even more important as time goes by?

Project 1.0: Character, integer

//Turgut Tezir

// project 1

#include<stdlib.h>

#include<iostream.h>

#include<time.h>

void main (void)

{

char loop,ch;

srand (unsigned (time (0)));

do // outer loop

{

cout<<“\n Character: “<<“\t\t”<<“Integer\n”;

do //inner loop

{

ch=char (65 + rand () %57);

//while (char (65 +rand () %57) <‘a’ && > ‘Z’){

}

while(ch < ‘a’ && ch > ‘Z’);// inner while

//ch=char (65 + rand () %57);

cout<< “\n\t” << ch << “\t\t”<< int (ch);

cout<< “\n do again (y/n) “;

cin>>loop;

}

while (loop==’y’ ||loop== ‘Y’); // outer while

}

// inner loop picks a random char. and checks if it is between ‘a’to ‘Z’

// if it is, it prints otherwise go back picks another Rand. char.

 

Project 2.1: Main Calculation (midterm)

//Turgut Tezir

//PROJ MIDTERM 1 Main Calculation

#include<iostream.h>

#include<ctype.h>

void Add(int n1, int d1, int n2, int d2, int &n, int &d);

void Sub(int n1, int d1, int n2, int d2, int &n, int &d);

void Mult(int n1, int d1, int n2, int d2, int &n, int &d);

void Div(int n1, int d1, int n2, int d2, int &n, int &d);

void Reduce(int &n, int &d);

void Compute(char &op, int n1, int d1, int n2, int d2, int &n, int &d);

void main(void)

{

int num1=0,dem1=0,num2=0,dem2=0,num=0,dem=0;

char loop=’n’, dummy=’/’, op;

do

{

cout<<“\n Enter operation: (ex: 1/2+2/3): “;

cin>>num1>>dummy>>dem1>>op>>num2>>dummy>>dem2;

Compute(op,num1,dem1,num2,dem2,num,dem);

Reduce(num,dem);

if(op==’+’)

cout<<“\n”<<” Addition :”;

else if(op==’-‘)

cout<<“\n”<<” Subtraction :”;

else if(op==’*’)

cout<<“\n”<<” Multiplication :”;

else if(op==’/’)

cout<<“\n”<<” Division :”;

if(dem==1)

cout<<num;

else if(num==dem)

cout<< ” 1 “;

else cout<<num<<dummy<<dem;

cout<<“\n Do another: (y/n)”;

cin>>loop;

}

while(tolower(loop)==’y’);

cout<<“\n Bye! Have a Nice Day”;

}

void Add(int n1, int d1, int n2, int d2, int &n, int &d)

{

d=(d1*d2);

n= (d/d1*n1) + ( d/d2*n2);

}

void Sub(int n1, int d1, int n2, int d2, int &n, int &d)

{

n=(n1*d2)+(d2*n2);

d=(d1*d2);

}

void Mult(int n1, int d1, int n2, int d2, int &n, int &d)

{

n=(n1*n2);

d=(d1*d2);

}

void Div(int n1, int d1, int n2, int d2, int &n, int &d)

{

n=(n1*d2);

d=(d1*n2);

}

void Reduce(int &n, int &d)

{

int i=2;

for(; i<n||i<d;i++)

while( (n%i==0) && (d%i==0) )

{

n/=i;

d/=i;

}

}

void Compute(char &op, int n1, int d1, int n2, int d2, int &n, int &d)

{

switch(op)

{

case’+':Add(n1,d1,n2,d2,n,d);

break;

case’-‘:Sub(n1,d1,n2,d2,n,d);

break;

case’*':Mult(n1,d1,n2,d2,n,d);

break;

case’/':Div(n1,d1,n2,d2,n,d);

break;

Default:cout<< “\n Invalid Operation!”;

}

}

 

Project 2.2: Main Calculation with Class-1 (midterm)

//Turgut Tezir

//Proj. 4…CLASS

#include<iostream.h>

#include<ctype.h>

class cls1

{

public:

Add(int n1, int d1, int n2, int d2, int &n, int &d);

Sub(int n1, int d1, int n2, int d2, int &n, int &d);

Mult(int n1, int d1, int n2, int d2, int &n, int &d);

Div(int n1, int d1, int n2, int d2, int &n, int &d);

Reduce(int &n, int &d);

Compute(char &op, int n1, int d1, int n2, int d2, int &n, int &d);

private:

int d,n;

};

cls1::Add(int n1, int d1, int n2, int d2, int &n, int &d)

{

d=(d1*d2);

n= (d/d1*n1) + ( d/d2*n2);

}

cls1::Sub(int n1, int d1, int n2, int d2, int &n, int &d)

{

d=(d1*d2);

n= (d/d1*n1) – ( d/d2*n2);

}

cls1::Mult(int n1, int d1, int n2, int d2, int &n, int &d)

{

n=(n1*n2);

d=(d1*d2);

}

cls1::Div(int n1, int d1, int n2, int d2, int &n, int &d)

{

n=(n1*d2);

d=(d1*n2);

}

cls1::Reduce(int &n, int &d)

{

int i=2;

for(; i<n||i<d;i++)

while( (n%i==0) && (d%i==0) )

{

n/=i;

d/=i;

}

}

void Compute(char &op, int n1, int d1, int n2, int d2, int &n, int &d);

cls1 cl;

void main(void)

{

int num1=0,dem1=0,num2=0,dem2=0,num=0,dem=0;

char loop=’n’, dummy=’/’, op;

do

{

cout<<“\n Enter operation: (ex: 1/2+2/3): “;

cin>>num1>>dummy>>dem1>>op>>num2>>dummy>>dem2;

Compute(op,num1,dem1,num2,dem2,num,dem);

cl.Reduce(num,dem);

if(op==’+’)

cout<<“\n”<<” Addition :”;

else if(op==’-‘)

cout<<“\n”<<” Subtraction :”;

else if(op==’*’)

cout<<“\n”<<” Multiplication :”;

else if(op==’/’)

cout<<“\n”<<” Division :”;

if(dem==1)

cout<<num;

else if(num==dem)

cout<< ” 1 “;

else cout<<num<<dummy<<dem;

cout<<“\n Do another: (y/n)”;

cin>>loop;

}

while(tolower(loop)==’y’);

cout<<“\n Bye! Have a Nice Day”;

}

void Compute(char &op, int n1, int d1, int n2, int d2, int &n, int &d)

{

switch(op)

{

case’+':cl.Add(n1,d1,n2,d2,n,d);

break;

case’-‘:cl.Sub(n1,d1,n2,d2,n,d);

break;

case’*':cl.Mult(n1,d1,n2,d2,n,d);

break;

case’/':cl.Div(n1,d1,n2,d2,n,d);

break;

default:cout<< “\n Invalid Operation!”;

}

}

Project 2.3: Main Calculation with different files in class (midterm)

//Turgut Tezir

//PROJECT 2-3: Main Calculation with different file in class

#ifndef CLS1_H

#define CLS1_H

class cls1

{

public:

Add(int n1, int d1, int n2, int d2, int &n, int &d);

Sub(int n1, int d1, int n2, int d2, int &n, int &d);

Mult(int n1, int d1, int n2, int d2, int &n, int &d);

Div(int n1, int d1, int n2, int d2, int &n, int &d);

Reduce(int &n, int &d);

Compute(char &op, int n1, int d1, int n2, int d2, int &n, int &d);

private:

int d,n;

};

#endif

#include<iostream.h>

#include<ctype.h>

#include “cls1.h”

void Compute(char &op, int n1, int d1, int n2, int d2, int &n, int &d);

cls1 cl;

void main(void)

{

int num1=0,dem1=0,num2=0,dem2=0,num=0,dem=0;

char loop=’n’, dummy=’/’, op;

do

{

cout<<“\n Enter operation: (ex: 1/2+2/3): “;

cin>>num1>>dummy>>dem1>>op>>num2>>dummy>>dem2;

Compute(op,num1,dem1,num2,dem2,num,dem);

cl.Reduce(num,dem);

if(op==’+’)

cout<<“\n”<<” Addition :”;

else if(op==’-‘)

cout<<“\n”<<” Subtraction :”;

else if(op==’*’)

cout<<“\n”<<” Multiplication :”;

else if(op==’/’)

cout<<“\n”<<” Division :”;

if(dem==1)

cout<<num;

else if(num==dem)

cout<< ” 1 “;

else cout<<num<<dummy<<dem;

cout<<“\n Do another: (y/n)”;

cin>>loop;

}

while(tolower(loop)==’y’);

cout<<“\n Bye! Have a Nice Day”;

}

void Compute(char &op, int n1, int d1, int n2, int d2, int &n, int &d)

{

switch(op)

{

case’+':cl.Add(n1,d1,n2,d2,n,d);

break;

case’-‘:cl.Sub(n1,d1,n2,d2,n,d);

break;

case’*':cl.Mult(n1,d1,n2,d2,n,d);

break;

case’/':cl.Div(n1,d1,n2,d2,n,d);

break;

default:cout<< “\n Invalid Operation!”;

}

}

// cls1.cpp: implementation of the cls1 class.

#include “cls1.h”

// Construction/Destruction

#include <iostream>

#include “cls1.h”

cls1::Add(int n1, int d1, int n2, int d2, int &n, int &d)

{

d=(d1*d2);

n= (d/d1*n1) + ( d/d2*n2);

}

cls1::Sub(int n1, int d1, int n2, int d2, int &n, int &d)

{

d=(d1*d2);

n= (d/d1*n1) – ( d/d2*n2);

}

cls1::Mult(int n1, int d1, int n2, int d2, int &n, int &d)

{

n=(n1*n2);

d=(d1*d2);

}

cls1::Div(int n1, int d1, int n2, int d2, int &n, int &d)

{

n=(n1*d2);

d=(d1*n2);

}

cls1::Reduce(int &n, int &d)

{

int i=2;

for(; i<n||i<d;i++)

while( (n%i==0) && (d%i==0) )

{

n/=i;

d/=i;

}

}

 

Project 3.0 Sort the array (20)

//Write a program that takes various integer inputs from the keyboard into

//integer array ar[] with a size of 20, sorts & outputs them to the screen.

#include <fstream.h>

#include <iomanip.h>

void main()

{

int temp, i, j, number1;

const int arraysize = 20;

int mynumbers[arraysize];

for (i=0; i<arraysize; i++)

{

cout<<“Give number: “<<i<<” “;

cin>>number1;

mynumbers[i] =number1;

cout<<endl;

}

cout<<“Original array”<<endl;

for (i=0; i<arraysize; i++)

cout<<mynumbers[i]<<” “;

 

for (i=0;i<arraysize-1;i++)

{

for(j= i+1 ; j<arraysize; j++)

{

if (mynumbers[i] > mynumbers[j])

{

temp=mynumbers[i];

mynumbers[i]=mynumbers[j];

mynumbers[j]=temp;

}

}

}

cout<<endl<<endl<<“Sorted array”<<endl;

for (i=0;i<arraysize;i++)

{

cout<<mynumbers[i]<<” “;

cout<<endl;

}

}

 

Project 5.0: Last Project Formula

//Turgut Tezir

//Last Project compute the values of formula that express y in terms of x((FORMULA))

#include<iostream.h>

#include<math.h>

void main()

{

double r1,r2,y;

for(double x=-3; x<=4; x += 0.5){

r1 = 9 * pow(x,3) – 27 * pow(x,2) – 4 * x + 12;

r2 = sqrt(3 * pow(x,2) + 1) + fabs(5 – pow(x,4));

if (r2==0)

cout<<“You can not divide by ZERO”<< endl;

else

y = r1 / r2;

cout << “X=” << x << “Y=” << y << endl;

 

if (y==0)

cout <<“Y is ZERO”<< endl;

else if (y>0)

cout <<“Y is POSITIVE”<< endl;

else

cout << “Y is NEGATIVE”<< endl;

 

}

}

Project 6.0: Final Project

//Turgut Tezir

//Project Final.

#include<iostream.h>

#include<iostream.h>

#include<fstream.h>

#include<time.h>

#include<stdlib.h>

void BubbleSort(int a[], const int SIZE);

void InsertSort(int c[], const int SIZE);

void main()

{

srand( unsigned(time(0)));

const int SIZE=1000;

int num,Arnum[SIZE];

ofstream newfile,last;

newfile.open (“a:\\wordi.txt”);

last.open(“a:\\results.txt”);

for (int i=0;i<1000;i++)

{ if(i%10==0)

newfile << endl;

num = 7+rand()%21993;

newfile << num << ‘\t';

}

newfile.close();

ofstream savfile;

savfile.open(“a:\\wordo.txt”);

ifstream infile;

infile.open(“a:\\wordi.txt”);

last<<“results”

<<“\n\n\n”;

cout<<“results”

<<“\n\n\n”;

last<<“Bubble Sort Now.\n\n\n”;

cout<<“Bubble Sort Now.\n\n\n”;

i=0;

while(infile >> num)

{

Arnum[i]= num;

if(i%10 == 0)

{

cout << Arnum[i] << ‘\t';

last << Arnum[i] << ‘\t';

}

i++;

}

clock_t start, finish;

float duration;

cout << “\n\n\nMeasuring BubbleSort Now.\n\n\n “;

start = clock();

BubbleSort(Arnum,SIZE);

finish = clock();

duration = (float)(finish – start) / CLOCKS_PER_SEC;

last << “\n\n Meassuring BubbleSort was:\n\n”;

cout << “seconds:” << duration << “\n\n”;

last << “seconds:” << duration << “\n\n”; //saving in results.txt

for(i = 0; i < SIZE; i+=10)

{ cout << Arnum[i] << ‘\t';

last << Arnum[i] << ‘\t';

}

cout<<“\n\n\n\n”;

last<<“\n\n\n\n”;

infile.close();

infile.open(“a:\\wordi.txt”);

last << “Selection Sort Now.\n\n\n”;

cout << “Selection Sort Now.\n\n\n”;

i = 0;

while(infile >> num)

{

Arnum[i] = num;

if(i%10 == 0)

{

cout << Arnum[i] << ‘\t';

last << Arnum[i] << ‘\t';

}

i++;

}

cout << “\n\nMeasuring Insertion Sort Now.\n\n”;

start = clock();

InsertSort(Arnum, SIZE);

finish = clock();

duration = (float)(finish – start) / CLOCKS_PER_SEC;

last << “\n\n Measuring Insertion Sort was:\n\n”;

cout << ” seconds: ” << duration << “\n\n”;

last << ” seconds: ” << duration << “\n\n”; //saving in results.txt

for(int j=0; j<SIZE; j++)

{

if(j%10 ==0)

{

savfile << ‘\n';

cout << Arnum[j] << ‘\t';

last << Arnum[j] << ‘\t';

}

savfile << Arnum[j] << ‘\t';

}

infile.close();

savfile.close();

last.close();

}

void BubbleSort(int a[], const int SIZE)

{

int hold = 0;

for( int pass = 1; pass < SIZE; pass++)

for( int j = 0; j < SIZE-1; j++)

if(a[j] > a[j+1])

{

hold = a[j];

a[j] = a[j+1];

a[j+1] = hold;

}

}

void InsertSort(int c[], const int SIZE)

{

int position, insert;

for(int i = 1; i < SIZE; i++)

{

insert = c[i];

for (position = i – 1; position >= 0 && insert < c[position];

position–)

c[position + 1] = c[position];

c[position + 1] = insert;

}

}