Question
6.01 Intro to Structures Associating multiple values together Now that you can store a single value in an array, its possible for you to write
6.01 Intro to Structures
Associating multiple values together Now that you can store a single value in an array, its possible for you to write programs that deal with a lot of data. As you work with more data, there will be times that you have several pieces of data that are all associated together. For example, you might want to store screen coordinates (x and y values) for players in a video game along with their names. Right now, you could do that with three separate arrays:
int x_coordinates[ 10 ]; int y_coordinates[ 10 ]; string names[ 10 ];
But youd have to remember that each array is matched up with another, so if you moved the position of an element in one of the arrays, you would have to move the associated element in the other two arrays.
This would also get pretty unwieldy when you need to keep track of a fourth value. Youd have add another array and keep it in sync with the original three. Fortunately, the people who design programming languages are not masochists, so there is a better way of associating values together: structures. Structures allow you to store different values in variables under the same variable name. Structures are useful whenever several pieces of data need to be grouped together.
Syntax The format for defining a structure is
struct SpaceShip { int x_coordinate; int y_coordinate; string name; }; // <- Notice that pesky semicolon; you must include it
Here, SpaceShip is the name of the particular type of structure that we are defining. In other words, you have created your own type, just like double or int, which you can use to declare a variable, like so:
SpaceShip my_ship;
The names x_coordinate, y_coordinate and name are the fields of our new type. Wait, fields, what does that mean exactly? Heres the story: weve just created a compound typea variable that stores multiple values that are all associated with each other (like two screen coordinates, or a first and last name). The way you tell the variable which one of those values you want is by naming the field you want to access. Its like having two separate variables with different names, except that the two variables are grouped together and you have a consistent way of naming them. You can think of a structure as a form (think drivers license application) with fieldsthe form stores a lot of data, and each field of the form is a particular piece of that related data. Declaring a structure is the way to define the form, and declaring a variable of that structures type creates a copy of that form that you can fill out and use to store a bunch of data.
To access fields, you put in the name of the variable (not the name of the structureeach variable has its own separate values for its fields) followed by a dot ., followed by the name of the field:
// declare the variable SpaceShip my_ship; // use it my_ship.x_coordinate = 40; my_ship.y_coordinate = 40; my_ship.name = "USS Enterprise (NCC-1701-D)";
As you can see, you can have many fields in a structure, practically as many as you want, and they do not all have to be the same type.
Now let's look at a full program where we write a structure of Scores which contain the players first name; last name; and score. The structure is set to a size of 2 and the user is prompt to enter the values. The program will then print the players first name and there score.
// StructExample.cpp : This file contains the 'main' function. Program execution begins and ends there. //
#include
struct Scores { string firstName; string lastName; int score; };
int main() { Scores score[2];
for (int i = 0; i < 2; i++) { cout << "Please enter the player's first name:"; cin >> score[i].firstName; cout << "Please enter the player's last name:"; cin >> score[i].lastName; cout << "Please enter the player's score:"; cin >> score[i].score; }
//Print the first names and scores of each player for (int i = 0; i < 2; i++) { cout << score[i].firstName << " scored a " << score[i].score << endl; } }
####################################################################
Write a C++ program named StudentStructure. This program will create a Student Structure with a string firstName; string lastName; double gpa. The size of the array will be 3 (holds three student objects). Loop three times and prompt the user to enter the first name, last name and gpa for three students.
Write another loop which will print the student's first and last name on one line and gpa (including header) on the next line. Add a line ____ between each student as shown in the example below.
Enter info for person 1 _______________________________ Please enter the student's first name: Bob Please enter the student's last name: Hope Please enter the student's gpa: 3.56
Enter info for person 2 _______________________________ Please enter the student's first name: Sam Please enter the student's last name: Smith Please enter the student's gpa: 2.00
Enter info for person 3 _______________________________ Please enter the student's first name: Mary Please enter the student's last name: Worth Please enter the student's gpa: 5.52
_______________________________ Bob Hope GPA: 3.56
_______________________________ Sam Smith GPA: 2
_______________________________ Mary Worth GPA: 5.52
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started