SlideShare ist ein Scribd-Unternehmen logo
1 von 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:
_____________________________________________________________________________
_______________________________________________________________
_____________________________________________________________________________
_______________________________________________________________

Weitere ähnliche Inhalte

Was ist angesagt?

FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
Object oriented programming in go
Object oriented programming in goObject oriented programming in go
Object oriented programming in goJaehue Jang
 
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サービスのメリット・デメリットRyo Nagaoka
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3 rohassanie
 
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)RichardWarburton
 
logistic regression with python and R
logistic regression with python and Rlogistic regression with python and R
logistic regression with python and RAkhilesh Joshi
 
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILECOMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILEAnushka Rai
 
C++ Overview
C++ OverviewC++ Overview
C++ Overviewkelleyc3
 
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 ServiceJolieLang
 

Was ist angesagt? (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
 

Andere mochten auch

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 PatelInqilab Patel
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012rohassanie
 
Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012rohassanie
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2rohassanie
 
Single level directory structure.55
Single level directory structure.55Single level directory structure.55
Single level directory structure.55myrajendra
 
File management
File managementFile management
File managementMohd Arif
 

Andere mochten auch (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
 

Ähnlich wie Labsheet 7 FP 201

Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 studrohassanie
 
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 conceptsbhargavi804095
 
computerscience-170129081612.pdf
computerscience-170129081612.pdfcomputerscience-170129081612.pdf
computerscience-170129081612.pdfKiranKumari204016
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 studrohassanie
 
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.docxaryan532920
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALPrabhu D
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPTTheVerse1
 
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 MelayiMuhammed Thanveer M
 
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 BillingHarsh Kumar
 
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 modelingMario Fusco
 
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 modelingCodemotion
 

Ähnlich wie 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
 

Mehr von rohassanie

FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2rohassanie
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5rohassanie
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1rohassanie
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3rohassanie
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2rohassanie
 
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 1rohassanie
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3rohassanie
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2rohassanie
 
Chapter 1 part 1
Chapter 1 part 1Chapter 1 part 1
Chapter 1 part 1rohassanie
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++rohassanie
 

Mehr von 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++
 

Kürzlich hochgeladen

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
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 WorkerThousandEyes
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 

Kürzlich hochgeladen (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

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: _____________________________________________________________________________ _______________________________________________________________ _____________________________________________________________________________ _______________________________________________________________