1 CSC103: Introduction to Computer and Programming Lecture No 17.

Slides:



Advertisements
Similar presentations
A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.
Advertisements

UNIT IV.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Chapter 7: Arrays In this chapter, you will learn about
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
One Dimensional Arrays
Programming and Data Structure
Structure.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
CSC Programming for Science Lecture 30: Pointers.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
‘C’ LANGUAGE PRESENTATION.  C language was introduced by Dennis Ritchie..  It is a programming language, which can make a interaction between user and.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
1 CSC103: Introduction to Computer and Programming Lecture No 26.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
1 CSC103: Introduction to Computer and Programming Lecture No 13.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Pointers to Functions In C programming language. Introduction  While many programming languages support the concept of pointers to data, only a few enable.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
1 CSC103: Introduction to Computer and Programming Lecture No 19.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Consultation Hours. Mubashir: – Tuesday from 12:30 to 1:30 with ease of Students. Zohaib – Wedneday b/w 9:30 -10:30 Location: TA Room (next to HOD Office)
Introduction to programming in java Lecture 21 Arrays – Part 1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Windows Programming Lecture 03. Pointers and Arrays.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Lecture 2. Algorithms and Algorithm Convention 1.
User-Written Functions
Arrays in C The c language provides a capability that enables the user to design a set of similar data types called array. Pointers and arrays are.
EKT150 : Computer Programming
C Programming Pointers
Introduction to Pointers
Getting Started With Coding
Presentation transcript:

1 CSC103: Introduction to Computer and Programming Lecture No 17

2 Previous lecture Function call by address or pointer Function return value Use pointers to get results in calling function from called function

3 Today’s lecture outline Recursive functions Introduction to Array Accessing elements of array A simple array program Write a program

4 Recursive function In C, it is possible for the functions to call themselves A function is called ‘recursive’ if a statement within the body of a function calls the same function

5 Example program - factorial Go to program

6 Example program – factorial using recursion Go to program

7 Cont.

8 main() { …. fact = rec(3); printf ( "%d ", fact); } rec ( 3 ) { int f ; if ( 3 == 1 ) return ( 1 ) ; else f = 3 * rec ( ) ; return ( f ) ; } false rec ( 2 ) { int f ; if ( 2 == 1 ) return ( 1 ) ; else f = 2 * rec ( ) ; return ( f ) ; } rec ( 1 ) { int f ; if ( 1 == 1 ) return ( 1 ) ; else f = 2 * rec ( ) ; return ( f ) ; } false true f = 2 * 1; f = 2 f = 3 * 2; f = 6 fact = 6 Go to program

9 Example program 2 Write a definition of a function that adds n integers using recursion and then return the sum. Prototype of the function is below int sum_number(int); – int sum_number(int); Write a program

10 Array Offers a simple way of grouping like variables for easy access It is a group of elements having same data type An array is a collective name given to a group of ‘similar quantities’ Arrays in C share a few common attributes Variables in an array share the same name Variables in an array share the same data type Individual variables in an array are called elements Elements in an array are accessed with an index number

11 Cont. Ordinary variables are capable of holding only one value at a time There are situations in which we would want to store more than one value at a time in a single variable

12 Cont. For example, suppose we want to arrange the percentage marks obtained by 100 students in ascending order In such a case we have two options to store these marks in memory: Declare 100 variables to store percentage marks obtained by 100 different students, i.e. each variable containing marks of single student int m1, m2, m3 ……… m100; Declare one variable (called array or subscripted variable) capable of storing or holding all the hundred values

13 Array declaration Like any other variable, arrays occupy memory space int marks[10]; Array name type size Index of elements in array Memory address of array elements marks [10]

14 How to access array elements int marks[10]; marks[0] = 2; int x; x= 2; marks[1] = 3; scanf(“%d”, &x); scanf (“%d”, &marks[2]) 16 x printf(“x = %d”, x); Output x = 43 printf (“marks [2] = %d”, marks[2]) Output marks [2] = 16

15 Points to remember Array is a collection of elements having same data type Memory allocate to array elements are continuous int marks [10]; Array size must be mentioned in array declaration ( int marks [10]; ) Array index always starts with 0 In 10 elements array, index of first elements is 0 and index of last element is 9 Array element can be access using array index

16 A Simple Program Using Array Write a program that take 10 integer from user and then display those integers Write a program

17 Marks program Go to program

18