Question

Project 7 Learning about Structs in C Background With the exception of strings, which you now...

Project 7

Learning about Structs in C

Background

With the exception of strings, which you now know to be arrays of char in C, all the variables we've talked about or used are scalars – they just hold one value at a time. Now that value might be an int, a double, or a single char, but each variable only names one value. Structures are aggregates – they hold multiple values together in a single variable.

You know how the authors of C like to abbreviate everything – “character” becomes “char”, “integer” becomes “int” and so on. Logically, then, structures are called simply “structs” in C. Covered in Chapter 7 of your zyBook, structs let you create variables that hold multiple values at the same time. Now arrays also do this, a[0], a[1], and so on, but all the array elements must be of the same type. No mixing and matching allowed.

But with structs, you can create variables that hold say, three ints, five strings and a double all at the same time. These pieces of the struct are called members. Arrays have elements, but structs have members.

Here's an example that, unsurprisingly for me, uses cats.

typedef struct cat_structure

{

char name[20] ; // could be a nickname

char color[30] ; // explain combinations

int ageInMonths ;

double weightInPounds ;

char temperament[50] ; // explain briefly, e.g. “sweet”

} Cat ;

Cat fluffy ;

strcpy( fluffy.name, “Fluffy” ) ; // Duh

strcpy( fluffy.color, “dilute calico” ) ;

fluffy.ageInMonths = 13 ;

fluffy.weightInPounds = 5.2 ;

strcpy( fluffy.temperament. “varies wildly”) ;

Now all of the information about fluffy is contained in a Cat struct. So if you wanted to clone fluffy (a scary thought if you knew that cat), you could code:

Cat fuzzy ;

fuzzy = fluffy ;

and all the Cat struct members from fluffy would get copied into fuzzy with just one assignment operator.

In C, structs implement the basic Computer Science ideal of “keep related information together”. You could have completely separate, scalar variables and char arrays for all the Cat info, but it would be really unwieldy. By keeping it all together in a struct we can even read and write Cats to and from files with a single command,

Deliverables

Maybe the usefulness of structs will be more clear after you design your own.

Designing your struct

a. Pick one of the following topics, and claim it with a post. (Two people, but no more can claim a topic, and must explain how their structs differ.)

Dogs

Jobs

Cars

Trucks

Friends

Valencia Classes

NFL teams

NBA teams

Soccer teams

Hockey teams

College sport teams

Hobbies

Games

Desktop computers

Phones

Tablets

Laptops

Programming Languages

Social media

Families

Children

Fashion

Shoes

Recreation

(If you think of something else you'd really like to create a struct for, then email/message me and we'll see if it will work). You can't do cats, because I'm doing that one.

b. Design a struct to hold at least five important characteristics of your topic. Use char arrays for text, ints for things that have a whole number, and doubles for things that need a decimal point. Code your struct in C, using the typedef method explained in zyBook Chapter 7.

Use comments alongside struct members that need more explanation, as I did with temperament in the Cat struct. This will help you and others figure out what to put there.

Use a variety of data types for your struct members, as in the Cat example (int, double, char []). Don't make it all ints or all doubles, or all strings.

All that goes in your .h file is your struct definition (typedef). No actual code to say, set the members to a value – that all goes in a .c file that #includes your .h.

c. Save your struct definition as a .h file, as (too briefly) described in zyBook section 7.5

d. Submit your .h file on this assignment.

e. Post your .h file in the discussion forum. Also attach it for easy downloading.

So to summarize::

  • Pick a topic

  • Claim it in a post

  • Design a struct for information related to that topic.

  • Save your struct as a .h file. Name it with the topic using all lower-case. So I am creating cat.h

  • Submit your .h file here
  • Post the .h file as an attachment in a reply to your claim.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

typedef struct phones
{
char manufacturer[50];
char model[15];
char color[25];
int camera;
float screen_size;
int ram;
float price;
}Phone;

--------------------------------------------

#include<stdio.h>
#include"a.h"

int main(){

Phone smartPhone1;
strcpy(smartPhone1.manufacturer,"Tecno" );
strcpy(smartPhone1.model,"Spark K7");
strcpy(smartPhone1.color,"Black");
smartPhone1.camera = 16; //mega pixels
smartPhone1.screen_size = 5.5; //inches
smartPhone1.ram = 1; //Gb
smartPhone1.price = 200; //USD

printf("\n Phone1");
printf("\n Model: %s, Price: %.2f USD, Camera: %d",smartPhone1.model,smartPhone1.price,smartPhone1.camera);

// declare smartPhone2, another model with different price but same other properties as smartPhone1
Phone smartPhone2 = smartPhone1;
strcpy(smartPhone2.model,"Spark Kx");
smartPhone2.price = 250;
printf("\n\n Phone2");
printf("\n Model: %s, Price: %.2f USD, Camera: %d\n",smartPhone2.model,smartPhone2.price,smartPhone2.camera);

return 0;}

COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
Project 7 Learning about Structs in C Background With the exception of strings, which you now...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT