Apllications. March 03, Indian Institute of Space Science and Technology. MA122 - Computer Programming and. Apllications.

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

Ch 6. Functions. Example: function calls function

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

CS 240 Computer Programming 1 Arrays

Lecture 23: Pointer Arithmetic

do { statements } while (condition);

Review Questions for Final Exam

Exceptions, Case Study-Exception handling in C++.

Unit 7. 'while' Loops

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.

Perfect square. #include<iostream> using namespace std; int main(){ int a=1; int square; while(true){ } cout<<square<<endl; }

Programming Language. Functions. Eng. Anis Nazer First Semester

1 #include <iostream> 2 using namespace std; 3 4 // implementing the dynamic List ADT using Linked List 5 6 class Node{ 7 8 private: 9 int data; 10

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

Linked List using a Sentinel

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

CS 115 Midterm 2A Solutions

Local and Global Variables

Lab Instructor : Jean Lai

Chapter 10 - Notes Applications of Arrays

University of Dublin

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Use the dot operator to access a member of a specific object.

Chapter 17 - Notes Recursion

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Study Guide for Test 2

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

Midterm Practice Exam

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, November 24, 2015

A SHORT COURSE ON C++

A First Program - Greeting.cpp

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

CSCI 111 Midterm 2 Exam Spring Solutions 09.00am 09.50am, Wednesday, May 04, 2016

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

BITG 1113: Function (Part 2) LECTURE 5

Scientific Computing

CS 115 Midterm 2 Solutions

Fundamentals of Programming. Lecture 19 Hamed Rasifard

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

CSCE Practice Midterm. Data Types

CSCE 110 PROGRAMMING FUNDAMENTALS

CMPS 221 Sample Final

Lab 7 (50 pts) Due Sunday, May 20)

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Practice test for midterm 1

typedef Labeling<unsigned char,short> LabelingBS; typedef Labeling<unsigned char,short>::regioninfo RegionInfoBS;

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

Notes on the 2008 Exam

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

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

1. (25 pts) Short Answer. Provide brief (1-3 sentence) answers to the following:

CS302 - Data Structures using C++

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

Increment and the While. Class 15

Do not turn to the next page until the start of the exam.

Introduction to Programming EC-105. Lecture 2

CSCE 206: Structured Programming in C++

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

Practice Problems CS2620 Advanced Programming, Spring 2003

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Object-Oriented Programming for Scientific Computing

Problem Solving: Storyboards for User Interaction

Module Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

C++ Scope Resolution Operator ::

4. C++ functions. 1. Library Function 2. User-defined Function

Applications. April 12, Indian Institute of Space Science and Technology. MA122 - Computer Programming and. Applications.

CMSC 202 Midterm Exam 1 Fall 2015

Object Oriented Design

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

As an example using arrays, let s write some code to get started with the Upthrust game. We can use a 2D array to represent the game board.

EECS402 Lecture 02. Functions. Function Prototype

Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

Computer Programming

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

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

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

Midterm Exam. Sample Solutions

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

Midterm Practice TA: Brian Choi Section Webpage:

C++ Programming: From Problem Analysis to Program Design, Third Edition

1 #include <iostream> 2 #include <stdlib.h> //srand, rand 3 #include <time.h>//clock_t, clock, CLOCKS_PER_SEC 4 using namespace std; 5 6 //

Fundamentals of Programming CS-110. Lecture 2

Run Time Environment

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

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Module 07: Programming in C++

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

CS197c: Programming in C++

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

CHAPTER 3 BASIC INSTRUCTION OF C++

Better Living with Automatic Differentiation

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Due Date: See Blackboard

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

Transcription:

Indian Institute of Space Science Technology March 03, 2017

Lecture 18 1 2 3

1 #include<iostream> 2 bool EvensFirst(int nx, int ny); 3 void SelectionSort(int *anarray, int nsize, bool (* pcomparison)(int, int)); 4 bool Ascending(int nx, int ny); 5 bool Descending(int nx, int ny); 6 void PrintArray(int *parray, int nsize); 7 using namespace std; 8 int main() 9 { 10 using namespace std; 11 12 int anarray[9] = { 4, 7, 9, 5, 6, 1, 8, 2, 3 }; 13 14 SelectionSort(anArray, 9, EvensFirst); 15 PrintArray(anArray, 9); 16 return 0; 17 }

1 bool EvensFirst(int nx, int ny) 2 { 3 // if nx is not even ny is, ny goes first 4 if ((nx % 2) &&!(ny % 2)) 5 return false; 6 7 // if nx is even ny is not, nx goes first 8 if (!(nx % 2) && (ny % 2)) 9 return true; 10 11 // otherwise sort in Ascending order 12 return Ascending(nX, ny); 13 }

1 void SelectionSort(int *anarray, int nsize, bool (* pcomparison)(int, int)) 2 { 3 for (int nstartindex= 0; nstartindex < nsize; nstartindex++) 4 { 5 int nbestindex = nstartindex; 6 7 for (int ncurrentindex = nstartindex + 1; ncurrentindex < nsize; ncurrentindex++) 8 { 9 if (pcomparison(anarray[ncurrentindex], anarray[ nbestindex])) 10 nbestindex = ncurrentindex; 11 } 12 swap(anarray[nstartindex], anarray[nbestindex]); 13 } 14 }

1 bool Ascending(int nx, int ny) 2 { 3 return ny > nx; 4 } 5 6 bool Descending(int nx, int ny) 7 { 8 return ny < nx; 9 } 10 11 void PrintArray(int *parray, int nsize) 12 { 13 for (int iii=0; iii < nsize; iii++) 14 cout << parray[iii] << " "; 15 cout << endl; 16 }

Lecture 18 1 2 3

1 #include <iostream> 2 #define SQUARE(X) X*X 3 // an inline function definition 4 inline double square(double x) { return x * x; } 5 6 int main() 7 { 8 using namespace std; 9 double a, b; 10 double c = 13.0; 11 a = square(5.0); 12 b = square(4.5 + 7.5); // can pass expressions 13 14 cout << "a = " << a << ", b = " << b << "\n"; 15 cout << "c = " << c; 16 cout << ", c squared = " << square(c++) << "\n"; 17 cout << "Now c = " << c << "\n";

1 int e=13.0; 2 3 cout<<square(4.5 + 7.5)<<endl; 4 cout<<square(e++)<<endl; 5 cout<<e<<endl; 6 7 return 0; 8 }

C++ Inline Functions 381 int main() { hubba (2); hubba (4); hubba (10); } void hubba(int n) { for (int i = 0; i < n; i++) cout << "hubba! "; cout << "\n"; } int main() { { n = 2; for (int i = 0; i < n; i++) cout << "hubba! "; cout << "\n"; } { n = 4; for (int i = 0; i < n; i++) cout << "hubba! "; cout << "\n"; } { n = 10; for (int i = 0; i < n; i++) cout << "hubba! "; cout << "\n"; } } A regular function transfers program execution to a separate function. An inline function replaces a function call with inline code. Figure 8.1 s versus regular functions.

Lecture 18 1 2 3

1 #include <iostream> 2 int main() 3 { 4 using namespace std; 5 int rats = 101; 6 int & rodents = rats; // rodents is a reference-toint 7 cout << "rats = " << rats; 8 cout << ", rodents = " << rodents << endl; 9 rodents++; 10 cout << "rats = " << rats; 11 cout << ", rodents = " << rodents << endl; 12 13 cout << "rats address = " << &rats; 14 cout << ", rodents address = " << &rodents << endl; 15 return 0; 16 }

1 #include <iostream> 2 int main() 3 { 4 using namespace std; 5 int rats = 101; 6 int & rodents = rats; // rodents is a reference 7 cout << "rats = " << rats; 8 cout << ", rodents = " << rodents << endl; 9 cout << "rats address = " << &rats; 10 cout << ", rodents address = " << &rodents << endl; 11 int bunnies = 50; 12 rodents = bunnies;// can we change the reference? 13 cout << "bunnies = " << bunnies; 14 cout << ", rats = " << rats; 15 cout << ", rodents = " << rodents <<endl; 16 cout << "bunnies address = " << &bunnies; 17 cout << ", rodents address = " << &rodents << endl; 18 return 0; }