Exercise 1.1

1
2
// Illustration won't be necessary.

Exercise 1.2

1
2
3
4
5
int main()
{
  return -1;
}

Exercise 1.3

1
2
3
4
5
6
int main()
{
  std::cout << "Hello, World." << std::endl;
  return 0;
}

Exercise 1.4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int main()
{
  std::cout << "Enter two numbers:" << std::endl;
  int v1 = 0, v2 = 0;
  std::cin >> v1 >> v2;
  std::cout << "The product of " << v1 << " and " << v2
	  << " is " << v1 * v2 << std::endl;
  return 0;
}

Exercise 1.5

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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;
}

Exercise 1.6

1
2
3
4
5
6
7
8
9
int main()
{
  int v1 = 0, v2 = 0;
  std::cout << "The sum of " << v1; // that is right
  std::cout << " and " << v2;	    // the former one misses a std::cout
  std::cout << " is " << v1 + v2 << std::endl; // same reason as the former one
  return 0;
}

Exercise 1.7

1
2
// Just run the program and you will see.

Exercise 1.8

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int main()
{
  std::cout << "/*";
  std::cout << "*/";
  // std::cout << /* "*/" */; missing terminating "
  std::cout << /* "*/" */";
  std::cout << /* "*/" /* "/*" */;
  return 0;
}

Exercise 1.9

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
int main()
{
  int sum = 0, val = 50;
  while(val <= 100)
    {
      sum += val;
      ++val;
    }
  std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl;
  return 0;
}

Exercise 1.10

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
int main()
{
  int val = 10;
  while(val >= 0)
    {
      std::cout << val << std::endl;
      --val;
    }
  return 0;
}

Exercise 1.11

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
int main()
{
  int v1, v2;
  std::cout << "Please enter two integers in ascending order:" << std::endl;
  std::cin >> v1 >> v2;
  while(v1 <= v2)
    {
      std::cout << v1 << std::endl;
      ++v1;
    }
  return 0;
}

Exercise 1.12

1
2
3
4
5
6
7
8
int main()
{
  int sum = 0;
  for(int i = -100; i <= 100; ++i)
    sum += i;			// the result is appearantly 0
  return sum;
}

Exercise 1.13

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main()
{
  // 1.9
  int sum = 0;
  for(int val = 50; val <= 100; ++val)
    {
      sum += val;
    }
  std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl;
  // 1.10
  for(int val = 10; val >= 0; --val)
    {
      std::cout << val << std::endl;
    }
  // 1.11
  int v1, v2;
  std::cout << "Please enter two integers in ascending order:" << std::endl;
  std::cin >> v1 >> v2;
  for(int val = v1; val <= v2; ++val)
    {
      std::cout << val << std::endl;
    }
  return 0;
}

Exercise 1.14

1
2
3
4
/* for statement as a loop task is easy to understand
 * but parameter initialized inside for could not be used outside
 */

Exercise 1.15

1
2
// Nothing to present.

Exercise 1.16

1
2
3
4
5
6
7
8
9
int main()
{
  int sum = 0, value = 0;
  while(std::cin >> value)
    sum += value;
  std::cout << "Sum is: " << sum << std::endl;
  return 0;
}

Exercise 1.17

1
2
3
4
5
/*
 * If all inputs are same, then there is only one output
 * if there is no identicalx repeated input, with every input there is a output
 */

Exercise 1.18

1
2
// Just run it.

Exercise 1.19

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
  int v1, v2;
  std::cout << "Please enter two integers:" << std::endl;
  std::cin >> v1 >> v2;
  if(v1 <= v2)
    {
      while(v1 <= v2)
      {
	std::cout << v1 << std::endl;
	++v1;
      }
    }
  else
    {
      while(v2 <= v1)
      {
	std::cout << v2 << std::endl;
	++v2;
      }
    }
  return 0;
}

Exercise 1.20

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int main()
{
  Sales_item item;
  while(std::cin >> item)
    {
      std::cout << item << std::endl;
    }
  return 0;
}

Exercise 1.21

1
2
3
4
5
6
7
8
9
int main()
{
  Sales_item item1, item2;
  std::cin >> item1 >> item2;
  // member function has not been introduced until 1.5.2
  std::cout << item1 + item2 << std::endl;
  return 0;
}

Exercise 1.22

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
int main()
{
  Sales_item sum, item;
  while(std::cin >> item)
    {
      sum += item;
    }
  std::cout << sum << std::endl;
  return 0;
}

Exercise 1.23

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int main()
{
  int cnt = 1;
  Sales_item item1, item2;
  if(std::cin >> item1)
    {
      while(std::cin >> item2)
      {
	// don't worry, Sales_item item is initialized when created
	if(item1.isbn() == item2.isbn())
	  cnt += 1;
	else
	  {
	    std::cout << item1.isbn() << " recorded " << cnt << std::endl;
	    item1 = item2;
	    cnt = 1;
	  }
      }
      // the last one
      std::cout << item1.isbn() << " recorded " << cnt << std::endl;
    }
  else
    {
      std::cerr << "No data?" << std::endl;
      return -1;
    }
  return 0;
}