DOC PREVIEW
Saddleback CS 1B - Chapter 11 structs (C++ records)

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11-1Chapter 11structs (C++ records)11-2In CS 1A we worked with a database program. We viewed the data in thedatabase as being stored in records. Each record was a collection of fields.A record then was a complete set of information about a person, place orobject and the fields defined the individual components of the record. C++refers to records as structs.We defined an array as a collection of information all of the same type(homogeneous). A struct then is a collection of information of different datatypes (heterogeneous). The fields of a struct are referred to as members.struct StructName{dataType memberName;..};Example:struct StudentRec{string name;string idNum;float gpa;}; theStudentStudentRec theStudent; nameidNumgpa11-3The individual members of the struct must be accessed by the name of thestruct followed by the name of the member.theStudent.name = "Sally";cin >> theStudent.idNum;cout << theStudent.gpa;Example:// This program demonstrates the use of a record (C++ struct)#include <iostream.h>struct PersonRec{string lastName;string firstName;int age;};void main(void){PersonRec thePerson;cout << "Enter first name: ";cin >> thePerson.firstName;cout << "Enter last name: ";cin >> thePerson.lastName;cout << "Enter age: ";cin >> thePerson.age;cout << "\n\nHello " << thePerson.firstName << ' '<< thePerson.lastName << ". How are you?\n";cout << "\nCongratulations on reaching the age of "<< thePerson.age << ".\n";}11-4// This program demonstrates the use of a nested structstruct GradeRec{float percent;char grade;};struct StudentRec{string lastName;string firstName;int age;GradeRec courseGrade;};void main(void){StudentRec student;cout << "Enter first name: ";cin >> student.firstName;cout << "Enter last name: ";cin >> student.lastName;cout << "Enter age: ";cin >> student.age;cout << "Enter overall percent: ";cin >> student.courseGrade.percent;if(student.courseGrade.percent >= 90){student.courseGrade.grade = 'A';}else if(student.courseGrade.percent >= 75){student.courseGrade.grade = 'B';}else{student.courseGrade.grade = 'F';}cout << "\n\nHello " << student.firstName << ' ' << student.lastName<< ". How are you?\n";cout << "\nCongratulations on reaching the age of " << student.age<< ".\n";cout << "Your overall percent score is "<< student.courseGrade.percent << " for a grade of "<< student.courseGrade.grade;}OUTPUT:Enter first name: SallyEnter last name: SmartEnter age: 19Enter overall percent: 98Hello Sally Smart. How are you?Congratulations on reaching the age of 19.Your overall percent score is 98 for a grade of A11-5// This program demonstrates the use of an array of structs#include <iostream.h>struct PersonRec{string lastName;string firstName;int age;};typedef PersonRec PeopleArrayType[10]; //an array of 10 structsvoid main(void){PeopleArrayType people; //a variable of the array typefor (int i = 0; i < 10; i++){cout << "Enter first name: ";cin >> people[i].firstName;cout << "Enter last name: ";cin >> people[i].lastName;cout << "Enter age: ";cin >> people[i].age;}for (int i = 0; i < 10; i++){cout << people[i].firstName << ‘ ‘ << people[i].lastName<< setw(10) << people[i].age;}}11-6#include <iostream.h>struct PersonRec{string lastName;string firstName;int age;};typedef PersonRec PeopleArrayType[10]; //an array of 10 structsvoid LoadArray(PeopleArrayType peop);void main(void){PeopleArrayType people; //a variable of the array typeLoadArray(people);// output the arrayfor (int i = 0; i < 10; i++){cout << people[i].firstName << ‘ ‘ << people[i].lastName<< setw(10) << people[i].age;}}void LoadArray(PeopleArrayType peop){for (int i = 0; i < 10; i++){cout << "Enter first name: ";cin >> peop[i].firstName;cout << "Enter last name: ";cin >> peop[i].lastName;cout << "Enter age: ";cin >> peop[i].age;}}11-7structs and Aggregate Operations- Aggregate I/O is not allowed. I/O must be performed on a member by member basis.- Aggregate assignment is allowed. All data members (fields) are copied.- Aggregate arithmetic is not allowed.- Aggregate comparison is not allowed. Comparisons must be performed on a memberby member basis.- structs may be passed by value or by reference.- A struct is a valid return type for a value returning function.11-8Name(s) ________________________________________ Due Date _____________Palindrome Lab - structWrite a C++ program to manage a user-defined string. The program will create a stringfrom the input buffer, output the string and its length, and check the string to determinewhether or not it is a palindrome. A palindrome is a string that reads the same forwardsand backwards. Examples:radarracecara man a plan a canal panamastruct StringRec{int strLen;char theStr[256];};void AddChar(StringRec& str, char theCh); // adds one character to the stringvoid OutputString(StringRec str); // outputs the string and the length of the stringbool CheckString(StringRec str); // returns true if string is a palindrome, false otherwisevoid main(void){StringRec theString;char theChar;theString.strLen = 0;cout << "Enter a string: ";cin.get(theChar);while(theChar != '\n'){AddChar(theString, theChar);cin.get(theChar);}OutputString(theString);if( CheckString(theString) )cout << "\n\nThe string is a palindrome";elsecout << "\n\nThe string is not a


View Full Document

Saddleback CS 1B - Chapter 11 structs (C++ records)

Download Chapter 11 structs (C++ records)
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 11 structs (C++ records) and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 11 structs (C++ records) 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?