SlideShare uma empresa Scribd logo
1 de 11
C++ Examples &Revisions
Hurts - Wonderful Life
Lecture 5
One Dimension Arrays
• // arrays example
• #include <iostream>
• using namespace std;
• int billy [] = {16, 2, 77, 40, 12071};
• int n, result=0;
• int main ()
• {
• for ( n=0 ; n<5 ; n++ )
• {
• result += billy[n];
• }
• cout << result; return 0;
• }
• Output
• 12206
•
Arrays as parameters
• #include <iostream>
• using namespace std;
• void printarray (int arg[], int length)
• {
• for (int n=0; n<length; n++)
• cout << arg[n] << " ";
• cout << "n";
• }
• int main ()
{
int firstarray[] = {5, 10, 15};
int secondarray[] = {2, 4, 6, 8, 10};
printarray (firstarray,3);
printarray (secondarray,5);
return 0;
}
Reference operator (&)
The address that locates a variable within memory is what we call a
reference to that variable. This reference to a variable can be obtained by
preceding the identifier of a variable with an ampersand sign (&), known
as reference operator, and which can be literally translated as "address
of". For example:
ted = &andy;
This would assign to ted the address of variable andy, since when
preceding the name of the variable andy with the reference operator (&)
we are no longer talking about the content of the variable itself, but about
its reference (i.e., its address in memory).
Consider the following code fragment:
andy=25
Fred=andy
Ted= &andy
andy = 25; fred = andy; ted = &andy;
Cont’d
The values contained in each variable after the execution of this, are
shown in the following diagram:
• The variable that stores the reference to another variable (like ted
in the previous example) is what we call a pointer.
Dereference operator (*)
• We have just seen that a variable which stores a
reference to another variable is called a pointer.
Pointers are said to "point to" the variable whose
reference they store.
Using a pointer we can directly access the value stored
in the variable which it points to. To do this, we simply
have to precede the pointer's identifier with an asterisk
(*), which acts as dereference operator and that can be
literally translated to "value pointed by".
Therefore, following with the values of the previous
example, if we write:
Cont’d
Beth=*ted
• that we could read as: "beth equal to value pointed by ted") beth would
take the value 25, since ted is 1776, and the value pointed by 1776 is 25.
• Notice the difference between the reference and dereference operators:
1. & is the reference operator and can be read as "address of"
2. * is the dereference operator and can be read as "value pointed by"
Example#1
• my first pointer
• #include <iostream>
• using namespace std;
• int main ()
• {
• int firstvalue, secondvalue;
• int * mypointer;
• mypointer = &firstvalue;
• *mypointer = 10;
• mypointer = &secondvalue;
• *mypointer = 20;
• cout << "firstvalue is " << firstvalue << endl;
• cout << "secondvalue is " << secondvalue << endl;
• return 0;
• }
• The Output
• firstvalue is 10
• secondvalue is 20
•
Example#2
• // more pointers
• #include <iostream>
• using namespace std;
• int main ()
• {
• int numbers[5];
• int * p;
• p = numbers;
• *p = 10;
• p++;
• *p = 20;
• p = &numbers[2];
• *p = 30;
• p = numbers + 3;
• *p = 40;
• p = numbers;
• *(p+4) = 50;
• for (int n=0; n<5; n++)
• {
• cout << numbers[n] << ", "; }
• }
• return 0;
• }
• The Output
• 10, 20, 30, 40, 50,
•
Arrays
• #include <iostream>
• using namespace std;
• void main()
• {
• int Nums[4];
• int Sum=0;
• cout<<"Enter 4 Numbers :n";
• for(int i=0;i<4;i++)
• cin>>Nums[i];
• for(int j=0;j<4;j++)
• Sum+=Nums[j];
• cout<<"Sum = "<<Sum<<endl;
• }
Pointer
• #include<iostream>
• using namespace std;
• void Change1(float Data)
• {
• Data = 9.99;
• }
• void Change2(float* Data)
• {
• *Data = 9.99;
• }
• void main()
• {
• float F = 3.14;
• float* FPtr = &F;
• Change1(F);
• cout<<"After Change1 : F = "<<F<<endl;
• Change2(FPtr);
• cout<<"After Change2 : F = "<<F<<endl;
• }

Mais conteúdo relacionado

Mais procurados

c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2sajidpk92
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2Mouna Guru
 
Java JIT Optimization Research
Java JIT Optimization Research Java JIT Optimization Research
Java JIT Optimization Research Adam Feldscher
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Abdullah khawar
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structurelodhran-hayat
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++NUST Stuff
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointersVivek Bhargav
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays kinan keshkeh
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control StructureMohammad Shaker
 
The Ring programming language version 1.8 book - Part 23 of 202
The Ring programming language version 1.8 book - Part 23 of 202The Ring programming language version 1.8 book - Part 23 of 202
The Ring programming language version 1.8 book - Part 23 of 202Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 25 of 210
The Ring programming language version 1.9 book - Part 25 of 210The Ring programming language version 1.9 book - Part 25 of 210
The Ring programming language version 1.9 book - Part 25 of 210Mahmoud Samir Fayed
 
【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽
【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽
【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽tbosstraining
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st StudyChris Ohk
 

Mais procurados (20)

Arrays matrix 2020 ab
Arrays matrix 2020 abArrays matrix 2020 ab
Arrays matrix 2020 ab
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
 
Session12 pointers
Session12 pointersSession12 pointers
Session12 pointers
 
Java JIT Optimization Research
Java JIT Optimization Research Java JIT Optimization Research
Java JIT Optimization Research
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointers
 
Lecture1 classes1
Lecture1 classes1Lecture1 classes1
Lecture1 classes1
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
 
The Ring programming language version 1.8 book - Part 23 of 202
The Ring programming language version 1.8 book - Part 23 of 202The Ring programming language version 1.8 book - Part 23 of 202
The Ring programming language version 1.8 book - Part 23 of 202
 
C++ L04-Array+String
C++ L04-Array+StringC++ L04-Array+String
C++ L04-Array+String
 
C++ L06-Pointers
C++ L06-PointersC++ L06-Pointers
C++ L06-Pointers
 
The Ring programming language version 1.9 book - Part 25 of 210
The Ring programming language version 1.9 book - Part 25 of 210The Ring programming language version 1.9 book - Part 25 of 210
The Ring programming language version 1.9 book - Part 25 of 210
 
【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽
【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽
【第一季第三期】Thinking in Javascript & OO in Javascript - 清羽
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 

Destaque

Why do we (teachers) need research?
Why do we (teachers) need research?Why do we (teachers) need research?
Why do we (teachers) need research?Joseph Anbarasu
 
Through the keyhole: why do we need research on research?
Through the keyhole: why do we need research on research?Through the keyhole: why do we need research on research?
Through the keyhole: why do we need research on research?Emma Kirkpatrick
 
Research Methods: Basic Concepts and Methods
Research Methods: Basic Concepts and MethodsResearch Methods: Basic Concepts and Methods
Research Methods: Basic Concepts and MethodsAhmed-Refat Refat
 
An Introduction to Research Methods in Education
An Introduction to Research Methods in EducationAn Introduction to Research Methods in Education
An Introduction to Research Methods in EducationEl Sameeha
 
Computers Instructional Tools
Computers Instructional ToolsComputers Instructional Tools
Computers Instructional Toolskirish43
 
Week 07 Overview
Week 07 OverviewWeek 07 Overview
Week 07 OverviewAmy G.
 
Main features of a psychological thriller
Main features of a psychological thrillerMain features of a psychological thriller
Main features of a psychological thrillermalachikhan
 
PDU 211 Research Methods: Mixed-Methods & Action Research Designs
PDU 211 Research Methods: Mixed-Methods & Action Research DesignsPDU 211 Research Methods: Mixed-Methods & Action Research Designs
PDU 211 Research Methods: Mixed-Methods & Action Research DesignsAgatha N. Ardhiati
 
Anthropological Foundations of Education
Anthropological Foundations of EducationAnthropological Foundations of Education
Anthropological Foundations of EducationSergz Diaz
 
Data structures / C++ Program examples
Data structures / C++ Program examplesData structures / C++ Program examples
Data structures / C++ Program examplesKevin III
 
Characteristics ofresearch
Characteristics ofresearchCharacteristics ofresearch
Characteristics ofresearchJimnaira Abanto
 
Philosophical Foundations of EDUCATION
Philosophical Foundations of EDUCATIONPhilosophical Foundations of EDUCATION
Philosophical Foundations of EDUCATIONGeraldine Balladares
 
Importance of Research
Importance of ResearchImportance of Research
Importance of ResearchpGRADS
 
Why Integrate Technology In Teaching
Why Integrate Technology In TeachingWhy Integrate Technology In Teaching
Why Integrate Technology In Teachingmustgo20
 
philosophical foundations of education
philosophical foundations of educationphilosophical foundations of education
philosophical foundations of educationTahira Jabeen
 
Historical Foundations
Historical FoundationsHistorical Foundations
Historical Foundationsguestcc1ebaf
 
The philosophical foundations of education
The philosophical foundations of educationThe philosophical foundations of education
The philosophical foundations of educationLo-Ann Placido
 

Destaque (20)

Why do we (teachers) need research?
Why do we (teachers) need research?Why do we (teachers) need research?
Why do we (teachers) need research?
 
Through the keyhole: why do we need research on research?
Through the keyhole: why do we need research on research?Through the keyhole: why do we need research on research?
Through the keyhole: why do we need research on research?
 
Research Methods: Basic Concepts and Methods
Research Methods: Basic Concepts and MethodsResearch Methods: Basic Concepts and Methods
Research Methods: Basic Concepts and Methods
 
An Introduction to Research Methods in Education
An Introduction to Research Methods in EducationAn Introduction to Research Methods in Education
An Introduction to Research Methods in Education
 
Bjarne essencegn13
Bjarne essencegn13Bjarne essencegn13
Bjarne essencegn13
 
Computers Instructional Tools
Computers Instructional ToolsComputers Instructional Tools
Computers Instructional Tools
 
EDA Lecture 1
EDA Lecture 1EDA Lecture 1
EDA Lecture 1
 
Week 07 Overview
Week 07 OverviewWeek 07 Overview
Week 07 Overview
 
Main features of a psychological thriller
Main features of a psychological thrillerMain features of a psychological thriller
Main features of a psychological thriller
 
PDU 211 Research Methods: Mixed-Methods & Action Research Designs
PDU 211 Research Methods: Mixed-Methods & Action Research DesignsPDU 211 Research Methods: Mixed-Methods & Action Research Designs
PDU 211 Research Methods: Mixed-Methods & Action Research Designs
 
Anthropological Foundations of Education
Anthropological Foundations of EducationAnthropological Foundations of Education
Anthropological Foundations of Education
 
Data structures / C++ Program examples
Data structures / C++ Program examplesData structures / C++ Program examples
Data structures / C++ Program examples
 
Characteristics ofresearch
Characteristics ofresearchCharacteristics ofresearch
Characteristics ofresearch
 
Philosophical Foundations of EDUCATION
Philosophical Foundations of EDUCATIONPhilosophical Foundations of EDUCATION
Philosophical Foundations of EDUCATION
 
Importance of Research
Importance of ResearchImportance of Research
Importance of Research
 
Why Integrate Technology In Teaching
Why Integrate Technology In TeachingWhy Integrate Technology In Teaching
Why Integrate Technology In Teaching
 
philosophical foundations of education
philosophical foundations of educationphilosophical foundations of education
philosophical foundations of education
 
ICT Centered Teaching & Learning
ICT Centered Teaching  & LearningICT Centered Teaching  & Learning
ICT Centered Teaching & Learning
 
Historical Foundations
Historical FoundationsHistorical Foundations
Historical Foundations
 
The philosophical foundations of education
The philosophical foundations of educationThe philosophical foundations of education
The philosophical foundations of education
 

Semelhante a C++ examples &revisions

Semelhante a C++ examples &revisions (20)

Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Intro to C++
Intro to C++Intro to C++
Intro to C++
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer
 
20.C++Pointer.pptx
20.C++Pointer.pptx20.C++Pointer.pptx
20.C++Pointer.pptx
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 
Pointers
PointersPointers
Pointers
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptx
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
Unit 3
Unit 3Unit 3
Unit 3
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
program#include iostreamusing namespace std;void calculatio.pdf
program#include iostreamusing namespace std;void calculatio.pdfprogram#include iostreamusing namespace std;void calculatio.pdf
program#include iostreamusing namespace std;void calculatio.pdf
 
Pointers
PointersPointers
Pointers
 
Lec 2.pptx
Lec 2.pptxLec 2.pptx
Lec 2.pptx
 
C Programming - Refresher - Part III
C Programming - Refresher - Part IIIC Programming - Refresher - Part III
C Programming - Refresher - Part III
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical Reviewer
 
C++ practical
C++ practicalC++ practical
C++ practical
 

Mais de Ibrahim El-Torbany (16)

Idea2
Idea2Idea2
Idea2
 
Cpp lernaufgabe linked_list
Cpp lernaufgabe linked_listCpp lernaufgabe linked_list
Cpp lernaufgabe linked_list
 
Lec6 mod linked list
Lec6 mod linked listLec6 mod linked list
Lec6 mod linked list
 
Lec5
Lec5Lec5
Lec5
 
Lec3
Lec3Lec3
Lec3
 
Lec2
Lec2Lec2
Lec2
 
Lec2&3 data structure
Lec2&3 data structureLec2&3 data structure
Lec2&3 data structure
 
Lec1
Lec1Lec1
Lec1
 
Lec4
Lec4Lec4
Lec4
 
Ass logic
Ass logicAss logic
Ass logic
 
Math lecture 4 Part 1
Math lecture 4 Part 1Math lecture 4 Part 1
Math lecture 4 Part 1
 
Tutorial 1
Tutorial 1Tutorial 1
Tutorial 1
 
Lec2&3_DataStructure
Lec2&3_DataStructureLec2&3_DataStructure
Lec2&3_DataStructure
 
Lecture 2 math 2
Lecture 2 math 2Lecture 2 math 2
Lecture 2 math 2
 
Lec1
Lec1Lec1
Lec1
 
Chapter 1 what is statistics
Chapter 1 what is statisticsChapter 1 what is statistics
Chapter 1 what is statistics
 

C++ examples &revisions

  • 1. C++ Examples &Revisions Hurts - Wonderful Life Lecture 5
  • 2. One Dimension Arrays • // arrays example • #include <iostream> • using namespace std; • int billy [] = {16, 2, 77, 40, 12071}; • int n, result=0; • int main () • { • for ( n=0 ; n<5 ; n++ ) • { • result += billy[n]; • } • cout << result; return 0; • } • Output • 12206 •
  • 3. Arrays as parameters • #include <iostream> • using namespace std; • void printarray (int arg[], int length) • { • for (int n=0; n<length; n++) • cout << arg[n] << " "; • cout << "n"; • } • int main () { int firstarray[] = {5, 10, 15}; int secondarray[] = {2, 4, 6, 8, 10}; printarray (firstarray,3); printarray (secondarray,5); return 0; }
  • 4. Reference operator (&) The address that locates a variable within memory is what we call a reference to that variable. This reference to a variable can be obtained by preceding the identifier of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of". For example: ted = &andy; This would assign to ted the address of variable andy, since when preceding the name of the variable andy with the reference operator (&) we are no longer talking about the content of the variable itself, but about its reference (i.e., its address in memory). Consider the following code fragment: andy=25 Fred=andy Ted= &andy andy = 25; fred = andy; ted = &andy;
  • 5. Cont’d The values contained in each variable after the execution of this, are shown in the following diagram: • The variable that stores the reference to another variable (like ted in the previous example) is what we call a pointer.
  • 6. Dereference operator (*) • We have just seen that a variable which stores a reference to another variable is called a pointer. Pointers are said to "point to" the variable whose reference they store. Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference operator and that can be literally translated to "value pointed by". Therefore, following with the values of the previous example, if we write:
  • 7. Cont’d Beth=*ted • that we could read as: "beth equal to value pointed by ted") beth would take the value 25, since ted is 1776, and the value pointed by 1776 is 25. • Notice the difference between the reference and dereference operators: 1. & is the reference operator and can be read as "address of" 2. * is the dereference operator and can be read as "value pointed by"
  • 8. Example#1 • my first pointer • #include <iostream> • using namespace std; • int main () • { • int firstvalue, secondvalue; • int * mypointer; • mypointer = &firstvalue; • *mypointer = 10; • mypointer = &secondvalue; • *mypointer = 20; • cout << "firstvalue is " << firstvalue << endl; • cout << "secondvalue is " << secondvalue << endl; • return 0; • } • The Output • firstvalue is 10 • secondvalue is 20 •
  • 9. Example#2 • // more pointers • #include <iostream> • using namespace std; • int main () • { • int numbers[5]; • int * p; • p = numbers; • *p = 10; • p++; • *p = 20; • p = &numbers[2]; • *p = 30; • p = numbers + 3; • *p = 40; • p = numbers; • *(p+4) = 50; • for (int n=0; n<5; n++) • { • cout << numbers[n] << ", "; } • } • return 0; • } • The Output • 10, 20, 30, 40, 50, •
  • 10. Arrays • #include <iostream> • using namespace std; • void main() • { • int Nums[4]; • int Sum=0; • cout<<"Enter 4 Numbers :n"; • for(int i=0;i<4;i++) • cin>>Nums[i]; • for(int j=0;j<4;j++) • Sum+=Nums[j]; • cout<<"Sum = "<<Sum<<endl; • }
  • 11. Pointer • #include<iostream> • using namespace std; • void Change1(float Data) • { • Data = 9.99; • } • void Change2(float* Data) • { • *Data = 9.99; • } • void main() • { • float F = 3.14; • float* FPtr = &F; • Change1(F); • cout<<"After Change1 : F = "<<F<<endl; • Change2(FPtr); • cout<<"After Change2 : F = "<<F<<endl; • }