Fundamentals of Programming Session 14

Similar documents
C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming Session 4

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming Session 15

Fundamentals of Programming Session 12

Programming for Engineers Arrays

Fundamentals of Programming Session 19

Fundamentals of Programming Session 8

C: How to Program. Week /Apr/23

Fundamentals of Programming Session 20

Fundamentals of Programming Session 7

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Fundamentals of Programming Session 25

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

C: How to Program. Week /Mar/05

Fundamentals of Programming Session 20

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Fundamentals of Programming Session 19

C: How to Program. Week /Apr/16

Chapter 2 - Introduction to C Programming

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

C Arrays Pearson Education, Inc. All rights reserved.

Deitel Series Page How To Program Series

Introduction to Programming

Lab 2: Structured Program Development in C

C Arrays. O b j e c t i v e s In this chapter, you ll:

Fundamentals of Programming Session 23

Fundamentals of Programming Session 24

Chapter 1 & 2 Introduction to C Language

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Lecture 04 FUNCTIONS AND ARRAYS

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Functions. Arash Rafiey. September 26, 2017

Introduction to Programming

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Two Dimensional Array - An array with a multiple indexs.

Fundamentals of Programming Session 13

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Fundamentals of Programming Session 9

AMCAT Automata Coding Sample Questions And Answers

Fundamentals of Programming Session 2

C++ PROGRAMMING SKILLS Part 4: Arrays

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Subject: Fundamental of Computer Programming 2068

Two Dimensional Array - An array with a multiple indexs.

Decision Making -Branching. Class Incharge: S. Sasirekha

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

Programming for Engineers Introduction to C

UNIT- 3 Introduction to C++

Unit 3 Decision making, Looping and Arrays

Content. In this chapter, you will learn:

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

C Functions. 5.2 Program Modules in C

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Introduction to C Programming

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

Chapter 2, Part III Arithmetic Operators and Decision Making

ARRAYS(II Unit Part II)

ESC101N: Fundamentals of Computing End-sem st semester

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

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

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Computer Science & Engineering 150A Problem Solving Using Computers

Array. Arrays. Declaring Arrays. Using Arrays

Multiple Choice Questions ( 1 mark)

Write a C program using arrays and structure

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

Introduction to Programming

CSC 270 Survey of Programming Languages

VARIABLES AND CONSTANTS

Arrays and Applications

Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C.

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

Chapter 2, Part I Introduction to C Programming

Outline. 7.1 Introduction. 7.2 Arrays. 7.2 Arrays

Outline Introduction Arrays Declaring Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

Fundamentals of Programming & Procedural Programming


Chapter 3: Arrays and More C Functionality

UNIVERSITY OF WINDSOR Winter 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated : Feb 7 th, Student Name: Student Number:

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Creating a C++ Program

UNIT III (PART-II) & UNIT IV(PART-I)

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Chapter 3 Structure of a C Program

Flow Control. CSC215 Lecture

UNIT 3 FUNCTIONS AND ARRAYS

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Transcription:

Fundamentals of Programming Session 14 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology

Outlines Some Examples of Recursive Functions Arrays Array Examples 2

Q1 Write a program to find sum of first n natural numbers using recursion. 3

A1 int sum(int n); int main() { int num, add; printf ("Enter a positive integer:\n"); scanf ("%d", &num); printf ("sum=%d", sum(num)); } 4 int sum (int n) { if(n==0) return n; else return (n + sum(n-1)); }

Q2 Write a program to take two integer numbers and find their addition using a recursive function. 5

A2 int add(int, int); main() { int a,b; scanf ("%d", &a); scanf ("%d", &b); printf("the value of addition is %d\n", add(a,b)); return 0; } 6 int add(int i, int j) { if(j==0) return(i); else return (1 + add (i, j-1)); }

Q3 Write a program to take the integer number n and calculate using recursion. You cannot use the function sqrt. Moreover, you should compute and show two digits after decimal point. 7

A3 void f1(int,int); int main(){ int i=1; int n; scanf("%d",&n); f1(n,i); getch(); return 0; } 8 void f1(int n,int i) { if((n*10000)-(i*i)<=0) printf("%f", (double)i/100); else f1(n,i+1); }

Arrays 9 Arrays are data structures consisting of related data items of the same type. An array is a group of memory locations related by the fact that they all have the same name and the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array. The first element in every array is the zeroth element. The position number contained within square brackets is more formally called a subscript (or index). If a program uses an expression as a subscript, then the expression is evaluated to determine the subscript. For example, consider the following statement for a = 5 and b = 6 c[ a + b ] += 2;

Arrays 10 Arrays occupy space in memory. To tell the computer to reserve 12 elements for integer array c, use the definition int c[ 12 ]; The following definition int b[ 100 ], x[ 27 ]; reserves 100 elements for integer array b and 27 elements for integer array x. Arrays may contain other data types. Figure 6.3 uses for statements to initialize the elements of a 10-element integer array n to zeros and print the array in tabular format.

11 Array Examples

12 Array Examples

Array Examples The elements of an array can also be initialized when the array is defined by following the definition with an equals sign and braces, {}, containing a commaseparated list of initializers. Figure 6.4 initializes an integer array with 10 values (line 9) and prints the array in tabular format. 13

14 Array Examples

15 Array Examples

Array Examples 16 If there are fewer initializers than elements in the array, the remaining elements are initialized to zero. For example, the elements of the array n in Fig. 6.3 could have been initialized to zero as follows: int n[ 10 ] = { 0 }; It s important to remember that arrays are not automatically initialized to zero. The array definition int n[ 5 ] = { 32, 27, 64, 18, 95, 14 }; causes a syntax error because there are six initializers and only five array elements. If the array size is omitted from a definition with an initializer list, the number of elements in the array will be the number of elements in the initializer list. For example, int n[] = { 1, 2, 3, 4, 5 }; would create a five-element array.

17 Array Examples

18 Array Examples

Array Examples 19 The #define preprocessor directive is introduced in this program. Line 4 #define SIZE 10 defines a symbolic constant SIZE whose value is 10. If the #define preprocessor directive in line 4 is terminated with a semicolon, all occurrences of the symbolic constant SIZE in the program are replaced with the text 10; by the preprocessor. This may lead to syntax errors at compile time, or logic errors at execution time. Figure 6.6 sums the values contained in the 12-element integer array a.

20 Array Examples

Array Examples Our next example uses arrays to summarize the results of data collected in a survey. Consider the problem statement. Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10 (1 means awful and 10 means excellent). Place the 40 responses in an integer array and summarize the results of the poll. This is a typical array application (see Fig. 6.7). 21

22 Array Examples

23 Array Examples

24 Array Examples

Array Examples 25 C has no array bounds checking to prevent the program from referring to an element that does not exist. Thus, an executing program can walk off the end of an array without warning. You should ensure that all array references remain within the bounds of the array. Our next example (Fig. 6.8) reads numbers from an array and graphs the information in the form of a bar chart or histogram each number is printed, then a bar consisting of that many asterisks is printed beside the number.

26 Array Examples

27 Array Examples

Array Examples Roll a single six-sided die 6000 times to test whether the random number generator actually produces random numbers. An array version of this program is shown in Fig. 6.9. 28

29 Array Examples

30 Array Examples

Array Examples 31 We now discuss storing strings in character arrays. So far, the only string-processing capability we have is outputting a string with printf. A string such as "hello" is really a static array of individual characters in C. A character array can be initialized using a string literal. For example, char string1[] = "first"; initializes the elements of array string1 to the individual characters in the string literal "first". In this case, the size of array string1 is determined by the compiler based on the length of the string.

Array Examples 32 The string "first" contains five characters plus a special string-termination character called the null character. Thus, array string1 actually contains six elements. The character constant representing the null character is '\0'. All strings in C end with this character. The preceding definition is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't', '\0' }; Figure 6.10 demonstrates initializing a character array with a string literal, reading a string into a character array, printing a character array as a string and accessing individual characters of a string.

33 Array Examples

34 Array Examples