Programming
Math Functions
& 2-D Arrays
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 2 Copyright © 2000 by Brooks/Cole Publishing Company
A division of International Thomson Publishing Inc.
2-D Array Example
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 3
2-D Array Example
// 2-D array of 30 uninitialized ints
int table[3][10];
0 1 2 3 4 5 6 7 8 9
0 -- -- -- -- -- -- -- -- -- --
1 -- -- -- -- -- -- -- -- -- --
2 -- -- -- -- -- -- -- -- -- --
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 4
2-D Array References
// 2-D array of 18 uninitialized chars
char table[3][6];
table[1][2] = 'a';
char resp = table[1][2];
0 1 2 3 4 5
tabl 0 -- -- -- -- -- --
e
1 -- -- a -- -- --
2 -- -- -- -- -- --
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 5
2-D Array Initialization
Declaration only reserves memory for the elements in the array. No
values will be stored. If we don’t initialize the array, the contents are
unpredictable. Generally speaking, all arrays should be initialized.
One way to initialize table is shown below
// 2-D array of 18 initialized chars
const int NUM_ROWS = 3, NUM_COLS = 6;
Char table[NUM_ROWS][NUM_COLS]={’a’,’b’,’c’,’d’,
’e’,’f’,’g’,’h’,’i’,’j’,’k’,’l’,’m’,’n’,
’o’,’p’,’q’,’r’};
0 1 2 3 4 5
tabl 0 a b c d e f
e
1 g h i j k l
2 m n o p q r
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 6
Ex. 1: 2-D Array Initialization
It is highly recommended, however, that you nest the data in
braces to show the exact nature of the array. For example, array
table is better initialized as :
// 2-D array of 18 initialized chars
const int NUM_ROWS = 3, NUM_COLS = 6;
Char table[NUM_ROWS][NUM_COLS]={
{’a’,’b’,’c’,’d’,’e’,’f’},
{’g’,’h’,’i’,’j’,’k’,’l’},
{’m’,’n’,’o’,’p’,’q’,’r’}
};
0 1 2 3 4 5
tabl 0 a b c d e f
e
1 g h i j k l
2 m n o p q r
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 7
Ex. 1: 2-D Array Inputting Values
Another way to fill up the values is to read them from the keyboard. For a two-dimensional
array this usually requires nested for loops. If the array is an n by m array, the first loop
varies the row from 0 to n-1. The second loop varies the column from 0 to m -1.
const int NUM_ROWS = 3, NUM_COLS = 6;
for (int row=0; row < NUM_ROWS; row++)
for (int column = 0; column < NUM_COLS; column++)
[Link](table[row][column]);
//input “two-dimensional array”
0 1 2 3 4 5
tabl 0 t w o - d i
e
1 m e n s i o
2 n a l a r
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 8
Ex. 1: 2-D Array Outputting Values
We can print the values of the elements one-by-one using two nested
loops. Again, if the array is an n by m array, the first loop varies the row
from 0 to n-1. The second loop varies the column from 0 to m-1.
const int NUM_ROWS = 3, NUM_COLS = 6;
for (int row=0; row < NUM_ROWS; row++)
for (int column = 0; column < NUM_COLS; column++)
cout << table[row][column];
0 1 2 3 4 5
tabl 0 t w o - d i
e
1 m e n s i o
2 n a l a r
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 9
Ex. 2: Addition Table
We can store the values of the elements in an addition
table in a two-dimensional array using two nested
loops. Again, if the array is an n by n array, the first
loop varies the row from 0 to n-1. The second loop
varies the column from 0 to n-1.
0 1 2 3
tabl 0 0 1 2 3
e
1 1 2 3 4
2 2 3 4 5
3 3 4 5 6
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 10
Ex. 2: Addition Table
// An addition table in a 2-dimensional array
#include <iostream.h>
const int TABLE_SIZE = 4; // global size of addition table
int main(){
int row, column;
int add_table [TABLE_SIZE][TABLE_SIZE]; // declare array
// create the array for the addition table
for (row = 0; row < TABLE_SIZE; row++){
for (column = 0; column < TABLE_SIZE; column++){
add_table[row][column] = row + column;
}
}
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 11
Ex. 2: Addition Table
// Print the addition table
for (row = 0; row < TABLE_SIZE; row++){
for (column = 0; column < TABLE_SIZE; column++){
// don't output 0
// if((row == 0) && (column == 0))
// cout << " ";
// else
cout << add_table[row][column] << " ";
}
cout << endl;
}
return 0;
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 12
Mathematical Functions
#include <math.h>
(text book page 785)
double log(double x) //natural logarithm
double log10(double x) //base 10 logarithm
double exp(double x) //e to the power x
double pow(double x, double y) //x to the power y
double sqrt(double x) //positive square root of x
double sin(double x), cos(double x), tan(double x)
//and many others
F ig u r e 4 -2 0
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 13
Mathematical Functions
C o p y r ig h t © 2 0 0 0 b y B r o o k s /C o le P u b lis h i n g C o m p a n y
A d iv is io n o f I n te r n a tio n a l T h o m s o n P u b lis h in g I n c .
double ceil(double x) //smallest integer not less than x
// ceil(1.1) == 2, ceil(-1.9) == -1
double floor(double x) //largest integer not greater than x
// floor(1.9) == 1, floor(-1.1) == -2
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 14
Ex. 3: Distance Between Two Points
• We can use the Pythagorean theorem to calculate
the distance between 2 points:
a2 + b 2 = c 2
x
0 1 2 3 4 5 6 7 8 0 10 11 12
1 *
y
2 c
3 b
4 *
a
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 15
Ex. 3: Distance Between Two Points
// Compute distance between two points
#include <iostream.h>
#include <math.h> // contains sqrt()
int main(){
double col1, row1, col2, row2; // coordinates for pnt 1 & 2
double dist; // distance between points
cout << "Enter col(x) & row(y) coordinates of 1st point: ";
cin >> col1 >> row1;
cout << "Enter col(x) & row(y) coordinates of 2nd point: ";
cin >> col2 >> row2;
// Compute the distance between points 1 & 2
//dist=sqrt((col2-col1)*(col2-col1)+(row2-row1)*(row2-row1));
dist = sqrt(pow((col2 – col1),2) + pow((row2-row1),2));
cout << "The distance is " << dist << endl;
return 0;
}
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 16
Ex. 4 & 5: Graphing Trajectories
• The next 2 examples use arrays to store the
characters in a grid.
• Example 4 graphs one line on the grid.
• Example 5 graphs multiple lines on the grid, using
an extra while loop.
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 17
Ex. 4: Trajectory Between 2 Points
x
0 1 2 3 4 5 6 7 8 0 10 11 12 13 14 15
1 *
2 *
y
3 *
4 *
5 *
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 18
Ex. 4: Graphing a Trajectory
// Graph line between two points
#include <iostream.h>
int const NUMBER_ROWS = 11; // global constants
int const NUMBER_COLS = 31;
int main(){
// set an array for the grid
char grid[NUMBER_ROWS][NUMBER_COLS];
int row, col, row1, col1, row2, col2; // coordinates
double rise, run, slope;
// for background, fill grid with '-' character
for for (row = 0; row < NUMBER_ROWS; row++)
for(col = 0; col < NUMBER_COLS; col++)
grid[row][col] = '-';
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 19
Ex. 4: Graphing a Trajectory
// get user input and check that it is on the grid
do{
cout << "Enter column (x<" << NUMBER_COLS
<< ") & row (y<"
<< NUMBER_ROWS <<") coordinates of the 1st point:
";
cin >> col1 >> row1;
} while((row1 < 0) || (row1 >= NUMBER_ROWS) || (col1<0) ||
(col1 >= NUMBER_COLS));
do{
cout << "Enter column (x<" << NUMBER_COLS
<< ") & row (y<"
<< NUMBER_ROWS <<") coordinates of the 2nd point:
";
cin >> col2 >> row2;
} while((row2<0) || (row2 >= NUMBER_ROWS) || (col2<0) ||
(col2 >= NUMBER_COLS));
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 20
Ex. 4: Graphing a Trajectory
// put characters into array to graph the line on grid
// handle special cases where col1 == col2 first
// to avoid division by 0
if((row1 == row2) && (col1 == col2)) // just one point
grid[row1][col1] = '*';
else if(col2 == col1){ //slope is infinite
if (row1 < row2) //fill downward
for(row = row1; row <= row2; row++)
grid[row][col1] = '*';
else //fill upward
for(row = row1; row >= row2; row--)
grid[row][col1] = '*';
}
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 21
Ex. 4: Graphing a Trajectory
else{
rise = row2 - row1;
run = col2 - col1;
slope = (double)rise / run; // run cannot = 0
if (run >0){
for(col = col1; col <= col2; col++){
// row1 is offset for starting point
row = (int)(slope * (col - col1) + row1);
grid[row][col] = '*';
}
}
else{
for(col = col1; col >= col2; col--){
row = (int)(slope * (col - col1) + row1);
grid[row][col] = '*';
}
}
}
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 22
Ex. 4: Graphing a Trajectory
// print the grid from the array to the screen
for(row = 0; row < NUMBER_ROWS; row++){
for(col = 0; col < NUMBER_COLS; col++){
cout << grid[row][col];
}
cout << endl;
}
return 0;
}
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 23
Ex. 5: Trajectories Among Points
x
0 1 2 3 4 5 6 7 8 0 10 11 12 13 14 15
1 * * * * *
2 * * * * * *
y
3 * *
4 *
5 *
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 24
The First Computer
The construction of the first electronic computer, named
the ENIAC, was commissioned by the US military in 1943 to
compute the trajectory of a 16-in naval shell. But WWII was
already over when the ENIAC was completed. All together,
the U.S. Army provided approximately $500,000 for the
ENIAC's development.
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 25
The First Computer
ENIAC (Electronic Numerical Integrator and
Computer)
Development period: 1943-1946
Designers: John Mauchly and J. Presper Eckert, Jr.
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 26
The First Computer
Development organization: The Moore School of
Electrical Engineering, University of Pennsylvania
Components: 18,000 vacuum tubes
Weight: 30 tons
Room to hold it: 1,500 square feet
Word size: 10 decimal digits
Cycle time: 100K Hz
Internal storage: none
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 27
Flight Distance of a Projectile
Newton’s laws of motion
V = V0 + a t (1)
S = V0 t + ½ a t² (2)
Where
V is the velocity after acceleration;
V0 is the initial velocity;
a is the acceleration;
t is the flight time;
S is the flight distance.
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 28
Flight Distance of a Projectile
Assuming no air resistance
Vy = 0 when it is half way into the flight
ay = -g gravity which is 9.8 meters/sec²
half flight time t = V0y/g = V0 * sin(¼ π)/9.8 (1)
flight_distance = V0x * 2 * t = V0 * cos(¼ π) * 2 * t (2)
For example, for a howitzer (Type-1954 122 mm, made in China)
with muzzle speed of 515 meters/sec pointing at 45º
upward.
flight_time = 2 * 515 * sin(¼ π )/9.8
flight_distance = 515 * cos(¼ π) * flight_time
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 29
Flight Distance of a Projectile
V0 cos() V0
V0 sin()
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 30
A Plotting Function
//Function that places a 'o' at location (x,y)
void graph_x_y(int x, int y){
if (x >= 0 && x < x_size &&
y >= 0 && y < y_size)
grid[x][y] = 'o';
}
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 31
Using the Plotting Function
/*Draw a trajectory for a gun whose muzzle speed is 515
meters/sec at 45 degrees upward angle */
v = 515.0;
theta_r =0.25 * PI;
// Find 1/2 flight time using 0 = vy - gt
t_half = v * sin(theta_r) / GRAV;
fl_time = 2 * t_half;
// Find proper x-scale
x_scale = fl_time / x_size;
// Find max flight height
height=v*sin(theta_r)*t_half - 0.5*GRAV*t_half*t_half;
// Find proper y-scale
y_scale = (y_size - 1) / height;
COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 32
Using the Plotting Function
// Graph this trajectory
for (i = 0; i < x_size; i++){
fl_time = i * x_scale;
h=int((v*sin(theta_r)*fl_time-
0.5*GRAV*fl_time*fl_time)*y_scale+0.5));
graph_x_y(i, h);
}
// Print the trajectories
for (int j = y_size-1; j >= 0; j--){
for (i = 0; i < x_size; i++)
cout << grid[i][j];
cout << endl;
}