Winter 2020

PIC 10A: Introduction to Programming

Discussion Section 3A

Wonjun Lee



Week2

Tuesday

Problems From The Textbook

Exercise P2.3. Write a program that prompts the user for two integers and then prints

Solution
#include < iostream >
#include < cmath > // for math

using namespace std;

int main()
{
  int num1;
  int num2;

  cout<< "Please type the first number : ";
  cin >>num1;
  cout<< "Please type the second number : ";
  cin >>num2;

  int sum = num1 + num2;
  int difference = num1 - num2;
  int product = num1*num2;
  double average = (num1 + num2)/2.0;
  int distance = fabs(difference); // or int distance = fabs(num1 - num2);

  // for example fabs(-13) = 13 fabs(13) = 13

  cout << "sum = \"" << sum << "\"" << endl;
  cout << "differnce = \"" << difference << "\"" << endl;
  cout << "product = \"" << product<< "\"" << endl;
  cout << "average = \"" << average << "\"" << endl;
  cout << "distance = \"" << distance << "\"" << endl;

  return 0;
}
    

Exercise P2.6. Write a program that asks the user for the lengths of the sides of a rectangle. Then print

Solution
#include < iostream >
#include < cmath >

using namespace std;

int main()
{
  double width, height;

  cout << "Please type width of a rectangle : ";
  cin  >> width;

  cout << "Please type height of a rectangle : ";
  cin  >> height;

  double area = width * height;
  double perimeter = 2 * width + 2 * height;
  // or
  // double perimeter = 2 * (width + height);

  double diagonal = sqrt(width*width + height*height);

  // or
  // double diagonal = sqrt( pow(width,2) + pow(height,2) );

  cout << "area = " << area << endl;
  cout << "perimeter = " << perimeter << endl;
  cout << "diagonal = " << diagonal << endl;

  return 0;

}
    

Thursday

Prefix and Postfix
#include < iostream >

using namespace std;

int main()
{
  int i = 0;

  int j = ++i; // add 1 -> i = 1, assign j = i = 1
  int k = i++; // assign k = i = 1, add1 -> i = 2

  int z = ++i + j++ + ++k; 
  // 1. add values to i and k -> i=3, k=2
  // 2. assign value to z -> z = i + j + k = 3 + 1 + 2 = 6
  // 3. add value to j -> j=2

  cout << "i=" << i << endl; //i=2 -> i=3
  cout << "j=" << j << endl; //j=1 -> j=2
  cout << "k=" << k << endl; //k=1 -> k=2
  cout << "z=" << z << endl; //z=2

  return 0;
}
    
Substring
#include < iostream >
#include < string > // You need to include this library to use substr member function.

using namespace std;

int main()
{
    int i = 0;
    string s = "hello world";

    cout << s << endl;

    /*

    s.substr(a, b)

    a: index where you want to start from
    b: the number of letters you are going to take

    */

    cout << s.substr(7 , 3) << endl; // This wil print out "orl"

    return 0;
}
    
More about Substring
/*

Example of using substr member function.

*/

#include< iostream >
#include< string >

using namespace std;

int main()
{
    string a = "hello world!\n";

    cout << a.substr(0,5) << endl; // "hello"

    cout << a.substr(2,2) << endl; // "ll"

    cout << a.substr(7,444) << endl; // "orld!\n"

    cout << a.substr(7) << endl; // "orld!\n"
}
    
Exercise P2.15 from the textbook

Write a program that reads in an integer and breaks it into a sequence of individual digits. For example, the input 16384 is displayed as


1 6 3 8 4


You may assume that the input has no more than five digits and is not negative.

Solution
#include < iostream >
#include < string >

using namespace std;

int main()
{
  int num;

  cout << "Please type something : ";
  cin  >> num;

  string num_string = to_string(num);

  num_string = num_string + "     ";  

  cout << "num_string : " << num_string << endl;

    cout << num_string.substr(0,1) << " " << num_string.substr(1,1) 
  << " " << num_string.substr(2,1) << " " << num_string.substr(3,1) 
  << " " << num_string.substr(4,1) << endl;

  return 0;
}