Write a C/C++ program that exhibits a single struct with an array of 20 pointers
ID: 3560884 • Letter: W
Question
Write a C/C++ program that exhibits a single struct with an array of 20 pointers to 'char' and an integer array with 20 elements (the number of char's allocated to each pointer). These integers are initialized according to the recursive definition:
f(0)=2900 & f(n+1)=2*f(n), where n is an index of the integer array for 0< n < 20.
At the start of the program, allocate memory to all pointers based on the size stored in the integer array, and initialize every char element to a random upper case letter.
Your program prints the main menu: (1) Access a pointer and (2) Exit program. On access: Your program prompts the user for the index of the array to manipulate. Then, present the user with these submenu items: (1) Display 10 random char's in the chosen array starting at some location the user specified (prompt user and validate input), (2) Delete all the char's associated with this pointer, and (3) Return to main menu. When your user chooses to delete the char's, deallocate memory (physically). When a user accesses a pointer whose char's have been deleted, your program notifies the user of the missing char's, re-allocates memory & re-initializes them. Remember to manage memory properly. On exit: Your program will deallocate all memory (even though the OS will automatically do it).
Explanation / Answer
C Implementation
#include <stdio.h>
#include <stdlib.h>
#include <cmath.h>
typedef struct charPtr{
int arr[20];
char** charr[20];
}charPtr;
int f( int n ){
if(n == 21)
return ;
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;
printf(" Main menu");
printf(" 1. Access pointer");
printf(" 2. Exit program ");
printf(" Enter choice : ");
scanf("%d", &mc);
switch(mc){
case 1:
printf(" Enter index of array to manipulate: ");
scanf("%d", &index);
if(index < 1 || index > 20){
printf(" Valid index: 1 - 20 ");
}else{
printf(" 1. Display 10 random characters.");
printf(" 2. Delete all the characters associated with this pointer, ");
printf(" 3. Return to main menu. ");
do{
printf(" Enter choice : ");
scanf("%d", &sc);
switch(sc){
case 1:
printf(" Enter starting location : ");
scanf("%d", &sc2);
if(dindex != -1)
printf(" Missing character at %d ", dindex+1);
printf(" ");
for(i = sc2-1; i < 20; i++){
if(i == dindex){
// initialize the deleted index
printf(" ");
realloc (cp->charr[i], sizeof(char));
*(cp->charr[i]) = 'A' + abs(rand()%20 - i);
dindex = -1;
}else{
printf("%c ",*(cp->charr[i]));
}
}
printf(" ");
break;
case 2:
free(cp->charr[index-1]);
printf(" Character pointer at index %i deleted. ", index);
dindex = index-1;
break;
case 3:
break;
default:
printf(" Invalid choice.");
break;
}
}while(sc != 3);
}
break;
case 2:
free(cp);
printf(" Program terminated.");
break;
default:
printf(" Invalid choice.");
break;
}
}while(mc != 2);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.