/***************************************************************
* Name: Prof. Rafael Orta
* Course: Computer Science & Programming
* Class: CS04225
*****************************************************************
* Purpose: Demonstrate the use of Dynamic Arrays
*****************************************************************/
#include <iostream>
using namespace std;
void arrayOps(int*, int); // Function used to populate the array.
int main()
{
int *age_ptr; // Declaring an pointer of integer data type.
int noStudents = 0; // variable used to hold the number of students in the class
cout << "How many students are in the class: ";
cin >> noStudents;
age_ptr = new int[noStudents]; // Allocates "noStudents" integers and points age_ptr to the first element.
arrayOps(age_ptr, noStudents);
delete [] age_ptr; // freeing heap memory used by the dynamic array.
age_ptr = NULL; // Clearing the pointer to avoid invalid memory reference
return 0;
}
void arrayOps(int* age_ptr, int noStudents){
cout << "\n*** DATA COLLECTION ***\n";
for (int x=0; x<noStudents; x++){ // Populates the array.
cout << "Insert the age for the student No. " << x+1 << ": ";
cin >> age_ptr[x];
}
cout << "\n*** DISPLAYING DATA ***\n";
for (int y=0; y<noStudents; y++){ // Populates the array.
cout << "The Student No. " << y+1 << " is " << age_ptr[y] << " years old" << endl;
}
}