Points for the most detailed answer! I need comments on each line and a detailed
ID: 3561183 • Letter: P
Question
Points for the most detailed answer! I need comments on each line and a detailed description of what this program is doing....1500 points available
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
typedef struct charPtr{
int arr[20];
char* charr[20];
}charPtr;
int f( int n ){
if(n == 21)
return 0;
else{
if(n == 0)
return 2900;
else
return 2*f(n-1);
}
}
int main(){
int i, mc, sc, sc2, index, dindex = -1; // main choice, sub choice, index & deleted index
char *temp;
charPtr* cp = (charPtr*) malloc (sizeof(charPtr));
// initialization
for( i = 0; i < 20; i++){
// initialize integer array
cp->arr[i] = f(i);
// initialize char array
cp->charr[i] = (char*) malloc (sizeof(char));
*(cp->charr[i]) = 'A' + abs(rand()%20 - i);
//printf("%c ",*(cp->charr[i]));
}
do{
dindex = -1;
cout<<" Main menu";
cout<<" 1. Access pointer";
cout<<" 2. Exit program ";
cout<<" Enter choice : ";
cin>>mc;
switch(mc){
case 1:
cout<<" Enter index of array to manipulate: ";
cin>>index;
if(index < 1 || index > 20){
cout<<" Valid index: 1 - 20 ";
}else{
cout<<" 1. Display 10 random characters.";
cout<<" 2. Delete all the characters associated with this pointer, ";
cout<<" 3. Return to main menu. ";
do{
cout<<" Enter choice : ";
cin>>sc;
switch(sc){
case 1:
cout<<" Enter starting location : ";
cin>>sc2;
if(dindex != -1)
cout<<" Missing character at ", dindex+1;
cout<<" ";
for(i = sc2-1; i < 10; i++){
if(i == dindex){
// initialize the deleted index
cout<<" "<< ;
//realloc (cp->charr[i], sizeof(char));
cp->charr[i] = (char*) realloc (cp->charr[i], sizeof(char));
*(cp->charr[i]) = 'A' + abs(rand()%20 - i);
dindex = -1;
}else{
cout<<*(cp->charr[i]);
}
}
cout<<" ";
break;
case 2:
free(cp->charr[index-1]);
cout<<" Character pointer at index"<<i<<" deleted. "<<index;
dindex = index-1;
break;
case 3:
break;
default:
cout<<" Invalid choice.";
break;
}
}while(sc != 3);
}
break;
case 2:
free(cp);
cout<<" Program terminated.";
break;
default:
cout<<" Invalid choice.";
break;
}
}while(mc != 2);
return 0;
}
Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
//A structure is defined here
typedef struct charPtr{
int arr[20];
char* charr[20];
}charPtr;
//A recursive function which return 0 if n=21 and 2900 if n =0, in other cases multiples of 2
int f( int n ){
if(n == 21)
return 0;
else{
if(n == 0)
return 2900;
else
return 2*f(n-1);
}
}
int main(){
int i, mc, sc, sc2, index, dindex = -1; // main choice, sub choice, index & deleted index
char *temp;
//Allocating memory to structure pointer cp
charPtr* cp = (charPtr*) malloc (sizeof(charPtr));
// initialization
//initializing the cp array with values return by function f(int n)
for( i = 0; i < 20; i++){
// initialize integer array
cp->arr[i] = f(i);
// initialize char array
cp->charr[i] = (char*) malloc (sizeof(char));
//Filling the char array with aplhabets fro A-T (20)
*(cp->charr[i]) = 'A' + abs(rand()%20 - i);
//printf("%c ",*(cp->charr[i]));
}
do{
dindex = -1;
//Displaying menu options
cout<<" Main menu";
cout<<" 1. Access pointer";
cout<<" 2. Exit program ";
//Asking user to enter a choice
cout<<" Enter choice : ";
cin>>mc;
//Handling the user entered choice
switch(mc){
case 1:
//Asking user to enter index of array to manipulate
cout<<" Enter index of array to manipulate: ";
cin>>index;
//check index lies b/w 1-20 so that entered character lies b/w A-T
if(index < 1 || index > 20){
cout<<" Valid index: 1 - 20 ";
}else{
//If user given option out of the range, it displaying few options
cout<<" 1. Display 10 random characters.";
cout<<" 2. Delete all the characters associated with this pointer, ";
cout<<" 3. Return to main menu. ";
do{
//asking user enter choice from above menu
cout<<" Enter choice : ";
cin>>sc;
//Handling the user choice
switch(sc){
case 1:
//Asking user enter to starting location to display 10 random chars
cout<<" Enter starting location : ";
cin>>sc2;
if(dindex != -1)
cout<<" Missing character at ", dindex+1;
cout<<" ";
//Looping from user entered character to atmost 10 characters
for(i = sc2-1; i < 10; i++){
if(i == dindex){
// initialize the deleted index
cout<<" "<< ;
//realloc (cp->charr[i], sizeof(char));
//re allocating memory to character
cp->charr[i] = (char*) realloc (cp->charr[i], sizeof(char));
//Filling the array randomly in those indexex
*(cp->charr[i]) = 'A' + abs(rand()%20 - i);
dindex = -1;
}else{
//if index not equal, printing the char
cout<<*(cp->charr[i]);
}
}
cout<<" ";
break;
case 2:
//Frreening memory as per option 2
free(cp->charr[index-1]);
cout<<" Character pointer at index"<<i<<" deleted. "<<index;
dindex = index-1;
break;
case 3:
//exiting from the menu
break;
default:
//displaying invalid choice
cout<<" Invalid choice.";
break;
}
}while(sc != 3); // looping by condition i.e till option to return to main menu is selected
}
break;
case 2:
//freing up the memory and exiting
free(cp);
cout<<" Program terminated.";
break;
default:
cout<<" Invalid choice.";
break;
}
}while(mc != 2); //loop till choice is not 2 ie exiting program
return 0;
}