arrow
arrow

Expressions and Interactivity

Conceptual Problems:

Sample:How the expressions work


                            
                                1 // This program uses a type cast expression to print a character 
                                2 // from a number. 
                                3 #include <iostream> 
                                4 using namespace std;
                                6 int main() 
                                7 { 
                                8 int number = 65;
                               10 // Display the value of the number variable. 
                               11 cout << number << endl; 
                               13 // Display the value of number converted to 
                               14 // the char data type. 
                               15 cout << static_cast<char>(number) << endl; 
                               16 return 0; }
                            
                        




To learn about the Expressions and operaters


Get Slides in which all concepts cover thoroughly


-----------------------------------------------------------------------------------------

Download PDF HERE

Write a program that calculates a car’s gas mileage. The program should ask the user to enter the number of gallons of gas the car can hold and the number of miles it can be driven on a full tank. It should then display the number of miles that may be driven per gallon of gas.


                        
                            #include<iostream>
                                using namespace std;
                                int main()
                                {
                                    
                                    float number_of_gallons;
                                    float number_of_miles;
                                    float miles_per_gallon;
                                    cout<<"__________________________________"<<endl;
                                    cout<<"-----------Miles Per Gallon------"<<endl;
                                    cout<<"__________________________________"<<endl;
                                    cout<<endl;
                                    cout<<"Enter the number of gallons Filed in the car:"<<endl;
                                    cin>>number_of_gallons;
                                    cout<<"Enter the number of miles driven:"<<endl;
                                    cin>>number_of_miles;
                                    //Calculations
                                    miles_per_gallon=number_of_miles/number_of_gallons;
                                    //Display
                                    cout<<"The miles per gallon diven by the car = "<<miles_per_gallon<<endl;
                                    
                                    return 0;
                                }
                          
                        
                    

There are three seating categories at a stadium. For a softball game, Class A seats cost $15, Class B seats cost $12, and Class C seats cost $9. Write a program that asks how many tickets for each class of seats were sold, then displays the amount of income generated from ticket sales. Format your dollar amount in fixed-point notation, with two decimal places of precision, and be sure the decimal point is always displayed.


                        
                            #include<iostream>
                                #include<iomanip>
                                using namespace std;
                                int main()
                                {
                                    const int cost_A=15;
                                    const int cost_B=12;
                                    const int cost_C=9;
                                    int A,B,C;
                                    float income;
                                    cout<<"__________________________________"<<endl;
                                    cout<<"-----------Stadium Seating------"<<endl;
                                    cout<<"__________________________________"<<endl;
                                    cout<<endl;
                                    cout<<"Enter the number of tickets sold for class_A categary:"<<endl;
                                    cin>>A;
                                    cout<<"Enter the number of tickets sold for class_B categary:"<<endl;
                                    cin>>B;	
                                    cout<<"Enter the number of tickets sold for class_C categary:"<<endl;
                                    cin>>C;	
                                    //Calculations
                                    cout<<setprecision(2)<<fixed<<endl;//Is used to display the point values after the answer fixed as
                                    income=(float)(A*cost_A)+(B*cost_B)+(C*cost_C);
                                    //Display
                                    cout<<"The income generated from the ticket sales = $"<<income;
                                    
                                    return 0;
                                }
                        
                    

Write a program that asks for five test scores. The program should calculate the average test score and display it.
The number displayed should be formatted in fixed-point
notation, with one decimal point of precision.


                        
                            #include<iostream>
                                #include<iomanip>
                                using namespace std;
                                int main()
                                {
                                    int  a,b,c,d,e;
                                    float average;
                                    cout<<"__________________________________"<<endl;
                                    cout<<"-----------Average score------"<<endl;
                                    cout<<"__________________________________"<<endl;
                                    cout<<endl;
                                    cout<<"Enter the score 1:"<<endl;
                                    cin>>a;
                                    cout<<"Enter the score 2:"<<endl;
                                    cin>>b;
                                    cout<<"Enter the score 3:"<<endl;
                                    cin>>c;
                                    cout<<"Enter the score 4:"<<endl;
                                    cin>>d;	
                                    cout<<"Enter the score 5:"<<endl;
                                    cin>>e;	
                                    //Calcualtions
                                    average=(float)(a+b+c+d+e)/5;
                                    cout<<setprecision(1)<<fixed;
                                    //Display
                                    cout<<"The average score = "<<average<<endl;
                                    
                                    return 0;
                                    
                                }
                        
                    

Write a program that calculates the average rainfall for three months. The program should ask the user to enter the name of each month, such as June or July, and the amount of rain (in inches) that fell each month. The program should display a message similar to the following: The average rainfall for June, July, and August is 6.72 inches.


                        
                            #include<iostream>
                                #include<string>
                                using namespace std;
                                int main()
                                {
                                    
                                    string month1,month2,month3;
                                    float rain_inche1=0.0;
                                    float rain_inche2=0.0;
                                    float rain_inche3=0.0;
                                    float average=0.0;
                                    cout<<"__________________________________"<<endl;
                                    cout<<"-----------Average Rainfall------"<<endl;
                                    cout<<"__________________________________"<<endl;
                                    cout<<endl;
                                    cout<<"Enter the name of month :1"<<endl;
                                    cin>>month1;
                                    cout<<"Now enter its rainfall per inches:"<<endl;
                                    cin>>rain_inche1;
                                    cout<<"Enter the name of month :2"<<endl;
                                    cin>>month2;
                                    cout<<"Now enter its rainfall per inches:"<<endl;
                                    cin>>rain_inche2;	
                                    cout<<"Enter the name of month :3"<<endl;
                                    cin>>month3;
                                    cout<<"Now enter its rainfall per inches:"<<endl;
                                    cin>>rain_inche3;
                                    //Calcualtions
                                    average=(rain_inche1+rain_inche2+rain_inche3)/3	;
                                    //Display
                                    cout<<"The average rainfall for "<<month1<<" , "<<month2<<" and "<<month3<<" = "<<average<<endl;
                                    
                                
                                    
                                    return 0;
                                }
                        
                    

Write a program that asks the user for the number of males and the number of females registered in a class. The program should display the percentage of males and females in the class


                        
                            #include<iostream>
                                using namespace std;
                                int main()
                                {
                                    
                                    int male=0;
                                    int female=0;
                                    int total=0;
                                    float male_per=0.0;
                                    float female_per=0.0;
                                    cout<<"Class male and female percentage:"<<endl;
                                    cout<<"_________________________________"<<endl;
                                    cout<<"Enter the number of males in your class:"<<endl;
                                    cin>>male;
                                    cout<<"Enter the number of females in your class:"<<endl;
                                    cin>>female;
                                    //Percentage Calculation
                                    total=male+female;
                                    male_per=((float)male/total)*100;
                                    female_per=((float)female/total)*100;
                                    cout<<"The total number of students in your classroom = "<<total<<endl;
                                    cout<<"The percentage of males = "<<male_per<<endl;
                                    cout<<"The percentage of females = "<<female_per<<endl;
                                    
                                    
                                    return 0;
                                }
                        
                    

A cookie recipe calls for the following ingredients:
• l.5 cups of sugar
• 1 cup of butter
• 2. 75 cups of flour
The recipe produces 48 cookies with this amount of the ingredients.
Write a program that asks the user how many cookies he or she wants to make, then displays the
number of cups of each ingredient needed for the specified number of cookies.

The output is as follows:

                            
                                Ingrediant_Calculator:
______________________
Enter the number of cookies that you want to make:
700
The ingrediants you need to make the 700 number of cookies :
You need 21.9 cups_of_sugar
You need 14 cups_of_butter
You need 40.1 cups_of_flour
                            
                            


                        
                            #include<iostream>
                                #include<iomanip>
                                using namespace std;
                                int main()
                                {
                                    float cups_of_sugar=1.5;
                                    int cups_of_butter=1;
                                    float cups_of_flour=2.75;
                                    int total_cookies=48;
                                    int cookies=0;
                                    cout<<"Ingrediant_Calculator:"<<endl;
                                    cout<<"______________________"<<endl;
                                    cout<<"Enter the number of cookies that you want to make:"<<endl;
                                    cin>>cookies;
                                    //Ingrediants_Calculation
                                    cups_of_sugar=(cups_of_sugar*cookies)/total_cookies;
                                    cups_of_butter=((float)cups_of_butter*cookies)/total_cookies;
                                    cups_of_flour=(cups_of_flour*cookies)/total_cookies;
                                    cout<<"The ingrediants you need to make the "<<cookies<<" number of cookies :"<<endl;
                                    cout<<"You need "<<setprecision(3)<<cups_of_sugar<<" cups_of_sugar"<<endl;
                                    cout<<"You need "<<cups_of_butter<<setprecision(3)<<" cups_of_butter"<<endl;
                                    cout<<"You need "<<cups_of_flour<<setprecision(3)<<" cups_of_flour"<<endl;
                                    
                                
                                    return 0;
                                }
                        
                    

A movie theater only keeps a percentage of the revenue earned from ticket sales. The
remainder goes to the movie distributor. Write a program that calculates a theater’s
gross and net box office profit for a night. The program should ask for the name of the
movie, and how many adult and child tickets were sold. (The price of an adult ticket is
$10.00 and a child’s ticket is $6.00.) It should display a report similar to
Movie Name: “Wheels of Fury”
Adult Tickets Sold: 382
Child Tickets Sold: 127
Gross Box Office Profit: $ 4582.00
Net Box Office Profit: $ 916.40
Amount Paid to Distributor: $ 3665.60
Assume the theater keeps 20 percent of the gross box office profit

                        
                            #include<iostream>
                                #include<string>
                                using namespace std;
                                int main()
                                {
                                    const int theater_keeps=20;
                                    const float cost_adult=10.00;
                                    const float cost_child=6.00;
                                    string movie_name;
                                    int adult_tickets=0;
                                    int child_tickets=0;
                                    float gross=0.0;
                                    double net=0.0;
                                    double amount_paid=0.0;
                                    cout<<"--------------BOX_OFFICE-----------"<<endl;
                                    cout<<"____________________________________"<<endl;
                                    cout<<"Enter the Movie_Name:"<<endl;
                                    getline(cin,movie_name);
                                    cout<<"Enter the number of Adult_ticksts sold:"<<endl;
                                    cin>>adult_tickets;
                                    cout<<"Enter the number of Child_ticksts sold:"<<endl;
                                    cin>>child_tickets;
                                    //calculations
                                    gross=(cost_adult*adult_tickets)+(cost_child*child_tickets);
                                    net=((float)20/100)*gross;
                                    amount_paid=gross-net;
                                    cout<<endl;
                                    //Display
                                    cout<<"Movie_name:"<<"\t \t \t \t"<<movie_name<<endl;
                                    cout<<"Adult_tickets_sold:"<<"\t \t \t"<<adult_tickets<<endl;
                                    cout<<"Child_tickets_sold:"<<"\t \t \t"<<child_tickets<<endl;
                                    cout<<"Gross_Box_Office_Profit:"<<"\t \t$"<<gross<<endl;
                                    cout<<"Net_Box_Office_Profit:"<<"\t \t \t$"<<net<<endl;
                                    cout<<"Amount_Paid_To_The_Distributer:"<<"\t \t$"<<amount_paid<<endl;
                                    
                                    
                                    return 0;
                                }
                          
                        
                    

The Yukon Widget Company manufactures widgets that weigh 12.5 pounds each. Write a program that calculates how many widgets are stacked on a pallet, based on the total weight of the pallet. The program should ask the user how much the pallet weighs by itself and with the widgets stacked on it. It should then calculate and display the number of widgets stacked on the pallet. .


                        
                            #include<iostream>
                                using namespace std;
                                int main()
                                {
                                    float indiv_weight_pallet,
                                          pallet_weight_with_widgets,
                                          weight_of_widgets,
                                          indiv_weight_of_widgets=12.5,
                                          num_of_widgets;
                                    cout<<"__________________________________"<<endl;
                                    cout<<"-----------Numbers of widgets------"<<endl;
                                    cout<<"__________________________________"<<endl;
                                    cout<<endl;    
                                    cout<<"Enter the Weight of the pallet without the widgets in pounds:"<<endl;
                                    cin>> indiv_weight_pallet;
                                    cout<<"Enter the Weight of the pallet with the widgets in pounds:"<<endl;
                                    cin>> pallet_weight_with_widgets;
                                    //Calculations
                                    weight_of_widgets=(pallet_weight_with_widgets)-(indiv_weight_pallet);//Calculate wight of widgtes
                                    num_of_widgets=weight_of_widgets/indiv_weight_of_widgets;
                                    //display
                                    cout<<"The number of widgets stacked to the pallet = "<<num_of_widgets<<endl;
                                        
                                    
                                    return 0;
                                }
                        
                    

A bag of cookies holds 30 cookies. The calorie information on the bag claims that there are 10 “servings” in the bag and that a serving equals 300 calories. Write a program that asks the user to input how many cookies he or she actually ate and then reports how many total calories were consumed.


                        
                            #include<iostream>
                                #include<iomanip>
                                using namespace std;
                                int main()
                                {
                                    
                                    cout<<setiosflags(ios::left)<<setw(12)<<"last Name"
                                    <<setiosflags(ios::right)<<setw(13)<<"First Name"
                                    <<setiosflags(ios::right)<<setw(17)<<"Street_Address"
                                    <<setiosflags(ios::right)<<setw(7)<<"Town"
                                    <<setiosflags(ios::right)<<setw(13)<<"State"<<endl;
                                    cout<<"-------------------------------------------------------------"<<endl;
                                    //First
                                    cout<<setiosflags(ios::left)<<setw(0)<<"Jones"
                                    <<setiosflags(ios::right)<<setw(17)<<"Bernard"
                                    <<setiosflags(ios::right)<<setw(20)<<"109 Pine Lance"
                                    <<setiosflags(ios::right)<<setw(13)<<"Littletown"
                                    <<setiosflags(ios::right)<<setw(5)<<"MI"<<endl;
                                    //Second
                                    cout<<setiosflags(ios::left)<<setw(0)<<"O'Brian"
                                    <<setiosflags(ios::right)<<setw(14)<<"Coleen"
                                    <<setiosflags(ios::right)<<setw(22)<<"42 E. 99th Ave."
                                    <<setiosflags(ios::right)<<setw(9)<<"Bigcity"
                                    <<setiosflags(ios::right)<<setw(8)<<"NY"<<endl;
                                    //Third
                                    cout<<setiosflags(ios::left)<<setw(0)<<"Wong"
                                    <<setiosflags(ios::right)<<setw(16)<<"Harry"
                                    <<setiosflags(ios::right)<<setw(25)<<"121-A Alabama St."
                                    <<setiosflags(ios::right)<<setw(7)<<"Lakeville"
                                    <<setiosflags(ios::right)<<setw(6)<<"IL"<<endl;
                                    
                                
                                    return 0;
                                }
                        
                    

Task 10.A serial transmission line can transmit 960 characters a second. Write a program that will calculate how long it will take to send a file, given the file's size. Try it on a 400MB (419,430,400 byte) file. Use appropriate units. (A 400MB file takes days.)


                        
                            #include<iostream>
                                #include<iomanip>
                                using namespace std;
                                iint main()
                                {
                                    
                                    const int characters_per_second=960;
                                    const int sizeOfCharacter_per_byte=1;
                                    long long int file_bytes=419430400;
                                    long double days=0.0;
                                    cout<<"------------File Sendig Time/400MB--------"<<endl;
                                    cout<<"___________________________________________"<<endl;
                                    cout<<"The size of the file is 400MB and sending speed is 960 characters_per_second:"<<endl;
                                    //calculation
                                    days=(double)file_bytes/characters_per_second;//gives seconds
                                    days=days/60;//gives minutes
                                    days=days/60;//gives hours
                                    days=days/24;//gives days
                                    //Display
                                    cout<<"The number of days that it would take = "<<setprecision(2)<<days<<" days"<<endl;
                                    
                                    return 0;
                                }