2018年7月21日 星期六

Using C++ Strings Exercise 1

#include <iostream>
#include <string>
using namespace std;

void cpp_strings() {
   
    string unformatted_full_name {"StephenHawking"};
   
    //----DO NOT MODIFY THE CODE ABOVE THIS LINE----
    //----WRITE YOUR CODE BELOW THIS LINE----
    string first_name{}, last_name{}, formatted_full_name{};
   
    first_name = unformatted_full_name.substr(0, 7);
    last_name = unformatted_full_name.substr(7, 14);
    formatted_full_name = first_name + " " + last_name;
    //----WRITE YOUR CODE ABOVE THIS LINE----
    //----DO NOT MODIFY THE CODE BELOW THIS LINE----
   
    cout << formatted_full_name;
}

Using C-style Strings

#include <iostream>
#include <cstring>
using namespace std;

void strings_and_functions() {
   
    //----WRITE YOUR CODE BELOW THIS LINE----
    char whole_name[50] {}, first_name[50] {"Bjarne"}, last_name[50] {"Stroustrup"};
    int first_name_length{}, last_name_length{}, whole_name_length{};
   
   
    first_name_length = strlen(first_name);
    last_name_length = strlen(last_name);
    strcpy(whole_name, first_name);
    strcat(whole_name, last_name);
    whole_name_length = strlen(whole_name);
   
    //----WRITE YOUR CODE ABOVE THIS LINE----
    //----DO NOT MODIFY THE CODE BELOW THIS LINE----
   
    cout << "The length of the first name, " << first_name << ", is " << first_name_length << " letters long and the length of the last name, " << last_name << ", is " << last_name_length << " letters long. This means that the length of the whole name must be " << whole_name_length << " letters long.";
}

2018年7月20日 星期五

Sum of the Product of all Pairs of Vector Elements

#include <vector>
using namespace std;

int calculate_pairs(vector<int> vec) {
    //----WRITE YOUR CODE BELOW THIS LINE----
    int result {0};
    for(int i=0; i<vec.size(); i++)
        for(int j = i + 1; j < vec.size(); j++)
            result += vec[i] * vec[j];
   
    //----WRITE YOUR CODE ABOVE THIS LINE----
    //----DO NOT MODIFY THE CODE BELOW THIS LINE----
    return result;
}

while loop exec -99

#include <iostream>
#include <vector>
using namespace std;

int count_numbers(const vector<int> &vec) {
    //---- WRITE YOUR CODE BELOW THIS LINE----
    int count{0};
    for(auto v:vec){
        if(v !=-99)
        count ++;
        if (v == -99)
        break;
    }
    //---- WRITE YOUR CODE ABOVE THIS LINE----
    //---- DO NOT MODIFY THE CODE BELOW THIS LINE-----
    return count;
}

======================================================

range based for loop

#include <vector>
using namespace std;

int count_divisible() {
   
    vector<int> vec {1,3,5,15,16,17,18,19,20,21,25,26,27,30,50,55,56,58,100,200,300,400,500,600,700};
    //---- WRITE YOUR CODE BELOW THIS LINE----
    int count{0};
    for(auto num:vec){
        count += ((!(num%3)||!(num%5))? 1: 0);
    }
   
   
    //---- WRITE YOUR CODE ABOVE THIS LINE----
    //---- DO NOT CHANGE THE CODE BELOW THIS LINE----
    return count;
}

Day of the week - beginning-c-plus-plus-programming

#include <iostream>
using namespace std;

void display_day(int day_code) {
    //----WRITE YOUR CODE BELOW THIS LINE----
 
    switch(day_code){
        case 0:
        cout << "Sunday";
        break;
        case 1:
        cout << "Monday";
        break;
        case 2:
        cout << "Tuesday";
        break;
        case 3:
        cout << "Wednesday";
        break;
        case 4:
        cout << "Thursday";
        break;
        case 5:
        cout << "Friday";
        break;
        case 6:
        cout << "Saturday";
        break;
        default:
        cout << "Error - illegal day code";
       
    }
   
   
    //----WRITE YOUR CODE ABOVE THIS LINE----
}

2018年7月19日 星期四

Car driving - Beginning C++ Programming - From Beginner to Beyond

https://ideone.com/OqaY2V

#include <iostream>
using namespace std;

void logical_operators(int age, bool parental_consent, bool ssn, bool accidents) {
 
    //----WRITE YOUR CODE BELOW THIS LINE----
 
    if (((age > 15) && parental_consent && ssn && (!accidents))||
    ((age >= 18) && (1 || parental_consent) && ssn && (!accidents)))//WRITE ALL YOUR CODE WITHIN THE PARENTHESES
        cout << "Yes, you can work.";
 
    //----WRITE YOUR CODE ABOVE THIS LINE----
    return;
}
int main(){

return 0;
}
//this is a solution for "Beginning C++ Programming - From Beginner to Beyond" in ch3,"Statement and Operator"