## Introduction to

# C++

J. Reizenstein,

Y. Lifanov,

J. Mascie-Taylor,

Q. Caudron.

# Important

Test your CSC account

    your_csc_username@godzilla.csc.warwick.ac.uk

# Recap

So far we've done

- Declaring variables
- Primitive expressions
- Standard I/O
- Conditionals
- For loops
- Guessing game

# Solution

Step 1

    int x = 19, guess;
    for(int i = 0; i < 5; i++) {
      std::cout << "Please enter your guess\n";
      std::cin >> guess;
      /* Step 2 here */
    }
    std::cout << "Out of tries :(\n";

# Solution

Step 2

    if(guess > x) {
      std::cout << "Your guess is too high\n";
    } else if(guess < x) {
      std::cout << "Your guess is too low\n";
    } else {
      std::cout << "Bingo :)\n";
      return 0;
    }

# Warm Up

Fibonacci sequence

1. Declare 3 integers
2. Assign values appropriately
3. Use a loop to

- Get current sequence term
- Update previous terms
- Print current sequence term
- Stop when term reaches 1000

# Solution

Using while loop

    int a = 0, b = 1, c = 0;
    std::cout << "0\n1\n";
    while(a < 1000) {
      a = b + c;
      c = b;
      b = a;
      std::cout << a << "\n";
    }

# Vectors

Indexable variables

# Vectors

Declaration

need `#include<vector>`

    std::vector<int> a(5, 2);     //integer vector with 5 elements, all set to 2
    std::vector<double> b;        //empty vector of doubles
    std::vector<int> a{1,2,3,4,7};//vector of specified elements

# Vectors

Element access

    std::vector<int> a = {1, 2, 3, 4};
    int one = a[0];      //access 1st element
    size_t index = 3;    //size_t is like a nonnegative int, used for indices
    int four = a[index]; //access via variable index
    a[index] = 100;      //arr[3] is now 100
    int hundred = a.back(); //access last element
    a.push_back(18);     //add another element at end
    size_t five = a.size();

    for(int i : a){
      std::cout << i << "\n";
    }
    for(size_t i=0; i<a.size() : ++i){
      std::cout << a[i] << "\n";
    }

# Vectors

Higher dimensions

    vector<vector<int>> arr = {
        {1, 2},
        {3, 4}
      };
    int two = arr[0][1];

# Exercise

1. Declare a vector of 3 doubles
2. Use for loop and std::cin to read in 3 values
3. Use for and if to find the smallest value
4. Print the smallest value

# Solution

Reading in vector

    const int nnum = 3;
    vector<double> v(nnum,0);
    std::cout << "Please enter " << nnum << " numbers\n";
    for(int i = 0; i < nnum; i++) {
      std::cin >> v[i];
    }

# Solution

Finding smallest entry

    double min = v[0];
    for(int j = 1; j < nnum; j++) {
      if(min > arr[j]) {
        min = arr[j];
      }
    }

# Arrays

Indexable variables

# Arrays

Declaration

    int a[5];     //integer array with 5 elements
    double b[10]; //double array with 10 elements

# Arrays

Declaration

    int five_ones[] = {1, 1, 1, 1, 1};

# Arrays

Element access

    int arr[] = {1, 2, 3, 4};
    int one = arr[0];    //access 1st element
    int idx = 3;         //declare index
    int four = arr[idx]; //access via variable index
    arr[idx] = 100;      //arr[3] is now 100

# Arrays

Good practice

    const int nel = 10; //number of elements
    double arr[nel];    //declare array
    for(int i = 0; i < nel; i++) {
      arr[i] = 0.0;     //set values
    }

# Arrays

Higher dimensions

    int arr[][2] = {
        {1, 2},
        {3, 4}
      };
    int two = arr[0][1];

# Functions

Self-contained code blocks

# Functions

Declaration

    //<type> <name>(<arguments>) { <body> }
    int square(int x) { return x*x; }

# Functions

Declaration

    #include <cmath>
    //<type> <name>(<arguments>) { <body> }
    double power(double x, double y) {
      return exp(y*log(x));
    }

# Functions

Usage

    #include <iostream>
    int square(int x) { return x*x; }
    int main() {
      std::cout << square(2) << "\n";
      return 0;
    }

# Functions

Prototypes

    #include <iostream>
    int square(int);
    int main() {
      std::cout << square(2) << "\n";
      return 0;
    }
    int square(int x) {
      return x*x;
    }

# Exercise

1. Implement a function:

    bool isprime(int);

2. Print the first 100 primes

# Solution

Naive prime test

    bool isprime(int x) {
      int y = 0.5*x;
      for(int i = 2; i <= y; i++) {
        if(x % i == 0) {
          return false;
        }
      }
      return true;
    }

# Solution

Printing 100 primes

    for(int i = 2, j = 0; j < 100; i++) {
      if(isprime(i)) {
        std::cout << i << "\n";
        j++;
      }
    }

# The End