Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Structs Application | Introduction to Structs
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Mastering C Structs

bookStructs Application

When designing programs, a very important step is the choice of the representation of the data that you will manage.

For example, you want to write a simple program to automate the calculation of the number of neutrons in an atom.

Where N is the number of neutrons, A is the atomic mass of the element, Z is the number of protons. A function to calculate the number of neutrons in C would look like this:

int NeutronCount(float A, int Z)
{
	int N = A - Z;
	return N;
}

To use this function, you need to declare a huge number of variables with the characteristics of each chemical element without getting confused.

You seem to have automated calculations, but have not gained any advantage in convenience or speed. It is very inconvenient to enter the parameters of a chemical element every time, then write down the result somewhere, etc.

char firstElementName[3];
int firstElementAtomicNum;
int firstElementValency;
double firstElementMass;

char secondElementName[3];
int secondElementAtomicNum;
int secondElementValency;
double secondElementMass;

char thirdElementName[3];
int thirdElementAtomicNum;
int thirdElementValency;
double thirdElementMass;

And so on for another 118 chemical elements.

"It would be great if my favorite language, C, had a mechanism to describe and manipulate complex data", you thought.

Let's visually describe the future structure that will describe any chemical element, for example silicon "Si":

It's quite convenient. Instead of creating a bunch of separate variables, you can define a structure to hold all the fields. In the C language, everything you need for this is already available.

question mark

Why is it inconvenient to calculate the number of neutrons using separate variables for each element?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookStructs Application

Swipe to show menu

When designing programs, a very important step is the choice of the representation of the data that you will manage.

For example, you want to write a simple program to automate the calculation of the number of neutrons in an atom.

Where N is the number of neutrons, A is the atomic mass of the element, Z is the number of protons. A function to calculate the number of neutrons in C would look like this:

int NeutronCount(float A, int Z)
{
	int N = A - Z;
	return N;
}

To use this function, you need to declare a huge number of variables with the characteristics of each chemical element without getting confused.

You seem to have automated calculations, but have not gained any advantage in convenience or speed. It is very inconvenient to enter the parameters of a chemical element every time, then write down the result somewhere, etc.

char firstElementName[3];
int firstElementAtomicNum;
int firstElementValency;
double firstElementMass;

char secondElementName[3];
int secondElementAtomicNum;
int secondElementValency;
double secondElementMass;

char thirdElementName[3];
int thirdElementAtomicNum;
int thirdElementValency;
double thirdElementMass;

And so on for another 118 chemical elements.

"It would be great if my favorite language, C, had a mechanism to describe and manipulate complex data", you thought.

Let's visually describe the future structure that will describe any chemical element, for example silicon "Si":

It's quite convenient. Instead of creating a bunch of separate variables, you can define a structure to hold all the fields. In the C language, everything you need for this is already available.

question mark

Why is it inconvenient to calculate the number of neutrons using separate variables for each element?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2
some-alt