0% found this document useful (0 votes)
366 views

Project 2 C++

The document describes a program that converts time between 24-hour and 12-hour notation formats. The program is menu-driven, allowing the user to choose which format conversion to perform. It prompts the user to enter a time in either format, performs the appropriate conversion, and displays the result. If the input in 12-hour format is invalid, an error message is displayed.

Uploaded by

sana fiaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
366 views

Project 2 C++

The document describes a program that converts time between 24-hour and 12-hour notation formats. The program is menu-driven, allowing the user to choose which format conversion to perform. It prompts the user to enter a time in either format, performs the appropriate conversion, and displays the result. If the input in 12-hour format is invalid, an error message is displayed.

Uploaded by

sana fiaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

1.

To make telephone numbers easier to remember, some companies use


letters to show their telephone number. For example, using letters, the
telephone number 438-5626 can be shown as GET LOAN. In some cases, to
make a telephone number meaningful, companies might use more than
seven letters. For example, 225-5466 can be displayed as CALL HOME, which
uses eight letters. Write a program that prompts the user to enter a
telephone number expressed in letters and outputs the corresponding
telephone number in digits. If the user enters more than seven letters, then
process only the first seven letters. Also output the – (hyphen) after the
third digit. Allow the user to use both uppercase and lowercase letters as
well as spaces between words. Moreover, your program should process as
many telephone numbers as the user wants.
Solution:
#include <iostream>

using namespace std;

int main(){

int counter;

char phoneNumber;

cout << "\nEnter a phone number in letters only."

<< endl;

for(counter = 0; counter < 7; counter ++)

cin >> phoneNumber;

if(counter == 3)

cout << "-";

if(phoneNumber >= 'A' && phoneNumber <= 'Z'

|| phoneNumber >= 'a' && phoneNumber <= 'z')

switch(phoneNumber)

case 'A':
case 'a':

case 'B':

case 'b':

case 'C':

case 'c':

cout << 2;

break;

case 'D':

case 'd':

case 'E':

case 'e':

case 'F':

case 'f':

cout << 3;

break;

case 'G':

case 'g':

case 'H':

case 'h':

case 'I':

case 'i':

cout << 4;

break;

case 'J':

case 'j':

case 'K':

case 'k':

case 'L':
case 'l':

cout << 5;

break;

case 'M':

case 'm':

case 'N':

case 'n':

case 'O':

case 'o':

cout << 6;

break;

case 'P':

case 'p':

case 'Q':

case 'q':

case 'R':

case 'r':

case 'S':

case 's':

cout << 7;

break;

case 'T':

case 't':

case 'U':

case 'u':

case 'V':

case 'v':

cout << 8;
break;

case 'W':

case 'w':

case 'X':

case 'x':

case 'Y':

case 'y':

case 'Z':

case 'z':

cout << 9;

break;

return 0;

2. You are given a list of students names and their test scores. Design a
pseudocode that does the following:

a) Calculates the average test scores.


b) Determines and prints the names of all the students whose test
scores are below the average test score.
c) Determines the highest test score.
d) Prints the names of all the students whose test scores are the same
as the highest test score.

You are to divide this problem into sub problems as follows:

a) The first sub problem determines the average test score.


b) The second sub problem determines and prints the names of all the
students whose test scores are below the average test score.
c) The third sub problem determines the highest test score.
d) The fourth sub problem prints the names of all the students whose
test scores are the same as the highest test score. The main
pseudocode combines the solutions of the sub problems.

Solution:

Step 1: declare and initialised integer array testScore and string array student

Step 2: calling average funtion:- avergae function add all the score saved in testscore

and then calculate the average of that score and return that average to main function

Step 3: calling belowAverage function that determine the student score below the average

Step 4: calling highest function that calculate highest among testScore

Step 5: calling highestScorer function that determine the students score highest score

3. Write a program that prompts the user to input a number of quarters,


dimes, and nickels. The program then outputs the total value of the coins in
pennies.
Solution:

#include<iostream>

using namespace std;

int main ()

// Declare Variables

int quarter, dimes, nickel, cent;

// Enter values for each

cout<<"Quarter: ";

cin>>quarter;

cout<<"Dimes: ";

cin>>dimes;
cout<<"Nickels: ";

cin>>nickel;

/*

In the United States, these coins have the following values

Quarter = 25 cents

Done = 10 cents

Nickel = 5 cent

Total cent is calculated below

*/

cent = 25 * quarter + 10 * dimes + 5 * nickel;

// Print Total

cout<<"The coins are worth "<<cent<<" cents";

return 0;

4. Write a program, using case statements, that mimics a calculator. The


program should take as input two integers and the operation to be
performed. It should then output the numbers, the operator, and the result.
For division, if the denominator is zero, output an appropriate message.

Some sample outputs are as follow:

3+4=7
13  * 5 = 65

Solution:

#include<iostream>

using namespace std;


int main() {

int var1, var2;

char operation;

cout << "Enter the first number : ";

cin >> var1;

cout << endl;

cout <<"Enter the operation to be perfomed : ";

cin >> operation;

cout << endl;

cout << "Enter the second nuber : ";

cin >> var2;

cout << endl;

bool right_input = false;

if (operation == '+') {

cout << var1 << " " << operation << " " << var2 << " = " << (var1 + var2);

right_input = true;

if (operation == '-') {

cout << var1 << " " << operation << " " << var2 << " = " << (var1 - var2);

right_input = true;

if (operation == '*') {

cout << var1 << " " << operation << " " << var2 << " = " << (var1 * var2);
right_input = true;

if (operation == '/' && var2 != 0) {

cout << var1 << " " << operation << " " << var2 << " = " << (var1 / var2);

right_input = true;

if (operation == '/' && var2 == 0) {

cout << "Error. Division by zero.";

right_input = true;

if (!right_input) {

cout << var1 << " " << operation << " " << var2 << " = " << "Error;";

cout << "Invalid Operation!";

cout << endl;

system("pause");

return 0;

5. Write a program that reads a set of integers and then finds and prints the
sum of the even and odd integers.
Solution:
#include <iostream>
using namespace std;
int main () {
int n, x, oddSum = 0, evenSum = 0;
cout << "Enter the number of values: ";
cin >> n;
cout << "Enter your values:" << endl;
for (int i = 0; i < n; i++) {
cin >> x;
if (x % 2 == 0)
evenSum += x;
else
oddSum += x;
}
cout << "Sum of Even Numbers: " << evenSum << endl;
cout << "Sum of Odd Numbers: " << oddSum << endl;
return 0;
}

6. Write a program that uses while loops to perform the following steps:

a) Prompt the user to input two integers: firstNum and secondNum


(firstNum must be less than secondNum).
b) Output all odd numbers between firstNum and secondNum.
c) Output the sum of all even numbers between firstNum and
secondNum.
d) Output the numbers and their squares between 1 and 10.
e) Output the sum of the square of the odd numbers between
firstNum and secondNum.
f) Output all uppercase letters.

Solution:

#include <iostream>

using namespace std;


int main()

//a. Prompt the user to input two integers: firstNum and secondNum

//(firstNum must be less than secondNum).

cout << "a. Prompt the user to input two integers. First must be less than second\n";

double firstNum = 2;

double secondNum = 1;

while (firstNum > secondNum)

cout << "enter first number: \n";

cin >> firstNum;

cout << "enter second number (must be more than first number): \n";

cin >> secondNum;

if (firstNum > secondNum)

cout << "first number must be less than second number. Try again. \n";

cout << "\n";


//b. Output all odd numbers between firstNum and secondNum.

cout << "b. Output all odd numbers between firstNum and secondNum.\n";

int number = firstNum;

while (number <= secondNum)

if (number % 2)

cout << number++ << "\n";

else

number++;

continue;

cout<<"\n";

//c. Output the sum of all even numbers between firstNum and secondNum.

cout << "c. Output the sum of all even numbers between firstNum and secondNum.\n";

number = firstNum;
int sum = 0;

while (number <= secondNum)

if (!(number % 2))

sum += number++;

else

number++;

continue;

cout << "sum = " << sum << "\n\n";

//d. Output the numbers and their squares between 1 and 10.

cout << "d.Output the numbers and their squares between 1 and 10.\n";

number = 1;

while (number <= 10)

cout << number << " -> " << number << "^2" << " = " << number * number << '\n';

number++;
}

cout << "\n";

//e. Output the sum of the square of the odd numbers between firstNum and secondNum.

cout << "Output the sum of the square of the odd numbers between firstNum and
secondNum\n";

number = firstNum;

sum = 0;

while (number <= secondNum)

if (number % 2)

sum += (number * number);

number++;

else

number++;

continue;

cout << "sum: " << sum << "\n\n";


//f. Output all uppercase letters.

cout << "Output all uppercase letters.\n";

int c = 65;

while (c < 91)

cout << char(c++) << ", ";

cout << "\n";

7. Write a program to convert the time from 24-hour notation to 12-hour


notation and vice versa. Your program must be menu driven, giving the user
the choice of converting the time between the two notations. Furthermore,
your program must contain at least the following functions: a function to
convert the time from 24-hour notation to 12-hour notation, a function to
convert the time from 12-hour notation to 24-hour notation, a function to
display the choices, function(s) to get the input, and function(s) to display
the results. (For 12-hour time notation, your program must display AM or
PM).
Solution:
#include <iostream>

using namespace std;

//function to convert 24 hours format into 12 hours format

void convert24To12()

int hh, mm, ss;

cout<<endl<<"Enter time in 24-hour format: ";

cin>>hh>>mm>>ss;

if(hh>12)
{

hh = hh % 12;

cout<<endl<<hh<<":"<<mm<<":"<<ss<<" PM";

else

cout<<endl<<hh<<":"<<mm<<":"<<ss<<" AM";

//function to convert 12 hours format into 24 hours format

void convert12To24()

string M;

int hh, mm, ss;

cout<<endl<<"Enter time in 12-hour format: ";

cin>>hh>>mm>>ss>>M;

if(M.compare("PM") == 0)

hh = hh + 12;

cout<<endl<<hh<<":"<<mm<<":"<<ss;

else if(M.compare("AM") == 0)

cout<<endl<<hh<<":"<<mm<<":"<<ss;

else

cout<<"Invalid input!";
}

int main()

//variable declaration

int choice;

//display menu

cout<<"Press 1: Convert from 24-hour notation to 12-hour notation"<<endl;

cout<<"Press 2: Convert from 12-hour notation to 24-hour notation";

//get user input

cout<<endl<<endl<<"Enter your choice: ";

cin>>choice;

//check the input format

if(choice==1)

//method calling

convert24To12();

else if(choice==2)

//method calling

convert12To24();

return 0;
8. }Write a program that uses a random number generator to generate a two
digit positive integer and allows the user to perform one or more of the
following operations:

a) Double the number.


b) Reverse the digits of the number.
c) Raise the number to the power of 2, 3, or 4.
d) Sum the digits of the number.
e) If the number is a two digit number, then raise the first digit to the
power of the second digit.
f) If the number is a three digit number and the last digit is less than or
equal to 4, then raise the first two digits to the power of the last digit.

After performing an operation if the number is less than 10, add 10 to the
number. Also, after each operation determine if the number is prime.

Each successive operation should be performed on the number generated


by the last operation. Your program should not contain any global
variables and each of these operations must be implemented by a
separate function. Also, your program should be menu driven.

Solution:

#include <iostream>

#include <ctime>

#include <cstdlib>

using namespace std;

int menu();

int doubleIt(int);

int getPower(int,int);

int reverse(int);

int power(int);

int sum(int);

int twoDigit(int);
int threeDigit(int);

bool prime(int);

int main()

{int n;

srand(time(0));

n=rand()%90+10;

do

cout<<"The number is: "<<n<<endl<<endl;

switch(menu())

  {case 1: n=doubleIt(n);

           break;

  case 2: n=reverse(n);

          break;

  case 3: n=power(n);

          break;

  case 4: n=sum(n);

          break;

  case 5: n=twoDigit(n);

          break;

  case 6: n=threeDigit(n);

          break;

  case 7: return 0;

 }

if(prime(n))

    cout<<n<<" is prime\n";

else
    cout<<n<<" is not prime\n\n";

if(n<10)

    n+=10;

  

}while(true);

int doubleIt(int n)

{return n*2;

int power(int n)

{int p;

cout<<"Enter 2, 3 or 4th power: ";

cin>>p;

while(p<2||p>4)

   {cout<<"invalid entry\n";

    cout<<"Enter 2, 3 or 4th power: ";

    cin>>p;

  }

return getPower(n,p);

int reverse(int n)

{int newnum=0;

while(n>0)

     {newnum=newnum*10+n%10;

      n=n/10;

     }

return newnum;  
}

int sum(int n)

{int sum=0;

while(n>0)

     {sum=sum+n%10;

      n=n/10;

     }

return sum;

int twoDigit(int n)

{int d1,d2;

   if(n<100)

      {d1=n%10;

       d2=n/10;

       return getPower(d1,d2);

       }

return n;

int threeDigit(int n)

{int d1,n2;

if(n>99)

  {d1=n%10;

   if(d1<=4)

       {n2=n/10;

        return getPower(n2,d1);

    }

}
return n;

bool prime(int n)

{int i;

for(i=2;i<n-1;i++)

  if(n%i==0)              

     return false;

return true;                

int getPower(int x,int y)

{int ans,i;

if(y==0)

     return 1;

  else

     {ans=x;

      for(i=2;i<=y;i++)

          ans=ans*x;

      return ans;

   }

   }

int menu()

{int choice=8;

while(choice<1||choice>7)

  {cout<<"1.   double the number\n";

   cout<<"2.   reverse the digit of the number\n";

cout<<"3.   raise the number to the power of 2,3, or 4\n";

cout<<"4.   sum thee digits of the number.\n";


cout<<"5.   if the number is a two digit number\n     then raise the first digit to the power of the second
digit\n";

cout<<"6.   If the number is a three digit number\n";

cout<<"     and the last digit is less than or equal to 4\n     then raise the first two digits to the power of
the last digit.\n";

cout<<"7.   exit\n";

cin>>choice;

return choice;

9. Write a program that can be used to assign seats for a commercial airplane.
The airplane has 13 rows, with six seats in each row. Rows 1 and 2 are first
class, rows 3 through 7 are business class, and rows 8 through 13 are
economy class. Your program must prompt the user to enter the following
information:

a) Ticket type (first class, business class, or economy class)


b) Desired seat Output the seating plan in the following form:
Here, * indicates that the seat is available; X indicates that the
seat is occupied. Make this a menu-driven program; show the
user’s choices and allow the user to make the appropriate
choices.

Solution:

#include <iostream>

#include <string>

#include <stdio.h>

#include <cstdlib>

using namespace std;

int main(int argc, char *argv[])

int airplane[13][6];

char airchar[13][6];

string ticket;

int row[2];

char seat;

//--------------------------------------------

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

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


{

airchar[i][j] = '*';

airplane[i][j] = 0;

//--------------------------------------------

for(;;)

cout<<"Rows 1 and 2 are first class (FC)"<<endl;

cout<<"Rows 3 through 7 are business class (BC)"<<endl;

cout<<"Rows 8 through 13 are economy class (EC)"<<endl;

cout<<endl;

cout<<"* - Available"<<endl;

cout<<"X - Occupied"<<endl;

cout<<endl;

cout<<" \tA\tB\tC\tD\tE\tF"<<endl;

cout<<"Row 1\t"<<airchar[0][0]<<"\t"<<airchar[0][1]<<"\t"

<<airchar[0][2]<<"\t"<<airchar[0][3]<<"\t"<<airchar[0][4]<<"\t"

<<airchar[0][5]<<endl;

cout<<"Row 2\t"<<airchar[1][0]<<"\t"<<airchar[1][1]<<"\t"
<<airchar[1][2]<<"\t"<<airchar[1][3]<<"\t"<<airchar[1][4]<<"\t"

<<airchar[1][5]<<endl;

cout<<"Row 3\t"<<airchar[2][0]<<"\t"<<airchar[2][1]<<"\t"

<<airchar[2][2]<<"\t"<<airchar[2][3]<<"\t"<<airchar[2][4]<<"\t"

<<airchar[2][5]<<endl;

cout<<"Row 4\t"<<airchar[3][0]<<"\t"<<airchar[3][1]<<"\t"

<<airchar[3][2]<<"\t"<<airchar[3][3]<<"\t"<<airchar[3][4]<<"\t"

<<airchar[3][5]<<endl;

cout<<"Row 5\t"<<airchar[4][0]<<"\t"<<airchar[4][1]<<"\t"

<<airchar[4][2]<<"\t"<<airchar[4][3]<<"\t"<<airchar[4][4]<<"\t"

<<airchar[4][5]<<endl;

cout<<"Row 6\t"<<airchar[5][0]<<"\t"<<airchar[5][1]<<"\t"

<<airchar[5][2]<<"\t"<<airchar[5][3]<<"\t"<<airchar[5][4]<<"\t"

<<airchar[5][5]<<endl;

cout<<"Row 7\t"<<airchar[6][0]<<"\t"<<airchar[6][1]<<"\t"

<<airchar[6][2]<<"\t"<<airchar[6][3]<<"\t"<<airchar[6][4]<<"\t"

<<airchar[6][5]<<endl;
cout<<"Row 8\t"<<airchar[7][0]<<"\t"<<airchar[7][1]<<"\t"

<<airchar[7][2]<<"\t"<<airchar[7][3]<<"\t"<<airchar[7][4]<<"\t"

<<airchar[7][5]<<endl;

cout<<"Row 9\t"<<airchar[8][0]<<"\t"<<airchar[8][1]<<"\t"

<<airchar[8][2]<<"\t"<<airchar[8][3]<<"\t"<<airchar[8][4]<<"\t"

<<airchar[8][5]<<endl;

cout<<"Row 10\t"<<airchar[9][0]<<"\t"<<airchar[9][1]<<"\t"

<<airchar[9][2]<<"\t"<<airchar[9][3]<<"\t"<<airchar[9][4]<<"\t"

<<airchar[9][5]<<endl;

cout<<"Row 11\t"<<airchar[10][0]<<"\t"<<airchar[10][1]<<"\t"

<<airchar[10][2]<<"\t"<<airchar[10][3]<<"\t"<<airchar[10][4]<<"\t"

<<airchar[10][5]<<endl;

cout<<"Row 12\t"<<airchar[11][0]<<"\t"<<airchar[11][1]<<"\t"

<<airchar[11][2]<<"\t"<<airchar[11][3]<<"\t"<<airchar[11][4]<<"\t"

<<airchar[11][5]<<endl;

cout<<"Row 13\t"<<airchar[12][0]<<"\t"<<airchar[12][1]<<"\t"

<<airchar[12][2]<<"\t"<<airchar[12][3]<<"\t"<<airchar[12][4]<<"\t"

<<airchar[12][5]<<endl;
cout<<endl;

cout<<"Enter Ticket type (FC, BC, or EC): ";

cin>>ticket;

cout<<"Desired Row: ";

cin>>row[0];

cout<<"Desired seat (A,B,C,D,E or F): ";

cin>>seat;

switch(seat)

case 'A':

case 'a':

row[0] = row[0] - 1;

row[1] = 1;

row[1] = row[1] - 1;

break;
case 'B':

case 'b':

row[0] = row[0] - 1;

row[1] = 2;

row[1] = row[1] - 1;

break;

case 'C':

case 'c':

row[0] = row[0] - 1;

row[1] = 3;

row[1] = row[1] - 1;

break;

case 'D':

case 'd':

row[0] = row[0] - 1;

row[1] = 4;

row[1] = row[1] - 1;

break;

case 'E':
case 'e':

row[0] = row[0] - 1;

row[1] = 5;

row[1] = row[1] - 1;

break;

case 'F':

case 'f':

row[0] = row[0] - 1;

row[1] = 6;

row[1] = row[1] - 1;

break;

if(ticket == "FC")

if(row[0]+1 == 1 || row[0]+1 == 2)

if(airplane[row[0]][row[1]] == 0)

{
airplane[row[0]][row[1]] = 1;

airchar[row[0]][row[1]] = 'X';

else if(airplane[row[0]][row[1]] == 1)

cout<<"Message: Seat "<<row[0] + 1<<seat<<" is already


occupied"<<endl;

system("Pause");

else

cout<<"Wrong Class"<<endl;

system("Pause");

system("cls");

continue;

else if(ticket == "BC")

if(row[0]+1 == 3 || row[0]+1 == 4 || row[0]+1 == 5

|| row[0]+1 == 6|| row[0]+1 == 7)


{

if(airplane[row[0]][row[1]] == 0)

airplane[row[0]][row[1]] = 1;

airchar[row[0]][row[1]] = 'X';

else if(airplane[row[0]][row[1]] == 1)

cout<<"Message: Seat "<<row[0] + 1<<seat<<" is already


occupied"<<endl;

system("Pause");

else

cout<<"Wrong Class"<<endl;

system("Pause");

system("cls");

continue;

else if(ticket == "EC")

if(row[0]+1 == 8 || row[0]+1 == 9 || row[0]+1 == 10


|| row[0]+1 == 11|| row[0]+1 == 12|| row[0]+1 == 13)

if(airplane[row[0]][row[1]] == 0)

airplane[row[0]][row[1]] = 1;

airchar[row[0]][row[1]] = 'X';

else if(airplane[row[0]][row[1]] == 1)

cout<<"Message: Seat "<<row[0] + 1<<seat<<" is already


occupied"<<endl;

system("Pause");

else

cout<<"Wrong Class"<<endl;

system("Pause");

system("cls");

continue;

row[0] = 0;
row[1] = 0;

system("cls");

return 0;

10.Define a struct, fruitType, to store the following data about a fruit: Fruit


name (string), color (string), fat (int), sugar (int), and carbohydrate (int).
Write a program that declares a variable of type fruitType, prompts the user
to input data about a fruit, and outputs the fruit data.

Solution:

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

struct fruitType{

string name;

string color;

int fat;

int sugar;

int carbohydrate;};
int main () {

fruitType fruit;

cout << "Which fruit do you like?" <<endl;

cin >> fruit.name;

cout <<" What is the fruits color?" <<endl;

cin >> fruit.color;

cout <<"What is the fat value in that fruit?" <<endl;

cin >> fruit.fat;

cout <<"What is the sugar value in that fruit?" <<endl;

cin >> fruit.sugar;

cout <<"what are the carbohydrate levels in that fruit?" <<endl;

cin >> fruit.carbohydrate;

cout<< "Fruit Details:" <<endl;

cout <<"Name: "<< fruit.name << endl;

cout <<"Color: " << fruit.color <<endl;

cout <<"Fat: " <<fruit.fat <<endl;

cout <<"Sugar: " <<fruit.sugar <<endl;

cout <<"Carbs: "<< fruit.carbohydrate <<endl;

return 0;

You might also like