Computer science Programs

Follow the directions for each of the indicated exercises. This is a closed book and notes test. Show all work. Use external files

  1. Write a program that will do the following: 1) generate, find the sum of, and print the odd integers between two positive integers Num1 and Num2, inclusive; and 2) generate. Find the sum of, and print the even integers between two positive integers Num1 and Num2, inclusive. Determine whether the sum of the even values is less than, equal to, or greater than the sum of the odd values. Write a descriptive message, Write a sample input file for the program.

 

Question 1

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

 

int main(){

ifstream inputFile;

inputFile.open(“inputNum.txt”);

int num1,num2;

int odd=0,even=0;

if(inputFile){

while(!inputFile.eof())

{

inputFile>>num1>>num2;

for(int i=num1;i<num2;i++){

if((i%2)==0){

even=even+i;

}else{

odd=odd+i;

}

 

}

if(odd>>even){

cout<<“The sum of Odd Numbers Between “<<num1 <<” “<<num2<<” is Greater Than EVen Numbers”<<endl;

}else if(odd==even){

cout<<“The sum of Odd Numbers Between “<<num1 <<” “<<num2<<” is Equa To EVen Numbers”<<endl;

}else{

cout<<“The sum of Even Numbers Between “<<num1 <<” “<<num2<<” is Greater Than Odd Numbers”<<endl;

}

 

}

}else{

cout<<“File Not Found”<<endl;

}

}

Input:

1 5

2 6

1 10

3 15

5 19

 

 

 

 

 

 

 

  1. You want to know how long it will take four different families to reach their destinations. The number of miles-per-hour and the number of miles traveled are given below. Write a program that will read the pieces of data and find and print the number of hours required to reach each destination. The output should show the type of car and the number of hours. Test your program with the data that appears below and show the output per car. Give the input and the output file.

 

Car                                                         Miles- per-hour                                               miles traveled

Honda Sportcoup                                         65                                                              260

Toyota Comet                                                55                                                              1025                                      

Dodge Bigcar                                                  49                                                              672

Ford Littlecar                                                 70                                                               315

 

Question 2

Input file:

Honda Sportcoup                    65        260

Toyota comet                          55        1025

Dodge Bigcar                          49        672

Ford Littlecar                          70        315

 

Output file:

Honda Sportcoup                                  4

Toyota comet              18.6364

Dodge Bigcar              13.7143

Ford    Littlecar                                    4.5

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
ifstream inputFile;

string fname,lname;
double MPS,Miles,hours=0;
ofstream myfile (“output.txt”);

int k=0;
inputFile.open(“input.txt”);
if(inputFile){
if (myfile.is_open()){

while(!inputFile.eof())
{

inputFile >> fname >>lname >> MPS >> Miles;
hours=Miles/MPS;

cout<<fname<<” “<<lname<<” “<<hours<<endl;
myfile<<fname<<” “<<lname<<” “<<hours<<endl;
k++;
}
k=k-1;
}else {
cout<<“input.txt File not Found !!!”<<endl;

exit(1);
}
}
cout<<endl<<endl<<“Data Saved in output.txt”;

inputFile.close();
}

 

 

 

 

 

 

 

 

 

  1. Jason typically uses the Internet to buy various items. If the total cost of the items ordered at the same time, is $200.00 or more, then the shipping and handling fee is waived. Otherwise, the shipping and handling fee is $10.00 per item. Find the cost of ordering N items. each item has an individual price. Your program must handle all cases. Use external files.

 

Question 3

 

#include <iostream>

using namespace std;

 

int main(){

int N=0;

double price=0,Totalprice=0;

cout<<“Please Enter the number of items you want to purchase : “;

cin>>N;

for(int i=0;i<N;i++){

cout<<“Enter the Price for Item “<<i+1<<” :”;

cin>>price;

Totalprice=Totalprice+price;

}

if(Totalprice<200){

Totalprice=Totalprice+10;

}

cout<<” Total Billing Amount : “<<Totalprice<<endl;

return 0;

}

 

 

 

  1. In an equilateral triangle, each of the three sides have the same length. In an isosceles triangle, two of the sides are equal to each other. In a scalene triangle, each of the sides has a different length. Write a program that will determine whether three lengths entered as input from an equilateral, isosceles, or a scalene triangle. The program then outputs a message indicating the type of triangle. Use external files.

 

Question 4

 

#include <iostream>

using namespace std;

 

int main(){

 

double side1,side2,side3;

cout<<“Enter the 1st side :”;

cin>>side1;

cout<<“Enter the 2nd side :”;

cin>>side2;

cout<<“Enter the 3rd side :”;

cin>>side3;

if(side1==side2 && side2 == side3){

cout<<“Equailateral”;

}else if(side1!=side2 && side2!=side3 && side1&&side3){

cout<<“Scalene”;

}else{

cout<<“Isosceles”;

}

}