Date: Jan 10,2003   When I started developing these Programs

C plus plus Programs
   Sample 1 ;A menu Driven Program using an Array
   Sample 2;A menu Driven Program without an array
   Sample 3;Example of Structure
   Sample 4;A program about Class
   Sample 5;Program about Nested if else statements
    Sample 6; Currency conversion Program



Programmer : Tariq Chaudhary
Tools: Visual C++ 6.0



Menu Driven Program with an Array

Write Program: Depending of the users input your program should perfom the following operations:

* 1:Read 10 real numbers from the keyboard and store them in an array.

* 2:Perform the following basic statistical operations on the 10 numbers entered by the user and print the results onto the screen:

o calculate the mean
o calculate the variance
o calculate the standard deviation
* 3:Leave the program.


Solution:

#include<iostream.h>
#include<math.h>    //this is for sqrt()
#include<iomanip.h> //this is for setw()

  const int size=10;

void bsort(int num[size]);

#include<iostream.h>
#include<math.h>  //for sqrt()

 const int size =10;
 static int  num[size];

 void menuprint();
 void GetData();
 void doCalculation();
   int i;
 void main()

	{

	     int choice;
	     cout<<endl;

		do

    	 	 {
    			menuprint();
       			cin>>choice;
    			cout<<endl;

   		       switch(choice)
      
			 { 	case (1): { GetData();
                        			                   	
		   		break;}
     
         				case (2): {doCalculation();
		       		 break;} 

				case (3):cout<<"Quiting the program\n";
                       		 break;
                        	default:

		       		if(choice!=1||2||3)
			   	cout<<"Error:You made an incorrect choice?try again\n";
                            		 break;
		    		      
	  		}
      		 }while(choice!=3);

      		 cout<<endl;

 }
     
       void menuprint()

     	  {
    	 	cout<<endl;
    		cout<<"Make your selection:";
    		cout<<endl;
    		cout<<endl;
		cout<<"1.*** Enter Data ***\n";
   		cout<<"2.*** Calcualte Mean,Vaiance and Standard Deviation ***\n";
    		cout<<"3.*** Quit ***\n";
    		cout<<endl;
    		cout<<"Please enter your choice(1,2,3)!";

		 //return(0);
	}


   void  GetData()
      {

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

    
      	    {
     		 cout<<"please enter number 1:";
     		 cin>>num[i];
     		 ++i;
     		cout<<"please enter number 2: ";
     		 cin>>num[i];
      		 ++i;
      		cout<<"please enter number 3: ";
      		cin>>num[i];
       		 ++i;
      		cout<<"please enter number 4: ";
      		cin>>num[i];
      		++i;
      	       	cout<<"please enter number 5: ";
      		cin>>num[i];
		++i;
	       	cout<<"please enter number 6: ";
      		cin>>num[i];
		++i;
		cout<<"please enter number 7: ";
      		cin>>num[i];
		++i;
		 cout<<"please enter number 8: ";
      		cin>>num[i];
		++i;
		cout<<"please enter number 9: ";
      		cin>>num[i];
		++i;
		cout<<"please enter number 10: ";
      		cin>>num[i];
		++i;  

	    }
     }     
    
	  void  doCalculation()
      
	   {
            
	     int     sum=0;
	     float   mean;
	     float   variance;
	     double  stdDev;

		for(i=0;  i<size;   i++)
    	        sum += num[i];   //formula for calculating sum
                mean = (sum)/size;   	//this is formula for calculating the mean

		//the following formula is  for calculating the variance

	      variance = ((num[0]-mean)*(num[0]-mean))+((num[1]-mean)*(num[1]-mean))+
			   	((num[2]-mean)*(num[2]-mean))+((num[3]-mean)*(num[3]-mean))+
			   	((num[4]-mean)*(num[4]-mean))+((num[5]-mean)*(num[5]-mean))+
		 	   	((num[6]-mean)*(num[6]-mean))+ ((num[7]-mean)*(num[7]-mean))
		  	  	+((num[8]-mean)*(num[8]-mean))+ ((num[9]-mean)*(num[9]-mean))/size-1;
                       
		  //formula for standard deviation

		stdDev = sqrt(variance);
	  	 cout<<endl;
		 cout<<"	The Sum of 10 numbers is  :	  "<<sum<<endl;
		 cout<<"	The Mean of 10 numbers is :	  "<<mean<<endl;
		 cout<<"	The Variance of  10 numbers is  : "<<variance<<endl;
		 cout<<"	The Standard deviation of 5 numbers is : "<<stdDev<<endl;

		  
	      }

      		void bsort(int num[size])

			{

	   			int temp;
	   			int ctr1,ctr2;

				for(ctr1=0; ctr1<(size-1); ctr1++)

		     		{ for (ctr2=(ctr1+1); ctr2<size; ctr2++)

			  	      {if(num[ctr1] > num[ctr2])  // this is for ascending order

			    		{  temp = num[ctr1];
					num[ctr1] = num [ctr2];
					num[ctr2] = temp;

			     		 }
			         }

		       }

	     }



*****************************End of the Program**********************************************                      top

Menu Driven Program without an Array



#include <iostream.h > 
#include <math.h >  // for pow()
#include<iomanip.h>  //for setw()

 	 void menuprint();
	 void GetData();
	 void GetInterest();
 	static int   p,r,n,i,f;

 <!--    //p=principle amount r=rate ,n=number of years,i =Interest,f=compound interest-->

	void  main()

	 {
 
	   int choice;

		//cout<<"Programmer:Tariq,Date:Dec 09,2001 \n";

	       // cout<<endl;

	   	do

   			{

    		    		menuprint();
          		    	 cin>>choice;
    	        		 	 cout<<"\n";
   	 				switch(choice)
      
		         		 { 	case (1): { GetData();
                        			      break;}
              					case (2): {GetInterest();
		        				break;} 
						case (3):cout<<"	Quiting the program\n";
                       		 			break;
                        			default:
						if(choice!=1||2||3)
				 		cout<<"Error:You made an incorrect choice?try again\n";
                            				break;
	  	       			  }

      	 }while(choice!=3);

       		cout<<endl;

   	    }
     
	 		void menuprint()

    		 	{
    	 			cout<<endl;
				cout<<"		Make your selection:";
    				cout<<endl;
    				cout<<endl;
				cout<<"		1.*** Enter Data ***\n";
   				cout<<"		2.*** Get Interest ***\n";
    				cout<<"		3.*** Quit *** \n";
    				cout<<endl;
			 	cout<<"		Please enter your choice(1,2,3)!";

			}


  	 		void  GetData()     //get data from the user
     			 {
                    
				cout<<"	Your selection is 1 which is Get Data:\n"<<endl;
				cout<<"		Please enter the Principle amount?$";
   	       			cin>>p;
      				cout<<endl;
				cout<<"		Please enter interest rate:";
				cin>>r;
				cout<<endl;
				cout<<"		Please enter Loan/Investment term in years)?:";
				cin>>n;  

			}
    
			void  GetInterest()     //will calculate the interest

        			{
		
				cout<<"	Your selection is 2 which is to Calculate Interest"<<<<endl;                          		 cout<<setw(8)<<" 	Data you entered is:"<<endl;
			 	cout<<setw(8)<<" 	Principle amount:$"<<p;endl;
			 	cout<<setw(8)<<" 	Interest Rate :"<<r<<endl;
			 	cout<<setw(8)<<"	Term In years:"<<n<<endl;
			   	//interest rate

                           	 //i1=r/100;
			   	 //i= p * (r/100);

			   	 i = (p* r/100 *n);  //this  does not work properly
			    	 //formula for compound interest

			 	//f = p * pow((1+i),n);

			 	cout<<"		The Interest is:$"<<i;
                         		cout<<endl;
        
        		 } //end of GetInterest()


*****************************End of the Program**********************************************                      top

Example of structure

Program:
Define a structure that will store information about a given stock. Include fields for: Name of company, symbol, shares owned, purchase price, previous close price. Create an instance of the struct and input some data into all of the fields.


Solution:

#include<iostream.h>
 
    struct stock_info //name of the structure
	
	{	
		char   comp_name;
		char  stock_symbol;
		long int shares_owned;
		float purchase_price;
         		float prev_close_price;

	};

		void main()

 		 {

     			stock_info stock;   //stock is a structure variable
		  	stock.comp_name = 'First Investment'; //assign values to structure
	          	stock.stock_symbol = 'FI';
		  	stock.shares_owned = 100000;
		  	stock.purchase_price =  45.00;
		  	stock.prev_close_price = 38.00;

                           //display the information

		  	cout<<"\n Company Name "<<stock.comp_name;
		  	cout<<",Stock Symbol  " <<stock.stock_symbol;
		  	cout<<" ,Shares Owned " <<stock.shares_owned;
		  	cout<<", Purchase Price "<<stock.purchase_price;
		         cout<<" ,Previous Close Price"<<stock.prev_close_price;
 		

		}


*****************************End of the Program**********************************************                      top

Example of Class


Program:
For this assignment you have to implement a class that models a table in a restaurant in terms of ordering. Every table (or rather the customers at the table) can order from a list of items. Every item is characterized by its name and its price. At the end the user wants to print out the bill for that particular table. You have to decide what private data members your class needs and then the following methods have to be implemented: default constructor void addItem(string itemName, double price): adds an item to the tables bill. void printBill():prints the tables bill onto the screen. You can assume that no table will order more than 50 items. Note that the main program and the class Table have to be in different files and you will only submit the file that contains the class table.

	#include<iostream.h>
	#include <iomanip.h> //for setw()
 
	//you can change value of max from 5 to 60 or any
		const int MAX =5;
		class table
  
		 {
  			private:
     			char item_name[12];
    			float item_price;
                		//int n;
 			 public:
 
   		 	void additem()
    		 	 {
                  
      			   cout<<"enter item name:";
      			   cin>>item_name;
      			   cout<<endl;
                    	   cout<<"enter the item price:";
       			   cin>>item_price;
     
      			 }
 
    
    		      void printBill()
 
     		       {
 
                          cout<<"\n   Item Name"<<setw(12);
        		  cout<<setw(2)<<"  item's price"<<endl;
      			  cout<<"  "<<item_name;
      			 cout<<"  "<<item_price;
 
    		       }
 	     };
 

     		void main(void)
 
    		 {
                     int n=0;
     		     table t1[MAX];
                     char ans;
            	     cout<<endl;
           	     cout<<" **********   Order for table1 ******"<<endl;
             		do
 
                 	 {
 
                       		cout<<" item no : "<<n+1<<endl;
                               t1[n++].additem(); //for adding items
                        	cout<<"Enter another  (y/n)?";
                        	cin>>ans;
                        	if(n >= MAX)
                           	{
                                 	cout<<" stop here"<<endl<<endl;
                                	break;
                          	 }
     			 }
 
       			while(ans !='n');
      			cout<<"******* BILL of Table1  *****";
       			  for(int j=0;j<n;j++)
             
       			{
       				cout<<endl;
        			cout<<j+1;      
        			t1[j].printBill(); //function for printing bill on the screen
                           	cout<<endl;
 
       			}
    	     }





*****************************End of the Program**********************************************                      top

Nested if and else statements

Program:
Write a program that will input 3 integers representing the MONTH, DAY, YEAR and will determine if these numbers represent an actual calendar date.
Step 1: Course check. Reject all dates with MONTH > 12 or MONTH < 1, or DAY > 31 or DAY<1. This check will still allow some invalid dates to pass
Step 2: Account for varied days of the month. Reject dates with DAY > 28 for MONTH == 2, DAY > 30 for months witth 30 days and DAY > 31 for months with 31 days. This check will not account for leap years.
Step 3: Account for leap years. If YEAR is a leap year and MONTH == 2 then reject DAY > 29.

//solution:

#include <iostream.h>
 
 void main(void)
 
 {
 
      int month,days,year;
      int even,odd;
      even = 2,4,6,8,10,12;
      odd  = 1,3,5,7,9,11;
 
       cout<<"Enter the month of the year in number:";
       cin>>month;
       cout<<"Enter days of this month:";
       cin>>days;
       cout<<"enter the year:";
       cin>>year;
  
  
       	  if(month> 12 || month < 1 ||days >31 || days<1 )        //for step:1
   
       		 cout<<" You did some thing wrong,so try again?"<<endl;
   
             else if (days  ==29 && month==2 ||days  >30 && month==even ||days >31 && month ==odd)   // for step 2:
 
        	  cout<<"you did  some thing wrong which is not allowed"<<endl;

            else if (days ==30 && month ==2)                   //for step: 3
				
		 cout<<"This is not allowed for leap years,try some thing else?"<<endl;
           else
 
                 cout<<"you entered correct date format:"<<endl;
 
     }


*****************************End of the Program**********************************************                       top
Currency Conversion Program //Filename:currcon.cpp //This is currency conversion program it will convert dollrs into rupees and vice versa // Also this will convert amount in pounds,shilling and pence into Pounds& pence only

#include<iostream.h>

void main()

   {

		float pound,shilling,pence;
		float decimalpounds;
		long int rupees,dollar;
        	int cents,paisa;
        	float currentRate;
			//cout<<"\n*********Programmer:Tariq  Chaudhary ******"<<endl;
			//cout<<"\n";
			cout<< "If you want to end this program just type 100.5"<<endl;
			cout<<"current rate:";
			cout<<endl;
		do
			{
	   
				cout<<"		Enter the amount in Pounds?";
				cin >> pound;
				cout<<"\n";
				cout<<"		Enter the amount in shilling?";
				cin >> shilling;
				cout<<"\n";
				cout<<"		Enter the amount in pence?";
				cin >> pence;
				cout<<"\n";
				decimalpounds=pound+((shilling /20)+(pence/100));//This will convert the amount in decimal pounds
				cout<<"\n";
				cout<<"The required amount in decimal pounds is: £"<<decimalpounds<<endl<<endl;
	
				cout<<"** Next is Currency conversion Program**";
				cout<<"\n";
				cout<<"		Enter the amount in Dollars?";
				cin >> dollar;
				cout<<"\n";
				cout<<"		Enter the amount in Cents?";
				cin >> cents;
				cout<<"\n";
				cout<<"		Enter the current Rate/value of the Pak currency?";
				cin >>currentRate;
				rupees =( dollar+(cents/100)) * currentRate; // This calculation depends upon the current market rate
				cout<<"\n";
				cout << "** The Equivalent ammount in Rupees is :Rs "<<rupees<<endl;
				cout<<"\n";
				cout<<"		Enter the amount in Rupees?";
				cin >> rupees;
				cout<<"\n";
				cout<<"		Enter the amount in Paisas?";
				cin >> paisa;
				cout<<"\n";
				dollar = (rupees+(paisa/100))/currentRate;  //This calculation will convert the rupees into US dollar
				cout << "** The Equivalent ammount in Dollars  is :$ "<<dollar<<endl;
				cout<<"\n";
        
	   }while ( currentRate != 100.5 );

			cout<<" $$$$$$$This is end of the program $$$$$$$$$" ;
   }


*****************************End of the Program**********************************************                       top