lab#1 : arrays & functions
DESCRIPTION
LAB#1 : Arrays & Functions. Arrays. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays. Exercise: Write a C++ program that reads n integers in an array of maximum size 14. After that your program should do the following: - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/1.jpg)
LAB#1 : Arrays & Functions
![Page 2: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/2.jpg)
• What is an array?• Initializing arrays• Accessing the values of an array• Multidimensional arrays
Arrays
![Page 3: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/3.jpg)
Exercise:• Write a C++ program that reads n integers in an array of
maximum size 14. After that your program should do the following:
• Print the array elements. • Modify the array element such that the element that is
even and multiple of 100 will be duplicated and store the result in the same element.
• Print the array after modifications.• Print the number of elements that is even and multiple of
100.
![Page 4: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/4.jpg)
![Page 5: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/5.jpg)
Functions
![Page 6: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/6.jpg)
• Define Function
DataType name (parameter1,…){ statements}• Function with no type.• Arguments passed by value and by reference.• Recursivity.• The Prototype of the functionDataType name (parameter1,…);
Functions
![Page 7: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/7.jpg)
• Trace the following programs, and show the output:
Ex#1:
![Page 8: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/8.jpg)
#include<iostream>using namespace std; void printtotal(int );void addxy(int , int , int );void subxy(int , int , int& ); void main() { int x, y, total; x = 10; y = 5; total = 0; printtotal(total); addxy(x, y, total); printtotal(total); subxy(x, y, total); printtotal(total);} void printtotal(int total) {
cout<<"Total in Main:" << total<<endl ;} void addxy(int x, int y, int total) { total = x + y; cout<<"Total from inside addxy: "<< total<<endl;} void subxy(int x, int y, int &total) { total = x - y; cout<<"Total from inside subxy:" << total<<endl ;}
![Page 9: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/9.jpg)
Separating Classes into Files• What are the benefit of separating classes in to files ?• Normally, to do the separation, the class declaration is
placed in one file (header file), and the implementation of all the methods is put in another file.
• The class declaration file is normally called ClassName.h.
• The implementation file is normally called ClassName.cpp.
• Then you include the header file in your program with an #include directive.
![Page 10: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/10.jpg)
• However, instead of including two files (ClassName.h and ClassName.cpp), you have to include only ClassName.h.
• The compiler will include the .cpp file automatically • Don’t forget: the two files need to be in the same
directory to achieve that).
![Page 11: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/11.jpg)
![Page 12: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/12.jpg)
![Page 13: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/13.jpg)
Exercise#1:Write a C++ program that defines array enable user to enter the elements of type integer, then print the sum of all elements.
Evaluation Question
![Page 14: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/14.jpg)
Answer of Evaluation Question
![Page 15: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/15.jpg)
15
Pointers
Lab#2
![Page 16: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/16.jpg)
16
• A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type.
• we can use pointers with:1. Arrays,2. Structures,3. Functions.
So what is a pointer?
![Page 17: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/17.jpg)
17
Reference operator (&) "address of"
andy = 25;fred = andy;ted = &andy;
Pointers
![Page 18: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/18.jpg)
18
Deference operator (*) "value pointed by“
andy = 25;ted = &andy;beth = *ted;
Pointers
![Page 19: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/19.jpg)
19
int * numberPtr;char * characterPtr;float * greatnumberPtr;int * mPtr, * nPtr, *j;
Declaring variables of pointer types
![Page 20: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/20.jpg)
20
Pointer to specific address:int number;int *tommy = &number;
Pointer to nothingint *tommy = NULL; equivalents to
int *tommy = 0;
Pointer initialiazation
![Page 21: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/21.jpg)
21
• Strings as pointer to characterschar * terry = "hello";
The fifth element can be accessed with: *(terry+4) or terry[4]
Pointer initialiazation
![Page 22: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/22.jpg)
22
Suppose the following piece of code:char *mychar;short *myshort;long *mylong;mychar++;myshort++;mylong++;
(++) and (--) operators have greater operator precedence than the dereference operator (*).
Pointer Arthematic
![Page 23: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/23.jpg)
23
int main (){int firstvalue = 5, secondvalue = 15;int * p1, * p2;p1 = &firstvalue; //p1 = address of firstvalue p2 = &secondvalue; //p2 = address of secondvalue*p1 = 10; //value pointed by p1 = 10 *p2 = *p1; //value pointed by p2=value pointed by p1
p1 = p2; // p1 = p2 (value of pointer is copied) *p1 = 20; // value pointed by p1 = 20 cout << "firstvalue is " << firstvalue << endl; cout <<"secondvalue is " << secondvalue << endl; return 0;}
Ex#1: Trace the code below
firstvalue is 10secondvalue is 20
![Page 24: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/24.jpg)
24
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;}
Ex#2: Pointers and Arrays (Trace)
10, 20, 30, 40, 50,
![Page 25: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/25.jpg)
25
void main (){int a = 50;int *aptr ;aptr = &a;// assume that aptr=0x0018FF44cout <<"The output of a= "<<a << "\n";cout <<"The output of *aptr = "<<*aptr << "\n";cout <<"The output of &a= "<<&a << "\n";cout <<"The output of &*aptr = "<<&*aptr << "\n";cout <<"The output of *&aptr = "<<*&aptr << "\n";}
Ex#3: What is the output
![Page 26: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/26.jpg)
26
• Assume we have char array called str which have 4 capital litters( elements) , like this:
• char Str[]="ABCD";• Required: print this array in this form " AbcD ".• Hint : use pointer.
Evaluation Question
![Page 27: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/27.jpg)
27
Answer of Evaluation Question
![Page 28: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/28.jpg)
Lab#3
Classes
![Page 29: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/29.jpg)
nora albabtin
![Page 30: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/30.jpg)
nora albabtin
![Page 31: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/31.jpg)
nora albabtin
![Page 32: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/32.jpg)
nora albabtin
![Page 33: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/33.jpg)
nora albabtin
#include <iostream>#include <string>using namespace std;
class person{
private:string name;int age;
public: person();person(string , int );void set(string,int);string getname();int getage();
};
Exercise#1:Trace the code below
![Page 34: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/34.jpg)
nora albabtin
person::person(){name="NO Name";age=0;}person::person(string pn,int pa){name=pn;age=pa;}
Exercise#1:Trace the code below
![Page 35: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/35.jpg)
nora albabtin
void person::set(string n, int a) {name=n;age=a;}string person::getname(){return name;}int person::getage(){return age;}
Exercise#1:Trace the code below
![Page 36: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/36.jpg)
nora albabtin
int main(){person a;person b("Fahad",24);cout<<"Persons information : "<<endl;cout << a.getname() << ": " << a.getage() <<endl;cout << b.getname() << ": " << b.getage() << endl;cout<<"*****************************************"<<endl;a.set("Ahmad",30);b.set("Khaled", 20); cout<<"Persons information after modification : "<<endl;cout << a.getname() << ": " << a.getage() <<endl;cout << b.getname() << ": " << b.getage() << endl;
return 0;}
Exercise#1:Trace the code below
![Page 37: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/37.jpg)
nora albabtin
Define a class Rectangle which contains:• Data members: length and width. • Member functions:– Function area() to compute the area of the rectangle. – Function getdata( ) to prompt the user to enter the length
and width for a rectangle. – Function showdata( ) to display length, width and area of a
rectangle
Exercise#2
![Page 38: LAB#1 : Arrays & Functions](https://reader036.vdocuments.us/reader036/viewer/2022062301/568149bb550346895db6f105/html5/thumbnails/38.jpg)
nora albabtin
Answer of Exercise#2