Henry is working at a ticket counter. He has to store the ticket numbers continuously so he asks for help to free the stored ticket numbers so that he can store the ticket number again.
Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, member variable names, and function names should be the same as specified in the problem statement.
Write a program to read the ticket numbers from the user and display the number after free the memory also.
In the main method, obtain input from the user in the console and display the ticket details.
Note:
Use free to deallocates a block of memory.
Input format:
Input consists of the number of tickets and the ticket numbers.
Output format:
Enter the number of tickets
3
Enter the ticket numbers
21
23
25
Ticket numbers are
21 23 25
After free, the ticket numbers are
0 0 25
#include <iostream>
using namespace std;
int main()
{
cout<<"\nEnter the number of tickets:";
int n;
cin>>n;
int *arr = new int[n];
cout<<"\nEnter the ticket numbers: \n";
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"\nTicket numbers are:\n";
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
cout<<"\n";
cout<<"\nAfter free, the ticket numbers are: ";
for(int i=0;i<(n-1);i++){
arr[i]=0;
}
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
delete [] arr;
return 0;
}
Comments
Leave a comment