Skip to content

Commit

Permalink
Add solutions to exercises in C++ Primer (5th edition)
Browse files Browse the repository at this point in the history
After thinking for a while, I finally decide to add my own solutions to
the exercises in the fifth edition of C++ Primer into this repo.

I didn't add those codes in the past (although I have been working on
this book for quite a while) because I am not confident about my own
work. I am afraid that some people might googling into this repo and
take these guesswork as some sample solutions. As a beginner, my
knowledge of the programming language is very limited, and my solutions
to the exercises might be hopelessly wrong.

The codes I put here definately have nothing to do with "standard
solutions," no... These are just something I write for practices (and
for fun). They are very personal. Sometime I won't even follow the
direction, but do something in my own way. I put them here mostly as
notes for my study, but it is also great if you find the codes helpful.
  • Loading branch information
horizon-blue committed Jul 10, 2016
1 parent 6ea40eb commit d3f7582
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Solutions-to-Books/C++Primer/Chapter1/1.1.cpp
@@ -0,0 +1,3 @@
int main(){
return 0;
}
11 changes: 11 additions & 0 deletions Solutions-to-Books/C++Primer/Chapter1/1.10.cpp
@@ -0,0 +1,11 @@
#include <iostream>

int main() {
int num = 10;
while (num >= 0) {
std::cout << num << std::endl;
--num;
}

return 0;
}
20 changes: 20 additions & 0 deletions Solutions-to-Books/C++Primer/Chapter1/1.11.cpp
@@ -0,0 +1,20 @@
#include <iostream>

int main() {
int bound1 = 0, bound2 = 0;
std::cout << "Input two integers:" << std::endl;
std::cin >> bound1 >> bound2;

if (bound1 > bound2) {
int temp = bound1;
bound1 = bound2;
bound2 = temp;
}

while (bound1 <= bound2) {
std::cout << bound1 << std::endl;
++bound1;
}

return 0;
}
31 changes: 31 additions & 0 deletions Solutions-to-Books/C++Primer/Chapter1/1.13.cpp
@@ -0,0 +1,31 @@
#include <iostream>

int main() {
std::cout << "-----Exercise 1.9-----" << std::endl;
int sum = 0;
for (int val = 50; val <= 100; ++val)
sum += val;
std::cout << "The sum of 50 to 100 inclusive is "
<< sum << "\n" << std::endl;

std::cout << "-----Exercise 1.10-----" << std::endl;
for (int val = 10; val >= 0; --val)
std::cout << val << std::endl;

std::cout << "-----Exercise 1.11-----" << std::endl;
int bound1 = 0, bound2 = 0;
std::cout << "\n" << "Input two integers:" << std::endl;
std::cin >> bound1 >> bound2;
std::cout << "\n" << std::endl;

if (bound1 > bound2) {
int temp = bound1;
bound1 = bound2;
bound2 = temp;
}

for (int i = bound1; i <= bound2; ++i)
std::cout << i << std::endl;

return 0;
}
11 changes: 11 additions & 0 deletions Solutions-to-Books/C++Primer/Chapter1/1.16.cpp
@@ -0,0 +1,11 @@
#include <iostream>

int main() {
int sum = 0, val = 0;
std::cout << "Input a list of numbers:" << std::endl;
while (std::cin >> val)
sum += val;
std::cout << "The sum of all numbers = " << sum << std::endl;

return 0;
}
23 changes: 23 additions & 0 deletions Solutions-to-Books/C++Primer/Chapter1/1.18.cpp
@@ -0,0 +1,23 @@
#include <iostream>
int main() {
// currVal is the number we're counting; we'll read new values into val
int currVal = 0, val = 0;
// read first number and ensure that we have data to process
if (std::cin >> currVal) {
int cnt = 1; // store the count for the current value we're processing
while (std::cin >> val) { // read the remaining numbers
if (val == currVal) // if the values are the same
++cnt; // add 1 to cnt
else { // otherwise, print the count for the previous value
std::cout << currVal << " occurs "
<< cnt << " times" << std::endl;
currVal = val; // remember the new value
cnt = 1; // reset the counter
}
} // while loop ends here
// remembre to print the count for the last value in the file
std::cout << currVal << " occurs "
<< cnt << " times" << std::endl;
} // outermost if statement ends here
return 0;
}
10 changes: 10 additions & 0 deletions Solutions-to-Books/C++Primer/Chapter1/1.20.cpp
@@ -0,0 +1,10 @@
#include <iostream>
#include "Sales_item.h"

int main() {
Sales_item book;
std::cout << "Enter a set of book sales transactions: " << std::endl;
while (std::cin >> book)
std::cout << book << std::endl;
return 0;
}
11 changes: 11 additions & 0 deletions Solutions-to-Books/C++Primer/Chapter1/1.21.cpp
@@ -0,0 +1,11 @@
#include <iostream>
#include "Sales_item.h"

int main() {
Sales_item book1, book2;
std::cout << "Enter two books with same ISBN:" << std::endl;
std::cin >> book1 >> book2;
std::cout << book1 + book2 << std::endl;

return 0;
}
6 changes: 6 additions & 0 deletions Solutions-to-Books/C++Primer/Chapter1/1.3.cpp
@@ -0,0 +1,6 @@
#include <iostream>

int main() {
std::cout << "Hello, World" << std::endl;
return 0;
}
16 changes: 16 additions & 0 deletions Solutions-to-Books/C++Primer/Chapter1/1.5.cpp
@@ -0,0 +1,16 @@
#include <iostream>
int main() {
std::cout << "Enter two numbers:";
std::cout << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1;
std::cin >> v2;
std::cout << "The product of ";
std::cout << v1;
std::cout << " and ";
std::cout << v2;
std::cout << " is ";
std::cout << v1 * v2;
std::cout << std::endl;
return 0;
}
6 changes: 6 additions & 0 deletions Solutions-to-Books/C++Primer/Chapter1/1.8.cpp
@@ -0,0 +1,6 @@
#include <iostream>

int main() {
std::cout << /* "*/" /* " /* " */;
return 0;
}
13 changes: 13 additions & 0 deletions Solutions-to-Books/C++Primer/Chapter1/1.9.cpp
@@ -0,0 +1,13 @@
#include <iostream>

int main() {
int sum = 0, val = 50;
while (val <= 100) {
sum += val;
++val;
}
std::cout << "The sum of 50 to 100 inclusive is "
<< sum << std::endl;

return 0;
}

0 comments on commit d3f7582

Please sign in to comment.