Consider the following code that checks to see if adjacent elements in an array
ID: 3867583 • Letter: C
Question
Consider the following code that checks to see if adjacent elements in an array of structs are similar, where means they have one of more fields in common. Create both an anonymous function and a regular function for each of the following cases that responds with true if the two elements are similar. Finally show how to invoke isSimilar to answer the requests that follow: (a) Compare two locations with fields continent, country, and capital. Example: loc1.continent = 'North America', loc1.country = 'Mexico', loca1.capital = 'Mexico City': loc2.continent = 'North America', loc2.country = 'USA', loc2.capital = 'Washing ton D.C': In the example, loc1 and loc2 are similar, because they are on the same continent. Don't forget that to compare two strings, you must use strcmp(). function s = isSimilar(a, cFun) s = true: for ii = 2: length(a) if ^- cFun(a(ii - 1), a(ii)) s = false: end endExplanation / Answer
The function to do the above description is as follows:
Strings.c
struct location
{
char continent[20] = "North America";
char country[20] = "Mexico"
char capital[20] = "Mexico City"
}loc1;
struct locations
{
char continent[20] = "North America";
char country[20] = "USA"
char capital[20] = "Washington D.C";
}loc2;
int compare(const loc1 *l1 , const loc2 *l2)
{
return strcmp(l1->continent,l2->continent);
}
int main()
{
if(compare(&l1,&l2)==0)
{
printf("Locations are identical. ");
}
else
{
printf("Locations are different. ");
}
return 0;
}
Please rate the answer if it helped........Thankyou
Hope it helps.....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.