SlideShare une entreprise Scribd logo
1  sur  5
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:
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;
FP 201 – PROGRAMMING FUNDAMENTALS WITH C++



} fruit;
void main ()
{
         //get data
         cout<<"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 data
       for(int x=1;x<=5;x++)
       {
               cout<<"Enter fruit's name: ";
               cin.getline(fruit[x].name,20);
               cout<<"Enter weight: ";
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<<"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 data
       for(int x=1;x<=5;x++)
       {
               cout<<"Enter fruit's name: ";
               cin.getline(fruit[x].name,20);
               cout<<"Enter weight: ";
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<<"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:
_____________________________________________________________________________
_______________________________________________________________
_____________________________________________________________________________
_______________________________________________________________

Contenu connexe

Tendances

FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
rohassanie
 
C++ Overview
C++ OverviewC++ Overview
C++ Overview
kelleyc3
 

Tendances (10)

Ch5 array nota
Ch5 array notaCh5 array nota
Ch5 array nota
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
Object oriented programming in go
Object oriented programming in goObject oriented programming in go
Object oriented programming in go
 
2017-08-22 Python×Djangoで作るHR Techサービスのメリット・デメリット
2017-08-22 Python×Djangoで作るHR Techサービスのメリット・デメリット2017-08-22 Python×Djangoで作るHR Techサービスのメリット・デメリット
2017-08-22 Python×Djangoで作るHR Techサービスのメリット・デメリット
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)
 
logistic regression with python and R
logistic regression with python and Rlogistic regression with python and R
logistic regression with python and R
 
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILECOMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
 
C++ Overview
C++ OverviewC++ Overview
C++ Overview
 
Embedding Java code in a Jolie Service
Embedding Java code in a Jolie ServiceEmbedding Java code in a Jolie Service
Embedding Java code in a Jolie Service
 

En vedette (7)

IGCSE & O Level Computer Workbook for P2 by Inqilab Patel
IGCSE & O Level Computer Workbook for P2 by Inqilab PatelIGCSE & O Level Computer Workbook for P2 by Inqilab Patel
IGCSE & O Level Computer Workbook for P2 by Inqilab Patel
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
 
Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2
 
Lab ex 1
Lab ex 1Lab ex 1
Lab ex 1
 
Single level directory structure.55
Single level directory structure.55Single level directory structure.55
Single level directory structure.55
 
File management
File managementFile management
File management
 

Similaire à Labsheet 7 FP 201

Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
rohassanie
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
rohassanie
 
Use of Classes and functions#include iostreamusing name.docx
 Use of Classes and functions#include iostreamusing name.docx Use of Classes and functions#include iostreamusing name.docx
Use of Classes and functions#include iostreamusing name.docx
aryan532920
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
Prabhu D
 

Similaire à Labsheet 7 FP 201 (20)

Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
power point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions conceptspower point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions concepts
 
computerscience-170129081612.pdf
computerscience-170129081612.pdfcomputerscience-170129081612.pdf
computerscience-170129081612.pdf
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
 
Use of Classes and functions#include iostreamusing name.docx
 Use of Classes and functions#include iostreamusing name.docx Use of Classes and functions#include iostreamusing name.docx
Use of Classes and functions#include iostreamusing name.docx
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
Computer science class 12 project on Super Market Billing
Computer science class 12 project on Super Market BillingComputer science class 12 project on Super Market Billing
Computer science class 12 project on Super Market Billing
 
Labsheet2
Labsheet2Labsheet2
Labsheet2
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
12Structures.pptx
12Structures.pptx12Structures.pptx
12Structures.pptx
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
C++ functions
C++ functionsC++ functions
C++ functions
 
C++ functions
C++ functionsC++ functions
C++ functions
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Labsheet1stud
Labsheet1studLabsheet1stud
Labsheet1stud
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 

Plus de rohassanie

FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
rohassanie
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
rohassanie
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
rohassanie
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
rohassanie
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
rohassanie
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2)
rohassanie
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
rohassanie
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3
rohassanie
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2
rohassanie
 
Chapter 1 part 1
Chapter 1 part 1Chapter 1 part 1
Chapter 1 part 1
rohassanie
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
rohassanie
 

Plus de rohassanie (17)

FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2)
 
Labsheet 5
Labsheet 5Labsheet 5
Labsheet 5
 
Labsheet 4
Labsheet 4Labsheet 4
Labsheet 4
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2
 
Chapter 1 part 1
Chapter 1 part 1Chapter 1 part 1
Chapter 1 part 1
 
Unit 3
Unit 3Unit 3
Unit 3
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 
Fp201 unit1 1
Fp201 unit1 1Fp201 unit1 1
Fp201 unit1 1
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

Labsheet 7 FP 201

  • 1. 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:
  • 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 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;
  • 3. FP 201 – PROGRAMMING FUNDAMENTALS WITH C++ } fruit; void main () { //get data cout<<"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 data for(int x=1;x<=5;x++) { cout<<"Enter fruit's name: "; cin.getline(fruit[x].name,20); cout<<"Enter weight: ";
  • 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<<"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 data for(int x=1;x<=5;x++) { cout<<"Enter fruit's name: "; cin.getline(fruit[x].name,20); cout<<"Enter weight: ";
  • 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<<"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: _____________________________________________________________________________ _______________________________________________________________ _____________________________________________________________________________ _______________________________________________________________