I practiced CPP today with Chapter 4 – Working with Batches of data:
This is my solution, they worked in Code::Block following C++14 convention.
//Exercise 4-1 string::size_type maxlen; StudentInfo s; max(s.name.size(), maxlen); //Exercise 4-2 for(int i = 0; i!=101; i++){ std::cout << std::setw(3) << i << std::setw(6) << i*i << std::endl; } //Exercise 4-3 int n = 1000; std::streamsize width = (unsigned int)(log10(n)) + 1; for(int i = 0; i!= n+1; i++){ std::cout << std::setw(width) << i << std::setw(width*2) << i*i << std::endl; } std::setw(0); //Exercise 4-4 int n = 1000; std::streamsize digits = (unsigned int)(log10(n)) + 1; std::streamsize p = digits*2; std::streamsize originalPrec = std::cout.precision(); std::streamsize originalWidth = std::cout.width(); for(int i = 0; i!= n+1; i++){ double d = i; std::cout << std::setprecision(p) << std::setw(digits) << d << std::setw(digits*2) << d*d <<std::setw(originalWidth) << std::setprecision(originalPrec) << std::endl; } //Exercise 4-5 struct Words{ std::string name; int tally ; }; using vecword_t = std::vector<Words>; using vecsz_t = std::vector<Words>::size_type; using strsz_t = std::string::size_type; std::istream& readTally(std::istream& in, vecword_t word){ if(in){ //empties word vector word.clear(); std::string wordInput; strsz_t maxlen = 0; while(in >> wordInput){ //initialize duplicate flag bool duplicate = 0; //get the longest word maxlen = std::max(maxlen, wordInput.size()); //check duplicate for(vecsz_t i = 0; i != word.size(); i++){ if(wordInput == word[i].name){ word[i].tally++; duplicate = 1; break; } } //if no duplicates found add vector if(duplicate == 0){ Words wordPush = {.name = wordInput, .tally = 1}; word.push_back(wordPush); } } //clear error state in istream in.clear(); //print the result std::cout << "Word count" << std::endl; for(vecsz_t i = 0; i != word.size(); i++){ std::cout << std::setw(maxlen + 1) << word[i].name << ' ' << std::setw(4) << word[i].tally << std::setw(0) << std::endl; } } return in; } std::istream& readTally(std::istream& in){ vecword_t word = {}; return readTally(in,word); } //Exercise 4-6 struct Student_info{ std::string name; double grade; }; bool compare(const Student_info& x, const Student_info& y){ return x.name < y.name; } std::istream& readHw(std::istream& , std::vector<double>& ); double medianVec(std::vector<double>); double gradeCalc(double midGrade, double finGrade, const std::vector<double>& hwGradeList); std::istream& read(std::istream& is, Student_info& s){ double mid, fin; std::vector<double> homeWork; is >> s.name >> mid >> fin; readHw(is, homeWork); s.grade = gradeCalc(mid,fin,homeWork); return is; } void printPrecision(double inputNum, std::streamsize sigFig){ std::streamsize prec = std::cout.precision(); std::cout << std::setprecision(sigFig) << inputNum << std::setprecision(prec) << std::endl; } int main() { Student_info buffer; std::vector<Student_info> students; std::string::size_type maxlen = 0; //get input and final grade while(read(std::cin, buffer)){ maxlen = std::max(maxlen, buffer.name.size()); students.push_back(buffer); } std::cin.clear(); //alphabetize student list std::sort(students.begin(),students.end(),compare); std::cout << "The result is ordered in alphabetical order.." << std::endl; // calculate grade for(unsigned int i=0; i != students.size(); ++i){ std::cout << students[i].name << std::string(maxlen + 1 - students[i].name.size(), ' '); printPrecision(students[i].grade,3); } return 0; } //Exercise 4-7 typedef std::vector<double>::size_type vecsz_t; std::vector<double> vecNum = {1.0, 1.0, 1.0, 2.0}; double sum = 0; for(vecsz_t i = 0; i != vecNum.size(); i++ ){ sum += vecNum[i]; std::cout << vecNum[i] <<std::endl; } double avg = sum/(double)vecNum.size(); std::cout<<"sum: " <<sum <<" avg: " << avg; //Exercise 4-8 //if statement below is legal then f has return type of std::vector<double> double d = f()[n];
Awesome dude!