Concept explainers
(a)
Create a Structure inventory which contains the following:
- A character array partName[30]
- An integer partNumber,
- A floating point price
- An integer stock
- An integer reorder.
(a)
Explanation of Solution
Explanation:
A structure is a user defined data type which contains related variables which have same name.
Following syntax is used to create a structure with a keyword struct:
struct <structure name> { variable 1; variable 2; ---; };
Structure named Inventory with the given variables is defined as follows:
struct Inventory { char partName[30]; int partNumber; float pointprice; int stock; int reorder; };
(b)
Define the union data containing char c, short s, long b, float f and double d.
(b)
Explanation of Solution
Explanation:
Union is a user defined data type which contains related variables just like structure which uses same data space for its variable.
Following syntax is used to create an Union data type:
union <union name> { variable 1; variable 2; ---; };
Union named data which contains char c, short s, long b, float f and double d is defined as follows:
union data { char c; short s; long b; float f; double d; };
(c)
Create a structure called address that contains character arrays
- streetAddress[ 25]
- city[ 20 ]
- state[ 3]
- zipCode[ 6].
(c)
Explanation of Solution
Explanation:
A structure is a derived defined data type which contains related variables which have same name.
Following syntax is used to create a structure with a keyword struct:
struct <structure name> { variable 1; variable 2; ---; };
Structure with the name address containing character arrays is defined as follows:
struct address { char streetAddress[25]; char city[20]; char state[3]; char zipCode[6]; };
(d)
Create a structure student that contains the following :
- An character array firstName[15]
- An character array lastName[15]
- variable homeAddress of type struct address.
(d)
Explanation of Solution
Given Information
Structure named address as follows:
struct address { char streetAddress[25]; char city[20]; char state[3]; char zipCode[6]; };
Explanation:
A structure is a derived defined data type which contains related variables which have same name but each variable in the structure has its own data space.
Keyword struct is used to create a structure as follows:
struct <structure name> { variable 1; variable 2; ---; };
Structures can be used to declare variable which can access variables of the structure with the use of following syntax:
struct <structure_name><variable_name>;
Structure name Student which contains a structure
A structure student that contains the variable homeAddress of type struct address along with character arrays firstName and lastName is defined as follows:
struct student { char firstName[15]; char lastName[15]; struct address homeAddress; };
(e)
Create a structure test which contains a 16 bit fields with widths of 1 bit. The names of the bit fields are the letters a to p.
(e)
Explanation of Solution
Explanation:
Bit fields can be defined in the structure which enables user to specify the number of bits a unsigned or int member of variable can store.
Bit fields are defined with the following syntax:
<data type> <bit-field name> <number of bits>
A structure test which contains a 16 bit fields with widths of 1 bit is defined as follows:
struct test { unsigned int a : 1; unsigned int b : 1; unsigned int c : 1; unsigned int d : 1; unsigned int e : 1; unsigned int f : 1; unsigned int g : 1; unsigned int h : 1; unsigned int i : 1; unsigned int j : 1; unsigned int k : 1; unsigned int l : 1; unsigned int m : 1; unsigned int n : 1; unsigned int o : 1; unsigned int p : 1; };
Want to see more full solutions like this?
Chapter 10 Solutions
C How to Program (8th Edition)
- Mark the following statements as true or false. A double type is an example of a simple data type. (1) A one-dimensional array is an example of a structured data type. (1) The size of an array is determined at compile time. (1,6) Given the declaration: int list[10]; the statement: list[5] - list[3] * list[2]; updates the content of the fifth component of the array list. (2) If an array index goes out of bounds, the program always terminates in an error. (3) The only aggregate operations allowable on int arrays are the increment and decrement operations. (5) Arrays can be passed as parameters to a function either by value or by reference. (6) A function can return a value of type array. (6) In C++, some aggregate operations are allowed for strings. (11,12,13) The declaration: char name [16] = "John K. Miller"; declares name to be an array of 15 characters because the string "John K. Miller" has only 14 characters. (11) The declaration: char str = "Sunny Day"; declares str to be a string of an unspecified length. (11) As parameters, two-dimensional arrays are passed either by value or by reference. (15,16)arrow_forwardHello,Q:Declare a structure whose tag name is Emp and that contains these fields (in the following order): a double field named d, a character pointer named str, and an array of eleven integers named arr. In addition, declare a array named emp of 30 of these structures. Assign the value 12 to the last element of the arr field of the last element of emp. Assign 3.5 to the d field of the first emp element. Assign the string Hello to the kth element of emp (assume k has been declared as an integer variable and assigned a value in the range of the array elements).==========My program however has errors, I would be grateful if you could correct them. ALSO, would your solution be like mine or do you have another way to do this? Thanks. MY PROGRAM:#include<stdio.h>struct Emp{double d;char *str;int arr[11];};int main(void){emp[30]; emp[29].arr[10] = 12;emp[0].d = 3.5; emp[k].str="Hello";return 0;}arrow_forwardThis lab will exercise your understanding of some of the concepts covered in Chapter 12:dynamic data, arrays, pointer addresses 1. Create a structure called students. The structure should contain:firstname, lastname, gpa. 2. Create a pointer to the student structure and dynamically create an array of thestudent structure, enough for 4 students.Hint: Store the address of the beginning of the array 3. Create a file that contains four rows of data with last name, first name and gpa valueor use the inFile.txt file attached. 4. Using pointer notation only (not array notation []), read the data from the file andstore the data into the structure.After all the data is read and stored, using pointer notation only, output to the console (cout)each student's first name, last name and gpa. Use information messages and ensure the datais displayed properly. 5. Before exiting, delete the dynamically allocated structure array.Complete as much as you can before you leave ensuring what you've completed…arrow_forward
- Using C/C++ . Assume that the following lists of numbers represents the physical view of a two dimensional array in memory. If the array has 3 columns and 3 rows, show the row majorordering and the column major ordering for each list.a) 4, 0, 1, 3, 5, 1, 7, 4, 4b) 7, 7, 7, 3, 3, 3, 1, 1, 1c) 1, 2, 3, 4, 5, 6, 7, 8, 9arrow_forwardThe main function, which calculates the row and column averages separately in the two-dimensional array of M × N size given to him, and calculates the row and column averages of which the difference is the smallest, taking into account these averages, and finds the row and column indices where the difference between them is the smallest. Write it in C language. Access to array elements and permissions must be made with pointers to be defined separately. Indexing (matrix (i) (j) array (j) etc.) should not be used. All array operations to be used will be processed with an external pointer, not indexing. Solutions that access the elements of arrays by indexing without using pointers will not be accepted. The number of rows and columns of the given two-dimensional array (matrix) will be different.arrow_forward4. (Data processing) a. Declare a single structure data type suitable for an employee structureof the type shown in the following chart:Number Name Rate Hours3462 Jones 14.62 406793 Robbins 15.83 386985 Smith 15.22 457834 Swain 16.89 408867 Timmins 16.43 359002 Williams 14.75 42b. Using the data type declared in Exercise 4a, write a C++ program that interactively acceptsthe chart’s data in an array of six structures. After the data has been entered, the programshould create a payroll report listing each employee’s name, number, and gross pay. Includethe total gross pay of all employees at the end…arrow_forward
- Declare a one-dimensional, two-dimensional and a three-dimensional array in a programming language (C, for example) that has the capability to display the addresses of array elements. Verify the various address calculation formulae that you have learnt in this chapter against the arrays that you have declared in the program.arrow_forwardWrite C++ statements to define and initialize the following arrays. a. Array heights of 10 components of type double. Initialize this array to the following values: 5.2, 6.3, 5.8, 4.9, 5.2, 5.7, 6.7, 7.1, 5.10, 6.0.b. Array weights of 7 components of type int. Initialize this array to the following values: 120, 125, 137, 140, 150, 180, 210.c. Array specialSymbols of type char. Initialize this array to the following values: '$', '#', '%', '@', '&', '!', '^'.d. Array seasons of 4 components of type string. Initialize this array to the following values: "fall", "winter", "spring", "summer".arrow_forwardSuppose you have the following C++ code: int* average; // Defines int pointer variables int* gradesArray; Show the single line of C++ code that creates a new int variable and store its address into average. Show the single line of C++ code that creates an eight-element array and stores the base address of the array into gradesArray. Using the example, show the lines of C++ code that deallocate the dynamic data.arrow_forward
- Solve the following C++ Program. you are supposed to DYNAMICALLY create 2 arrays of length 5 using pointer, Then take values from the user and put those values in ONE of the dynamic arrays and COPY those values from the first array to the second Array.arrow_forwardWrite, compile and run a C++ program that finds and displays the maximum value in a two dimensional array of integers. The array should be declared as a 4-by-5 array of integers and initialized with the data 16, 22, 99, 4, 18, - 258, 4, 101, 5, 98, 105, 6, 15, 2, 45, 33, 88, 72, 12 and 3arrow_forwardWrite a code in C++ and create an Array (1) Insert the following names of courses in the array.(2) Create a dynamic array using new operator.(3) Print all elements of the Array. OOPC++ Programming Operating SystemsNetworksData StructuresAIarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr