Programming Language A

Size: px
Start display at page:

Download "Programming Language A"

Transcription

1 Programming Language A Takako Nemoto (JAIST) 12 November Takako Nemoto (JAIST) 12 November 1 / 25

2 From Quiz 5 // A program to print the double of the input. int no printf("input an integer: "); scanf("%d", &no); printf("the double of it is %d.", 2 * no); quiz5-1.c: In function 'main': quiz5-1.c:7:5: error: expected '=', ',', ';', 'asm' or ' attribute ' before 'printf' printf("input an integer: "); ^~~~~~ quiz5-1.c:8:18: error: 'no' undeclared (first use in this function) scanf("%d", &no); ^~ quiz5-1.c:8:18: note: each undeclared identifier is reported only once for each function it appears in Takako Nemoto (JAIST) 12 November 2 / 25

3 From Quiz 5 // A program determines the input is even or not. int no; printf("input an integer: "); scanf("%d", &no); if(no % 2 = 0) printf("it is even."); else printf("it is odd."); quiz5-2.c: In function 'main': quiz5-2.c:7:11: error: lvalue required as left operand of assignment if(no % 2 = 0) ^ Takako Nemoto (JAIST) 12 November 3 / 25

4 Array: For multiple data to refer later int i; int x[5]; for (i = 0; i < 5; i++){ printf("x[%d]: ", i); scanf("%d", &x[i]); for (i = 0; i < 3; i++){ int temp = x[i]; x[i] = x[4-i]; x[4-i] = temp; x[0]: 1 x[1]: 2 x[2]: 3 x[3]: 4 x[4]: 5 Reverse them! x[0]: 5 x[1]: 4 x[2]: 3 x[3]: 2 x[4]: 1 puts("reverse them!"); for (i = 0; i < 5; i++){ printf("x[%d] = %d\n", i, x[i]); Takako Nemoto (JAIST) 12 November 4 / 25

5 Array: For multiple data to refer later int i; int x[5]; for (i = 0; i < 5; i++){ printf("x[%d]: ", i); scanf("%d", &x[i]); for (i = 0; i < 3; i++){ int temp = x[i]; x[i] = x[4-i]; x[4-i] = temp; puts("reverse them!"); for (i = 0; i < 5; i++){ printf("x[%d] = %d\n", i, x[i]); int x[5]; declares that x is an array with 5 elements of type int. By the 1st for statement, we can input and assign the value of each element of x. The 2nd for statement reverse the each element of x. x[4-i] x[i] temp Takako Nemoto (JAIST) 12 November 5 / 25

6 Object-like macro: For a versatile program #define NUMBER 5 int i; int x[number]; for (i = 0; i < NUMBER / 2 + 1; i++){ printf("x[%d]: ", i); scanf("%d", &x[i]); for (i = 0; i < NUMBER /2 + 1; i++){ int temp = x[i]; x[i] = x[number - (i + 1)]; x[number - (i + 1)] = temp; x[0]: 1 x[1]: 2 x[2]: 3 x[3]: 4 x[4]: 5 Reverse them! x[0]: 5 x[1]: 4 x[2]: 3 x[3]: 2 x[4]: 1 puts("reverse them!"); for (i = 0; i < NUMBER; i++){ printf("x[%d] = %d\n", i, x[i]); Takako Nemoto (JAIST) 12 November 6 / 25

7 Object-like macro: For a versatile program #define NUMBER 5 int i; int x[number]; for (i = 0; i < NUMBER; i++){ printf("x[%d]: ", i); scanf("%d", &x[i]); for (i = 0; i < NUMBER /2; i++){ int temp = x[i]; x[i] = x[number - (i +1)]; x[number - (i + 1)] = temp; puts("reverse them!"); for (i = 0; i < NUMBER; i++){ printf("x[%d] = %d\n", i, x[i]); #define NUMBER 5 means In the following, replace NUMBER with 5 It is easy to change the value of NUMBER in this style. This is called object-like macro. NUMBER is called macro name, which usually named with capital letters. Takako Nemoto (JAIST) 12 November 7 / 25

8 Tips: Assignment of arrays The following 3 programs give the same results: int v[5]; v[0] = 1; v[1] = 2; v[2] = 3; v[3] = 4; v[4] = 5; v[0]=1 v[1]=2 v[2]=3 v[3]=4 v[4]=5 int i; for (i = 0; i < 5; i++){ printf("v[%d]=%d\n", i, v[i]); Takako Nemoto (JAIST) 12 November 8 / 25

9 Tips: Assignment of arrays The following 3 programs give the same results: int v[5] = {1, 2, 3, 4, 5; int i; for (i = 0; i < 5; i++){ printf("v[%d]=%d\n", i, v[i]); v[0]=1 v[1]=2 v[2]=3 v[3]=4 v[4]=5 Takako Nemoto (JAIST) 12 November 9 / 25

10 Tips: Assignment of arrays The following 3 programs give the same results: int v[5]; int i; for (i = 0; i < 5; i++){ v[i] = i+1; for (i = 0; i < 5; i++){ printf("v[%d]=%d\n", i, v[i]); v[0]=1 v[1]=2 v[2]=3 v[3]=4 v[4]=5 Takako Nemoto (JAIST) 12 November 10 / 25

11 Tips: Assignment of arrays If { has less elements: int v[5] = {1; int i; for (i = 0; i < 5; i++){ printf("v[%d]=%d\n", i, v[i]); v[0]=1 v[1]=0 v[2]=0 v[3]=0 v[4]=0 Takako Nemoto (JAIST) 12 November 11 / 25

12 Tips: Copying an array Note that a = b does not work. int a[5] = {1, 2, 3, 4, 5; int b[5]; int i; for (i = 0; i < 5; i++){ b[i] = a[i]; for (i = 0; i < 5; i++){ printf("a[%d]=%d, b[%d] = %d\n", i, a[i], i, b[i]); a[0]=1, b[0]=1 a[1]=2, b[1]=2 a[2]=3, b[2]=3 a[3]=4, b[3]=4 a[4]=5, b[4]=5 Takako Nemoto (JAIST) 12 November 12 / 25

13 #define NUMBER 80 int i,j; int num; int score[number]; int dist[11] = {0; printf("input the number of the students: "); do{ scanf("%d", &num); if (num < 1 num > NUMBER){ printf("input a number between 1 and %d: ", NUMBER); while (num < 1 num > NUMBER); for (i = 0; i < num; i++){ printf("student No.%d: ", i+1); do{ scanf("%d", &score[i]); if (score[i] < < score[i]){ printf("the score should be between 0 and 100: \n"); while (score[i] < < score[i]); dist[score[i] / 10]++; puts("\n---distribution---"); printf(" 100: "); for (j = 0; j < dist[10]; j++){ putchar('*'); putchar('\n'); for (i = 9; i >= 0; i--){ printf("%3d-%3d: ", 10 * i, 10 * i + 9); for (j = 0; j < dist[i]; j++){ putchar('*'); putchar('\n'); Input the number of the students: 10 Student No.1: 20 Student No.2: 59 Student No.3: 38 Student No.4: 49 Student No.5: 39 Student No.6: 29 Student No.7: 50 Student No.8: 97 Student No.9: 69 Student No.10: Distribution : 90-99: ** 80-89: 70-79: 60-69: * 50-59: ** 40-49: * 30-39: ** 20-29: ** 10-19: 0-9: Takako Nemoto (JAIST) 12 November 13 / 25

14 #define NUMBER 80 int i,j; int num; int score[number]; int dist[11] = {0; printf("input the number of the students: "); do{ scanf("%d", &num); if (num < 1 num > NUMBER){ printf("input a number between 1 and %d: ", NUMBER); while (num < 1 num > NUMBER); for (i = 0; i < num; i++){ printf("student No.%d: ", i+1); do{ scanf("%d", &score[i]); if (score[i] < < score[i]){ printf("the score should be between 0 and 100: \n"); while (score[i] < < score[i]); dist[score[i] / 10]++; puts("\n---distribution---"); printf(" 100: "); for (j = 0; j < dist[10]; j++){ putchar('*'); putchar('\n'); for (i = 9; i >= 0; i--){ printf("%3d-%3d: ", 10 * i, 10 * i + 9); for (j = 0; j < dist[i]; j++){ putchar('*'); putchar('\n'); NUMBER is the upper bound of the number of data. If the value of score[i] is 0-9, then score[i] / 10 is 0 and so the value of dist[0] is incremented by 1. If the value of score[i] is 10-19, then score[i] / 10 is 1 and so the value of dist[1] is incremented by 1. Takako Nemoto (JAIST) 12 November 14 / 25

15 Multidimensional array int a[2][3]; int i, j; for (i = 0; i < 2; i++){ for (j = 0; j < 3; j++){ printf("input the (%d, %d) element: ", i+1, j+1); scanf("%d", &a[i][j]); for (i = 0; i < 2; i++){ for (j = 0; j < 3; j++){ printf("%d ", a[i][j]); printf("\n"); Input the (1, 1) element: 1 Input the (1, 2) element: 2 Input the (1, 3) element: 3 Input the (2, 1) element: 4 Input the (2, 2) element: 5 Input the (2, 3) element: Takako Nemoto (JAIST) 12 November 15 / 25

16 Function int max2(int a, int b){ if (a > b) return a; else return b; int n1, n2; puts("input 2 integers."); printf("integer 1: "); scanf("%d", &n1); printf("integer 2: "); scanf("%d", &n2); printf("the bigger number is %d.", max2(n1, n2)); Takako Nemoto (JAIST) 12 November 16 / 25

17 Structure of a function int max2(int a, int b){ if (a > b) return a; else return b; Function header: int max2(int a, int b) The 1st int is the return type, which is the type of return value of the function. max2 is the function name. Inside ( ) is the parameter type list. In this case, max2 has 2 arguments of type int. Function body: Inside { If a > b, then the return value is the value of a. Else, then the return value is the value of b. Takako Nemoto (JAIST) 12 November 17 / 25

18 Function call expression printf("the bigger number is %d.", max2(n1, n2)); It calls the function max2, giving the value of n1 and n2 as the arguments. Takako Nemoto (JAIST) 12 November 18 / 25

19 Example int sqr(int x){ return x * x; int diff(int a, int b){ return (a > b)? a - b : b - a; int x, y; puts("input 2 integers."); printf("integer x: "); scanf("%d", &x); printf("integer y: "); scanf("%d", &y); printf("the difference of x^2 and y^2 is %d.", diff(sqr(x), sqr(y))); Takako Nemoto (JAIST) 12 November 19 / 25

20 Example int max2(int a, int b){ if (a > b) return a; else return b; int n1, n2, n3, n4; puts("input 2 integers. "); printf("integer 1: "); scanf("%d", &n1); printf("integer 2: "); scanf("%d", &n2); printf("integer 3: "); scanf("%d", &n3); printf("integer 4: "); scanf("%d", &n4); printf("the bigger number is %d.", max2(max2(n1, n2), max2(n3,n4))); Takako Nemoto (JAIST) 12 November 20 / 25

21 Example void put_star(int n){ while (n-- > 0) putchar('*'); int i, len; puts("let's draw an isosceles triangle."); printf("length of the shorter edge?: "); scanf("%d", &len); for (i = 1; i<= len; i++){ put_star(i); putchar('\n'); Takako Nemoto (JAIST) 12 November 21 / 25

22 Example int scan_pint(void){ int temp; do{ printf("input a positive integer: "); scanf("%d", &temp); if (temp < 0) puts("do not input any non-positive integer."); while(temp < 0); return temp; int rev_int(int num){ int temp = 0; while (num > 0){ temp = temp * 10 + num % 10; num /= 10; return temp; int nx = scan_pint(); printf("the inversed value is %d.\n", rev_int(nx)); Takako Nemoto (JAIST) 12 November 22 / 25

23 Today s homework 1 Write a program in C s.t., for input data (max 50), it draw a histogram as follows: Input the number of the data: 7 Input data No.1: 5 Input data No.2: 3 Input data No.3: 5 Input data No.4: 4 Input data No.5: 6 Input data No.6: 2 Input data No.7: 7 1 ***** 2 *** 3 ***** 4 **** 5 ****** 6 ** 7 ******* Takako Nemoto (JAIST) 12 November 23 / 25

24 Today s homework 2 (optional) Modify the program in 1 to draw a vertical diagram: Input the number of data: 7 Input data No.1: 3 Input data No.2: 5 Input data No.3: 3 Input data No.4: 6 Input data No.5: 4 Input data No.6: 6 Input data No.7: 3 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Takako Nemoto (JAIST) 12 November 24 / 25

25 Today s homework 3 Write a program to calculate, for inputs of a 4 3 matrix and a 3 4 matrix, the product of them. As usual, send the programs as attached files to me via , with the title Homework Lecture6 by the next lecture. Please make sure to include your name and student ID number. Takako Nemoto (JAIST) 12 November 25 / 25

Programming Language A

Programming Language A Programming Language A Takako Nemoto (JAIST) 22 October Takako Nemoto (JAIST) 22 October 1 / 28 From Homework 2 Homework 2 1 Write a program calculate something with at least two integer-valued inputs,

More information

Programming Language B

Programming Language B Programming Language B Takako Nemoto (JAIST) 7 January Takako Nemoto (JAIST) 7 January 1 / 13 Usage of pointers #include int sato = 178; int sanaka = 175; int masaki = 179; int *isako, *hiroko;

More information

Programming Language A

Programming Language A Programming Language A Takako Nemoto (JAIST) 30 October Takako Nemoto (JAIST) 30 October 1 / 29 From Homework 3 Homework 3 1 Write a program to convert the input Celsius degree temperature into Fahrenheight

More information

Programming Language B

Programming Language B Programming Language B Takako Nemoto (JAIST) 17 December Takako Nemoto (JAIST) 17 December 1 / 17 A tip for the last homework 1 Do Exercise 9-5 ( 9-5) in p.249 (in the latest edtion). The type of the second

More information

Programming Language B

Programming Language B Programming Language B Takako Nemoto (JAIST) 3 December Takako Nemoto (JAIST) 3 December 1 / 18 Today s topics 1 Function-like macro 2 Sorting 3 Enumeration 4 Recursive definition of functions 5 Input/output

More information

Programming Language B

Programming Language B Programming Language B Takako Nemoto (JAIST) 16 January Takako Nemoto (JAIST) 16 January 1 / 15 Strings and pointers #include //11-1.c char str[] = "ABC"; char *ptr = "123"; printf("str = \"%s\"\n",

More information

Programming Language B

Programming Language B Programming Language B Takako Nemoto (JAIST) 10 December Takako Nemoto (JAIST) 10 December 1 / 15 Strings A string literal is a strings of characters included by double quotations. Examples String literals

More information

Programming Language A

Programming Language A Programming Language A Takako Nemoto (JAIST) 26 November Takako Nemoto (JAIST) 26 November 1 / 12 Type char char is a datatype for characters. char express integers in certain region. For each ASCII character,

More information

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60) INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Date:.FN / AN Time: 2 hrs Full marks: 60 No. of students: 643 Spring Mid Semester Exams, 2011 Dept: Comp. Sc & Engg. Sub No: CS11001 B.Tech 1 st Year (Core) Sub

More information

Fundamentals of Programming. Lecture 14 Hamed Rasifard

Fundamentals of Programming. Lecture 14 Hamed Rasifard Fundamentals of Programming Lecture 14 Hamed Rasifard 1 Outline Two-Dimensional Array Passing Two-Dimensional Arrays to a Function Arrays of Strings Multidimensional Arrays Pointers 2 Two-Dimensional Array

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 10: Arrays Readings: Chapter 9 Introduction Group of same type of variables that have same

More information

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60) INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Date:.FN / AN Time: 2 hrs Full marks: 60 No. of students: 643 Spring Mid Semester Exams, 2011 Dept: Comp. Sc & Engg. Sub No: CS11001 B.Tech 1 st Year (Core) Sub

More information

CSCI 2132 Software Development. Lecture 19: Generating Permutations

CSCI 2132 Software Development. Lecture 19: Generating Permutations CSCI 2132 Software Development Lecture 19: Generating Permutations Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 19-Oct-2018 (19) CSCI 2132 1 Previous Lecture Mergesort implementation

More information

C Programming Language

C Programming Language C Programming Language Arrays & Pointers I Dr. Manar Mohaisen Office: F208 Email: manar.subhi@kut.ac.kr Department of EECE Review of Precedent Class Explain How to Create Simple Functions Department of

More information

BSM540 Basics of C Language

BSM540 Basics of C Language BSM540 Basics of C Language Chapter 9: Functions I Prof. Manar Mohaisen Department of EEC Engineering Review of the Precedent Lecture Introduce the switch and goto statements Introduce the arrays in C

More information

only in the space provided. Do the rough work in the space provided for it. The question paper has total 12 pages.

only in the space provided. Do the rough work in the space provided for it. The question paper has total 12 pages. Instructions: Answer all five questions. Total marks = 10 x 2 + 4 x 10 = 60. Time = 2hrs. Write your answer only in the space provided. Do the rough work in the space provided for it. The question paper

More information

Functions Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay

Functions Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay Functions 60-141 Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay Motivation A complex program Approximate Ellipse Demo Ellipse2DDouble.java

More information

Programming Language B

Programming Language B Programming Language B Takako Nemoto (JAIST) 28 January Takako Nemoto (JAIST) 28 January 1 / 20 Today s quiz The following are program to print each member of the struct Student type object abe. Fix the

More information

Worksheet 11 Multi Dimensional Array and String

Worksheet 11 Multi Dimensional Array and String Name: Student ID: Date: Worksheet 11 Multi Dimensional Array and String Objectives After completing this worksheet, you should be able to Declare multi dimensional array of elements and string variables

More information

Chapter 7 Solved problems

Chapter 7 Solved problems Chapter 7 7.4, Section D 20. The coefficients of a polynomial function are stored in a single-dimensional array. Display its derivative. If the polynomial function is p(x) = a n x n + a n-1 x n-1 + + a

More information

COL 100 Introduction to Programming- MINOR 1 IIT Jammu

COL 100 Introduction to Programming- MINOR 1 IIT Jammu COL 100 Introduction to Programming- MINOR 1 IIT Jammu Time 1 Hr Max Marks 40 03.09.2016 NOTE: THERE 4 QUESTIONS IN ALL NOTE: 1. Do all the questions. 2. Write your name, entry number and group in all

More information

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size] (November 10, 2009 2.1 ) Arrays An array is a collection of several elements of the same type. An array variable is declared as type array name[size] I The elements are numbered as 0, 1, 2... size-1 I

More information

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 Name :. Roll No. :..... Invigilator s Signature :.. 2011 INTRODUCTION TO PROGRAMMING Time Allotted : 3 Hours Full Marks : 70 The figures in the margin indicate full marks. Candidates are required to give

More information

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways! Exercise 3 / Ch.7 Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways! Exercise 3 / Ch.8 Given the following array, write code

More information

Dynamic Allocation of Memory Space

Dynamic Allocation of Memory Space C Programming 1 Dynamic Allocation of Memory Space C Programming 2 Run-Time Allocation of Space The volume of data may not be known before the run-time. It provides flexibility in building data structures.

More information

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook Chapter 2 Section 2.5 while Loop CS 50 Hathairat Rattanasook Loop Iteration means executing a code segment more than once. A loop is an iterative construct. It executes a statement 0..n times while a condition

More information

Computer Programming: Skills & Concepts (CP) Variables and ints

Computer Programming: Skills & Concepts (CP) Variables and ints CP Lect 3 slide 1 25 September 2017 Computer Programming: Skills & Concepts (CP) Variables and ints C. Alexandru 25 September 2017 CP Lect 3 slide 2 25 September 2017 Week 1 Lectures Structure of the CP

More information

ESC101N: Fundamentals of Computing End-sem st semester

ESC101N: Fundamentals of Computing End-sem st semester ESC101N: Fundamentals of Computing End-sem 2010-11 1st semester Instructor: Arnab Bhattacharya 8:00-11:00am, 15th November, 2010 Instructions 1. Please write your name, roll number and section below. 2.

More information

'C' Programming Language

'C' Programming Language F.Y. Diploma : Sem. II [DE/EJ/ET/EN/EX] 'C' Programming Language Time: 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1(a) Define pointer. Write syntax

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 8 Array typical problems, Search, Sorting Department of Computer Engineering Outline

More information

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock) C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 By the end of this lecture, you will be able to identify the

More information

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 1 By the end of this lecture, you will be able to identify

More information

Structured programming

Structured programming Exercises 6 Version 1.0, 25 October, 2016 Table of Contents 1. Arrays []................................................................... 1 1.1. Declaring arrays.........................................................

More information

Goals of this Lecture

Goals of this Lecture C Pointers Goals of this Lecture Help you learn about: Pointers and application Pointer variables Operators & relation to arrays 2 Pointer Variables The first step in understanding pointers is visualizing

More information

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

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size]; Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data

More information

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed This homework assignment focuses primarily on some of the basic syntax and semantics of C. The answers to the following questions can be determined by consulting a C language reference and/or writing short

More information

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number: UNIVERSITY OF WINDSOR 60-106-01 Fall 2007 QUIZ # 2 Solution Examiner : Ritu Chaturvedi Dated :November 27th, 2007. Student Name: Student Number: INSTRUCTIONS (Please Read Carefully) No calculators allowed.

More information

Programming & Data Structure Laboratory. Day 2, July 24, 2014

Programming & Data Structure Laboratory. Day 2, July 24, 2014 Programming & Data Structure Laboratory Day 2, July 24, 2014 Loops Pre and post test loops for while do-while switch-case Pre-test loop and post-test loop Condition checking True Loop Body False Loop Body

More information

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input Loops / Repetition Statements Repetition s allow us to execute a multiple times Often they are referred to as loops C has three kinds of repetition s: the while loop the for loop the do loop The programmer

More information

Structured programming

Structured programming Exercises 9 Version 1.0, 13 December, 2016 Table of Contents 1. Remainders from lectures.................................................... 1 1.1. What is a pointer?.......................................................

More information

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10 EECE.2160: ECE Application Programming Spring 2018 Exam 2 March 30, 2018 Name: Lecture time (circle 1): 8-8:50 (Sec. 201) 12-12:50 (Sec. 202) For this exam, you may use only one 8.5 x 11 double-sided page

More information

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example: Collecting Elements How to collect elements of the same type? Eg:., marks on assignments: APS105 Arrays Textbook Chapters 6.1-6.3 Assn# 1 2 3 4 5 6 Mark 87 89 77 96 87 79 Eg: a solution in math: x 1, x

More information

Arrays. CSE 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures. Arrays

Arrays. CSE 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures. Arrays CSE 142 Programming I Chapter 8 8.1 Declaration and Referencing 8.2 Subscripts 8.3 Loop through arrays Arrays 8.4 & 8.5 Arrays arguments and parameters 8.6 Example 8.7 Multi-Dimensional Arrays 2000 UW

More information

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler) 1 Arrays General Questions 1. What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array? A. The element will be set to 0. B. The compiler would

More information

Chapter 8. Arrays, Addresses, and Pointers : Structured Programming Structured Programming 1

Chapter 8. Arrays, Addresses, and Pointers : Structured Programming Structured Programming 1 Chapter 8 Arrays, Addresses, and Pointers 204112: Structured Programming 204112 Structured Programming 1 Pointer Pointer is a variable that contains an address. If num_ptr is a pointer, *num_ptr means

More information

Arrays. CSE / ENGR 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures.

Arrays. CSE / ENGR 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures. CSE / ENGR 142 Programming I Arrays Chapter 8 8.1 Declaration and Referencing 8.2 Subscripts 8.3 Loop through arrays 8.4 & 8.5 Arrays arguments and parameters 8.6 Example 8.7 Multi-Dimensional Arrays 1998

More information

Operators And Expressions

Operators And Expressions Operators And Expressions Operators Arithmetic Operators Relational and Logical Operators Special Operators Arithmetic Operators Operator Action Subtraction, also unary minus + Addition * Multiplication

More information

Lesson 7. Reading and Writing a.k.a. Input and Output

Lesson 7. Reading and Writing a.k.a. Input and Output Lesson 7 Reading and Writing a.k.a. Input and Output Escape sequences for printf strings Source: http://en.wikipedia.org/wiki/escape_sequences_in_c Escape sequences for printf strings Why do we need escape

More information

About Codefrux While the current trends around the world are based on the internet, mobile and its applications, we try to make the most out of it. As for us, we are a well established IT professionals

More information

What we have learned so far

What we have learned so far What we have learned so far Straight forward statements Conditional statements (branching) Repeated statements (loop) Arrays One variable many data Problem: Read 10 numbers from the keyboard and store

More information

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions Lecture 02 Summary C/Java Syntax Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 1 2 By the end of this lecture, you will be able to identify the

More information

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE 1. Write a C program to perform addition, subtraction, multiplication and division of two numbers. # include # include int a, b,sum,

More information

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C Sample Test Paper-I Marks : 25 Time:1 Hrs. Q1. Attempt any THREE 09 Marks a) State four relational operators with meaning. b) State the use of break statement. c) What is constant? Give any two examples.

More information

(2 7 *0) + (2 6 *1) + (2 5 *1) + (2 4 *0) + (2 3 *1) + (2 2 *1)+(2 1 *0)+(2 0 *1)

(2 7 *0) + (2 6 *1) + (2 5 *1) + (2 4 *0) + (2 3 *1) + (2 2 *1)+(2 1 *0)+(2 0 *1) BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER 2017-2018 COURSE : COMPUTER PROGRAMMING (CS F111) COMPONENT : Tutorial# 1 SOLUTIONS DATE : 27-AUG-2017 Q1. Represent the

More information

Programming Language B

Programming Language B Programming Language B Takako Nemoto (JAIST) 21 January Takako Nemoto (JAIST) 21 January 1 / 18 Today s quiz The following is a which returns 0 if s1 and s2 are same, a negative number if s1 is prior to

More information

CS 223: Data Structures and Programming Techniques. Exam 2

CS 223: Data Structures and Programming Techniques. Exam 2 CS 223: Data Structures and Programming Techniques. Exam 2 Instructor: Jim Aspnes Work alone. Do not use any notes or books. You have approximately 75 minutes to complete this exam. Please write your answers

More information

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen Programming Fundamentals for Engineers - 0702113 6. Arrays Yacoub Sabatin Muntaser Abulafi Omar Qaraeen 1 One-Dimensional Arrays There are times when we need to store a complete list of numbers or other

More information

Programming for Electrical and Computer Engineers. Pointers and Arrays

Programming for Electrical and Computer Engineers. Pointers and Arrays Programming for Electrical and Computer Engineers Pointers and Arrays Dr. D. J. Jackson Lecture 12-1 Introduction C allows us to perform arithmetic addition and subtraction on pointers to array elements.

More information

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2 CMPT 125 Arrays Arrays and pointers Loops and performance Array comparison Strings John Edgar 2 Python a sequence of data access elements with [index] index from [0] to [len-1] dynamic length heterogeneous

More information

Write a C program using arrays and structure

Write a C program using arrays and structure 03 Arrays and Structutes 3.1 Arrays Declaration and initialization of one dimensional, two dimensional and character arrays, accessing array elements. (10M) 3.2 Declaration and initialization of string

More information

CSCI 2132 Software Development. Lecture 18: Functions

CSCI 2132 Software Development. Lecture 18: Functions CSCI 2132 Software Development Lecture 18: Functions Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 18-Oct-2017 (18) CSCI 2132 1 Previous Lecture Example: binary search Multidimensional

More information

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING APS 105 Computer Fundamentals Midterm Examination October 20, 2011 6:15 p.m. 8:00 p.m. (105 minutes) Examiners: J. Anderson, T. Fairgrieve,

More information

Week 2 / Lecture 2 15 March 2017 NWEN 241 Control constructs, Functions. Alvin Valera

Week 2 / Lecture 2 15 March 2017 NWEN 241 Control constructs, Functions. Alvin Valera Week 2 / Lecture 2 15 March 2017 NWEN 241 Control constructs, Functions Alvin Valera School of Engineering and Computer Science Victoria University of Wellington Admin stuff Tutorial #2 Expectations on

More information

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

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 Unti 4: C Arrays Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type An array is used to store a collection of data, but it is often more useful

More information

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009 Lecture 5: Multidimensional Arrays CS209 : Algorithms and Scientific Computing Wednesday, 11 February 2009 CS209 Lecture 5: Multidimensional Arrays 1/20 In today lecture... 1 Let s recall... 2 Multidimensional

More information

Loops / Repetition Statements

Loops / Repetition Statements Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

More information

Programming and Data Structures Mid-Semester - Solutions to Sample Questions Dept. of Computer Science and Engg. IIT Kharagpur Spring

Programming and Data Structures Mid-Semester - Solutions to Sample Questions Dept. of Computer Science and Engg. IIT Kharagpur Spring Programming and Data Structures Mid-Semester - s to Sample Questions Dept. of Computer Science and Engg. IIT Kharagpur Spring 2015-16 February 15, 2016 1. Tick the correct options. (a) Consider the following

More information

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

CSCI 2132 Software Development. Lecture 17: Functions and Recursion CSCI 2132 Software Development Lecture 17: Functions and Recursion Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 15-Oct-2018 (17) CSCI 2132 1 Previous Lecture Example: binary

More information

CS 101, Spring 2016 March 22nd Exam 2

CS 101, Spring 2016 March 22nd Exam 2 CS 101, Spring 2016 March 22nd Exam 2 Name: Question 1. [3 points] Which of the following loop statements would most likely cause the loop to execute exactly n times? You may assume that n will be set

More information

UIC. C Programming Primer. Bharathidasan University

UIC. C Programming Primer. Bharathidasan University C Programming Primer UIC C Programming Primer Bharathidasan University Contents Getting Started 02 Basic Concepts. 02 Variables, Data types and Constants...03 Control Statements and Loops 05 Expressions

More information

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables Chapter 11 Pointers The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into bytes, with each byte capable

More information

QUIZ on Ch.8. What is kit?

QUIZ on Ch.8. What is kit? QUIZ on Ch.8 What is kit? QUIZ on Ch.8 What is kit? A: A vector of nested structures! QUIZ on Ch.8 C crash-course Compilation and Execution of a C Program Run-time Interpreted languages (like MATLAB, Python,

More information

{C} Programming. Part 1/2 Basics Variables, Conditions, Loops, Arrays, Pointer basics

{C} Programming. Part 1/2 Basics Variables, Conditions, Loops, Arrays, Pointer basics {C Programming Part 1/2 Basics Variables, Conditions, Loops, Arrays, Pointer basics Variables A variable is a container (storage area) to hold data. Eg. Variable name int potionstrength = 95; Variable

More information

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

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering 2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 1 Introduction to C Language ALQUDS University Department of Computer Engineering Objective: Our objective for today s lab session is

More information

Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1.

Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1. Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations 4th October, 2010 Integer arithmetic in C. Converting pre-decimal money to decimal. The

More information

COMP 2001/2401 Test #1 [out of 80 marks]

COMP 2001/2401 Test #1 [out of 80 marks] COMP 2001/2401 Test #1 [out of 80 marks] Duration: 90 minutes Authorized Memoranda: NONE Note: for all questions, you must show your work! Name: Student#: 1. What exact shell command would you use to:

More information

Structured Programming. Functions and Structured Programming. Functions. Variables

Structured Programming. Functions and Structured Programming. Functions. Variables Structured Programming Functions and Structured Programming Structured programming is a problem-solving strategy and a programming methodology. The construction of a program should embody topdown design

More information

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

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation. Lab 4 Functions Introduction: A function : is a collection of statements that are grouped together to perform an operation. The following is its format: type name ( parameter1, parameter2,...) { statements

More information

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17 Grade Distribution Exam 1 Exam 2 Score # of Students Score # of Students 16 4 14 6 12 4 10 2 8 1 Total: 17 Exams 1 & 2 14 2 12 4 10 5 8 5 4 1 Total: 17 Score # of Students 28 2 26 5 24 1 22 4 20 3 18 2

More information

Array1.c. all arrays start at 0 sometimes known as a list

Array1.c. all arrays start at 0 sometimes known as a list Learning Outcomes 1) EXPLAIN AND APPLY THE CONCEPT OF A PROCEDURAL PROGRAMMING PARADIGM AND APPLY THE ASSOCIATED DESIGN PRINCIPLES TO A RANGE OF PROBLEM SOLUTIONS 2) BE ABLE TO DESIGN, IMPLEMENT, AND DOCUMENT

More information

Special PRG Lecture No. 2. Professor David Brailsford Special PRG Lecture: Arrays

Special PRG Lecture No. 2. Professor David Brailsford Special PRG Lecture: Arrays Special PRG Lecture No. 2 Professor David Brailsford (dfb@cs.nott.ac.uk) School of Computer Science University of Nottingham Special PRG Lecture: Arrays Page 1 The need for arrays Suppose we want to write

More information

Assignment 1 Clarifications

Assignment 1 Clarifications Assignment 1 Clarifications Q1b: State whether having the code snippet in your code will cause compilation errors. Explain why or why not. Q3: For the code snippets, how many times is the printf statement

More information

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013 Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013 One Dimensional Q1: Write a program that declares two arrays of integers and fills them from the user. Then exchanges their values and display the

More information

ARRAYS(II Unit Part II)

ARRAYS(II Unit Part II) ARRAYS(II Unit Part II) Array: An array is a collection of two or more adjacent cells of similar type. Each cell in an array is called as array element. Each array should be identified with a meaningful

More information

Q1: Multiple choice / 20 Q2: Arrays / 40 Q3: Functions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Q1: Multiple choice / 20 Q2: Arrays / 40 Q3: Functions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10 EECE.2160: ECE Application Programming Spring 2017 Exam 2 March 29, 2017 Name: Section (circle 1): 201 (Dr. Li, MWF 8-8:50) 202 (Dr. Geiger, MWF 12-12:50) For this exam, you may use only one 8.5 x 11 double-sided

More information

Answer all questions. Write your answers only in the space provided. Full marks = 50

Answer all questions. Write your answers only in the space provided. Full marks = 50 Answer all questions. Write your answers only in the space provided. Full marks = 50 1. Answer the following: [2+3+2+1=8 marks] a) What are the minimum and maximum numbers that can be represented in 10-bit

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A

More information

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 I/O in C Standard C Library I/O commands are not included as part of the C language. Instead, they are part of the Standard C Library. A collection of functions and macros that must be implemented

More information

Functions. Arash Rafiey. September 26, 2017

Functions. Arash Rafiey. September 26, 2017 September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic

More information

Principles of C and Memory Management

Principles of C and Memory Management COMP281 Lecture 8 Principles of C and Memory Management Dr Lei Shi Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic, Functions Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic,

More information

CSCI 2132 Software Development. Lecture 20: Program Organization

CSCI 2132 Software Development. Lecture 20: Program Organization CSCI 232 Software Development Lecture 20: Program Organization Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 22-Oct-208 (20) CSCI 232 Previous Lecture Generating permutations

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using

More information

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04 Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Topic: Arrays and Strings Practice Sheet #04 Date: 24-01-2017 Instructions: For the questions consisting code segments,

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

EXAMINATION FOR THE BSC (HONS) INFORMATION SYSTEMS; BSC (HONS) INFORMATION TECHNOLOGY & BSC (HONS) COMPUTER SCIENCE; YEAR 1

EXAMINATION FOR THE BSC (HONS) INFORMATION SYSTEMS; BSC (HONS) INFORMATION TECHNOLOGY & BSC (HONS) COMPUTER SCIENCE; YEAR 1 FACULTY OF SCIENCE AND TECHNOLOGY EXAMINATION FOR THE BSC (HONS) INFORMATION SYSTEMS; BSC (HONS) INFORMATION TECHNOLOGY & BSC (HONS) COMPUTER SCIENCE; YEAR 1 ACADEMIC SESSION 2014; SEMESTER 1 & 2 FINAL

More information

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

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh Remember! Practice will make you perfect!!! :D Learning C Language For BEGINNERS The 6 th week / May 24 th, 26 Su-Jin Oh sujinohkor@gmail.com 1 Index Basics Operator Precedence Table and ASCII Code Table

More information

Classification s of Data Structures

Classification s of Data Structures Linear Data Structures using Sequential organization Classification s of Data Structures Types of Data Structures Arrays Declaration of arrays type arrayname [ arraysize ]; Ex-double balance[10]; Arrays

More information

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers Maltepe University Computer Engineering Department BİL 133 Algoritma ve Programlama Chapter 8: Arrays and pointers Basics int * ptr1, * ptr2; int a[10]; ptr1 = &a[2]; ptr2 = a; // equivalent to ptr2 =

More information

Computer Programming: C++

Computer Programming: C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming: C++ Experiment #7 Arrays Part II Passing Array to a Function

More information