Computer Programming

Similar documents
Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

How to declare an array in C?

Computer Programming

Software Pipelining 1/15

Computer Programming

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

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

True or False (14 Points)

ECE G205 Fundamentals of Computer Engineering Fall Exercises in Preparation to the Midterm

Computer Programming

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

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

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 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

UNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.

Lecture 3 Tao Wang 1

Introduction to Programming I COS1511 School of Computing Revision Notes

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

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

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

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

BITG 1113: Array (Part 1) LECTURE 8

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

TWO DIMENSIONAL ARRAY OR MATRIX common name scores Defined as scores[10][5] IE 10 rows x 5 columns showing name of each position; some values below

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

True or False (15 Points)

Functions! Objectives! 1E3! Topic 9! programming! n This topic should allow students to! n Read chapter 6 of the textbook now.!

Review of Important Topics in CS1600. Functions Arrays C-strings

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

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

A SHORT COURSE ON C++

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

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

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

CS 2150 Exam 1, Spring 2018 Page 1 of 6 UVa userid:

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

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

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Math 302 Introduction to Proofs via Number Theory. Robert Jewett (with small modifications by B. Ćurgus)

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

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

A First Program - Greeting.cpp

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

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

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

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

CS222 Homework 4(Solution)

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

Data Structures Through C. Student Workbook

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

CSE030 Fall 2012 Final Exam Friday, December 14, PM

13.4 Case Study: vector<t>-based Matrices

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

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

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

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

(Refer Slide Time: 01.26)

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

Introduction to Algorithms and Programming (COMP151)

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

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)

Lecture Transcript While and Do While Statements in C++

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CS 101 Computer Programming and Utilization. Lecture 5, More Numerical computing (Slides courtesy Prof Ranade)

Data Structures and Algorithms

CSE 100: GRAPH ALGORITHMS

General Instructions. You can use QtSpim simulator to work on these assignments.

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

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

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

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Chapter 6: Arrays. Starting Out with Games and Graphics in C++ Second Edition. by Tony Gaddis

Transcription:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Recap of Arrays and Simultaneous Equations (S31 S35) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

Video Lecture Recap Quiz Arrays and Simultaneous Equations Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 2

Q1) From the following scenario, choose the one which is more appropriate on the use of Array A] to deal with large number of values. B] to get sum of 2 numbers. C] to get average of 3 numbers. D] None of these Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 3

Q2) Let a = {5,9,12,34,56,43}, where a 1 =5 Sum of (a 2 +a 4 +a 6 ) =? A] 21 B] 86 C] 73 D] None of these Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4

Q3) Which of the following properties are true for arrays? A] To store a large number of similar values B] It is a collection of adjacent memory locations C] An index could be used to refer to an individual element. D] All of these Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5

Q4) Choose appropriate answer for arrays in C++? 1. We must declare its name and size 2. Size could be float. A] only 1 is true B] only 2 is true C] Both are true D] Both are false Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6

Q5) In C++, a[5]={2,5,6,3,2}; What will be the value corresponding to a[2*10-3*6]? A] 2 B] 3 C] 6 D] None of these Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 7

Q6) Consider the following code segment: /* Assume marks is an array of type int defined earlier */ int sum = 0; for (int i =0; i < N; i = i +1) { sum = sum + marks[i]; } int a = sum/n; What does a represent? A] Standard deviation B] Average C] sum D] none Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 8

Q7) Which of the following are aspects of practical programming? A] Check if the value given by user is out of bound B] Display error message if value is out of bound C] write explanatory comments D] None of these Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9

Recap Session Arrays and Simultaneous Equations Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 10

Array in C++ Array is a collection of elements of the same type It has a name, chosen by us, and a fixed size (number of elements) Declaring an array int marks[500], roll_numbers[500]; float distances[25]; Only one element participates in an operation Input or output As an operand in an expression As a location on LHS of an assignment statement Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 11

A pictorial view of C++ array A[0] A[1] A[2] A[3] A[4]... A[99] Array A has 100 int elements int A[100]; Suppose it stores 5 values 53, 79, 41, 94, 38 A[0] A[1] A[2] A[3] A[4] = 53 = 79 = 41 = 94 = 38 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 12

Program to find sum of marks of N students int main(){ // program to find the sum of N marks int marks[600], sum = 0, count, N; cin >> N; for (count =0; count < N; count = count +1){ cin >> marks[count]; sum = sum + marks[count]; } cout << sum; return 0; } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13

Multidimensional Array (Matrix) We can declare and use arrays with more than one dimension int A[50][40]; Declares a two dimensional array with 50 rows and 40 columns Each element is accessed by a reference requiring two index expressions, e.g., A[i][j] = 3782; Row index i, can have a value from 0 to 49, Column index j can have a value from 0 to 39 All rules for index expression, apply to index for each dimension Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 14

Simultaneous Equations Matrices are used to represent a system of simultaneous equations in multiple variables Consider the following equations in two variables 2x + 4y = 8 eq.1 4x + 3y = 1 eq.2 These equations can be represented as 2 4 x 8 4 3 y = 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15

Simultaneous Equations The Gaussian elimination technique essentially reduces the coefficient matrix to an upper triangular form: 1 a 01 a 02... a 0n-1 x 0 b 0 0 1 a 12... a 1n-1 x 1 = b 1 0 0 1... 1 x n-1 b n-1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

System in upper triangular form When the coefficient matrix is reduced to the upper triangular form, we have the following system of equations x[0] + a[0][1] x[1] + a[0][2] x[2]+ + a[0][n-1] x[n-1] = b[0] x[1] + a[1][2] x[2]+ + a[1][n-1] x[n-1] = b[1] x[n-1] = b[n-1] Note that values of a[][] and b[] now, will be different from the original values Back substitution can be applied to calculate values of variables Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17

Practice Problem1: Sieve Of Eratosthenes: The Sieve of Eratosthenes is an ancient and famous algorithm to find the prime numbers in the range 2 to n (both 2 and n included). As we know, prime numbers are numbers which are only divisible by one and itself. We use this fact to solve the problem. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Algorithm: 1. We start with a list of consecutive integers from 2 to n: {2,3,4,...,n} (Assume all the elements are unmarked). 2. Mark the lowest unmarked element, p 3. Remove all multiples of the p in the list, except p itself 4. Repeat steps 2 & 3 until all elements are marked. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Algorithm (Contd.) All elements left in list are prime numbers less than n. Examples: Let us say n = 14 Input list: 2 3 4 5 6 7 8 9 10 11 12 13 14 Output list: 2 3 5 7 11 13 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Algorithm (Contd.) Step by step execution: We mark 2 and remove 4,6,8,10,12,14 New list 2,3,5,7,9,11,13 Next we mark 3 and remove 9 New list 2,3,5,7,11,13 Now we mark 5,7,11,13 (nothing to remove as no multiples present) in consecutive steps Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Algorithm (Contd.) Finally our list contains: 2,3,5,7,9,11,13 We got the list of all primes from 2 to 14! Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Code Outline: #include<iostream> using namespace std; int main() { int removed[100]; /*array of size 100 to keep track of all removed numbers 0 = not removed, 1 = removed */ Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Code Outline (Contd.) //code to mark all elements with 0 code_snippet1 for( int i = 2; i < 100; i++) { /* check if i is not removed then remove all multiples of i < 100 except i */ code_snippet2 } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Code Outline (Contd.) //code to output all the primes < 100 code_snippet3 } Fill in the three code snippets Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Practice Problem2: Queue Queue is an ordered list that supports only two operations, namely enqueue and dequeue. Enqueue is an insertion operation that inserts the supplied element at the end of the queue. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Practice Problem2: Queue Dequeue is a deletion operation that deletes the element at the start of the queue. The ends of the queue are called front and rear ends. Insertion happens at the rear end and deletion happens at the front end Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Practice Problem2: Queue (contd ) Initially queue is empty Enqueue(4) 4 Enqueue(5) 4 5 Enqueue(6) 4 5 6 Dequeue() 5 6 Enqueue(8) 5 6 8 Enqueue(9) 5 6 8 9 Dequeue() 6 8 9 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Practice Problem2: Queue (contd ) Question 1: Consider the queue 4, 6, 7, 8, 9 10. Perform the following operations on the queue and check your answers with your TA. Enqueue(11) Enqueue(13) Dequeue() Dequeue() Dequeue() Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, Enqueue(12)

Practice Problem2: Queue (contd ) Question 2 What is the minimum number of operations needed to remove 12 out of the queue? Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Practice Problem2: Queue (contd ) Question 3 Consider the following code which is an array implementation of queues. Consider the array to be of size 10. Consider the array to be global (which means can be accessed from anywhere in the program). Complete the function definitions of the enqueue and dequeue. Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Practice Problem2: Queue (Code Snippet ) #include<iostream> using namespace std; int A[10]; int rear = -1; //stores the rear index int front = -1; //stores the front index Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Practice Problem2: Queue (Code Snippet ) void enqueue(int ele) { /* check if the queue is empty * check if queue if full using conditions on rear and * front(ask your TA if you can't!) * if space available at last insert the element * else shift all elements to start of array & insert the new element */ code_snippet1 } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Practice Problem2: Queue (Code Snippet ) void dequeue() { /* * check if queue is empty * remove the front most element */ code_snippet2 } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Practice Problem2: Queue (Code Snippet ) int main() { //code snippet to create a queue : 1, 4, 6, 7 code_snippet3 return 0; } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,