Professional Documents
Culture Documents
#include <iostream>
using namespace std;
int main() {
string originalString;
originalString = "And now for something completely different.";
string::size_type len;
len = originalString.length();
cout << "length is " << len << endl;
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
char y, n; // place to store y/n input letter
char letter; // Place to store input letter
int tempIn; // Temperature to be converted
int tempOut; // Converted temperature
do
{
while (letter != 'C' && letter != 'c' && letter != 'F' && letter != 'f')
{
cout << "Wrong letter." << endl;
cout << "Type a C or an F, then press return." << endl;
cin >> letter;
Labs 1
}
cout <<"Type an integer number, then press return." << endl;
cin >> tempIn;
if (letter == 'C' || letter == 'c')
{
tempOut = (9 * tempIn / 5) + 32;
}
else
{
tempOut = 5 * (tempIn - 32) / 9;
}
cout << "Temperature to convert: " << tempIn << endl;
cout << "Converted temperature: " << tempOut << endl;
cout << "Do you want to convert another temperature (y/n) ";
cin >> letter;
}
while (letter == 'y' || letter == 'Y');
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main() {
cout << fixed << showpoint;
float val;
float sum = 0;
float average = 0;
int count = 0;
ifstream inData; // declares input stream
ofstream outData; // declares output stream
inData.open("inputfile.txt");
if (!inData) {
cout << "Can't open the input file successfuly." << endl;
return 1;
}
outData.open("outputfile.txt");
if (!outData) {
cout << "Can't open the output file successfuly." << endl;
return 2;
}
cout << "The initial value for count is " << count << ". " << endl;
cout << "Starting to process input file ... " << endl;
Labs 2
if (count != 0 ) // if statement: executes when count is not equal to 0
average = sum / count;
else
average = 0;
outData << "The sum of the input values is " << sum << "." << endl;
outData << "The number of valid input values is " << count << "." << endl;
outData << "The average is " << average << "." <<endl;
cout << "Output has been placed in the file: outputfile.txt" << endl;
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main() {
double csong;
double balance = 10.00;
while (balance >= 0.99) {
cout << "Enter the cost of a song: ";
cin >> csong;
if (csong > balance) {
cout << "Your choice is too expensive!!! Choose another one please!" << endl;
}
else {
cout << "Song purchased for $" << csong << "! Enjoy!!!" << endl;
balance = balance - csong;
}
cout << "Your remaining balance is: "<< balance << endl;
cout << endl;
}
cout << "Your balance is $" << balance << ". You cannot buy more songs." << endl << "Come again!";
return 0;
}
#include <iostream>
using namespace std;
int main() {
char grade;
switch (grade)
{
case 'A':
cout << "Great work. " << endl;
break;
case 'B':
cout << "Good work." << endl;
Labs 3
break;
case 'C':
cout << "Passing work. " << endl;
break;
case 'D':
case 'F':
cout << "Unsatisfictory work. " << endl;
cout << "See your instructor." << endl;
break;
default:
cout << grade << " is not a legal grade." << endl;
break;
} //end of switch statement
} //end of for loop
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
void printStars(int);
int main() {
int numOfStars;
cout << "Enter the number of stars in the first line: " << endl;
cin >> numOfStars;
return 0;
}
#include <iostream>
using namespace std;
Labs 4
int main() {
double length, width;
float area;
cout << "This program computes the area of a rectangle." << endl;
cout << "You will be prompted to enter both the length and width." << endl;
cout << "Enter a real number (such as 7.88 or 6.3) for each." << endl;
cout << "The program will then compute and print the area.";
cout << endl;
getValues(length, width);
area = computeArea(length, width);
printArea(area);
return 0;
}
#include<iostream>
using namespace std;
int main() {
double pR, hW, tP;
cout << "This program calculates total pay for an employee" << endl;
getData(pR, hW);
tP = calcPay(pR, hW);
printPay(pR, hW, tP);
return 0;
}
Labs 5
void printPay(double pR, double hW, double tP) {
cout << "For an employee who worked " << hW << " hours with a pay rate $" << pR << " per hour," << endl;
cout << "The total pay is $" << tP << endl;
}
#include <iostream>
int main() {
int arr[10], sum = 0;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n = 10;
int max;
int arr[10];
read_array(arr, n);
print_array(arr, n);
max = find_max(arr, n);
cout << "The maximum value of the array is : " << max << endl;
Labs 6
return 0;
}
return max;
}
Labs 7