1. Following the bit-level floating-point coding rules, implement the function w
ID: 441702 • Letter: 1
Question
1. Following the bit-level floating-point coding rules, implement the function with the following prototype: // Compute -f. If f is NaN, then return f float_bits float_negate(float_bits f); For floating-point number f, this function computes -f. If f is NaN, your function should simply return fExplanation / Answer
FOLLOW THIS /* define template */ struct Person { char ssn[12] ,last_name[20] ,first_name[16] ,street[20] ,city[20] ,state[3] ,zip_code[11] ; int age; float height ,weight ; double salary; }; In the C language, a variable of the above data type would be declared as follows: Fig. 9-3 main() { /* declare variable of structure person type */ struct Person frank; ... } In the C++ language, the use of the keyword struct would not be required in the declaration of a variable of the above type. Fig. 9-4 main() { // declare variable of structure person type Person frank; ... } Each member of a structure is in a contiguous memory location in relationship to its previous member and next member. To access individual members of a structure, the dot operator is used to connect a variable name with an element in the structure. For example frank.last_name The composed name is treated as a variable of with the type of the member variable. Fig. 9-5 #include int main() { struct Person ralph; printf( " Enter your first name:" ); gets( ralph.first_name ); . . . printf( " Enter your age:" ); scanf( "%d", &ralph.age ); return 0; } or in C++ Fig. 9-6 #include int main() { Person ralph; cout > ralph.first_name; . . . cout > ralph.age; return 0; } 9.3 Initializing Structures Structures can be initialized when the template is created, if a variable of the structure type is declared with the template creation. Fig. 9-7 Person employ = {"Ralph", "Jones", "123 Main", "Nowhere", "NV", "12345-9878" 30, 72, 165 }; 9.4 Assignment of Structures Values are assigned to individual members of the structure by using the structure variable name connected with the structure member name. employ.age = 35; The . operator is used to provide the connection between variable name and member name. When referencing a particular member of a structure, the resulting type of that expression is that of the member being referenced. strcpy(employ.street,"3188 Trinity Drive"); The value of one structure variable can be assigned to another structure variable of the same type. Person employ, employ2; employ = employ2; The above assignment is a valid statement. The data stored in employ2 would be stored in employ, because objects of like type can be assigned to one another. Structure variables cannot be compared through a relational operator. The following example attempts to compare two structure variables of the same type. Fig. 9-8 int main() { Person employee, staff; employee = staff; if( employee == staff ) ... The above code is invalid, each member of the structure must be compared in order to establish equality or non-equality. 9.5 Nested Structures A structure variable can be a member of another structure template. Fig. 9-9 struct Vital { int age ,height ,weight ; }; struct Home { char f_name[12] ,l_name[20] ,street[20] ,city[20] ,state[3] ,zip_code[11] ; }; struct Person { Vital emp; Home place; } employ; In order to access a member of one of the nested structures, the dot operator is used until the lowest member is reached in the structure hierarchy. Fig. 9-10 cout > employ.emp.age; cout > employ.place.lname; 9.6 Arrays of Structures Like any other data type in C, a variable of a structure type can be arrayed. Fig. 9-11 struct Person { Vital emp; Home place; } stats[100]; The above declaration would allow for the storage of 100 items of type people. Any specific item could be referenced as: stats[indx].place.f_nameRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.