Introduction to C++ Tables

1. Video introduction

Video introduction to C++

2. Input table A

The main components of the C++ tables are: The input table A, the C++ code, and the output table B.

The input table is also called the input matrix. The cells of the matrix are arranged in rows and columns. There are \(20\) rows and \(20\) columns in the input matrix. The rows are labeled with 0, 1, 2, ..., 19. The columns are also labeled by 0, 1, 2, ..., 19. The cell A[r][c] is at the intersection of the row r and the column c.

3. C++ code

After scrolling below the input table, you will see the place were you can insert a C++ code. Your code is allowed to read the cells from the input matrix A. Your code is allowed to write into the cells of the output matrix B. After the execution of the code, the website will display the content of the output matrix B.

4. Example: Sum of numbers A[0][0] and A[0][1]

The following code will add the numbers from the cells A[0][0] and A[0][1]. It will write the result into the cell B[0][0]. The complete code is given below

B[0][0]=A[0][0]+A[0][1];

You can fill out the input table A as shown in the picture below

0 1 2 3 4 5 6
0 51.2220.590 0000
1 000 0000
2 000 0000
3 000 0000
4 000 0000

Then, you need to insert the instruction B[0][0]=A[0][0]+A[0][1]; in the field called C++ code. You must press the button Save/Update so your matrix and your code are submitted to the website. Finally, you need to press the button Calculate/Display, which is located at the top of the page. You will be taken to a new page that will display the input matrix A and the output matrix B whose field B[0][0] contains the sum 51.22+20.59=71.81.

5. Example: Absolute value of the difference between A[0][0] and A[0][1]

We will first calculate the difference

B[0][0]=A[0][0]-A[0][1];

Then, we will check whether it is negative. If it is, then we need to multiply it by \(-1\) because we need absolute value.

This is the complete code

B[0][0]=A[0][0]-A[0][1];
if(B[0][0]<0){B[0][0]=B[0][0]*(-1);}

6. Example: Sums of the first 8 elements of the row 0

The first eight cells of the row 0 are A[0][0], A[0][1], ..., A[0][7]. The sum of these cells will be placed in B[0][0].

There are three ways to do this: 1) very ugly, 2) ugly, and 3) elegant. We will do each of these.

6.1. Very ugly solution

B[0][0]=A[0][0];
B[0][0]=B[0][0]+A[0][1];
B[0][0]=B[0][0]+A[0][2];
B[0][0]=B[0][0]+A[0][3];
B[0][0]=B[0][0]+A[0][4];
B[0][0]=B[0][0]+A[0][5];
B[0][0]=B[0][0]+A[0][6];
B[0][0]=B[0][0]+A[0][7];

6.2. Ugly solution

We will now introduce a variable c that will take values 0, 1, ..., 7 and we will always use the same two instructions: B[0][0]=B[0][0]+A[0][c]; and c=c+1;.

In C++ tables every variable (except for A and B) must be introduced before it is used. The instructions that introduce variables are called declarations. The declaration of the variable c is int c;. The declaration must specify what kind of values will be stored. The variable c will store integers, hence the declaration int c;.

int c; 
c=0;
B[0][0]=0;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;
B[0][0]=B[0][0]+A[0][c]; c=c+1;

Remark: In the standard C++, every variable must be declared. In C++ tables, the variables A and B are declared by the website. So, you must not re-declare them.

6.3. Elegant solution

The subsection 6.2. has identical instructions that appear multiple times. The programming language C++ allows us to use loops. With loops, we will write the repeating instructions only once. Although we will write them only once, they will be executed multiple times. The most common loop is the while loop. We will execute the loop for as long as the number c is in the set \(\{0\), \(1\), \(2\), \(3\), \(4\), \(5\), \(6\), \(7\}\), i.e. \(c< 8\).

int c;
c=0;
while(c<8){
  B[0][0]=B[0][0]+A[0][c];
  c=c+1;
}

7. Example: The maximal and minimal difference of adjacent cells

In this problem we will take every pair of adjacent cells, calculate the absolute value of their difference, and then find which of these differences is maximal and which of them is minimal. The maximal difference will be stored in B[0][0]. The minimal difference will be stored in B[0][1].

We will declare two variables row and col of type int. We will visit each cell. We will analyze each cell A[row][col] and its two neighbors: The right neighbor A[row][col+1] and the neighbor below A[row+1][col]. We will evaluate the differences and their absolute values. Then, we'll compare these absolute values with the current maximum B[0][0] and the current minimum B[0][1]. It would be very convenient to introduce another variable diff in which we will keep the differences. The variable diff has to be declared in such a way that it can store real numbers. Real numbers in C++ are called double The declaration of diff is double diff;.

int row;
int col;
double diff;
row=0; col=0;
diff=A[0][0]-A[0][1];
if(diff<0){diff = diff *(-1);}
B[0][0]=diff; B[0][1]=diff;
while(row<20){
  while(col<20){
    if(col<19){
      diff=A[row][col]-A[row][col+1];
      if(diff<0){diff = diff *(-1);}
      if(diff>B[0][0]){B[0][0]=diff;}
      if(diff<B[0][1]){B[0][1]=diff;}
    }
    if(row<19){
      diff=A[row][col]-A[row+1][col];
      if(diff<0){diff = diff *(-1);}
      if(diff>B[0][0]){B[0][0]=diff;}
      if(diff<B[0][1]){B[0][1]=diff;}
    }
    col=col+1;
  }
  row=row+1;
}