Project 2 C++
Project 2 C++
int main(){
int counter;
char phoneNumber;
<< endl;
if(counter == 3)
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:
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 5: calling highestScorer function that determine the students score highest score
#include<iostream>
int main ()
// Declare Variables
cout<<"Quarter: ";
cin>>quarter;
cout<<"Dimes: ";
cin>>dimes;
cout<<"Nickels: ";
cin>>nickel;
/*
Quarter = 25 cents
Done = 10 cents
Nickel = 5 cent
*/
// Print Total
return 0;
3+4=7
13 * 5 = 65
Solution:
#include<iostream>
char operation;
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;
cout << var1 << " " << operation << " " << var2 << " = " << (var1 / var2);
right_input = true;
right_input = true;
if (!right_input) {
cout << var1 << " " << operation << " " << var2 << " = " << "Error;";
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:
Solution:
#include <iostream>
//a. Prompt the user to input two integers: firstNum and secondNum
cout << "a. Prompt the user to input two integers. First must be less than second\n";
double firstNum = 2;
double secondNum = 1;
cout << "enter second number (must be more than first number): \n";
cout << "first number must be less than second number. Try again. \n";
cout << "b. Output all odd numbers between firstNum and secondNum.\n";
if (number % 2)
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;
if (!(number % 2))
sum += number++;
else
number++;
continue;
//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;
cout << number << " -> " << number << "^2" << " = " << number * number << '\n';
number++;
}
//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;
if (number % 2)
number++;
else
number++;
continue;
int c = 65;
void convert24To12()
cin>>hh>>mm>>ss;
if(hh>12)
{
hh = hh % 12;
cout<<endl<<hh<<":"<<mm<<":"<<ss<<" PM";
else
cout<<endl<<hh<<":"<<mm<<":"<<ss<<" AM";
void convert12To24()
string M;
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
cin>>choice;
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:
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.
Solution:
#include <iostream>
#include <ctime>
#include <cstdlib>
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
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;
cin>>p;
while(p<2||p>4)
{cout<<"invalid entry\n";
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 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<<" 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:
Solution:
#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
int airplane[13][6];
char airchar[13][6];
string ticket;
int row[2];
char seat;
//--------------------------------------------
airchar[i][j] = '*';
airplane[i][j] = 0;
//--------------------------------------------
for(;;)
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;
cin>>ticket;
cin>>row[0];
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)
system("Pause");
else
cout<<"Wrong Class"<<endl;
system("Pause");
system("cls");
continue;
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)
system("Pause");
else
cout<<"Wrong Class"<<endl;
system("Pause");
system("cls");
continue;
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)
system("Pause");
else
cout<<"Wrong Class"<<endl;
system("Pause");
system("cls");
continue;
row[0] = 0;
row[1] = 0;
system("cls");
return 0;
Solution:
#include <iostream>
#include <iomanip>
#include <string>
struct fruitType{
string name;
string color;
int fat;
int sugar;
int carbohydrate;};
int main () {
fruitType fruit;
return 0;