Last active
August 29, 2015 14:13
-
-
Save c0ldlimit/79ff5db8a62f39062593 to your computer and use it in GitHub Desktop.
#lecture2 cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Grading.h" | |
#include<numeric> | |
using namespace mpcs51044; | |
using namespace std; // when you're not in a header you are no imposing your choices on anyone else | |
// it's okay to use this here | |
double Student_info::grade() const | |
{ | |
double avg = accumulate(homework.begin(), homework.end(), 0.0) / homework.size(); | |
// accumulate takes a sequence and an initial value and adds | |
// since starts a 0.0, will always get a double | |
return (midterm + final + avg) / 3; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// header contains all the declarations of the symbols | |
// you are using for sure | |
// may or may not include definitions | |
#ifndef GRADING_H | |
#define GRADING_H | |
namespace mpsc5104 { | |
struct Student_info { | |
string na;e | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <numeric> | |
#include <iostream> | |
#include <string> | |
#include <iomanip> | |
#include <algorithm> | |
#include <sstream> | |
// all these libraries are defined in the namespace "std" | |
using namespace std; | |
typedef vector<int> Row; | |
typedef vector<Row> Triangle; | |
nextRow(Row row) { | |
Row result; | |
int previous = 0; | |
// it is a record of where we are in a container | |
// to get the element you have to dereference the iterator using '*' | |
for (auto it = row.begin(); it != row.end(); it++) { | |
result.push_back(previous + *it); | |
previous = *it; | |
} | |
/* | |
for (auto elt : row) { | |
result.push_back(previous + elt); | |
previous = elt; | |
} | |
*/ | |
result.push_back(previous); | |
return result; | |
} | |
const int size = 12; | |
// scan triangle to find the biggest digit | |
int numDigits(int i) { | |
// keeping dividing by 10 and see how many times you can divide by 10 | |
int digits = 1; | |
// "/=" is short for i = i/10; | |
while ( (i/=10) != 0) { | |
digits++; | |
} | |
return digits; | |
} | |
int numDigits_alternative(int i) { | |
// the more C++ way | |
ostringstream os; // instead of outputting to console we are outputting to string | |
os << i; | |
return os.str().size(); | |
} | |
string centeredInt(int i) { | |
ostringstream os; | |
os << i; | |
string str = os.str(); | |
return string((eltSize - str.size())/2, ' ') + str; | |
} | |
void printRow(Row row) { | |
for(auto elt: row) { | |
// "left" left justify | |
// setw(eltSize) set the width to the max element size | |
// centeredInt | |
cout << left << setw(eltSize) << centeredInt(elt) << " "; | |
} | |
cout << endl; | |
} | |
void printTriangle(Triangle triangle) { | |
Row lastRow = triangle[size-1]; | |
// int maxElement = accumulate(lastRow.begin(), lastRow.end(), 0, max<int>) | |
int maxElement = *max_element(lastRow.begin(), lastRow.end()); | |
eltSize = numDigits(maxElement); | |
for (int i = 0; i < size; i++) { | |
string spaces((size-i-1) * (eltSize+1) /2, ' '); | |
cout << spaces; | |
printRow(triangle[i]); | |
} | |
} | |
int main() { | |
Triangle triangle; | |
Row previousRow; // when building a new row, base it on the row before | |
previousRow.push_back(1); | |
for(int i = 0; i < size; i++) { | |
triangle.push_back(prevoiusRow); // put previous row on triangle | |
previousRow = nextRow(previousRow); // create next row | |
} | |
printTriangle(triangle); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment