Tuesday, 17 January 2012

Write a C++ program that will merge the contents of two sorted (ascending order) arrays of type double values by storing the result in a third array (still in ascending order). Both of the input arrays can be of same or different lengths declare arrays with fixed size of 50 and then asks user to specify the...

Write a C++ program that will merge the contents of two sorted (ascending order) arrays of type double values by storing the result in a third array (still in ascending order). Both of the input arrays can be of same or different lengths declare arrays with fixed size of 50 and then asks user to specify the number values he wish to enter for each arrays individually. The resultant array should not contains duplicate values, so while merging contents in the third arrays you have to take care that if a value exist in both input arrays then only its one copy should be placed in the resultant array.







#include <iostream>

#include <conio.h>

using namespace std;



int main()

{

   int m,n;

   cout << "\n Enter the Number of Elements in First Array(MAx 50) = ";

   cin >> m;

   cout << "\n Enter the Number of Elements in Second Array(MAx 50) = ";

   cin >> n;



    double a[50],b[50];



    cout << "\nEnter " << m << " Numbers in First Array \n";

    for(int e=0; e<m; e++)

    {     cout << e+1 << ": ";

            cin >> a[e];    }

      

//      Using Bubble Sort Method

   

     double loc1;



    for(int o=0;o<m-1;o++)     //Sorting of First Array in Ascending Order

    {    for(int p=0;p<m-1;p++)

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

            {    loc1=a[p];

a[p]=a[p+1];

a[p+1]=loc1;    }

        }



    cout << "\nEnter " << n << " Numbers in Second Array \n";

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

    {       cout << f+1 << ": ";

            cin >> b[f];    }



    double loc2;

        for(int q=0;q<n-1;q++)     //Sorting of Second Array in Ascending Order

        {    for(int r=0;r<n-1;r++)

                        if(b[r]>b[r+1])

                {    loc2=b[r];

                    b[r]=b[r+1];

                    b[r+1]=loc2;    }

        }



    int l=m+n;            //Combining First and Second Array Length

    double c[l];



//Embedding First and Second Array in a Third Array

  

     for(int g=0;g<m;g++)

        c[g] = a[g];      

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

        c[m+h] = b[h];



    double loc3;



    for(int i=0;i<(l-1);i++)

    {    //Sorting of Third (Embedded) Array in Ascending Order

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

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

        {    loc3=c[j];

            c[j]=c[j+1];

            c[j+1]=loc3;    }

        }

    double e[l];

    int r=0,p=0;

    for(int k=0; k<l; k++)

      {

        if(c[k+1]!=c[k])        //Removing Duplicates

            {e[r]=c[k];         //Embedding Third Array in Fourth

                r++;}

        else

            p=l-1;

      }

        l=l-(p-r+1);

//Printing Out the Final Sorted Array discarding Duplicate Values

        cout << endl << "\tThe Sorted Array is \n";

    for(int q=0;q<l;q++)

        cout << e[q] << endl;

    getche();

    return 0;

}



Write a program that will calculate and print bills for the city power company. The rates vary depending on whether the use is residential, commercial, or industrial. A code of R means residential use, a code of c means commercial use, and a code of I means industrial use


Write a program that will calculate and print bills for the city power company. The rates vary depending on whether the use is residential, commercial, or industrial. A code of R means residential use, a code of c means commercial use, and a code of I means industrial use. Any other code should be treated as error.

The rates are computed as follows:

R: $6.00Rs plus $0.052 per kwh used
C: $60.00 for the first 1000kwh and $0.045 for additional kwh.
I: Rate varies depending upon time of usage given below:
 
      Peak hours: $76.00 for first 1000 kwh and $0.065 for each
                                                             additional kwh.
      Off-peak hours: $40.00 for first 1000 kwh and $0.028 for each
                                                                     additional kwh.

Your program should prompt the use to enter his integer id, the use code (type char), and the necessary consumption figures in whole numbers of kilowatt-hours. Your program should display the amount due from the user.


#include<iostream>
#include<conio.h>
#include <iomanip>

using namespace std;

int main()
{
    int id, consumption;
    double bill;
    char code,ch;

    cout << "Enter your ID: ";
    cin >> id;

    cout << "\nEnter your Use Code (R/r, C/c or I/i): ";
    cin >> code;

    if(code=='r' || code=='R')
    {
        cout << "\nEnter your consumption figures in whole numbers of kwh: ";
        cin >> consumption;
        bill = consumption*(0.052 + 6.00);
    }

    else if(code=='c' || code=='C')
    {
        cout <<"\nEnter your consumption figures in whole numbers of kwh: ";
        cin >> consumption;

        if (consumption <= 1000 )
            bill = 60.00;
        else
            bill = (consumption-1000)*0.045 + 60.00;
    }

    else if(code=='i' || code=='I')
    {
        cout << "\n Enter P/p for Peak hours and O/o for off-peak hours consumptions: ";
        cin >> ch;

            if(ch=='O' || ch=='o')
            {
                cout << "\nEnter your off-peak hours consumption figures in whole numbers of kwh: ";
                cin >> consumption;

                if (consumption <= 1000)
                    bill = 40.00;

                else
                    bill = (consumption-1000)*0.028 + 40.00;
            }
            else if(ch=='P' || ch=='p')
            {
                cout << "\nEnter your peak hours consumption figures in whole numbers of kwh: ";
                cin >> consumption;

                if (consumption <= 1000)
                    bill = 76.00;

                else
                    bill = (consumption-1000)*0.065 + 76.00;
            }
            else
                    cout << endl << "Invalid Character\n\n";
    }
    else
        cout  << endl << "\n \t\tERROR in CODE \n";

    if(code=='r' || code=='R' || code=='c' || code=='C' || code=='i' || code=='I')
    {
    cout << "\nThe Amount Due is:\t" << fixed << setprecision(2) << (double)bill << " $\n";
    }
    getche();
    return 0;
}



Write a function that computes and displays a table of negative powers of two (2-1, 2-2 …..) as both common fractions and decimals. The range of powers printed should be determined by the function’s input arguments. Test your function using a driver.


Write a function that computes and displays a table of negative powers of two (2-1, 2-2 …..) as both common fractions and decimals.
The range of powers printed should be determined by the function’s input arguments. Test your function using a driver.




#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;

int main()
{
    void Power(int a, int b);
    int a, b;
    cout << "Enter Upper value for power of 2 = ";
    cin >> a;
    cout << "Enter Lower value for power of 2 = ";
    cin >> b;
    Power(a, b);
    getche();
    return 0;
}
void Power(int a, int b)
{
        cout << endl << "Power of 2 \t Fraction \t Decimal Values\n";
    for(int i=a; i>=b; i--)
        cout << "    " << i << "\t\t   1/" << pow(2,-i) << "\t\t\t"
           << pow(2,i) << endl;
}


Write a C++ program that contains three user defined function named input, largest and output, and one main function. All the inputs must be taken in input function, and all the results must be displayed in output function that will receive one argument of type float. The function named largest receives three floating point arguments whose values must be given by user in the input function and then this function will find out the largest of three no’s and the result of the largest will be displayed in output function. In main function you just have to call input function. So there...


Write a C++ program that contains three user defined function named input, largest and output, and one main function. All the inputs must be taken in input function, and all the results must be displayed in output function that will receive one argument of type float.  The function named largest receives three floating point arguments whose values must be given by user in the input function and then this function will find out the largest of three no’s and the result of the largest will be displayed in output function.
In main function you just have to call input function. So there will be no other function call in the main function.





#include<iostream>
#include<conio.h>
#include<process.h>

using namespace std;
    void Input();
    void Largest(float x, float y, float z);
    void Output(float Large);

int main()
{
    Input();
    getche();
    return 0;
}

void Input()
{
    float x, y, z;
    cout << "\n Enter Three Numbers : " << endl;
    cin >> x >> y >> z;
    Largest(x, y, z);
}

void Largest(float x, float y, float z)
{
    float Large;
    if(x>y && x>z)
    Large = x;
    else if(y>x && y>z)
    Large = y;
    else
    Large = z;
    Output(Large);
}

void Output(float Large)
{
    cout << "\n Largest Number = " << Large;
}


Write a function called printChar having two value parameters c and n. The parameter c is of type char, and n is of type int. The function prints a single line of n copies of the character c. For example, if n = 5 and c = &, the function would print &&&&&. Use for loop.



Write a function named "sum_from_to" that takes two integer arguments, call them "first" and "last", and returns as its value the sum of all the integers between first and last inclusive





#include<iostream>

#include<conio.h>



using namespace std;



int main()

{

    void printChar(char m, int n);

    char k;

    int l;



    cout << " Enter a Character = ";

    cin >> k ;

    cout << "\n Enter the number of times to print that character : ";

    cin >> l ;

    printChar(k,l);



    getche();

    return 0;

}

void printChar(char m, int n)

{

    for(int a=1;a<=n;a++)

    cout << m ;

}

Write a function named "digit_name" that takes an integer argument in the range from 1 to 9, inclusive, and then prints the English name for that integer on the computer screen. The function should not return a value.

Write a function named "digit_name" that takes an integer argument in the range from 1 to 9, inclusive, and then prints the English name for that integer on the computer screen. The function should not return a value.




#include<iostream>

#include<conio.h>

#include<process.h>



using namespace std;



int main()

{

    void digit_name(int a);

    int a;

    cout<<"Enter a number for its English Word = ";

    cin>>a;



    digit_name(a);



    getche();

    return 0;

}

void digit_name(int a)

{

    char names[9][6]={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};



    if(a>=1 && a<=9)

        cout << names[a-1];

    else

        cout<<".....Digit Error...\n";

}

Thursday, 12 January 2012

Bicycle shop in Hyannis rents bicycles by the day at different rates throughout the year, according to the season (see table below).The proprietor also gives a 25% discount if the rental period is greater than 7 days. Renters must also pay a 50$ returnable deposit for each bicycle rented.


A bicycle shop in Hyannis rents bicycles by the day at different rates throughout the year, according to the season (see table below).The proprietor also gives a 25% discount if the rental period is greater than 7 days. Renters must also pay a 50$ returnable deposit for each bicycle rented. Write a program to input the season and the number of days of rental and then calculate and display the total charge that includes the deposit. (Use switch statement).
Season Charge
S[spring] $5.00
M[Summer]   $7.50
A[Autumn]   $3.75
W[Winter]   $2.75





#include<iostream.h>

#include<conio.h>

#include<iomanip>



void main()

{

int days;

char season;

double bill;



cout << "Enter the Season:" << endl

        <<" S for Spring, M for Summer, A for Autumn, W  for Winter:  ";

cin >> season;



cout << "Enter the number of days of Rental:  ";

cin >> days;



switch(season)

{

case 's':

case 'S':

            if(days>7)

            bill=((days*5)+50)-(13.75);

            else

            bill=(days*5)+50;

            break;



    case 'm':

    case 'M':

        if(days>7)

            bill=((days*7.50)+50.00)-(14.375);

            else

            bill=(days*7.50)+50.00;

    break;



  



 case 'a':

 case 'A':

            if(days>7)

            bill=((days*3.75)+50.00)-(13.4375);

            else

            bill=(days*3.75)+50.00;

    break;



    case 'w':

    case 'W':

            if(days>7)

            bill=((days*2.75)+50.00)-(13.1875);

            else

            bill=(days*2.75)+50.00;

    break;



default:

cout << endl << "\t\t Invalid Character";

}



cout << "Your Bill is : " << fixed << setprecision(2) << bill << endl;

getche();

}