Homework Assignment #2 (revised)

Similar documents
Homework Assignment #1

CS2141 Software Development using C/C++ C++ Basics

pointers + memory double x; string a; int x; main overhead int y; main overhead

C++ Structures Programming Workshop 2 (CSCI 1061U)

In this chapter, you will learn about: Pointers. Dynamic Arrays. Introduction Computer Science 1 CS 23021

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

CMPS 221 Sample Final

Name. CPTR246 Spring '17 (100 total points) Exam 2

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9.

CSC 211 Intermediate Programming. Arrays & Pointers

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.

Homework #3 CS2255 Fall 2012

C++ For Science and Engineering Lecture 15

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

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

Unit 14. Passing Arrays & C++ Strings

Programming Assignment 3

EE 355 Lab 4 - Party Like A Char Star

CS 103 Lab 6 - Party Like A Char Star

CMSC 202 Midterm Exam 1 Fall 2015

Engineering Problem Solving with C++, Etter

CS 103 Lab - Party Like A Char Star

Programming in C/C Lecture 2

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

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

Chapter 9. Pointers and Dynamic Arrays

CS242 COMPUTER PROGRAMMING

The C++ Language. Arizona State University 1

CSC 270 Survey of Programming Languages. What is a Pointer?

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

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

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

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

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Pointers, Arrays and C-Strings

What have we learned about when we learned about function parameters? 1-1

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Computer Programming

Integer Data Types. Data Type. Data Types. int, short int, long int

Pointers II. Class 31

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

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

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Lab 8 The Stack (LIFO) Structure

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Objectives. In this chapter, you will:

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

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.

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

EE 109 Lab 8a Conversion Experience

Chapter 2 Basic Elements of C++

Exercise 1.1 Hello world

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

Data Types & Variables

Reference operator (&)

Understanding main() function Input/Output Streams

Chapter 2. Procedural Programming

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

Homework 11 Program Setup (with some IMPORTANT NEW STEPS!)

A First Program - Greeting.cpp

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

Introduction to Programming EC-105. Lecture 2

ECE250: Algorithms and Data Structures C++ Language Tutorial

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Computer Programming. Dr. Fahad Computer Science Deptt.

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

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

CS31 Discussion. Jie(Jay) Wang Week6 Nov.4

C++ basics Getting started with, and Data Types.

Computer Programming

Chapter 2: Introduction to C++

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

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

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

BITG 1113: Array (Part 2) LECTURE 9

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

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

IT 1033: Fundamentals of Programming Data types & variables

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

CS101 PLEDGED SPRING 2001

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Lab Instructor : Jean Lai

CSc Introduction to Computing

Chapter 7 Array. Array. C++, How to Program

UNIT- 3 Introduction to C++

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

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

1. In C++, reserved words are the same as predefined identifiers. a. True

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Transcription:

CISC 2000 Computer Science II Fall, 2018 1 Recall the following functions and operators: Homework Assignment #2 (revised) sizeof function: returns the size of a variable (i.e., the number of bytes used by a variable). N ote when you cout a pointer variable or value, it will be shown in hexadecimal, as indicated by the 0X prefix of the number. You can cast the pointer to long if you want the address to be displayed in decimal. Write a C++ program that declares three variables and three pointers that pointing to them (as below), and add cout statements to display out the size, and address of the variables. Run your program and write down your answers in the blank. int a=10; // the size of a is: double d=3.1415; // the size of d is: char c= A ; // the size of c is: char str[21]="seeing is believing!"; // the size of str is:, the length of str is: int numbers[10]=100, 101, 102; // the size of numbers is: int* p = NULL; // the size of p is:. // the value of p is: double* q=null; // the size of q is: // the value of q is: char* t=null; // the size of t is:, the value of t is: //Use cout statement to find out values of p, q, t p = &a; // the value of p is q = &d; // the value of q is t = &c; // the value of t is cout << *p << endl; // This displays (be specific). When accessing *p, the // bytes are accessed, and their addresses // are from // to. cout << *q << endl; // This displays. When accessing *q, // bytes are accessed, and their addresses // are from // to. cout << *t << endl; // This displays. When accessing *t, // bytes are, and there addresses are from

// are from // to. // Array vs poiner cout << "numbers is" << numbers << endl; //This displays: p = numbers; cout << "p=" << p <<endl; //This displays: int* p2 = &(numbers[0]); cout << "p2=" <<p2 <<endl; // This displays: cout << "p+1=" << p+1 << endl; // How much is p differs from p+1? // p+1 is the address of which element in numbers: // p+2 is the address of which element in numbers: cout << "q+1=" << q+1 << endl; // How much is q differs from q+1? cout << "t+1=" << t+1 << endl; // How much is t differs from t+1? // We observe that all pointer variables are of the same size, but we need to differentiate // a pointer to an int against a pointer to a double, because:. // char array is special, supported by library functions such as strlen(), strcpy(), // input and output using cin/cout t = str; // What happens here? cout << t <<endl; //What will be the output? Why? //Hint: recall pointer is same as array // cout <<t<<endl; is similar to // char t[size]; // cout << t <<endl; //this will display the char array, C string cout <<t+1 <<endl; // Why? //What will be the output? 2 Draw memory diagram to represent variables used in the following program, and trace the program to find out the output of the program. #include <iostream> using namespace std; 2

int c=20; int a=10; int b[20]=1,2,3,4,5; int * d = new int; int * p = NULL; /////////////////////////////////////////////// // Draw all varilables to the right side of the program p = &a; *p = c; *d = *p+20; /////Redraw the current status of variables at this point cout <<"*d"<<endl; //what s the output of the statement above? p = d; *p = *p+100; cout <<"*d"<<endl; /////Redraw the current status of variables at this point //what s the output of the statement above? p = b; *p = 0; p++; *p = 0; /////Redraw the current status of variables at this point cout <<b[0]<<endl<<b[1]<<endl; //what s the output of the statement above? 3 Array and pointer exercises. Answer the following questions, and verify them by typing up, compiling and running the program. int sum (int b[], int size) int total = 0; for (int i=0;i<size;i++) total+=b[i]; return total; 3

int a[5]=100,200,300,400,500; int *p= a; //Todo: Write a statement to use p to set a[0] to 1000 //What s the difference of the two function calls if any? Explain and confirm your answer: int total1 = sum (a, 5); int total2 = sum (p, 5); //Todo: Write a statement to set p to points to a[2] //After the above statement (that sets p to poitns to a[2], What s the output of // the following statement? //Explain by tracing the function call cout <<sum (p,2) <<endl; 4 Finish the following program, test, and debug the code. /* A function that reads a sequence of integers from input (with the length of sequence, followed by the numbers), and saves the numbers in an array of the given length For example, if the length is 3, and the numbers are 123 345 99 then the array returned will be of size 3, and stores values 123, 345, and 99 @param length: upon return, stores the length/size of the array @return the pointer pointing to the array */ int * ReadNumberSequence (int & size) do cout <<"Enter the length of the number sequence:"; cin >> size; while (size<=0); 4

int array[size]; // Todo: write a loop to read size # of int from input, and save them to the array return array; // Todo: delcare necesssary variables //Todo: call the ReadNumberSequence function to read a sequence of numbers //Todo: write a loop to display the elements in the array returned... 5 Draw memory diagram to illustrate all variables used in the following program. (a) Structure/class type object and pointers class Node int a; Node* next; ; Node* p = NULL; Node* q = NULL; 5

p = new Node; //Todo: What s the sizeof *p? (*p).a = 0; //accessing member variables of the Node variable that p is pointing to (*p).next = NULL; q = new Node; q->a = 1; //a shorthand for the combination of * and. above q->next = NULL; //Todo: Draw memory diagram to illustrate all variables at this point //store address of second node in the first node p->next = q; //Todo: Draw memory diagram to illustrate all variables at this point //Todo: allocate another node variable (dynamically) // and stores its address in the second node s next field (b) Tracing a program and find out what it prints in the terminal. char stars[11]="**********"; char spaces[11]=" "; //10 spaces for (int i=0;i<10;i++) cout << stars+i <<endl; for (int i=0;i<10;i++) cout <<spaces+i<< stars+(10-i)<<endl; (c) array of arrays 6

typedef int* IntPtr; int a[3]=1,2,3; int b[2]=4,5; IntPtr arrayofptr[2]; arrayofptr[0] = a; arrayofptr[1] = b; // arrayofptr is now storing a "2D" arrays: first row is a, second row is b //Draw memory diagram at this point //Todo: use arrayofptr to access b[1] // Todo: dynamically allocate two arrays of size 10, and stores // their addresses in arrayofptr // arrayofptr is now storing a 2x10 "2D" arrays, an array of pointers // Write a loop to fill the array of arrays with 0 //Draw the memory diagram at this point 7