This assignment has three simple programs. 1. Write a program named DynamicStruc
ID: 3684119 • Letter: T
Question
This assignment has three simple programs. 1. Write a program named DynamicStructs. Create a data structure for automobiles. It should have the name of the car, the color, etc. Dynamically allocate a data structure, ask the user to enter the information from within a function named EnterInfo, then display the information in a function named DisplayInfo. 2. Write a program named CompareStrings. You will need two char arrays, name them one and two, and have enough room for single words. Ask the user to enter two words (lower case), compare the words, and say if the first word is before or after the second word in lexigraphical order. 3. Create a program named DynamicList. Create a data structure for classes at UCF. It should have the class identifier such as COP3223 and other information such as the department. Create a linked list of four classes from within a function named CreateList. Now display all items in list from a function named WalkListAndDisplay. Zip the source code for all three programs and submit.
Explanation / Answer
First program:
#include<stdio.h>
#include<string.h>
int main()
{
char one[100], two[100];
printf("Enter first word: ");
gets(one);
printf("Enter second word: ");
gets(two);
if(strcmp(one,two)>0)
{
printf("%s is after %s", one, two);
}
else
printf("%s is after %s", two, one);
return(1);
}
Second Program:
/*
While you use GCC remove ---->#include<conio.h> and add System("clear") instead of clrscr();
While you use Boarland compiler add getch() just above return(1) in main function and add clrscr();
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct automobile
{
char car_name[100];
char car_color[100];
struct automobile *next;
};
typedef struct automobile car;
car *head=NULL;
void EnterInfo();
void DisplayInfo();
int main()
{
int opp;
while(1)
{
//clrscr();
printf("1. Enter car details ");
printf("2. Diplay car details ");
printf("3. Exit ");
printf("Enter your choice:");
scanf("%d", &opp);
switch(opp)
{
case 1:
EnterInfo();
break;
case 2:
DisplayInfo();
break;
case 3:
exit(0);
break;
default:
printf("Wrong choice try again!!!");
break;
}
}
return(1);
}
void EnterInfo()
{
car *p, *s;
char name[100], clr[100];
p=head;
if(head==NULL)
{
p=(car *)malloc(sizeof(car));
printf("Enter car name: ");
gets(name);
gets(name); // this extra gets() is for garbage value return by first one it varies compiler to compiler
strcpy(p->car_name, name);
printf("Enter car color: ");
gets(clr);
strcpy(p->car_color,clr);
p->next=NULL;
head=p;
}
else
{
while(p->next!=NULL)
p=p->next;
s=(car *)malloc(sizeof(car));
printf("Enter car name: ");
gets(name);
gets(name);
strcpy(s->car_name, name);
printf("Enter car color: ");
gets(clr);
strcpy(s->car_color,clr);
s->next=NULL;
p->next=s; //adding another node at the end of the linked list
}
}
void DisplayInfo()
{
car *p;
p=head;
if(head==NULL)
printf("No Data Present");
else
while(p!=NULL)
{
printf(" Car Name: %s", p->car_name);
printf(" Car Color: %s", p->car_color);
p=p->next;
}
printf(" ");
}
3rd Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct UCF
{
int year;
char subject1[100];
char subject2[100];
char subject3[100];
struct UCF *next;
};
typedef struct UCF ucf;
ucf *head=NULL;
void CreatList();
void WalkListAndDisplay();
int main()
{
int opp;
while(1)
{
//clrscr();
printf("1. Enter UCF details ");
printf("2. Diplay UCF details ");
printf("3. Exit ");
printf("Enter your choice:");
scanf("%d", &opp);
switch(opp)
{
case 1:
CreatList();
break;
case 2:
WalkListAndDisplay();
break;
case 3:
exit(0);
break;
default:
printf("Wrong choice try again!!!");
break;
}
}
return(1);
}
void CreatList()
{
ucf *p, *s;
char sub[100];
int y;
p=head;
if(head==NULL)
{
p=(ucf *)malloc(sizeof(ucf));
printf("Enter year: ");
scanf("%d", &y);
p->year=y;
printf("Subject 1: ");
gets(sub);
gets(sub);
strcpy(p->subject1, sub);
printf("Subject 2: ");
gets(sub);
strcpy(p->subject2, sub);
printf("Subject 3: ");
gets(sub);
strcpy(p->subject3, sub);
p->next=NULL;
head=p;
}
else
{
while(p->next!=NULL)
p=p->next;
s=(ucf *)malloc(sizeof(ucf));
printf("Enter year: ");
scanf("%d", &y);
s->year=y;
printf("Subject 1: ");
gets(sub);
gets(sub);
strcpy(s->subject1, sub);
printf("Subject 2: ");
gets(sub);
strcpy(s->subject2, sub);
printf("Subject 3: ");
gets(sub);
strcpy(s->subject3, sub);
s->next=NULL;
p->next=s; //adding another node at the end of the linked list
}
}
void WalkListAndDisplay()
{
ucf *p;
p=head;
if(head==NULL)
printf("No Data Present");
else
{
printf("Year Subject1 Subject2 Subject3");
while(p!=NULL)
{
printf(" %d %s %s %s", p->year, p->subject1, p->subject2, p->subject3);
p=p->next;
}
printf(" ");
}
}