Skip to content

Instantly share code, notes, and snippets.

@KyeRussell
Created August 16, 2011 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyeRussell/1148216 to your computer and use it in GitHub Desktop.
Save KyeRussell/1148216 to your computer and use it in GitHub Desktop.
The end result of three hours of work. This class is /boring/.
/* ex1.cpp
* Author: Kye Russell
* Date: 15/08/2011
*
* - Read in temperatures for every day in Jan (31). Provide a prompt with a runnin
* day total.
* - Print the averge of all temperatures:
* - >30 degrees.
* - 20-30 degrees.
* - <20 degrees.
*/
#include <iostream>
using namespace std;
int main() {
/* Variable Declaration */
float temp, lowtotal = 0, mediumtotal = 0, hightotal = 0;
unsigned short lowcount = 0, mediumcount = 0, highcount = 0, totaldays = 31, day;
/* Banner */
cout << "ex1.cpp - Kye Russell" << endl;
cout << "15/08/2011 - Central Institute of Technology." << endl;
cout << "See the source code for instructions on use..." << endl << endl;
cout << "Enter a temperature for day:" << endl; //beginning of prompt.
for (day = 1; day <= totaldays; day++) { //loop through days
cout << "\t" << day << " of " << totaldays << ": "; //prompt.
cin >> temp; //read in temperature.
/* Calculate range that temp. belongs to.
* Add current temp. to total of relevant range, and increment its counter.
*/
if (temp < 20) { //low range.
lowcount++;
lowtotal += temp;
}else if (temp < 30) { //medium range.
mediumcount++;
mediumtotal += temp;
}else{ //high range.
highcount++;
hightotal += temp;
}
}
/* Calculate the results.
* For every range (low, medium, high) check for divide by zero errors.
* If the division is valid, calculate the average.
* If the division is invalid, print "no data" for the range.
*/
cout << endl << "Averages:" << endl;
/* Low Range */
cout << "\tLow (below 20): ";
if (lowcount != 0) {
cout << lowtotal / lowcount << endl;
}else{
cout << "No data." << endl;
}
/* Medium Range */
cout << "\tMedium (20 to 30): ";
if (mediumcount != 0) {
cout << mediumtotal / mediumcount << endl;
}else{
cout << "No data." << endl;
}
/* High Range */
cout << "\tHigh (above 30): ";
if (highcount != 0) {
cout << hightotal / highcount << endl;
}else{
cout << "No data." << endl;
}
system("pause"); //pause cli.
return 0;
}
/* ex2.cpp
* Author: Kye Russell
* Date: 15/08/2011
*
* Read in fuel consumption figures as floating point data until a negative value is read.
* Print the total of:
* - 'Good' cars (<7)
* - 'Fair' cars (7-10)
* - 'Poor' cars (>10)
*/
#include <iostream>
using namespace std;
int main() {
/* Declare Variables */
unsigned short goodcount = 0, faircount = 0, poorcount = 0;
float consumption;
/* Banner */
cout << "ex2.cpp - Kye Russell" << endl;
cout << "15/08/2011 - Central Institute of Technology." << endl;
cout << "See the source code for instructions on use..." << endl << endl;
/* Initial Prompt */
cout << "Enter a consumption value: ";
cin >> consumption;
/* Enter the loop, starting with the value just obtained.
* Add to relevant total. */
while (consumption > 0) { //loop until megative value received.
if (consumption < 7) { //good.
goodcount++;
}else if (consumption <= 10) { //fair.
faircount++;
}else{ //poor.
poorcount++;
}
/* Prompt for the next value */
cout << "Enter another value: ";
cin >> consumption;
}
/* Negative value received. Print the results */
cout << "Results:" << endl;
cout << "\t'Good' (less than 7L/100km):\t" << goodcount << endl;
cout << "\t'Fair' (between 7L-10L/100km):\t" << faircount << endl;
cout << "\t'Poor' (over 10L/100km):\t" << poorcount << endl;
system("pause"); //pause cli.
return 0;
}
/* ex3.cpp
* Author: Kye Russell
* Date: 15/08/2011
*
* Read in grades for 10 students.
* - Validate grades (>0 AND <100).
* - Print average of:
* - Students that passed (>=50)
* - Students that failed (<50)
*/
#include <iostream>
using namespace std;
int main() {
/* Declare Variables */
unsigned short passcount = 0, passtotal = 0, failcount = 0, failtotal = 0, student;
signed short mark;
/* Banner */
cout << "ex3.cpp - Kye Russell" << endl;
cout << "15/08/2011 - Central Institute of Technology." << endl;
cout << "See the source code for instructions on use..." << endl << endl;
/* Loop through the students */
for (student = 1; student <= 10; student++) {
do {
cout << "Enter a mark (" << student << "/10): "; //prompt
cin >> mark;
}while (mark < 0 || mark > 100); //prompt until we get a valid result.
if (mark >= 50) { //check if result is pass.
passcount++; //it is, increment counter.
passtotal += mark; //and add to total;
}else{
failcount++; //it isn't, increment fail counter.
failtotal += mark; //and add to total.
}
}
/* Print Results
* Calculate average of pass/fail grades. Do divide-by-zero checking.
*/
cout << "Results:" << endl;
cout << "\tPass: " ;
if (passtotal != 0) { //won't 0/0
cout << passtotal / passcount;
}else{ //no marks for the range.
cout << "No data.";
}
cout << endl;
cout << "\tFail: ";
if (failcount != 0) { //won't 0/0
cout << failtotal / failcount;
}else{ //no marks for the range.
cout << "No data.";
}
cout << endl;
system("pause"); //pause cli.
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment