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();

}







Write a C++ program that will calculate the total rate of receiving dollars for each bushel. You have to take total number of dollars you have received and total number of bushels for which you get dollars as inputs from user, and...

Write a C++ program that will calculate the total rate of receiving dollars for each bushel. You have to take total number of dollars you have received and total number of bushels for which you get dollars as inputs from user, and then display the result up to two decimal places




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

int main()
{
        clrscr();
    float bushels, dollars, rate;
    cout << "Enter the Number of Dollars" << endl;
    cin >> dollars;
    cout << "Enter the Number of Bushels" << endl;
    cin >> bushels;
    rate=dollars/bushels;
    cout. precision (4);
    cout << "You have received $ " << rate << " for each bushel.";
    getche();
    return 0;
}

Write a program that takes a word less than 25 characters long and prints a statement like this: Fractal starts with the letter ‘F’

Write a program that takes a word less than 25 characters long and prints a statement like this: Fractal starts with the letter ‘F’.




#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

int main()
{    clrscr();
char a[25];
cout << "Enter a Word (less then 25 characters): ";
cin >> a;

cout << "\n\n The Word Starts with the Letter : " << a[0];




getche();
return 0;
}

Write a program that inputs several lines of text (such as a paragraph or a poem), then counts the number of times the letter‘d’ of the alphabet


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

int main()
{
    char a[100];
    cout << "Enter a string : ";
    cin.get(a,99,'+');

    int count=0;

    for(int j=0; j<100; j++)
        if( a[j]=='d' || a[j]=='D' )
            count++;


    cout << "\n The Number of Times the Letter'd' of Alphabet appeared is : "  << count;

    getche();
    return 0;
}

Write a C++ program that inputs a string of maximum length 50 named test. The program then count and print the following with appropriate format.


Write a C++ program that inputs a string of maximum length 50 named test. The program then count and print the following with appropriate format.
i) Count number of characters in test string.
ii) Count number of words in test string.
iii) Count number of sentences in test string.
iv) Count number of vowels in test string.
v) Count all the occurrence of letter ‘a’ in test string.
#include<stdio.h>
#include <iostream>
#include <conio.h>
#include <string.h>

using namespace std;
int main()
{
char test[50];
cout << "Enter a String: ";
gets(test);

cout << test;
int y=strlen(test);

int count1=0, count2=0, count3=0, count4=0;
for(int j=0; j<50; j++)
   {
    if(test[j]==' ')
        count1++;
    if(test[j]=='.')
        count2++;
    if(test[j]=='a')
        count3++;
    if((test[j]=='a')||(test[j]=='A')||(test[j]=='e')||(test[j]=='E')||(test[j]=='i')||(test[j]=='I')||(test[j]=='o')||(test[j]=='O')||(test[j]=='u')||(test[j]=='U'))
    count4++;
   }

cout << "\n\nNumber of Characters in Test String are : " << y;
cout << "\n\nNumber of Words in Test String are : " << ++count1;
cout <<"\n\nNumber of Sentences in Test String are : " << count2;
cout << "\n\nNumber of Vowels in Test String are : " << count4;
cout << "\n\nNumber of Occurrence of Letter 'a' in Test String are : " << count3;

getche();
return 0;
}

Write a program that inputs name of two students and then print the names in alphabetical order


Write a program that inputs name of two students and then print the names in alphabetical order on the screen.(The student having name start with d will appear first on the screen than the student having name start with i)


#include "iostream"

#include "conio.h"





using namespace std;

int main()

{

   

    char nameOne[20],nameTwo[20];

    cout << "Enter Names seperated by space:\n";

    cin >> nameOne >> nameTwo;

    char chOne,chTwo;

    cout << "\n\n";

    chOne = nameOne[0];

    chTwo = nameTwo[0];

    if(chTwo > chOne)

    cout << nameOne << "\n" << nameTwo;

    else if(chOne>chTwo)

    cout << nameTwo << "\n" << nameOne;

    else if (chOne==chTwo)

    cout << "Both of are same order\n";

    else

    cout <<"Invalid Input!!!!";

}



Wednesday, 11 January 2012

Write a program to mimic a calculator. Input two real numbers and state whether the numbers are to be added, subtracted, multiplied or divided. Cater for the possibility of denominator being zero in the division two numbers.

Write a program to mimic a calculator. Input two real numbers and state whether the numbers are to be added, subtracted, multiplied or divided. Cater for the possibility of denominator being zero in the division two numbers.


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

void main()
{    clrscr();
    int a, b;
    char op;
    cout << "Enter Two Numbers with Operand" << endl;
    cin >> a >> op >> b;
   
if (op == '+')
    {
        cout << "Operation is to be performed is Addition" << endl;
        cout << "\n\t" << a << "+" << b << "=" << a+b << endl;
    }
    else if (op == '-')
    {
         cout << "Operation is to be performed is Subtraction" << endl;
         cout << "\n\t" << a << "-" << b << "=" << a-b << endl;
    }
    else if (op == '*')
    {
        cout << "Operation is to be performed is Multiplication" << endl;
        cout << "\n\t" << a << "*" << b << "=" << a*b << endl;
    }
    else if (op == '/')
    {      
if (b == 0)
                cout << "Division not possible " <<endl;
        else
        {
                       double g, h;
            g = (double) a;
            h = (double) b;
            cout << "Operation is to be performed" << endl;
            cout << fixed << setprecision(2) << g << "/" << h << "=" << g/h << endl;
        }
    }
    else
    {
        cout << endl << "\t Invalid Operators or operand";
    }
    getche();
}

Excellent Online CPP related resource

Today a friend of mine shared an excellent C++ web site called learncpp.com
This web site is a good starting point for the beginners.

Welcome to CppSoltions

Welcome to CppSolution.com!!
At this blog I will be posting solutions to Some C++ related programming questions usually asked in high school,This blog is geared towards students who are looking for solution to their C++ Programming Questions,In addition to answers to the question you will also find some good C++ related resource Material.

The idea behind this blog is help to student to compare their solutions with solutions posted here in no way we "encourage: student to copy paste solutions from here.To learn programming they to learn how to think like a programmer simply copy/past-ing may help you to pass assignment but in real world scenario you won't be able to do anything so it's encourage to only use this site to compare solutions.

Let's get started!!!