C++ Intermediate
Beginner | Intermediate | Advance
Lecture Notes
Other Programs:
String
Mystery String
Math. Calculation
Class Rectangle
Semester
Calculate Tax
Power
Total Price xyz
Roll a six-sided die 6000 times
40 Student Cafeteria 1 to 10
Random | 2 String & mystery
Operation Program
Compute calculation Add, Sub, Mult, Div
Getline
Getline2
Prints the array in tabular format
//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";
}
}
Mystery String
//10/22/02 COMPLITE THIS PROGRAM>>>>>>>>>>>>>>>>>>>>>>
// Write a program that takes a string from the keyboard &
//uses a function to output one character per line each charactre
//of the string and its integer equivalant
#include<iostream.h>
void mystery1(char *, const char *);
int main()
{
char string1[80];
cout <<"Enter the string:";
cin >>string1;
mystery1(string1);
cout<<string1<<endl;
return 0;
}
void mystery1(char *s1)
{
while (*s1 !='\0')
++s1;
for (; *s1 = *s2; s1++, s2++)
; // emty statement
}
Math. Calculation
/Turgut Tezir
//PROJECT 2
#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)
{
n=(n1*d2)+(d2*n2);
d=(d1*d2);
}
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!";
}
}
Class Rectangle
#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();
}
Semester
//10/22/02
//Write a program that takes a string from the keyboard &
//uses a function to output one character per line each charactre
//of the string and its integer equivalant
//PROJECT: Write a Program or generate this program
//Input file:Wordi.txt
//a Contains Generated by your program
//b Contained 1000 rondom integer values between 7 And 21000
//OUTPUT FILE: Wordo.txt
//Generated by this modified program
//sort all integer values
//RESULT: Result.txt
//screen outputs
//you should have two program
//After you complite all program you will total 5 programs
//NOTE clock_t (type)
//Defined In
//time.h
//Description
//The CLK_TCK constant defines the number of clock ticks per second. This data type
//is returned by the clock function stores an elapsed time measured in clock ticks.
//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';
}
Calculate Tax
#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;
}
Power
#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;
}
Total Price xyz
#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;
cout << "\n Without tax Pric is...........:"<< sum << endl;
cout <<"\n Tax is.........................: "<< setprecision(2)
<<setiosflags(ios::fixed | ios::showpoint)<< tax << endl;
total = sum + tax;
cout<<"\nThe total price is...............:"<< total << endl;
return 0;
}
Roll a six-sided die 6000 times.
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
//#include <ctime.h>
int main()
{
const int arraySize =7;
int face, frequency[arraySize]={0};
srand(time(0));
for (int roll=1; roll<=6000; roll++)
++frequency[1+rand() %6]; // replace 20 line switch
cout<< "Face" <<setw(13) <<"Frequency" <<endl;
// ignore element zero inthe frequency array
for (face=1; face < arraySize; face++)
cout<< setw(4) << face <<setw(13) << frequency[face] <<endl;
return 0;
}
40 student Cafeteria 1 to 10
#include <iostream.h>
#include <iomanip.h>
int main()
{
const int responseSize =40, frequencySize = 11;
int responses[ responseSize ] = {1,1,1,1,1,1,2,2,2,3,4,5,6,7,8,6,5,4,5,3,5,3,
3,5,6,7,4,2,1,1,2,1,2,3,4,5,6,7,8,9};
int frequency [ frequencySize ] = {0};
for ( int answer = 0; answer < responseSize; asnwer++)
++frequency [responses[answer] ];
cout <<"Rating" << setw(17) << "Frequency" << endl;
for ( int rating = 1; rating < frequencySize; rating++)
cout << setw(6) << rating
<<setw(17) <<frequency[ rating] endl;
return 0;
}
Random
#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;
}
2 String & mystery
// Turgut Tezir
#include<iostream.h>
void mystery1(char *, const char *);
int main()
{
char string1[80], string2[80];
cout <<"Enter two string:";
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++)
; // emty statement
}
Operation Program
//Turgut Tezir
//CS503.2330
// This is operation program
#include<iostream.h>
#include<ctype.h>
float Add(float n1, float n2);
float Sub(float n1, float n2);
float Mult(float n1, float n2);
float Div(float n1, float n2);
float pow(float n1, float n2)
float Compute(float &n1, float &n2);
void main(void)
{
int num1, num2;
cout <<"\n Enter mum1: ";
cin >> n1;
cout <<"\n Enter num2: ";
cin >> n2;
}
void Add(float n1, float n2)
{
n=(n1+n2);
}
float Sub(float n1, float n2)
{
n=(n1-n2);
}
float Mult(float n1, float n2)
{
n=(n1*n2);
}
void Div(int n1, int d1, int n2, int d2, int &n, int &d)
{
n=(n1/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!";
}
}
Compute calculation Add, Sub, Mult, Div
#include<iostream.h>
#include<ctype.h>
//typedef int integer
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>>den1>>op>>num2>>dummy>>den2;
compute(op,num1,den1,num2,den2,num,den);
reduce(num,den);
if (dem==1)
cout<<'\n'<<num1<<dummy<<den1<<op<<num2<<dummy<<den2<<'=';
if(dem==1)
cout<<num;
else if(num==den)
cout<< " 1 ";
else cout<<num<<dummy<<den;
cout<<"\n Do another: (y/n)";
cin>>loop;
{
while(tolower(loop)=='y');
cout<<"\n Bye!";
}// closin main
void Add(int n1, int d1, int n2, int d2, int &n, int &d)
{
n=(n1*d2)+ (d2*n2);
d=(d1*d2);
}
void Sub(int n1, int d1, int n2, int d2, int &n, int &d)
{
n=(n1*d2)- (d1*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*n2);
d=(d1*d2);
}
void Reduce(int &n, int &d)
{
int i=2;
for(; i<num||i<den;i++)
while(num%i==0&d
den%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!";
}
}
Getline
// 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;
//}
Getline2
// 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];
}
Prints the array in tabular format
// initialize the elements of a ten elements integer array n to zeros
//and prints the array in tabular format.
#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;
}
Turgut Tezir @ tezir.com