In this problem, you will design a structure in C that contains data to model a
ID: 3818172 • Letter: I
Question
In this problem, you will design a structure in C that contains data to model a state capital and write several functions that process and act on that data. The first step will be to design the StateCapital structure. Your structure should include the following components: state name, capital name, land area, and date of union entry (the year). A sample record of Nebraska is like: (Nebraska, Lincoln, 77358, 1867). To make using the webgrader easier for you to use on this program, a psudocode outline is provided for you below so that you know how data is to be inputed into the program. Basically there will be two loops in the main function. The first is a for loop that lets the user enter a specified number of states that are inputed via the scanf() function from the terminal. Then the program goes into a while loop for a menu that prompts the user for choices involving the other function calls. The program exits the while loop when user selects the “Exit” option.
Implement these functions:
StateCapital *createStateCapital(const char *state_name, int date_of_statehood, const char *capital, double land_area);
StateCapital *findOldestState(StateCapital *stateCapital, int size);
StateCapital *findSmallestState(StateCapital *stateCapital, int size);
StateCapital *findYoungesgtState(StateCapital *stateCapital, int size);
StateCapital *findGreatestLandArea(StateCapital *stateCapital, int size);
StateCapital *findStateCapital(StateCapital *stateCapital, int size, char *stateName);
Structure to use:
typedef struct { char *state_name; int date_of_statehood; char * capital; double land_area; } StateCapital;
Program flow: Begin program: //programer supplied data createStateCapital() createStateCapital() ... 8 //begin webgrader supplied data while true input user-choice if 1 findOldestState() print result end if if 2 findYoungesgtState print result end if if 3 findGreatestLandArea() print result end if if 4 findSmallestState() print result end if if 5 input user-choice findStateCapital() print result end if if 6 break end while memory clean up return End Program
Explanation / Answer
// state.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Define a structure to hold details of a state
typedef struct
{
char *name;
char *capital;
double landarea;
int unionyear;
}StateCapital;
/*
Creates a state by making a deep copy relevant arguments passed and returns the pointer to
newly created structure
*/
StateCapital *createStateCapital(const char *state_name,int date_of_statehood,const char *capital, double landarea)
{
StateCapital *state=(StateCapital *)malloc(sizeof(StateCapital)); //allocate memory for the sturcture
state->name=strdup(state_name); //makes a copy of the state_name
state->unionyear=date_of_statehood;
state->capital=strdup(capital); //make a copy of capital
state->landarea=landarea;
return state;
}
/*
Returns the string representation of the given State
*/
char *StateToString(StateCapital *stateCapital)
{
char buffer[100];
sprintf(buffer,"%s, %s, %.2f, %d",stateCapital->name,stateCapital->capital,stateCapital->landarea,stateCapital->unionyear);
char *str= malloc(strlen(buffer)+1); //allocate memory for the characters in the buffer
strcpy(str,buffer);//copy the string from buffer
return str;
}
/*
Given the array of states of given size, the function finds the oldest state i.e the one
with lowest unionyear value
*/
StateCapital *findOldestState(StateCapital *stateCapital,int size)
{
StateCapital *oldest=stateCapital;//assume the first structure element to be oldest and store that as oldest
int i;
for(i=1;i<size;i++)//remaining elements other than first
{
if((stateCapital+i)->unionyear < oldest->unionyear) //check if current state is older than the previously stored oldest state
{
oldest=stateCapital+i;
}
}
return oldest;
}
/*
Given the array of states of given size, the function finds the yongest state i.e the one
with highest unionyear value
*/
StateCapital *findYoungestState(StateCapital *stateCapital,int size)
{
StateCapital *youngest=stateCapital;//assume the first structure element to be youngest and store that as youngest
int i;
for(i=1;i<size;i++)//remaining elements other than first
{
if((stateCapital+i)->unionyear > youngest->unionyear) //check if current state is younger than the previously stored youngest state
{
youngest=stateCapital+i;
}
}
return youngest;
}
/*
Find the smallest state in terms of landarea
*/
StateCapital *findSmallestState(StateCapital *stateCapital,int size)
{
StateCapital *smallest=stateCapital;//assume the first structure element to be smallest
int i;
for(i=1;i<size;i++)//remaining elements other than first
{
if((stateCapital+i)->landarea < smallest->landarea) //check if current state's landarea is is lesser than that of the previously stored smallest state
{
smallest=stateCapital+i;
}
}
return smallest;
}
/*
Find the state with greatest landarea
*/
StateCapital *findGreatestLandArea(StateCapital *stateCapital,int size)
{
StateCapital *greatest=stateCapital;//assume the first structure element to be greatest
int i;
for(i=1;i<size;i++)//remaining elements other than first
{
if((stateCapital+i)->landarea > greatest->landarea) //check if current state's landarea is is greatter than that of the previously stored greatest state
{
greatest=stateCapital+i;
}
}
return greatest;
}
/*
Find the state capital structure for the specified statename. Returns the structure for matching statename if one exists otherwise returns NULL
*/
StateCapital *findStateCapital(StateCapital *stateCapital,int size,char *statename)
{
int i;
for(i=0;i<size;i++)
{
if(strcmp((stateCapital+i)->name,statename)==0) //check if current state's name is the same as the one we are looking for
{
return stateCapital+i;
}
}
return NULL;//not found a match so far
}
int main()
{
int n=10;
int i,choice;
StateCapital *states,*s;
char *sname;
size_t len=25;
sname=malloc(sizeof(len));//for statename input from user
states= (StateCapital *)malloc(sizeof(StateCapital) * n);
*(states) = *createStateCapital("Alabama",1819,"Montgomery",52420);
*(states+1) = *createStateCapital("Alaska",1959,"Juneau",665384);
*(states+2) = *createStateCapital("California",1850,"Sacramento",163695);
*(states+3) = *createStateCapital("Delaware",1787,"Dover",2489);
*(states+4) = *createStateCapital("Kentucky",1792,"Frankfort",40408);
*(states+5) = *createStateCapital("Michigan",1837,"Lansing",56539);
*(states+6) = *createStateCapital("Nebraska",1867,"Lincoln",77348);
*(states+7) = *createStateCapital("New York",1788,"Albany",47126);
*(states+8) = *createStateCapital("Texas",1845,"Austin",261232);
*(states+9) = *createStateCapital("Washington",1889,"Olympia",66456);
//display the states
printf("Here are the %d states we have : ",n);
for(i=0;i<n;i++)
printf(" %s ",StateToString(states+i));
while(1)
{
//display menu
printf("1. Find Oldest State ");
printf("2. Find Youngest State ");
printf("3. Find Greatest Land Area ");
printf("4. Find Smallest State ");
printf("5. Find State Capital ");
printf("6. Exit ");
printf("Your choice: ");
scanf("%d",&choice);
if(choice==1)
{
s=findOldestState(states,n);
printf("Oldest state is %s ",StateToString(s));
}
else if(choice==2)
{
s=findYoungestState(states,n);
printf("Youngest state is %s ",StateToString(s));
}
else if(choice==3)
{
s=findGreatestLandArea(states,n);
printf("Greatest Land area state is %s ",StateToString(s));
}
else if(choice==4)
{
s=findSmallestState(states,n);
printf("Smallest state is %s ",StateToString(s));
}
else if(choice==5)
{
printf("Enter a state name: "); getchar();//flush new line character
gets(sname);
s=findStateCapital(states,n,sname);
if(s!=NULL)
printf("State is %s ",StateToString(s));
else
printf("No such state ");
}
else if(choice==6)
{
break;
}
}
free(states);
free(sname);
}
/*
output:
Here are the 10 states we have :
Alabama, Montgomery, 52420.00, 1819
Alaska, Juneau, 665384.00, 1959
California, Sacramento, 163695.00, 1850
Delaware, Dover, 2489.00, 1787
Kentucky, Frankfort, 40408.00, 1792
Michigan, Lansing, 56539.00, 1837
Nebraska, Lincoln, 77348.00, 1867
New York, Albany, 47126.00, 1788
Texas, Austin, 261232.00, 1845
Washington, Olympia, 66456.00, 1889
1. Find Oldest State
2. Find Youngest State
3. Find Greatest Land Area
4. Find Smallest State
5. Find State Capital
6. Exit
Your choice: 1
Oldest state is Delaware, Dover, 2489.00, 1787
1. Find Oldest State
2. Find Youngest State
3. Find Greatest Land Area
4. Find Smallest State
5. Find State Capital
6. Exit
Your choice: 2
Youngest state is Alaska, Juneau, 665384.00, 1959
1. Find Oldest State
2. Find Youngest State
3. Find Greatest Land Area
4. Find Smallest State
5. Find State Capital
6. Exit
Your choice: 3
Greatest Land area state is Alaska, Juneau, 665384.00, 1959
1. Find Oldest State
2. Find Youngest State
3. Find Greatest Land Area
4. Find Smallest State
5. Find State Capital
6. Exit
Your choice: 4
Smallest state is Delaware, Dover, 2489.00, 1787
1. Find Oldest State
2. Find Youngest State
3. Find Greatest Land Area
4. Find Smallest State
5. Find State Capital
6. Exit
Your choice: 6
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.