SlideShare a Scribd company logo
1 of 16
Structures
Structured Programming 2012/2013
Problems
Calculate Distance
Write a program that represents the distance in
the form of a structure containing meters, and
centimeters
Define 3 variables and input their values, as
follows:
The first value from the user.
The second value from initialization.
The third value is the result of addition of the
other two values.
#include<iostream>
using namespace std;
// Define distance structure
struct dist
{
int meters;
int centimeters;
};
int main()
{
dist d1, d2, d3;
// Enter the values of the first variable members
cout << "Enter the values of the first variable: meters
then centimeters" <<endl;
cin >> d1.meters >> d1.centimeters;
// Initialize the second variable members
d2.meters = 15;
d2.centimeters = 40;
// Add the first and second variable members adn put
the result in the third variable
d3.meters = d1.meters + d2.meters;
d3.centimeters = d1.centimeters + d2.centimeters;
// Put the third variable in the correct meters and
centimeters format
while (d3.centimeters >= 100)
{
d3.meters++;
d3.centimeters -= 100;
}
//Display the third variable members
cout<< d1.meters << "." << d1.centimeters << " + "
<< d2.meters << "." << d2.centimeters << " = "
<< d3.meters << "." << d3.centimeters << endl;
}
Room Area
Write a program that creates a data structure
that stores the 2 dimensions of a room (its
length and width), putting into account that the
length and width are both members of the
distance struct represented in program1, and
then outputs the area of the room in square
meters.
#include <iostream>
using namespace std;
void main()
{
//Distance struct that we have seen before!
struct distance
{
int meters;
int centimeters;
};
//Define Room Struct
struct room
{
//member length is another struct !!
distance length;
distance width;
distance height;
};
//Declare a main variable
room living;
//initialize variable members
living.length.meters = 10;
living.length.centimeters = 17;
living.width.meters = 12;
living.width.centimeters = 10;
//Calculate approximate length and width
int approxLength = living.length.meters +
living.length.centimeters / 100.0;
int approxWidth = living.width.meters +
living.width.centimeters / 100.0;
//calculate area and display it..
cout<<"Area : "<< approxLength * approxWidth<<" square
meters. "<<endl;
}
CodeTip
Braces
Array of Student
Write a program that represents the students’
data by name (char array), id (int) and 6 grades.
The user should input the data of 3 students
(array of struct), and then the program should
output the sum of grades of each student beside
his name.
#include <iostream>
#include<stdio.h>
using namespace std;
void main()
{
//Declare student struct
struct student
{
int id;
char name[30];
int grades[6];
};
//define array of 3 students
student s[3];
for(int i = 0; i < 3; i++)
{
//read id
cout<<"Enter the id of student"<<i + 1<<": ";
cin>>s[i].id;
cin.ignore();
//read name
cout<<"Enter the name of student"<<i + 1<<endl;
gets(s[i].name);
//read 6 grades
cout<<"Enter the grades of student "<<i + 1<<": ";
for(int j = 0 ; j < 6 ; j++)
{
cin>>s[i].grades[j];
}
}
int sum;
for(int i = 0 ; i < 3 ; i++)
{
sum = 0;
cout<<"Sum of grades of student"<<s[i].name<<": ";
//calculate total grade for each student
for(int j = 0 ; j < 6 ; j++)
{
sum += s[i].grades[j];
}
cout<<sum<<endl;
}
}
Thanks!

More Related Content

Viewers also liked

Computer Math Day 03 Addition
Computer Math Day 03 AdditionComputer Math Day 03 Addition
Computer Math Day 03 Addition
A Jorge Garcia
 
Database Q&A
Database  Q&ADatabase  Q&A
Database Q&A
eduafo
 
FINAL PAPER FP304 DATABASE SYSTEM
FINAL PAPER FP304 DATABASE SYSTEMFINAL PAPER FP304 DATABASE SYSTEM
FINAL PAPER FP304 DATABASE SYSTEM
Amira Dolce Farhana
 
Database Management System And Design Questions
Database Management System And Design QuestionsDatabase Management System And Design Questions
Database Management System And Design Questions
Samir Sabry
 
2009 Punjab Technical University B.C.A Database Management System Question paper
2009 Punjab Technical University B.C.A Database Management System Question paper2009 Punjab Technical University B.C.A Database Management System Question paper
2009 Punjab Technical University B.C.A Database Management System Question paper
Monica Sabharwal
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming
hccit
 
Previous question papers of Database Management System (DBMS) By SHABEEB
Previous question papers of Database Management System (DBMS) By SHABEEBPrevious question papers of Database Management System (DBMS) By SHABEEB
Previous question papers of Database Management System (DBMS) By SHABEEB
Shabeeb Shabi
 
Computers as information and communication technology
Computers as information and communication technologyComputers as information and communication technology
Computers as information and communication technology
Junarie Ramirez
 
LA5_Generation of Programming Languages
LA5_Generation of Programming LanguagesLA5_Generation of Programming Languages
LA5_Generation of Programming Languages
Cma Mohd
 

Viewers also liked (20)

Computer Based Math
Computer Based MathComputer Based Math
Computer Based Math
 
Computer Math Day 03 Addition
Computer Math Day 03 AdditionComputer Math Day 03 Addition
Computer Math Day 03 Addition
 
Computer math
Computer mathComputer math
Computer math
 
Database Q&A
Database  Q&ADatabase  Q&A
Database Q&A
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
 
Fskik 1 nota
Fskik 1   notaFskik 1   nota
Fskik 1 nota
 
FINAL PAPER FP304 DATABASE SYSTEM
FINAL PAPER FP304 DATABASE SYSTEMFINAL PAPER FP304 DATABASE SYSTEM
FINAL PAPER FP304 DATABASE SYSTEM
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
Database Management System And Design Questions
Database Management System And Design QuestionsDatabase Management System And Design Questions
Database Management System And Design Questions
 
2009 Punjab Technical University B.C.A Database Management System Question paper
2009 Punjab Technical University B.C.A Database Management System Question paper2009 Punjab Technical University B.C.A Database Management System Question paper
2009 Punjab Technical University B.C.A Database Management System Question paper
 
Dbms Final Examination Answer Key
Dbms Final Examination Answer KeyDbms Final Examination Answer Key
Dbms Final Examination Answer Key
 
Mcs 16 solved assignment 2015-16
Mcs 16 solved assignment 2015-16Mcs 16 solved assignment 2015-16
Mcs 16 solved assignment 2015-16
 
Ada 95 - Structured programming
Ada 95 - Structured programmingAda 95 - Structured programming
Ada 95 - Structured programming
 
Fundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and AnswersFundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and Answers
 
Objective structured practical question
Objective structured practical questionObjective structured practical question
Objective structured practical question
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming
 
Previous question papers of Database Management System (DBMS) By SHABEEB
Previous question papers of Database Management System (DBMS) By SHABEEBPrevious question papers of Database Management System (DBMS) By SHABEEB
Previous question papers of Database Management System (DBMS) By SHABEEB
 
Computers as information and communication technology
Computers as information and communication technologyComputers as information and communication technology
Computers as information and communication technology
 
LA5_Generation of Programming Languages
LA5_Generation of Programming LanguagesLA5_Generation of Programming Languages
LA5_Generation of Programming Languages
 
Math
MathMath
Math
 

Similar to [SpLab3]Structures

Classes(or Libraries)#include #include #include #include.docx
Classes(or Libraries)#include #include #include #include.docxClasses(or Libraries)#include #include #include #include.docx
Classes(or Libraries)#include #include #include #include.docx
brownliecarmella
 
Need to revise working code below,A good design means the applicat.pdf
Need to revise working code below,A good design means the applicat.pdfNeed to revise working code below,A good design means the applicat.pdf
Need to revise working code below,A good design means the applicat.pdf
archgeetsenterprises
 
Write a class Runner that contains the following data members.docx
Write a class Runner that contains the following data members.docxWrite a class Runner that contains the following data members.docx
Write a class Runner that contains the following data members.docx
zahid javed
 
3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf
atozshoppe
 

Similar to [SpLab3]Structures (20)

12Structures.pptx
12Structures.pptx12Structures.pptx
12Structures.pptx
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
 
Classes(or Libraries)#include #include #include #include.docx
Classes(or Libraries)#include #include #include #include.docxClasses(or Libraries)#include #include #include #include.docx
Classes(or Libraries)#include #include #include #include.docx
 
PROGRAMMING QUESTIONS.docx
PROGRAMMING QUESTIONS.docxPROGRAMMING QUESTIONS.docx
PROGRAMMING QUESTIONS.docx
 
Presentation for structure in c
Presentation for  structure in cPresentation for  structure in c
Presentation for structure in c
 
Ch3
Ch3Ch3
Ch3
 
Need to revise working code below,A good design means the applicat.pdf
Need to revise working code below,A good design means the applicat.pdfNeed to revise working code below,A good design means the applicat.pdf
Need to revise working code below,A good design means the applicat.pdf
 
OOPS 22-23 (1).pptx
OOPS 22-23 (1).pptxOOPS 22-23 (1).pptx
OOPS 22-23 (1).pptx
 
Unit3 overview of_c_programming
Unit3 overview of_c_programmingUnit3 overview of_c_programming
Unit3 overview of_c_programming
 
Oop assignment 02
Oop assignment 02Oop assignment 02
Oop assignment 02
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
Pointer
PointerPointer
Pointer
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
Write a class Runner that contains the following data members.docx
Write a class Runner that contains the following data members.docxWrite a class Runner that contains the following data members.docx
Write a class Runner that contains the following data members.docx
 
C++
C++C++
C++
 
3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf3- In the program figurespointers we have a base class location and va.pdf
3- In the program figurespointers we have a base class location and va.pdf
 
Bc0037
Bc0037Bc0037
Bc0037
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 

More from Nora Youssef

More from Nora Youssef (17)

[ASM] Lab2
[ASM] Lab2[ASM] Lab2
[ASM] Lab2
 
[ASM] Lab1
[ASM] Lab1[ASM] Lab1
[ASM] Lab1
 
Welcome to dragons’ land
Welcome to dragons’ landWelcome to dragons’ land
Welcome to dragons’ land
 
[SpLab1]Review
[SpLab1]Review[SpLab1]Review
[SpLab1]Review
 
[SpLab2]Arrays
[SpLab2]Arrays[SpLab2]Arrays
[SpLab2]Arrays
 
[SpLab5] Pointers II
[SpLab5] Pointers II[SpLab5] Pointers II
[SpLab5] Pointers II
 
[SpLab4]Pointers I
[SpLab4]Pointers I[SpLab4]Pointers I
[SpLab4]Pointers I
 
[SpLab7]Functions I
[SpLab7]Functions I[SpLab7]Functions I
[SpLab7]Functions I
 
[Splab8]Functions II
[Splab8]Functions II[Splab8]Functions II
[Splab8]Functions II
 
[SpLab9]Functions III
[SpLab9]Functions III[SpLab9]Functions III
[SpLab9]Functions III
 
[ASM]Lab8
[ASM]Lab8[ASM]Lab8
[ASM]Lab8
 
[ASM]Lab7
[ASM]Lab7[ASM]Lab7
[ASM]Lab7
 
[ASM]Lab6
[ASM]Lab6[ASM]Lab6
[ASM]Lab6
 
[ASM]Lab5
[ASM]Lab5[ASM]Lab5
[ASM]Lab5
 
[ASM]Lab4
[ASM]Lab4[ASM]Lab4
[ASM]Lab4
 
[ASM]Lab3
[ASM]Lab3[ASM]Lab3
[ASM]Lab3
 
Abstract
AbstractAbstract
Abstract
 

Recently uploaded

Recently uploaded (20)

dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 

[SpLab3]Structures

  • 2.
  • 4. Calculate Distance Write a program that represents the distance in the form of a structure containing meters, and centimeters Define 3 variables and input their values, as follows: The first value from the user. The second value from initialization. The third value is the result of addition of the other two values.
  • 5. #include<iostream> using namespace std; // Define distance structure struct dist { int meters; int centimeters; };
  • 6. int main() { dist d1, d2, d3; // Enter the values of the first variable members cout << "Enter the values of the first variable: meters then centimeters" <<endl; cin >> d1.meters >> d1.centimeters; // Initialize the second variable members d2.meters = 15; d2.centimeters = 40; // Add the first and second variable members adn put the result in the third variable d3.meters = d1.meters + d2.meters; d3.centimeters = d1.centimeters + d2.centimeters;
  • 7. // Put the third variable in the correct meters and centimeters format while (d3.centimeters >= 100) { d3.meters++; d3.centimeters -= 100; } //Display the third variable members cout<< d1.meters << "." << d1.centimeters << " + " << d2.meters << "." << d2.centimeters << " = " << d3.meters << "." << d3.centimeters << endl; }
  • 8. Room Area Write a program that creates a data structure that stores the 2 dimensions of a room (its length and width), putting into account that the length and width are both members of the distance struct represented in program1, and then outputs the area of the room in square meters.
  • 9. #include <iostream> using namespace std; void main() { //Distance struct that we have seen before! struct distance { int meters; int centimeters; }; //Define Room Struct struct room { //member length is another struct !! distance length; distance width; distance height; };
  • 10. //Declare a main variable room living; //initialize variable members living.length.meters = 10; living.length.centimeters = 17; living.width.meters = 12; living.width.centimeters = 10; //Calculate approximate length and width int approxLength = living.length.meters + living.length.centimeters / 100.0; int approxWidth = living.width.meters + living.width.centimeters / 100.0; //calculate area and display it.. cout<<"Area : "<< approxLength * approxWidth<<" square meters. "<<endl; } CodeTip Braces
  • 11. Array of Student Write a program that represents the students’ data by name (char array), id (int) and 6 grades. The user should input the data of 3 students (array of struct), and then the program should output the sum of grades of each student beside his name.
  • 12. #include <iostream> #include<stdio.h> using namespace std; void main() { //Declare student struct struct student { int id; char name[30]; int grades[6]; }; //define array of 3 students student s[3];
  • 13. for(int i = 0; i < 3; i++) { //read id cout<<"Enter the id of student"<<i + 1<<": "; cin>>s[i].id; cin.ignore(); //read name cout<<"Enter the name of student"<<i + 1<<endl; gets(s[i].name); //read 6 grades cout<<"Enter the grades of student "<<i + 1<<": "; for(int j = 0 ; j < 6 ; j++) { cin>>s[i].grades[j]; } }
  • 14. int sum; for(int i = 0 ; i < 3 ; i++) { sum = 0; cout<<"Sum of grades of student"<<s[i].name<<": "; //calculate total grade for each student for(int j = 0 ; j < 6 ; j++) { sum += s[i].grades[j]; } cout<<sum<<endl; } }
  • 15.