Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given this code, in case 1 where the program is to display 10 random characters

ID: 3561967 • Letter: G

Question

Given this code, in case 1 where the program is to display 10 random characters and where the starting location is, I need to be in a range from say 0 to 3000 that the user can input only between those locations. If its within 10 of 3000 print the values by the index for example:

2990: A

2991:D

....

3000: C

But only do this if the location inputted is between those 10 values. Else just print the 10 random values.

Thanks Please ask if you dont understand anything

#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;
}

Explanation / Answer

#include #include char *randstring(int length) { static int mySeed = 25011984; char *string = "abcdefghij"; size_t stringLen = strlen(string); char *randomString = NULL; srand(time(NULL) * length + ++mySeed); if (length < 1) { length = 1; } randomString = malloc(sizeof(char) * (length +1)); if (randomString) { short key = 0; for (int n = 0;n
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote