You are on page 1of 7

Labs

#include <iostream>
using namespace std;

int main() {

string originalString;
originalString = "And now for something completely different.";

cout << originalString << endl;

string::size_type len;
len = originalString.length();
cout << "length is " << len << endl;

string srch = " completely"; // declare srch

int pos = originalString.find(srch);


cout << "pos is " << pos << endl;

string newString = originalString.substr(0, pos);


cout << "Modified sentence: " << newString << endl;

newString += originalString.substr(pos + srch.length());

cout << "Completed sentence: " << newString << 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

cout <<"Input Menu" << endl << endl;


cout <<"F: Convert from Fahrenheit to Celsius" << endl;
cout <<"C: Convert from Celsius to Fahrenheit" << endl;

do
{

cout << "Type a C or an F, then press return." << endl;


cin >> letter;

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;

inData >> val; //read in the first value


while (inData) //while previous read succeeded ...
{
sum = sum + val;
count++;
inData >> val;
}

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;

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


cout << "Please enter a character grade (A, B, C, D, or F): ";
cin >> 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;

while (numOfStars > 0) {


printStars(numOfStars);
numOfStars = numOfStars - 1;
}

return 0;
}

void printStars(int numOfStars) {


int i;
i = 0;
while (i < numOfStars) {
cout << "*";
i++;
}

cout << endl;


}

#include <iostream>
using namespace std;

void getValues(double &, double &);


float computeArea(double, double);
void printArea(double);

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;
}

void getValues(double & length, double & width) {


cin >> length;
cin >> width;
}

float computeArea(double length, double width) {


return length * width;
}

void printArea(double area) {


cout << area;
}

#include<iostream>
using namespace std;

void getData(double &,double &);


double calcPay(double, double);
void printPay(double, double, double);

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;
}

void getData(double & pR, double & hW) {


cout << "please enter pay rate per hour: ";
cin >> pR;
cout << "please enter hours worked: ";
cin >> hW;
}

double calcPay(double pR, double hW) {


return pR * hW;
}

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>

using namespace std;

int main() {
int arr[10], sum = 0;

cout << "Please give me 10 numbers: " << endl;

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


{
cin >> arr[i];
}

cout << "Index Ascending: ";

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


{
cout << arr[i] << " ";
}

cout << "\nIndex Descending: ";

for(int i = 9; i >= 0; i--)


{
cout << arr[i] << " ";
sum = sum + arr[i];
}

cout << "\nThe Total is: " << sum;

return 0;
}

#include <iostream>
using namespace std;

void read_array(int arr[10],int n);


void print_array(int arr[10],int n);
int find_max(int arr[10],int n);

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;
}

void read_array(int arr[10],int n) {


for(int i = 0; i < n; i++) {
cout << "Enter Number " << i + 1 << ": ";
cin >> arr[i];
}
}

void print_array(int arr[10], int n) {


cout << "Displaying array: "<< endl;

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


cout << arr[i] << endl;
}
}

int find_max(int arr[10],int n) {


int max = arr[0];

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


if (arr[i] > max)
max = arr[i];
}

return max;
}

Labs 7

You might also like