You are on page 1of 19

C++ Programming: From Problem Analysis to

Program Design, Fourth Edition


1
Dynamic Arrays
C++ Programming: From Problem Analysis to
Program Design, Fourth Edition
2
Dynamic array: array created during the execution of a
program
Example:
int *p;
p = new int[10];

*p = 25;
p++; //to point to next array component
*p = 35;
stores 25 into the first memory location
stores 35 into the second memory location
Dynamic Arrays (continued)
C++ Programming: From Problem Analysis to
Program Design, Fourth Edition
3
C++ allows us to use array notation to access these memory
locations
The statements:
p[0] = 25;
p[1] = 35;
store 25 and 35 into the first and second array components,
respectively

Example on Dynamic Array Operation
6
void main()
{ int *p;
p=new int[5];
*p=2;

for(int i=1;i<5;i++)
p[i]=p[i-1]+i;

for(int i=1 0;i<5;i++)
cout<<p[i]<<" ";
}
Output:
2 3 5 8 12
Exercise
7
void main()
{ int *secret;
int j;
secret=new int[10];
int value=2;

for(j=0;j<10;j++)
{ secret[j]=value+1;
value=value+secret[j];
}

for(j=0;j<10;j++)
cout<<secret[j]<<" \n";
}
Output:
1. What is the output for the given program:
Exercise(continue)
C++ Programming: From Problem Analysis to
Program Design, Fourth Edition
8
2. Write a program to input 10 characters into dynamic array and display
the number of z entered by user.

3. Write a program to input 5 numbers into dynamic array and display how
many numbers is odd number.
Functions and pointers
9
Static one dimensional array operations(Programming sample) :

void inputData(double array[ ],int numOfRow);
void findSum(double array[ ],int numOfRow);

void main()
{ double value[5];

inputData(value,5);
findSum(value,5);
getch();
}

void inputData(double array[],int numOfRow)
{
cout<<"Enter values:";
for(int row=0;row<numOfRow;row++)
cin>>array[row];
}
void findSum(double array[],int numOfRow)
{
double sum=0;

for(int r=0;r<numOfRow;r++)
sum=sum+array[r];

cout<<"\nSum is "<<sum;
}
Functions and pointers
10
Dynamic one dimensional array operations(first way) :

void inputData(double *array,int numOfRow);
void findSum(double * array,int numOfRow);

void main()
{ double *value;
int numOfRow;

cout<<"\nEnter a number of data:";
cin>>numOfRow;

value=new double[numOfRow];

inputData(value,numOfRow);
findSum(value,numOfRow);
getch();
}

void inputData(double *array,int numOfRow)
{
cout<<"Enter values:";
for(int row=0;row<numOfRow;row++)
cin>>array[row];
}

void findSum(double *array,int numOfRow)
{
double sum=0;

for(int row=0;row<numOfRow;row++)
sum=sum+array[row];

cout<<"\nSum is "<<sum;
}
Functions and pointers
11
Dynamic one dimensional array operations(Second way) :

void inputData(double *array,int numOfRow);
void findSum(double * array,int numOfRow);

void main()
{ double *value;
int numOfRow;

cout<<"\nEnter a number of data:";
cin>>numOfRow;

value=new double[numOfRow];

inputData(value,numOfRow);
findSum(value,numOfRow);
getch();
}

void inputData(double *array,int numOfRow)
{
cout<<"Enter values:";
for(int row=0;row<numOfRow;row++)
{ cin>>*array;
array++;
}
}
void findSum(double *array,int numOfRow)
{
double sum=0;

for(int row=0;row<numOfRow;row++)
{ sum=sum+*array;
array++;
}
cout<<"\nSum is "<<sum;
}
Exercise 1
12
1. Write a full program that will input a number of row or data, input
the integer numbers into a dynamic array and display the highest value
of the numbers. You must use these functions and apply first way:

a) inputData
-Input a data

b) displayData
-display all the data

c) findHighest
-find the highest value and return the highest value

Exercise 2
13
2. Write a full program that will input a number of row or data, input the
integer numbers into a dynamic array and display the number of odd
numbers in the data. You must use these functions and apply second way:

a) inputData
-Input a data
-Return an input value through reference parameter.

b) displayData
-display a data

c) countOdd
-count the number of odd numbers and display the
odd numbers.

Dynamic Two-Dimensional Arrays
C++ Programming: From Problem Analysis to
Program Design, Fourth Edition
14
To create dynamic multidimensional arrays
declares board to be a pointer to a pointer
Dynamic Two-Dimensional Arrays
15
Programming sample :

void main()
{ int * *board;
int numOfCol,numOfRow;

cout<<"\nEnter a number of row:";
cin>>numOfRow;
board=new int*[numOfRow];

cout<<"\nEnter a number of column:";
cin>>numOfCol;

for(int row=0;row<numOfRow;row++)
board[row]=new int[numOfCol];

cout<<"\nEnter a numbers:\n";
for(int row=0;row<numOfRow;row++)
{ for(int col=0;col<numOfCol;col++)
cin>>board[row][col];
}

int sum=0;
for(int row=0;row<numOfRow;row++)
{ for(int col=0;col<numOfCol;col++)
sum=sum+board[row][col];
}
cout<<"\nSum is "<<sum;
int ave=sum/(numOfRow*numOfCol);
cout<<"\nAverage is "<<ave;
getch();
}
Functions and pointers
(2D Dynamic Arrays
16
Dynamic two dimensional arrays operations(Programming sample) :

void inputData(double **array,int rowSize,int colSize);
void findSum(double **array,int rowSize,int colSize);

void main()
{ double **board;
int numOfRow,numOfCol;

cout<<"\nEnter a number of row:";
cin>>numOfRow;

board=new double*[numOfRow];

cout<<"\nEnter a number of column:";
cin>>numOfCol;

for(int row=0;row<numOfRow;row++)
board[row]=new double[numOfCol];

inputData(board,numOfRow,numOfCol);
findSum(board,numOfRow,numOfCol);
getch();
}
void inputData(double **array,int rowSize,int colSize)
{
cout<<"Enter values:";
for(int row=0;row<rowSize;row++)
for(int col=0;col<colSize;col++)
cin>>array[row][col];
}

void findSum(double **array,int rowSize,int colSize)
{
double sum=0;

for(int row=0;row<rowSize;row++)
for(int col=0;col<colSize;col++)
sum=sum+array[row][col];

cout<<"\nSum is "<<sum;
}
Exercise 3
17
1. Write a full program that will input a number of row and column, input the
integer numbers into a dynamic two dimensional array and display the sum
value for each row and highest value for column 0. You must use these
functions:

a) inputData
-Input a data

b) findSum
-display sum value for each row

c) findHighest
-find the highest value for column 0 and return the highest
value.

Exercise 4
18
1. Write a full program that will input a number of sales
person(represent row) and a number of branch(represent column .
Your program should use the function to do the following:

a) inputData
-Input a data for sales value into dynamic 2D arrays

b) findAvg
-display average value for each branch.

c) countLess300
-count and return the number of sales that below 300.

Exercise 5
Write a full program that will input a number of row and column, input
the name of your friends into a dynamic two dimensional array. Then
display the longest name and the location of name Aliya if it is found.
You must use these functions:
a) inputData
-Input a data
b) findLongest
-display the longest name
c) findAliya
-find the name Aliya and return the location(row number) of it name if
it is found or return -1 if it is not found.
19

You might also like