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

I need some help writing this code. CS 100 Lab Eight Fall 2017 Create a director

ID: 3606388 • Letter: I

Question

I need some help writing this code.

CS 100 Lab Eight Fall 2017 Create a directory called lab8 on your machine using mkdir lab8. Complete the following two problems. 1. Name this program one.c Complete the program shown belovw typedef struct letter ( char info struct letter next: Letter int main (void) ( Letter a, b, c, d, e, f, g, h, i, ,k, l, m a.info a.next &b; b.infoLb.next c.info = 'E' ; c,next-Ed; info- 'P'; g.nex t= &h; h . info- L ' ; h . next- s1 ; i,info = 'U'; i, next = &J; ; m, info = ' ..; m.nex t = NULL ; Letter*ptr&a; while ( ptr !=NULL) { printf("%c ", ptr->info) ; ptr ptr->next; printf ("n") // rearrange pointers so that it spells "TWELVEPLUSONE " // YOU CANNOT CHANGE ANY OF THE "info" VALUES IN ANY Letter // After rearranging, print the new word while ( ptr !=NULL) { printf ("c", ptr->info); ptr = ptr->next; printf ("n") return 0 2. Name this program two.c In your one.c program, replace the variable declaration line shown in blue below with the line shown in red. For each variable, you now need to dynamically allocate space for that variable using malloc (as shown for the variable a) Letter a, b, c, d, e, f, g, h I, j Letter a, *b, *c, *d, e, f, g, *h, *I, a = malloc ( sizeof (Letter) ); Modify the rest of your program so that your program runs the same as your one. c program runs. Submit your lab First, on your local machine, compress your lab8 directory into a single (compressed) file. Second, once you have a compressed file that contains your two lab8 programs, submit that file to Blackboard.

Explanation / Answer

1.one.c program:

//#one.c


#include<stdio.h>
typedef struct letter{
  
char info;
struct letter *next;
}Letter;
int main(void)
{


Letter a,b,c,d,e,f,g,h,i,j,k,l,m;

a.info='E'; a.next=&b;
b.info='L';b.next=&c;
c.info='E';c.next=&d;
d.info='V'; d.next=&e;
e.info='E';e.next=&f;
f.info='N';f.next=&g;
g.info='P'; g.next=&h;
h.info='L';h.next=&i;
i.info='U';i.next=&j;
j.info='S'; j.next=&k;
k.info='T';k.next=&l;
l.info='W';l.next=&m;
m.info='O'; m.next=NULL;

Letter * ptr=&a;

while(ptr!=NULL)
{
printf("%c",ptr->info);
ptr=ptr->next;
  
}

printf(" ");
//To make 'TWELVEPLUSONE' first we need to search 'T'
// k.info - we know that k.info already has 'T'.So we need to make next pointer pointing to 'W'
k.next=&l; // This is used to display 'W' .Next we have to search for 'E'
l.next=&a;//used for displaying 'E'.Next letter is 'L' which will be taken care by a.next(i.e b) which has 'L'

b.next=&d;//used for displaying ' V'and so on we have to rearrange next pointing address according to the seqeuence 'TWELVE PLUS ONE'
d.next=&c;//used for displaying 'E'
c.next=&g;//used for displaying 'P
//g->h->i->j this sequence completes 'LUS'
j.next=&m;//used for displaying 'O
m.next=&f;//used for displaying 'n'
f.next=&e;//used for displaying 'e'
e.next=NULL;//to end the sequence;


ptr=&k;

while(ptr!=NULL)
{
printf("%c",ptr->info);
ptr=ptr->next;
  
}

printf(" ");
return 0;
}

2.two.c


#include<stdio.h>
#include<stdlib.h>
typedef struct letter{
  
char info;
struct letter *next;
}Letter;


int main(void)
{


Letter *a=(Letter *)malloc(sizeof(Letter)); //This indicates dynamically declaring memory on heap
Letter *b=(Letter *)malloc(sizeof(Letter));//using malloc function.It returns address of size of Letter.
Letter *c=(Letter *)malloc(sizeof(Letter));//Sometimes we need more memory than available in stack.So We use this concept for allocating on Heap memory
Letter *d=(Letter *)malloc(sizeof(Letter));
Letter *e=(Letter *)malloc(sizeof(Letter));
Letter *f=(Letter *)malloc(sizeof(Letter));
Letter *g=(Letter *)malloc(sizeof(Letter));
Letter *h=(Letter *)malloc(sizeof(Letter));
Letter *i=(Letter *)malloc(sizeof(Letter));
Letter *j=(Letter *)malloc(sizeof(Letter));
Letter *k=(Letter *)malloc(sizeof(Letter));
Letter *l=(Letter *)malloc(sizeof(Letter));
Letter *m=(Letter *)malloc(sizeof(Letter));


a->info='E';a->next=b; //rearrange the next sequence such that it displays 'ELEVENPLUSTWO'

b->info='L';b->next=c;
c->info='E';c->next=d;
d->info='V';d->next=e;
e->info='E';e->next=f;
f->info='N';f->next=g;
g->info='P';g->next=h;
h->info='L';h->next=i;
i->info='U';i->next=j;
j->info='S';j->next=k;
k->info='T';k->next=l;
l->info='W';l->next=m;
m->info='O';m->next=NULL;//make m->next is NULL as this indicates the End of string

Letter *temp=a; //Take a temp pointer variable of Letter and intialise t to a
while(temp!=NULL)//Upto temp not equal to NULL
{
  
printf("%c",temp->info);//print the value of info
temp=temp->next; //reassign t to t->next;
}

printf(" ");
return 0;
}