Upcoming SlideShare
×

# Labsheet 7 FP 201

1,008 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
1,008
On SlideShare
0
From Embeds
0
Number of Embeds
50
Actions
Shares
0
22
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Labsheet 7 FP 201

1. 1. FP 201 – PROGRAMMING FUNDAMENTALS WITH C++LAB 7 : STRUCTURESLearning Outcomes:By the end of this lab, students should be able to :• Define structures.• Identify the differences between structures and arrays• Declare structures variable.• Assign values to structures variable.• Write programs in using structures.Theory/ Topics• What is a Structure?Structure is a collection of variables under a single name. Variables can be of any type: int, float,char etc. The main difference between structure and array is that arrays are collections of thesame data type and structure is a collection of variables under a single name.• Declaring a Structure:The structure is declared by using the keyword struct followed by structure name, also called atag. Then the structure members (variables) are defined with their type and variable names insidethe open and close braces { and }. Finally, the closed braces end with a semicolon denoted as ;following the statement. The above structure declaration is also called a Structure Specifier.• For examples:Three variables: custnum of type int, salary of type int, commission of type float are structuremembers and the structure name is Customer. This structure is declared as follows:
2. 2. FP 201 – PROGRAMMING FUNDAMENTALS WITH C++In the above example, it is seen that variables of different types such as int and float are groupedin a single structure name Customer.Arrays behave in the same way, declaring structures does not mean that memory is allocated.Structure declaration gives a skeleton or template for the structure.After declaring the structure, the next step is to define a structure variable.• How to declare Structure Variable?This is similar to variable declaration. For variable declaration, data type is defined followed byvariable name. For structure variable declaration, the data type is the name of the structurefollowed by the structure variable name.In the above example, structure variable cust1 is defined as:Activity 7AProcedures:Step 1: Type the programs given below.Step 2: Save the program as _________________Step 3: Compile and run the program. Write the output#include <iostream>#include <string>using namespace std;struct product{ char name[20]; int weight; double price;
3. 3. FP 201 – PROGRAMMING FUNDAMENTALS WITH C++} fruit;void main (){ //get data cout<<"Enter fruits name: "; cin.getline(fruit.name,20); cout<<"Enter weight: "; cin>>fruit.weight; cout<<"Enter price: "; cin>>fruit.price; //display cout<<"nFruits name:"<<fruit.name; cout<<"nFruits weight:"<<fruit.weight<<"gram"; cout<<"nFruits price: RM"<<fruit.price<<"n";}Activity 7BProcedures:Step 1: Type the programs given below.Step 2: Save the program as _________________Step 3: Compile and run the program. Write the output#include <iostream>#include <string>using namespace std;struct product{ char name[20]; int weight; double price;} fruit[5];void main (){ //get data for(int x=1;x<=5;x++) { cout<<"Enter fruits name: "; cin.getline(fruit[x].name,20); cout<<"Enter weight: ";
4. 4. FP 201 – PROGRAMMING FUNDAMENTALS WITH C++ cin>>fruit[x].weight; cout<<"Enter price: "; cin>>fruit[x].price; } //display for(int x=1;x<=5;x++) { cout<<"nFruits name:"<<fruit[x].name; cout<<"nFruits weight:"<<fruit[x].weight<<"gram"; cout<<"nFruits price: RM"<<fruit[x].price<<"n"; }}Activity 7CProcedures:Step 1: Type the programs given below.Step 2: Save the program as _________________Step 3: Compile and run the program. Write the output#include <iostream>#include <string>#include <sstream>using namespace std;struct product{ char name[20]; int weight; double price;} fruit[5];void main (){ char w[10],p[10]; //get data for(int x=1;x<=5;x++) { cout<<"Enter fruits name: "; cin.getline(fruit[x].name,20); cout<<"Enter weight: ";
5. 5. FP 201 – PROGRAMMING FUNDAMENTALS WITH C++ cin.getline(w,10); stringstream(w) >> fruit[x].weight; cout<<"Enter price: "; cin.getline(p,10); stringstream(p) >> fruit[x].price; } //display for(int x=1;x<=5;x++) { cout<<"nFruits name:"<<fruit[x].name; cout<<"nFruits weight:"<<fruit[x].weight<<"gram"; cout<<"nFruits price: RM"<<fruit[x].price<<"n"; }}LAB EXERCISE.1. Create a struct name staff with structure member name, age and position.2. Refer to the Activity 7A, change the code from input by user to input by the programmer. Set the name = Apple ; weight=200 ;price=5.3. Write a program to accept and display 10 data of football players. Each player should have NAME, AGE, SALARY, and JERSEY_NO. Then get an average of salary for a player. [hint: use one dimensional array]CONCLUSION:________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________