Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Similar documents
Week 3: Pointers (Part 2)


Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

LAB 4.1 Relational Operators and the if Statement

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

Homework #3 CS2255 Fall 2012

Exam 3 Chapters 7 & 9

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Name SECTION: 12:45 2:20. True or False (12 Points)

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Lab Instructor : Jean Lai

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Computer Programming

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

True or False (15 Points)

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Lab 8 The Stack (LIFO) Structure

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

FORM 2 (Please put your name and form # on the scantron!!!!)

True or False (15 Points)

Linked List using a Sentinel

True or False (14 Points)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CS201- Introduction to Programming Current Quizzes

Lecture 23: Pointer Arithmetic

Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

Pointers. Variable Declaration. Chapter 10

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

True or False (12 Points)

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

What is an algorithm?

CS2255 HOMEWORK #1 Fall 2012

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

CHAPTER 4 FUNCTIONS. 4.1 Introduction

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

EECS402 Lecture 02. Functions. Function Prototype

CSCE Practice Midterm. Data Types

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Tema 6: Dynamic memory

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

C++ For Science and Engineering Lecture 12

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Midterm Review. PIC 10B Spring 2018

CS 103 Unit 11. Linked Lists. Mark Redekopp

Lab # 02. Basic Elements of C++ _ Part1

Pointers Pointer Variables. Pointer Variables Getting the Address of a Variable. Each variable in program is stored at a

BITG 1113: Array (Part 1) LECTURE 8

1. Which of the following best describes the situation after Line 1 has been executed?

Pointers II. Class 31

Pointers, Dynamic Data, and Reference Types

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

CSCE Practice Midterm. Data Types

Local and Global Variables

BITG 1113: Function (Part 2) LECTURE 5

Today USING POINTERS. Functions: parameters and arguments. Todaywewilllookattopicsrelatingtotheuseofpointers

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Introduction to Programming I COS1511 School of Computing Revision Notes

Name Section: M/W or T/TH. True or False (14 Points)

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

Constructor - example

Intermediate Programming, Spring 2017*

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Dynamic Memory Allocation

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Pointers, Arrays and Parameters

Exercise 1.1 Hello world

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

CMPS 221 Sample Final

Computer Programming

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Due Date: See Blackboard

Functions, Arrays & Structs

CS 103 Unit 11. Linked Lists. Mark Redekopp

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

TEMPLATE IN C++ Function Templates

C++ Programming for Non-C Programmers. Supplement

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

Programming Studio #9 ECE 190

CS 103 Unit 13 Slides

Object-Oriented Programming for Scientific Computing

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

Chapter 11: Structured Data

Transcription:

Lab 2: Pointers 1. Goals Further understanding of pointer variables Passing parameters to functions by address (pointers) and by references Creating and using dynamic arrays Combing pointers, structures and arrays 2. Practice on pointer variables Step 1: Download pointer.cpp: /home/fac/testzhuy/cpsc152/lab2/download poiner.cpp Step 2: Edit pointer.cpp using emacs emacs pointer.cpp /* Your Name * pointer.cpp *demonstrate use of pointers #include <iostream> using namespace std; int main() { int x = 5; //declare a pointer variable ptr1 pointing to x //display the value of x using ptr1 //change the value of x to 10 through ptr1 /*declare a double data type pointer variable ptr2 which points to a dynamically allocated memory //input data into the newly allocated memory using cin //display the double data value using cout //increase this double data value by 2.5

//dispay the value using cout //destroy the dynamically allocated memory return 0; Step 3: Complete the code as indicated by the comments in main() Step 4: Save the file and exit Emacs. Step 5: Compile the file and execute it. (Or you can adapt your Makefile.) g++ -Wall -o pointer pointer.cpp./pointer 3. Parameter passing by pointers and by references Step 1: Download para.cpp: Step 2: Edit para.cpp home/fac/testzhuy/cpsc152/lab2/download para.cpp emacs para.cpp /* Your Name: * param.cpp, * demonstrate calling functions by value, by pointers, and by references #include <iostream> using namespace std; void swap1(int x, int y); //exchange the two inputed values //add your swap2 and swap3 function prototype here

int main() { int num1, num2; cout << "\nenter two numbers separated by a space: " << endl; cin >> num1 >> num2; cout << "\n num1 is " << num1 << ", num2 is " << num2 << endl; swap1(num1, num2); //exchange their values cout << "\n num1 is " << num1 << ", num2 is " << num2 << endl; //add your swap2 function call to exchange values of num1 and num2 //display num1 and num2 //add your swap3 function call to exchange values of num1 and num2 //display num1 and num2 return 0; void swap1(int x, int y) { int tmp = x; x = y; y = tmp; Step 3: Save the file and exit Emacs. Compile and execute it. Do the values get exchanged? Step 4: Add swap2 function that takes parameters by pointers. Save the code, compile it and execute it to see the difference. Step 5: Add swap3 function that takes parameters by references. Save the code, compile it and execute it to see the difference.

4. Combining pointers, structures and dynamic arrays Step 1: Download para.cpp: home/fac/testzhuy/cpsc152/lab2/download dynamic.cpp Step 2: Edit para.cpp emacs dynamic.cpp /* Your name: * dynamic.cpp, * demonstrate combination of pointers, structures, and dynamic arrays #include <iostream> #include <string> using namespace std; struct Student { int ID; //student ID string name; // student name double gpa; //GPA short year_in_school; // Year in school, e.g., 2003 ; /* * Return a numer of n students' info from user inputs * PRE: assume a positive integer n * POST: return a dynamic array holding n students' info Student* getstudentdata(int n); /* * Display a student's data in a line * PRE: A pointer to a student structure * POST: none void displaystudentdata(const Student* stud); int main() { int num_of_students = 0; do { cout << "Number of students you want to create: ";

cin >> num_of_students; while (num_of_students <= 0); /* call getstudentdata(num_of_students) to get the student data into a pointer variable ptr which is a pointer to Student structure /* Use a loop to call displaystudentdata() to display all students in the array by manipulating the pointer variable ptr // destroy the dynamic memory return 0; //Complete the function Student* getstudentdata(int n) { //Complete the function void displaystudentdata(student* stud) { Step 3: Complete the code. In particular, you need to finish up getstudentdata(), displaystudentdata() and main(). Step 4: Save the file, compile and execute it. Take-home message: Always check first whether a pointer is valid before using it! 5. Extra Exercises on Two-Dimensional Dynamic Arrays You can create a new program named twodim.cpp which handles two-dimensional dynamic arrays. You mainly have to implement two functions: int** getmatrix(int m, int n) --- The function takes two parameters m and n, specifying a matrix s number of rows and number of columns respectively. It allocate a 2-dimensional dynamic int array for this matrix, and initialize element at [i,j] with i*n + j.

void destroymatrix(int** matrix, int m) --- This function deallocate the dynamic memory of the 2-dimensional array with m rows. void displaymatrix(int** matrix, int m, int n) --- This function display matrix with m rows and columns. In your main(), you need to test these three functions.