Cpp Codes example (strings arrays.. 1 )

Lab 1
Q2
// Q2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>

#define PI 3.14159265



int main()
{
// part a
int negativeInt = 0;
std::cout << "Input a negative integer" << std::endl;
std::cin >> negativeInt;

// part b
float absInt = abs(negativeInt);

// part c
std::cout << ' ' << pow(absInt, 2) << ' ' << sqrt(absInt) << std::endl;

// part d
double positiveDouble = 0;
std::cout << "Input a positive real number" << std::endl;
std::cin >> positiveDouble;

// part e
double square = pow(positiveDouble,2);
double squareRoot = sqrt(positiveDouble);
double natLog = log(positiveDouble);
double base10Log = log10(positiveDouble);

//part f
double angleInDegrees = 0;
std::cout << "Input an angle in degrees" << std::endl;
std::cin >> angleInDegrees;

// part g
double sine = sin(angleInDegrees*PI / 180.0);
double cosine = cos(angleInDegrees*PI / 180.0);
double tangent = tan(angleInDegrees*PI / 180.0);

std::cout << std::setprecision(3) << sine << ' ' << cosine << ' ' << tangent << std::endl;

// part h
std::cout << "The square of " << positiveDouble << " is " << square << std::endl;
std::cout << "The square root of " << positiveDouble << " is " << squareRoot << std::endl;
std::cout << "The natural log of " << positiveDouble << " is " << natLog << std::endl;
std::cout << "The log to the base 10 of " << positiveDouble << " is " << base10Log << std::endl;
std::cout << "The sine of " << angleInDegrees << " is " << sine << std::endl;
std::cout << "The cosine of " << angleInDegrees << " is " << cosine << std::endl;
std::cout << "The tangent of " << angleInDegrees << " is " << tangent << std::endl;


return 0;
}


Q3
// Q3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

int main()
{
int numGallons = 0;
std::cout << "Enter the number of gallons of gas in a tank" << std::endl;
std::cin >> numGallons;

double numMiles = 0;
std::cout << "Enter the number of miles on a tank" << std::endl;
std::cin >> numMiles;

std::cout << "The car can drive " << numMiles / (double)numGallons << " miles per gallon of gas" << std::endl;

return 0;
}


Q4
// Q4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


int main()
{
int numCookies = 0;
std::cout << "How many cookies did you eat?" << std::endl;
std::cin >> numCookies;

int numCookiesPerBag = 30;
int numServingsPerBag = 10;
int numCaloriesPerServing = 300;

double numCaloriesPerCookie = (double)(numCaloriesPerServing*numServingsPerBag) / (double)numCookiesPerBag;

double numCaloriesEaten = (double)numCookies*numCaloriesPerCookie;

std::cout << "You consumed " << numCaloriesEaten << " calories" << std::endl;

return 0;
}


Lab 2
Q2
// Lab 2 Q2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


int main()
{
//user input - weight and distance shipped
//weights between 0 and 20, distance between 10 and 3000
double weight = 0;

std::cout << "Enter Weight (greater than 0 and less than 20)" << std::endl;
std::cin >> weight;
if (weight <= 0 || weight > 20)
{
//exit - because we haven't done while loops yet...
std::cout << "Invalid Weight" << std::endl;
return 0;
}

double distance = 0;
std::cout << "Enter Distance (10 or greater and 3000 or less)" << std::endl;
std::cin >> distance;
if (distance < 10 || distance > 3000)
{
//exit - because we haven't done while loops yet...
std::cout << "Invalid Distance" << std::endl;
return 0;
}

double ratePerMile = 0;
//determine rate based on a valid weight
if (weight <= 2)
{
ratePerMile = 1.10/500.0;
}
else if (weight > 2 && weight <= 6)
{
ratePerMile = 2.20/500.0;
}
else if (weight > 6 && weight <= 10)
{
ratePerMile = 3.70/500.0;
}
else //weight is greater than 10 and <= 20
{
ratePerMile = 4.80/500;
}

//calculate shipping cost
double cost = ratePerMile*distance;

//display charges
std::cout << "It costs " << cost << " dollars to ship a " << weight << " kilo package " << distance << " miles" << std::endl;


return 0;
}


Q3

// Lab 2 Q3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

int main()
{
//ask the user for a subscription package
char package = 0;
std::cout << "Choose a package - A, B or C" << std::endl;
std::cin >> package;
if (!(package == 'A' || package == 'B' || package == 'C'))
{
// exit on an invalid selection, because we haven't covered while loops...
std::cout << "invalid selection";
return 0;
}

//ask for number of minutes
//they shouldn't be less than 0 but that isn't in the specificaiton
double minutes = 0;
std::cout << "Enter number of minutes used" << std::endl;
std::cin >> minutes;

double totalCost = 0;
//calculate the charges based on the package and the minutes.
if (package == 'A')
{
//first calculate overage, if there is any
double overage = minutes - 450;
if (overage < 0)
{
overage = 0;
}
// add the base rate and the overage charge to get the total cost
totalCost = 39.99 + overage*0.45;
}
else if (package == 'B')
{
//first calculate overage, if there is any
double overage = minutes - 900;
if (overage < 0)
{
overage = 0;
}
// add the base rate and the overage charge to get the total cost
totalCost = 59.99 + overage*0.40;

}
else // package == C
{
totalCost = 69.99;
}

//display the charges
std::cout << "The total charges are " << totalCost << " dollars" << std::endl;

return 0;
}


Hello procppgram,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

The answer is 42.

https://www.youtube.com/watch?v=aboZctrHfK8
Topic archived. No new replies allowed.