Skip to content

Commit

Permalink
added ex3.14_3.20
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Aug 31, 2014
1 parent 66ae3b3 commit 2b670d8
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 116 deletions.
20 changes: 20 additions & 0 deletions ch03/ex3_14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
///
///@Author PEZY
///@Date Aug. 2014
///@Brief
/// read a sequence of ints from cin and
/// store those values in a vector.
///

#include <iostream>
#include <vector>

int main()
{
std::vector<int> vec;
int i;
while (std::cin >> i)
vec.push_back(i);

return 0;
}
21 changes: 21 additions & 0 deletions ch03/ex3_15.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
///
///@Author PEZY
///@Date Aug. 2014
///@Brief
/// read a sequence of strings from cin and
/// store those values in a vector.
///

#include <iostream>
#include <vector>
#include <string>

int main()
{
std::vector<std::string> vec;
std::string str;
while (std::cin >> str)
vec.push_back(str);

return 0;
}
86 changes: 86 additions & 0 deletions ch03/ex3_16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
///
///@Author PEZY
///@Date Aug. 2014
///@Brief
/// print the size and contents of the vectors from exercise 3.13.
/// Check whether your answers to that exercise were correct.
/// If not, restudy § 3.3.1 (p. 97) until you understand why you were wrong.
///

#include <iostream>
#include <vector>
#include <string>

using std::vector;
using std::string;
using std::cout;
using std::endl;

int main()
{
vector<int> v1;
cout << "{\n \"v1\":{\"size\":\"" << v1.size() << "\",\"value\":[";
for (auto i : v1)
cout << i << ",";
if (!v1.empty()) cout << "\b";
cout << "]}" << endl;

vector<int> v2(10);
cout << " \"v2\":{\"size\":\"" << v2.size() << "\",\"value\":[";
for (auto i : v2)
cout << i << ",";
if (!v2.empty()) cout << "\b";
cout << "]}" << endl;

vector<int> v3(10, 42);
cout << " \"v3\":{\"size\":\"" << v3.size() << "\",\"value\":[";
for (auto i : v3)
cout << i << ",";
if (!v3.empty()) cout << "\b";
cout << "]}" << endl;

vector<int> v4{10};
cout << " \"v4\":{\"size\":\"" << v4.size() << "\",\"value\":[";
for (auto i : v4)
cout << i << ",";
if (!v4.empty()) cout << "\b";
cout << "]}" << endl;

vector<int> v5{10, 42};
cout << " \"v5\":{\"size\":\"" << v5.size() << "\",\"value\":[";
for (auto i : v5)
cout << i << ",";
if (!v5.empty()) cout << "\b";
cout << "]}" << endl;

vector<string> v6{10};
cout << " \"v6\":{\"size\":\"" << v6.size() << "\",\"value\":[";
for (auto i : v6)
if (i.empty()) cout << "(null)" << ",";
else cout << i << ",";
if (!v6.empty()) cout << "\b";
cout << "]}" << endl;

vector<string> v7{10, "hi"};
cout << " \"v7\":{\"size\":\"" << v7.size() << "\",\"value\":[";
for (auto i : v7)
if (i.empty()) cout << "(null)" << ",";
else cout << i << ",";
if (!v7.empty()) cout << "\b";
cout << "]}\n}" << endl;

return 0;
}

///
///@Output(JSON-like format)
///{
/// "v1":{"size":"0","value":[]}
/// "v2":{"size":"10","value":[0,0,0,0,0,0,0,0,0,0]}
/// "v3":{"size":"10","value":[42,42,42,42,42,42,42,42,42,42]}
/// "v4":{"size":"1","value":[10]}
/// "v5":{"size":"2","value":[10,42]}
/// "v6":{"size":"10","value":[(null),(null),(null),(null),(null),(null),(null),(null),(null),(null)]}
/// "v7":{"size":"10","value":[hi,hi,hi,hi,hi,hi,hi,hi,hi,hi]}
///}
///
84 changes: 41 additions & 43 deletions ch03/ex3_17.cpp
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
///
///@Author PEZY
///@Date Aug. 2014
///@Brief
/// Read a sequence of words from cin and store the values a vector.
/// After you’ve read all the words, process the vector and change each word to uppercase.
/// Print the transformed elements, eight words to a line.
///

#include <iostream>
#include <string>
#include <vector>
#include <string>

using namespace std;
//using std::string;

//using namespace std;

using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;

int main()
{

string str;

vector<string> vec_str;
while(cin>>str)
{
if(str.compare("e")==0)
{
break;
}


if(str.compare("q")!=0)
{
vec_str.push_back(str);
}
else
{
for (unsigned int i=0; i<vec_str.size();i++)
{
cout<<vec_str[i]
<<" ";


if(i%7==0 && i>0)
{
cout<<"\n";
}
}
cout<<"\nthe size is "
<<vec_str.size()
<<"\n";
vec_str.clear();
}
}

return 0;
vector<string> vec;
string word;
while (cin >> word)
vec.push_back(word);

for (auto &str : vec)
for (auto &c : str)
c = toupper(c);

for (decltype(vec.size()) i=0; i<vec.size(); ++i)
{
if (i%8 == 0) cout << endl;
cout << vec[i] << " ";
}
cout << endl;

return 0;
}

///
///@Output
///pezy is a good programmer you can ask any question to him all the time he is playing the codes
///^D(EOF)
///PEZY IS A GOOD PROGRAMMER YOU CAN ASK
///ANY QUESTION TO HIM ALL THE TIME HE
///IS PLAYING THE CODES
///
10 changes: 10 additions & 0 deletions ch03/ex3_18.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <vector>

using std::vector;

int main()
{
vector<int> ivec{0};
ivec[0] = 42;
return 0;
}
24 changes: 24 additions & 0 deletions ch03/ex3_19.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
///
///@Author PEZY
///@Date Aug. 2014
///@Brief
/// List three ways to define a vector and give it ten elements,
/// each with the value 42.
/// Indicate whether there is a preferred way to do so and why.

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

int main()
{
vector<int> ivec1(10, 42);
vector<int> ivec2{42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
vector<int> ivec3;
for (decltype(ivec3.size()) i=0; i != 10; ++i)
ivec3.push_back(42);

std::cout << "The first one more better!" << std::endl;

return 0;
}
47 changes: 47 additions & 0 deletions ch03/ex3_20.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
///
///@Author PEZY
///@Date Aug. 2014
///@Brief
/// Read a set of integers into a vector.
/// Print the sum of each pair of adjacent elements.
/// Change your program so that it prints the sum of the first and last elements,
/// followed by the sum of the second and second-to-last, and so on.
///

#include <iostream>
#include <vector>

using std::vector;
using std::cout;
using std::endl;
using std::cin;

int main()
{
vector<int> ivec;
int i;
while (cin >> i)
ivec.push_back(i);

for(decltype(ivec.size()) i=0; i!=ivec.size()-1; ++i)
cout << ivec[i] + ivec[i+1] << " ";
cout << endl;

decltype(ivec.size()) size = ivec.size();
if (size%2 != 0) size = size/2 + 1;
else size /= 2;

for(decltype(ivec.size()) i=0; i!=size; ++i)
cout << ivec[i] + ivec[ivec.size()-i-1] << " ";
cout << endl;

return 0;
}

///
///@Output
///1 2 3 4 5 6 7 8 9 10
///EOF
///3 5 7 9 11 13 15 17 19
///11 11 11 11 11
///
32 changes: 0 additions & 32 deletions ch03/ex3_20part1.cpp

This file was deleted.

40 changes: 0 additions & 40 deletions ch03/ex3_20part2.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion docs/ch03/ex3.12_3.13.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
##Exercise 3.12>Which, if any, of the following vector definitions are in error?For those that are legal, explain what the definition does.For those that are not legal, explain why they are illegal.- (a) vector<vector<int>> ivec; // legal(c++11), vectors.- (b) vector<string> svec = ivec; // illegal, different type.- (c) vector<string> svec(10, "null"); // legal, vector have 10 strings: "null".##Exercise 3.13>How many elements are there in each of the followingvectors? What are the values of the elements?- (a) vector<int> v1; // size:0, value:0- (b) vector<int> v2(10); // size:10, value:0- (c) vector<int> v3(10, 42); // size:10, value:42- (d) vector<int> v4{10}; // size:1, value:10- (e) vector<int> v5{10, 42}; // size:2, value:10, 42- (f) vector<string> v6{10}; // size:10, value:""- (g) vector<string> v7{10, "hi"}; // size:10, value: "hi"
##Exercise 3.12>Which, if any, of the following vector definitions are in error?For those that are legal, explain what the definition does.For those that are not legal, explain why they are illegal.```cppvector<vector<int>> ivec; // legal(c++11), vectors.vector<string> svec = ivec; // illegal, different type.vector<string> svec(10, "null"); // legal, vector have 10 strings: "null".```##Exercise 3.13>How many elements are there in each of the followingvectors? What are the values of the elements?```cppvector<int> v1; // size:0, no values.vector<int> v2(10); // size:10, value:0vector<int> v3(10, 42); // size:10, value:42vector<int> v4{10}; // size:1, value:10vector<int> v5{10, 42}; // size:2, value:10, 42vector<string> v6{10}; // size:10, value:""vector<string> v7{10, "hi"}; // size:10, value:"hi"```
Expand Down
7 changes: 7 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ C++ Primer 5th Answer Note
[3.10](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_10.cpp) |
[3.11](/Cpp-Primer/ch03/ex3.11)
- [Exercise 3.12 ~ 3.13](/Cpp-Primer/ch03/ex3.12_3.13)
- Exercise [3.14](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_14.cpp) |
[3.15](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_15.cpp) |
[3.16](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_16.cpp) |
[3.17](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_17.cpp) |
[3.18](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_18.cpp) |
[3.19](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_19.cpp) |
[3.20](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_20.cpp)

- Chapter 4. Expressions
- Chapter 5. Statements
Expand Down

0 comments on commit 2b670d8

Please sign in to comment.