LAB#1 : Arrays & Functions. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays Arrays.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Pointers. andy = 25; fred = andy; ted = &andy; andy = 25; ted = &andy; beth = *ted;
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
Overview creating your own functions calling your own functions.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
February 11, 2005 More Pointers Dynamic Memory Allocation.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
1 CSE 2341 Object Oriented Programming with C++ Note Set #2.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data.
Pointers. The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte).
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Pointer to an Object Can define a pointer to an object:
Chapter 7 Pointers and C-Strings
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointers Psst… over there.
Pointers Psst… over there.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
More About Data Types & Functions
Pointers Kingdom of Saudi Arabia
5.1 Introduction Pointers Powerful, but difficult to master
7 Arrays.
Engineering Problem Solving with C++ An Object Based Approach
Pointers and Pointer-Based Strings
Arrays Arrays A few types Structures of related data items
Presentation transcript:

LAB#1 : Arrays & Functions

What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays 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: 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.

Functions

Define Function DataType name (parameter1,…){ statements} Function with no type. Arguments passed by value and by reference. Recursivity. The Prototype of the function DataType name (parameter1,…); Functions

Trace the following programs, and show the output: Ex#1:

#include 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 ; }

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.

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).

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

Answer of Evaluation Question

Pointers Lab#2 15

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. 16 So what is a pointer?

Reference operator (&) "address of" andy = 25; fred = andy; ted = &andy; Pointers 17

Deference operator (*) "value pointed by“ andy = 25; ted = &andy; beth = *ted; Pointers 18

int * numberPtr; char * characterPtr; float * greatnumberPtr; int * mPtr, * nPtr, *j; Declaring variables of pointer types 19

 Pointer to specific address: int number; int *tommy = &number;  Pointer to nothing int *tommy = NULL; equivalents to int *tommy = 0; Pointer initialiazation 20

Strings as pointer to characters char * terry = "hello"; The fifth element can be accessed with: *(terry+4) or terry[4] Pointer initialiazation 21

 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 22

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 10 secondvalue is 20 23

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, 24

void main () { int a = 50; int *aptr ; aptr = &a; // assume that aptr=0x0018FF44 cout <<"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 25

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. 26 Evaluation Question

27 Answer of Evaluation Question

Lab#3 Classes

nora albabtin

#include 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 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 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 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<<"*****************************************"<<end l; 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 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 nora albabtin

Answer of Exercise#2 nora albabtin