The document discusses dynamic arrays in C++. It provides examples of declaring and using dynamic arrays, including initializing values, accessing elements using indexes and pointers, and passing dynamic arrays to functions. It also covers dynamic two-dimensional arrays, with an example that inputs values into a 2D array, calculates the sum and average of elements, and uses functions to modularize the input and calculation logic. Exercises at the end involve writing programs that perform tasks like inputting values into dynamic arrays and calling functions to find the highest/summed values, count odd numbers, or locate a specified name in a 2D array.
The document discusses dynamic arrays in C++. It provides examples of declaring and using dynamic arrays, including initializing values, accessing elements using indexes and pointers, and passing dynamic arrays to functions. It also covers dynamic two-dimensional arrays, with an example that inputs values into a 2D array, calculates the sum and average of elements, and uses functions to modularize the input and calculation logic. Exercises at the end involve writing programs that perform tasks like inputting values into dynamic arrays and calling functions to find the highest/summed values, count odd numbers, or locate a specified name in a 2D array.
The document discusses dynamic arrays in C++. It provides examples of declaring and using dynamic arrays, including initializing values, accessing elements using indexes and pointers, and passing dynamic arrays to functions. It also covers dynamic two-dimensional arrays, with an example that inputs values into a 2D array, calculates the sum and average of elements, and uses functions to modularize the input and calculation logic. Exercises at the end involve writing programs that perform tasks like inputting values into dynamic arrays and calling functions to find the highest/summed values, count odd numbers, or locate a specified name in a 2D array.
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++) 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) :
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;
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