labsheet 7 fp 201

6
FP 201 – PROGRAMMING FUNDAMENTALS WITH C++ LAB 7 : STRUCTURES Learning 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 the same 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 a tag. Then the structure members (variables) are defined with their type and variable names inside the 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 structure members and the structure name is Customer. This structure is declared as follows:

Upload: rohassanie

Post on 22-May-2015

694 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Labsheet 7 FP 201

FP 201 – PROGRAMMING FUNDAMENTALS WITH C++

LAB 7 : STRUCTURES

Learning 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 the same 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 a tag. Then the structure members (variables) are defined with their type and variable names inside the 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 structure members and the structure name is Customer. This structure is declared as follows:

Page 2: Labsheet 7 FP 201

FP 201 – PROGRAMMING FUNDAMENTALS WITH C++

In the above example, it is seen that variables of different types such as int and float are grouped in 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 by variable name. For structure variable declaration, the data type is the name of the structure followed by the structure variable name.

In the above example, structure variable cust1 is defined as:

Activity 7A

Procedures: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;

Page 3: Labsheet 7 FP 201

FP 201 – PROGRAMMING FUNDAMENTALS WITH C++

void main (){

//get datacout<<"Enter fruit's name: ";cin.getline(fruit.name,20);cout<<"Enter weight: ";cin>>fruit.weight;cout<<"Enter price: ";cin>>fruit.price;

//display

cout<<"\nFruit's name:"<<fruit.name;cout<<"\nFruit's weight:"<<fruit.weight<<"gram";cout<<"\nFruit's price: RM"<<fruit.price<<"\n";

}

Activity 7B

Procedures: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 datafor(int x=1;x<=5;x++){

cout<<"Enter fruit's name: ";cin.getline(fruit[x].name,20);cout<<"Enter weight: ";cin>>fruit[x].weight;

Page 4: Labsheet 7 FP 201

FP 201 – PROGRAMMING FUNDAMENTALS WITH C++

cout<<"Enter price: ";cin>>fruit[x].price;

}

//displayfor(int x=1;x<=5;x++){

cout<<"\nFruit's name:"<<fruit[x].name;cout<<"\nFruit's weight:"<<fruit[x].weight<<"gram";cout<<"\nFruit's price: RM"<<fruit[x].price<<"\n";

}}

Activity 7C

Procedures: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 datafor(int x=1;x<=5;x++){

cout<<"Enter fruit's name: "; cin.getline(fruit[x].name,20);

cout<<"Enter weight: ";cin.getline(w,10);

Page 5: Labsheet 7 FP 201

FP 201 – PROGRAMMING FUNDAMENTALS WITH C++

stringstream(w) >> fruit[x].weight;cout<<"Enter price: ";cin.getline(p,10);stringstream(p) >> fruit[x].price;

}

//displayfor(int x=1;x<=5;x++){

cout<<"\nFruit's name:"<<fruit[x].name;cout<<"\nFruit's weight:"<<fruit[x].weight<<"gram";cout<<"\nFruit's 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:

______________________________________________________________________________

______________________________________________________________

______________________________________________________________________________

______________________________________________________________