SCSP Programming Technique C

Size: px
Start display at page:

Download "SCSP Programming Technique C"

Transcription

1 Objectives n this chapter, you will learn: To use the arrray data structure to represent lists and tables of values. CHAPTER 1: To define an array, initialize an array and refer to individual elements of an array. ARRAYS To pass arrays to functions. To define and manipulate multidimensional arrays. 1 Muhalim Mohamed Amin Faculty of 2 Contents 1.1 ntroduction An arrays is a group of contiguous memory location consisting of related data items of the same type. 1.1 ntroduction To refer to a particular location or element in the array, we specify the array s name and the position number of it in square brackets ([]). 1.5 Array of Strings The definition : int data[5]; Allocation in memory : 1. Summary First element ntroduction Example 1: int data[5]; Deitel, H.M. and Deitel, P.J (21). C How to Program /E. United State of America: Pearson Education (pg.245) 1.1 ntroduction Size declarator data[] 5 data[1] 15 data[2] data[] 2 data[4] Fifth element ndexes: nteger number. ndex for the first element. Elements Second Third Fourth element element element 25 Named constants are commonly used as size declarators. Name of the arrays const int SZE = 5; int tests[sze]; data int is the data type of the array elements. data is the name of the array. 5 in [5] is the size declarator. t shows the number of This eases program maintenance when the size of the array needs to be changed. elements in the array. The size of an array is (number of elements)*(size of each element) 5 6 1

2 1.1 ntroduction Contents 1.1 ntroduction Size of an array Declaration and definition Accessing element and storing values in arrays The size of an array is the total number of bytes allocated for it. Size = (number of elements) * (byte size of each element) Examples 1: int data[5] with 4 bytes each. Total size = 5 * 4 = 2 bytes 1.5 Array of Strings Examples 2: double data[1] with 8 bytes each. Total size = 1 * 8 = 8 bytes 1. Summary 8 ntroduction (a) Declaration and definition of arrays There are 2 things to do when using arrays: Since an array is a variable, it must be declared and defined before it can be used. a) Declaration and definition of arrays. Parallel arrays Declaration and definition tell the compiler: o b) Accessing elements and storing values in arrays. nitialization nputting values Assigning values Processing values Outputting values o o Syntax: data_type variable_name[n]; // n is number of elements 9 Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.55) The name of the array The data type of each element The number of elements in the array 1 Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.54) Examples: Declaring and defining arrays. Parallel arrays int data[]; Two or more arrays with the same number of elements data[] data[1] used for storing related information about a collection of data objects. data[2] Type of each element Example: double x[5]; Name of the array char y[]; int id[num_students]; double gpa[num_students];... y[] y[1] y[2] The arrays id and gpa each have elements. We can store the id and gpa for each student i in id[i] y[2] y[28] y[29] Number of elements Number of elements #define NUM_STUDENTS x[] x[1] x[2] x[] x[4] 11 and gpa[i]. 12 Hanly, J.R and Koffman, E.B. (21). C Program Design for Engineers 2/E. United State of America: Addison Wesley Longman nc. (pg: ) 2

3 (b) Accessing elements and storing values in arrays Example: Declaring and defining parallel arrays. The integer enclosed in brackets is the unique array int id[]; subscript.... id[] id[1] id[2] id[29] ts integer value must be in the range from to (n-1), where n is the number of element in the array. Student 1 Student 2 Student Student Example: The last subscript number for element is (-1) = 29. double gpa[];... gpa[] 1 gpa[1] gpa[2] Hanly, J.R and Koffman, E.B. (21). C Program Design for Engineers 2/E. United State of America: Addison Wesley Longman nc. (pg: ) char y[]; gpa[29]... y[] y[1] y[2] 14 y[2] y[28] y[29] Hanly, J.R and Koffman, E.B. (21). C Program Design for Engineers 2/E. United State of America: Addison Wesley Longman nc. (pg: ) nitialization iii) Partial initialization: Like ordinary variable, arrays may also be initialized using any of these 4 ways. i) Basic initialization: int x[5] =,,12,24,; x[] x[1] x[2] x[] x[4] Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.56) All filled with s x[] x[1] x[2] x[] x[4] int x[5] = ; 12 iv) nitialization to all zeros: int x[ ] =,,12,24,; ii) nitialization without size: x[] x[1] x[2] x[] x[4] x[] x[1] x[2] x[] x[4] 15 The rest are filled with s int x[5] =,; 16 Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.56) Default initialization nitialization with a string Global array à all elements initialized to by default. Local array à all elements uninitialized by default. Character array can be initialized by enclosing string in " ": const int SZE = 6; char fname[sze] = Ahmad"; Example: By default: global[5]=,,,, Must leave room for the null character \ at end of array. f initializing character-by-character, must add in \ explicitly: const int SZE = 6; char fname[sze] = A, h, m, a, d,'\'; 1 18

4 Example 1: Example 2: nitialize an array, name with string Ahmad. nitialize an array, name with string Ahmad character-bycharacter. Null character \ marks the end of a string A h m a d \ name[] name[1]name[2]name[]name[4]name[5]name[2]name[]name[4]name[5] Figure: name in the memory after initialization Activity 1.1: Write a C program to display your name as the nputting values 1 The array can be filled by reading the values from the Define the validity of the following arrays definition. f invalid, explain the reason. (a) (b) (c) (d) (e) (f) (g) keyboard or a file. Example: nput of array data for 5 elements. int numbers[l] =,,1,,,1,,,1,1; int matrix[5] = 1,2,,4,5,6,; double radix[1] =.2,4.; int table[] = 2,,,2,,45,9; char codes[] = A,'X,'1,'2,'s'; int blanks[]; char name[1] = Mubassyir ; (value:,,12,24,) (Program_28b.c) Here are the contents of the data array: data[] data[1] data[2] data[] data[4] 22 Coding: nputting values 2 The input value also can be done using a loop. To enter any 5 integer nputs of array data. The most appropriate loop is the for because the number of elements is fixed and known. Example: nput of array data for 5 elements. for (i = ; i < 5; i++) scanf( %d, &data[i]); 2 24 Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.55) 4

5 Example 1: Using for loop to enter 5 input values into an Example 2: Using for loop to enter 5 input values into a array from the keyboard. parallel array from the keyboard Examples: Assigning values The values can be assigned to individual array element Assigning variable n with value of the first element of an using the assignment operator. data[] data[1] data[2] data[] data[4] Here are the contents of the data array: = = = = = array data. ; data[] + 4; data[] + data[1]; data[2] * 2; pow(data[2],2); 1 int data[] =,,1,2,1; int n; n = data[]; // n = Assigning variable n with value of the sum of second and (Program_28a.c) 2 third element of an array data. int x = 5,n; int data[x] =,,1,2,1; n = data[1] + data[2]; // n = 1 data[] data[1] data[2] data[] data[4] 2 28 match fully in type and size. fourth element. The arrays have to copy at the individual element levels. int data[] =,,1,2,1; data[1] = data[]; // data[1] = 2 Example: int data[5] =,,1,2,; int bonus[5] = 1,2,,4,5; Assigning variable n with value of the last element of an array data. int x = 5,n; int data[x] =,,1,2,1; n = data[x-1]; // n = 1 One array cannot be assigned to another array, even if they Assigning the second element of an array data with the 29 data = bonus; data[] = bonus[4]; // not legal // data[] = 5 5

6 Activity 1.2: Given an array with double type as below. Processing values (a) How to declare the array with the initial values? (b) Explain what will be happened and the output for each statement in the table. Array elements can be treated as ordinary variables of the same type as the array When using ++, -- operators, don t confuse the element Statement x[] x[1] x[2] x[] x[4] Explanation with the subscript: Example: Output x[] = 25.; sum = x[] + x[1]; sum += x[2]; x[] += 1. tests[i]++; // add 1 to tests[i] tests[i++]; // increment i, // no effect on tests x[2] = x[] + x[1]; 1 Hanly, J.R and Koffman, E.B. (21). C Program Design for Engineers 2/E. United State of America: Addison Wesley Longman nc. (pg: ) Activity 1.: 2 Given int y[]=1,14,28,26,2,1,5; int sum =, i = ; Outputting values Another common application is printing the contents of an Assume the statements are execute in sequence. array. (a) Draw the array and label with it subscript contain the initial values. (b) Explain what will be happened and the output for each statement in the table. Statement Explanation printf( %d, data[]); printf( %d, data[1]); printf( %d, data[]); Output This is easily done with a for loop. y[i]++; sum = y[] for (i = ; i < ; i++) printf( %d, data[i]); --y[1]; sum = y[i++] + ; y[i++] -= y[5]; 4 Example 2: To enter any 5 integer nputs of array data. and display all entered values. Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.58) Example 1: 5 To display the initialized array, name with string Ahmad. 6 6

7 Example : int id[5]; id[] To display all 5 students with their id and gpa entered. id[1] id[2] 161 id[4] Student Student 1 Student 2 Student Student 4 double gpa[5]; gpa[] gpa[1]..99 gpa[2] 4. gpa[4] Figure: id and gpa in the memory. 8 Activity 1.4: Contents Write a C program to display your name as the What is the output of the following code? 1.1 ntroduction Passing individual elements Passing the whole array (You may need to use a calculator.) 1.5 Array of Strings 1. Summary 9 4 ntroduction To pass an array to a function, just use the array name: To process arrays in a large program, we have to be able to pass them to functions. showscores(tests); To define a function that takes an array parameter, use This can be done in two ways: empty [] for array argument: (a) Pass individual elements. // function prototype void showscores(int []); (b) Pass the whole array. // function header void showscores(int tests[]) 41 Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.55) 42 Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.55)

8 (a) Pass individual elements When passing an array to a function, it is common to pass They array can be passed as individual elements to a function like any other variable. array size so that function knows how many elements to process: The array element type must matches the function parameter type. showscores(tests, ARRAY_SZE); Array size must also be reflected in prototype, header: Passing the individual element of array to function can be in two forms: Pass by value (pass its contents). // function prototype void showscores(int [], int); e.g. printf ( %d, A[2]); // function header void showscores(int tests[], int size) Pass by reference (pass its addresses). e.g. scanf ( %d, &A[2]); 4 44 int main(void) int i; int base[] =, 4, ; Passing individual element by value. // Program_1b.c void print_square(int *y); Example 2: base[] 4 base[1] base[] return ; void print_square(int y) printf( %d, y * y); y return; Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.6) Activity 1.5: Tips:. 46 base[] y Requires * deference &base Address (pointer) Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.6) Passing the whole array to a function can only be done by using pass by reference. t usually passes the address of the first element. Example: Example output: Activity_1.5.c base[1] (b) Pass the whole array Modified program to in display Activity Write a the C program your1.4 namesoasthat the the bill can be calculated in a calculation function without return value (pass individual element) and print from display function. your file as 4 Activity 1.4 program and save 4 Refer Comment in your base[] Address operator & void print_square(int *y) printf( %d, *y * *y); return; for(i = ; i < ; i++) print_square(&base[i]); Prints return ; Type include * int main(void) int i; int base[] =, 4, ; Passing individual element by address. for(i = ; i < ; i++) print_square(base[i]); Prints Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.6) // Program_1a.c void print_square(int y); Example 1: 48 void main(void) int A[] = 1, 2, ; increase (A); // or, increase (&A[]); void increase(int x[]) x[] += 1; // x[] = 11 x[1] += 2; // x[1] = 22 x[2] += ; // x[2] = 8

9 // Program_2.c void average_square(int y[]); Example 1: Passing the whole element by reference (address). int main(void) int base[] =, 4, ; average_square(base); return ; Activity 1.6: base[] 4 base[1] base[] Prints. y Modified program to in display Activity Write a the C program your1.5 namesoasthat the the bill can be calculated in a calculation function without return value (pass the whole array) and print from display function. Refer Activity 1.5.c Comment in your void average_square(int y[]) int i; double sum = ; Average 24.6 Tips: program and save Example output: your file as Activity_1.6.c i for(i = ; i < ; i++) sum += y[i] * y[i]; printf(" Average %.2f ", sum/); return; sum 49 Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.64) Contents 5 ntroduction 1.1 ntroduction An array can be defined as a multiple sets of data. A 2-dimensional array can be thought of as a table, which has x number of rows and y number of columns. Use two size declarators in definition: const int ROWS = 4, COLS = ; int exams[rows][cols]; 1.5 Array of Strings 1. Summary First declarator is number of rows; second is number of columns Representation nitialization Example: Basic initialization for an array, data. const int ROW = 2, COLUMN = ; data[row][column] = 1,5,, 92,8,4; [] Figure: A 2-dimensional array with rows and 4 columns. [2] 1 5 [1] To access and assign value to an element in the array, use two subscripts data[][2] = 2; The array a is identified by an element name of the form a[row,column]. 5 [1] [] 54 9

10 Passing function arguments Use array name as argument in function call: getdata(student,); // function call Use empty [] for row, size declarator for column in prototype, header: const int COLUMN = ; // Prototype void getdata(int [][COLUMN], int); Example 1: (without function) To calculate average mark of quizzes taken by each of 5 students. (get the marks from keyboard) // header definition void getdata(int student[][column], int row); Activity 1.: Answer following questions on theas linethe Write athe C program to displaybased your name number of the program in the previous example (page 64). (a) Why does the program need to have if-else statements at lines 24? (b) Why the value of i and j are written as i+1 at line 21 and 45, and j+1 at lines 26 and 1? (c) Why the sum need to be at line 41? (d) What we need to do if we want the statement at line 45 as: printf(" -- Student %d: %.2f \n",i+1,average); 5 Example 2: (with function) To calculate average mark of quizzes taken by each of 5 students. (get the marks from keyboard) Write a C program to display your name as the Write a C program to display your name as the below. The programexample (Program_.c) in previous example (page 64) is modified so that each marks will be entered using enter function, calculate the average with calculate function that return the average value, and print from display function. Assume that we remain the records[stud][quz] as local in the main function. 6 1

11 Write a C program to display your name as the Write a C program to display your name as the Activity 1.8: Tips: Refer. Activity_1.5.c Comment in your program and save Modified program to Activity_1.5.c Write a the C program display your namewith as the the following array that contain the electric usage (kwh) within few months for a group of users. Expected Write a Coutput: program to display your name as the The bill for each month and total should be calculated in a calculation functions without return value (pass the whole array) and print all monthly and total bill for each user from display function. your file as kwh / Months Users Activity_1.8.c Contents 1.5 Arrays of Strings ntroduction 1.1 ntroduction Use a two-dimensional array of characters as an array of strings: const int NAMES =, SZE = 1; char students[names][sze] = Ali", Siti", Ahmad" ; 1.5 Array of Strings Each row contains one string Can use row subscript to reference the string in a particular row: 1. Summary printf( %d, students[i]);

12 1.5 Arrays of Strings 1.5 Arrays of Strings Example 1: To displays the number of days in each month in a year. 6 Activity 1.9: Tips:. Comment in your program and save your file as Activity_1.9.c 1.5 Arrays of Strings 68 Write that define an array strings Writea aprogram C program to display yourofname astothe store your friend s name in this class. nitialize the array with 5 names. Print those names. Contents 1.1 ntroduction n the program, write a function to change any name in the initialized array and reprint the names. void changename(char[][strng_sze], int size); 1.5 Array of Strings 1. Summary 69 ntroduction This type of arrays can have more than two dimensions. Example: Define arrays with any number of dimensions. short rectsolid[2][][5]; double timegrid[][4][][4]; //D //4D (a,b,c) When used as parameter, specify all but 1st dimension in prototype, heading: b c void getrectsolid(short [][][5]); void gettimegrid(double [][4][][4]); a Figure: Example of a three-dimensional array (5 x 4 x ). 1 Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.4)

13 Activity 1.1: Contents Consider the following diagram of three-dimensional array, table. Answer the questions. y x 99 z (a) How to declare the array with integer type? 1.1 ntroduction (b) Write the statement to access value and 99. (c) How to assign value 4 to x? 1.5 Array of Strings (d) What is the similarity between x, y and z? 1. Summary (e) Add x and y? and store at z. Chapter 9 1. Summary 4 Chapter 9 n this chapter, you have learnt: The C language facilitates a structured and disciplined approach to write a computer program. the arrray data structure to represent lists and tables of values. Always write comments to document the programs and practice coding with a good programming style to define an array, initialize program an array and refer to individual will improve readability. elements of an array. A syntax error is caused when compiler cannot passing arrays to functions innormally a program. recognize a statement. the compiler will issues an error message to help us to locate and fix the manipulation ofthe two-dimensional arrays and briefly incorrect statement. overview on multidimensional arrays. 5 Self-Reviews 6 Self-Review Chapter 9 Exercise 1.1: Answer the following questions. Exercise 1.2: (a) The type of all elements in an array must be the same. (True / False) (b) When an array is defined, C automatically sets the value of its element to zero. (True / False) (c) When an array is passed to a function, C uses pass by address. (True / False) (d) Which of the following array(s) initialization statements is valid? i) int ary4 = 1,2,,4; ii) int ary[] = [1,2,,4]; iii) int ary[4] = 1,2; iv) int ary = [1,2,,4]; v) int ary[] = ; Self-Review Tips:. Refer Activity_1.8.c Comment in your program and save Modified program to Activity_1.8.c Write a the C program display your namewith as the the array value of the electric usage (kwh) for 4 months for a group of users that obtained from keyboard using getreading function. Get and store the user name as well. The bill for each month and total should be calculated in a calculation function without return value (pass the whole array). your file as Exercise_1.2.c Print from display function: all user s name; their monthly and total bill; Forouzan, B.A. and Gilberg, R.F (21). Computer Science: A Structured Programming Approach Using C (2/E). United State of America: Brook/Cole.(pg.41) 8 1

14 Self-Review Write a C program to display your name as the Expected output: 9 14

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology Chapter 7: Arrays 7.1 Arrays Hold Multiple Values Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Addison-Wesley Pearson Education, Inc. Publishing as Pearson Addison-Wesley

More information

Chapter 7: Arrays Copyrig opy ht rig 2012 Pea e rson a Educa Educ ti a on, Inc I. nc

Chapter 7: Arrays Copyrig opy ht rig 2012 Pea e rson a Educa Educ ti a on, Inc I. nc Chapter 7: Arrays 7.1 Arrays Hold Multiple Values Arrays Hold Multiple Values Array variable that can store multiple values of the same type (aggregate type) Values are stored in adjacent memory locations

More information

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology 8/23/2014. Arrays Hold Multiple Values

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology 8/23/2014. Arrays Hold Multiple Values Chapter 7: Arrays 7.1 Arrays Hold Multiple Values Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations Declared using

More information

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator:

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator: Chapter 7: 7.1 Arrays Arrays Hold Multiple Values Arrays Hold Multiple Values Array - Memory Layout A single variable can only hold one value int test; 95 Enough memory for 1 int What if we need to store

More information

(8-1) Arrays I H&K Chapter 7. Instructor - Andrew S. O Fallon CptS 121 (October 8, 2018) Washington State University

(8-1) Arrays I H&K Chapter 7. Instructor - Andrew S. O Fallon CptS 121 (October 8, 2018) Washington State University (8-1) Arrays I H&K Chapter 7 Instructor - Andrew S. O Fallon CptS 121 (October 8, 2018) Washington State University What is an array? A sequence of items that are contiguously allocated in memory All items

More information

Arrays. Arizona State University 1

Arrays. Arizona State University 1 Arrays CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 8 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

(9-1) Arrays IV Parallel and H&K Chapter 7. Instructor - Andrew S. O Fallon CptS 121 (March 5, 2018) Washington State University

(9-1) Arrays IV Parallel and H&K Chapter 7. Instructor - Andrew S. O Fallon CptS 121 (March 5, 2018) Washington State University (9-1) Arrays IV Parallel and H&K Chapter 7 Instructor - Andrew S. O Fallon CptS 121 (March 5, 2018) Washington State University Parallel Arrays (1) Often, we'd like to associate the values in one array

More information

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University (13-2) Dynamic Data Structures I H&K Chapter 13 Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University Dynamic Data Structures (1) Structures that expand and contract

More information

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

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Array Many applications require multiple data items that have common

More information

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

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur 1 Array Many applications require multiple data items that have common

More information

(9-2) Strings I H&K Chapter 8. Instructor - Andrew S. O Fallon CptS 121 (March 7, 2018) Washington State University

(9-2) Strings I H&K Chapter 8. Instructor - Andrew S. O Fallon CptS 121 (March 7, 2018) Washington State University (9-2) Strings I H&K Chapter 8 Instructor - Andrew S. O Fallon CptS 121 (March 7, 2018) Washington State University String Fundamentals A string is a sequence of characters terminated by the null character

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

Programming for Engineers Arrays

Programming for Engineers Arrays Programming for Engineers Arrays ICEN 200 Spring 2018 Prof. Dola Saha 1 Array Ø Arrays are data structures consisting of related data items of the same type. Ø A group of contiguous memory locations that

More information

Module 6: Array in C

Module 6: Array in C 1 Table of Content 1. Introduction 2. Basics of array 3. Types of Array 4. Declaring Arrays 5. Initializing an array 6. Processing an array 7. Summary Learning objectives 1. To understand the concept of

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

(7-1) Modular Programming H&K Chapter 6. Instructor - Andrew S. O Fallon CptS 121 (February 21, 2018) Washington State University

(7-1) Modular Programming H&K Chapter 6. Instructor - Andrew S. O Fallon CptS 121 (February 21, 2018) Washington State University (7-1) Modular Programming H&K Chapter 6 Instructor - Andrew S. O Fallon CptS 121 (February 21, 2018) Washington State University Functions with Output Parameters (1) In many situations, we would like to

More information

Chapter 7 Array. Array. C++, How to Program

Chapter 7 Array. Array. C++, How to Program Chapter 7 Array C++, How to Program Deitel & Deitel Spring 2016 CISC 1600 Yanjun Li 1 Array Arrays are data structures containing related data items of same type. An array is a consecutive group of memory

More information

(1-1) C Review: Pointers, Arrays, Strings, & Structs. Instructor - Andrew S. O Fallon CptS 122 (January 10, 2018) Washington State University

(1-1) C Review: Pointers, Arrays, Strings, & Structs. Instructor - Andrew S. O Fallon CptS 122 (January 10, 2018) Washington State University (1-1) C Review: Pointers, Arrays, Strings, & Structs Instructor - Andrew S. O Fallon CptS 122 (January 10, 2018) Washington State University Crash Review on Critical C Topics Pointers Arrays Strings Structs

More information

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

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA. Arrays Defining arrays, declaration and initialization of arrays Introduction Many applications require the processing of multiple data items that have common characteristics (e.g., a set of numerical

More information

Array Part dimensional arrays - 2-dimensional array operations - Arrays of Strings - N-dimensional arrays

Array Part dimensional arrays - 2-dimensional array operations - Arrays of Strings - N-dimensional arrays Array Array Part 1 - Accessing array - Array and loop arrays must be used with loops - Array and bound checking Be careful of array bound: invalid subscripts => corrupt memory; cause bugs - Array initialization

More information

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming 1 2 3 CMPE 013/L and Strings Gabriel Hugh Elkaim Spring 2013 4 Definition are variables that can store many items of the same type. The individual items known as elements, are stored sequentially and are

More information

Array Initialization

Array Initialization Array Initialization Array declarations can specify initializations for the elements of the array: int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ; initializes primes[0] to 2, primes[1] to 3, primes[2]

More information

(7-2) Operator Overloading D & D Chapter 10. Instructor - Andrew S. O Fallon CptS 122 (February 23, 2018) Washington State University

(7-2) Operator Overloading D & D Chapter 10. Instructor - Andrew S. O Fallon CptS 122 (February 23, 2018) Washington State University (7-2) Operator Overloading D & D Chapter 10 Instructor - Andrew S. O Fallon CptS 122 (February 23, 2018) Washington State University Key Concepts Keyword operator Operator overloading 2 What is Operator

More information

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

(7-2) Modular Programming II H&K Chapter 6. Instructor - Andrew S. O Fallon CptS 121 (February 22, 2019) Washington State University

(7-2) Modular Programming II H&K Chapter 6. Instructor - Andrew S. O Fallon CptS 121 (February 22, 2019) Washington State University (7-2) Modular Programming II H&K Chapter 6 Instructor - Andrew S. O Fallon CptS 121 (February 22, 2019) Washington State University Pitfalls of Pointers and Output Parameters Not assigning a pointer a

More information

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

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays. CS 2060 Week 5 1 Arrays Arrays Initializing arrays 2 Examples of array usage 3 Passing arrays to functions 4 2D arrays 2D arrays 5 Strings Using character arrays to store and manipulate strings 6 Searching

More information

COMPUTER APPLICATION

COMPUTER APPLICATION Total No. of Printed Pages 16 HS/XII/A.Sc.Com/CAP/14 2 0 1 4 COMPUTER APPLICATION ( Science / Arts / Commerce ) ( Theory ) Full Marks : 70 Time : 3 hours The figures in the margin indicate full marks for

More information

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

More information

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010 CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011 Lectures 1-22 Moaaz Siddiq Asad Ali Latest Mcqs MIDTERM EXAMINATION Spring 2010 Question No: 1 ( Marks: 1 ) - Please

More information

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays Maltepe University Computer Engineering Department BİL 133 Algorithms and Programming Chapter 8: Arrays What is an Array? Scalar data types use a single memory cell to store a single value. For many problems

More information

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.

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. UNIT IV 1. Appropriately comment on the following declaration int a[20]; a. Array declaration b. array initialization c. pointer array declaration d. integer array of size 20 2. Appropriately comment on

More information

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

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be

More information

Lab 3. Pointers Programming Lab (Using C) XU Silei

Lab 3. Pointers Programming Lab (Using C) XU Silei Lab 3. Pointers Programming Lab (Using C) XU Silei slxu@cse.cuhk.edu.hk Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions

More information

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd El-Shorouk Academy Acad. Year : 2013 / 2014 High Institute of Computer Science & Information Technology Term : 1 st Year : 2 nd Computer Science Department Object Oriented Programming Section (1) Arrays

More information

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

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18) BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18) 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional

More information

Principles of Programming. Chapter 6: Arrays

Principles of Programming. Chapter 6: Arrays Chapter 6: Arrays In this chapter, you will learn about Introduction to Array Array declaration Array initialization Assigning values to array elements Reading values from array elements Simple Searching

More information

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff and Arrays Comp Sci 1570 Introduction to C++ Outline and 1 2 Multi-dimensional and 3 4 5 Outline and 1 2 Multi-dimensional and 3 4 5 Array declaration and An array is a series of elements of the same type

More information

Unit 7. Functions. Need of User Defined Functions

Unit 7. Functions. Need of User Defined Functions Unit 7 Functions Functions are the building blocks where every program activity occurs. They are self contained program segments that carry out some specific, well defined task. Every C program must have

More information

Fundamentals of Programming Session 4

Fundamentals of Programming Session 4 Fundamentals of Programming Session 4 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2011 These slides are created using Deitel s slides, ( 1992-2010 by Pearson Education, Inc).

More information

BITG 1113: Array (Part 1) LECTURE 8

BITG 1113: Array (Part 1) LECTURE 8 BITG 1113: Array (Part 1) LECTURE 8 1 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional (1 D)

More information

CHAPTER 1: INTRODUCTION TO COMPUTERS AND PROGRAMMING. 1 Muhalim Mohamed Amin Faculty of

CHAPTER 1: INTRODUCTION TO COMPUTERS AND PROGRAMMING. 1 Muhalim Mohamed Amin Faculty of CHAPTER 1: INTRODUCTION TO COMPUTERS AND PROGRAMMING 1 Muhalim Mohamed Amin Faculty of Computing @2015/2016-1 Objectives In this chapter, you will learn: Basic computer concepts. The different types of

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

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

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays A First Book of ANSI C Fourth Edition Chapter 8 Arrays Objectives One-Dimensional Arrays Array Initialization Arrays as Function Arguments Case Study: Computing Averages and Standard Deviations Two-Dimensional

More information

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University (6-1) Basics of a Queue Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University What is a Queue? 2 A linear data structure with a finite sequence of nodes, where nodes

More information

Arrays in C C Programming and Software Tools. N.C. State Department of Computer Science

Arrays in C C Programming and Software Tools. N.C. State Department of Computer Science Arrays in C C Programming and Software Tools N.C. State Department of Computer Science Contents Declaration Memory and Bounds Operations Variable Length Arrays Multidimensional Arrays Character Strings

More information

Single Dimension Arrays

Single Dimension Arrays ARRAYS Single Dimension Arrays Array Notion of an array Homogeneous collection of variables of same type. Group of consecutive memory locations. Linear and indexed data structure. To refer to an element,

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

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017 COP 3223 Introduction to Programming with C - Study Union - Fall 2017 Chris Marsh and Matthew Villegas Contents 1 Code Tracing 2 2 Pass by Value Functions 4 3 Statically Allocated Arrays 5 3.1 One Dimensional.................................

More information

Technical Questions. Q 1) What are the key features in C programming language?

Technical Questions. Q 1) What are the key features in C programming language? Technical Questions Q 1) What are the key features in C programming language? Portability Platform independent language. Modularity Possibility to break down large programs into small modules. Flexibility

More information

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

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. 1992-2010 by Pearson Education, Inc. 1992-2010 by Pearson Education, Inc. This chapter serves as an introduction to the important topic of data

More information

Multi-Dimensional arrays

Multi-Dimensional arrays Multi-Dimensional arrays An array having more then one dimension is known as multi dimensional arrays. Two dimensional array is also an example of multi dimensional array. One can specify as many dimensions

More information

AMCAT Automata Coding Sample Questions And Answers

AMCAT Automata Coding Sample Questions And Answers 1) Find the syntax error in the below code without modifying the logic. #include int main() float x = 1.1; switch (x) case 1: printf( Choice is 1 ); default: printf( Invalid choice ); return

More information

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size APS105 Malloc and 2D Arrays Textbook Chapters 6.4, 10.2 Datatype Size Datatypes have varying size: char: 1B int: 4B double: 8B int sizeof(): a builtin function that returns size of a type int x =

More information

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.

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. Data Structures Introduction 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. Representation of a large number of homogeneous

More information

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will: Chapter 8 Arrays and Strings Objectives In this chapter, you will: Learn about arrays Declare and manipulate data into arrays Learn about array index out of bounds Learn about the restrictions on array

More information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) CSE 2031 Fall 2012 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. Arrays: Example Syntax type name[size];

More information

SCSP Programming Technique C

SCSP Programming Technique C SCSP1103 - Programming Technique C 9/27/15 Objectives In this chapter, you will learn: CHAPTER 1: Basic computer concepts. The different types of programming languages in general. INTRODUCTION TO COMPUTERS

More information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) CSE 2031 Fall 2010 17 October 2010 1 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. 2 1 Arrays: Example

More information

(2-1) Data Structures & The Basics of a Linked List I. Instructor - Andrew S. O Fallon CptS 122 (August 27, 2018) Washington State University

(2-1) Data Structures & The Basics of a Linked List I. Instructor - Andrew S. O Fallon CptS 122 (August 27, 2018) Washington State University (2-1) Data Structures & The Basics of a Linked List I Instructor - Andrew S. O Fallon CptS 122 (August 27, 2018) Washington State University How do we Select a Data Structure? (1) Select a data structure

More information

Computers Programming Course 10. Iulian Năstac

Computers Programming Course 10. Iulian Năstac Computers Programming Course 10 Iulian Năstac Recap from previous course 5. Values returned by a function A return statement causes execution to leave the current subroutine and resume at the point in

More information

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University (5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University Key Concepts Function templates Defining classes with member functions The Rule

More information

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14 C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition

More information

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a

More information

IV Unit Second Part STRUCTURES

IV Unit Second Part STRUCTURES STRUCTURES IV Unit Second Part Structure is a very useful derived data type supported in c that allows grouping one or more variables of different data types with a single name. The general syntax of structure

More information

(7 2) Classes: A Deeper Look D & D Chapter 9. Instructor - Andrew S. O Fallon CptS 122 (February 22, 2019) Washington State University

(7 2) Classes: A Deeper Look D & D Chapter 9. Instructor - Andrew S. O Fallon CptS 122 (February 22, 2019) Washington State University (7 2) Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O Fallon CptS 122 (February 22, 2019) Washington State University Key Concepts Composition relationship const objects const member functions

More information

Chapter 7 Functions. Now consider a more advanced example:

Chapter 7 Functions. Now consider a more advanced example: Chapter 7 Functions 7.1 Chapter Overview Functions are logical groupings of code, a series of steps, that are given a name. Functions are especially useful when these series of steps will need to be done

More information

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo 1. (a)what is an algorithm? Draw a flowchart to print N terms of Fibonacci

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

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

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 7/e This chapter serves as an introduction to data structures. Arrays are data structures consisting of related data items of the same type. In Chapter 10, we discuss C s notion of

More information

Government Polytechnic Muzaffarpur.

Government Polytechnic Muzaffarpur. Government Polytechnic Muzaffarpur. Name of the Lab: COMPUTER PROGRAMMING LAB (MECH. ENGG. GROUP) Subject Code: 1625408 Experiment: 1 Aim: Programming exercise on executing a C program. If you are looking

More information

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University (12-1) OOP: Polymorphism in C++ D & D Chapter 12 Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University Key Concepts Polymorphism virtual functions Virtual function tables

More information

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are: LESSON 1 FUNDAMENTALS OF C The purpose of this lesson is to explain the fundamental elements of the C programming language. C like other languages has all alphabet and rules for putting together words

More information

Introduction to Scientific Computing and Problem Solving

Introduction to Scientific Computing and Problem Solving Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:

More information

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. CSE 2031 Fall November 11, 2013 Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary GATE- 2016-17 Postal Correspondence 1 C-Programming Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key concepts, Analysis

More information

Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 CMPE13. Cyrus Bazeghi

Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 CMPE13. Cyrus Bazeghi Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 Cyrus Bazeghi POINTERS AND ARRAYS We have briefly seen these before, here are the details Pointer Array Address of a variable in memory Allows

More information

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

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

Chapter 6: Arrays. Starting Out with Games and Graphics in C++ Second Edition. by Tony Gaddis Chapter 6: Arrays Starting Out with Games and Graphics in C++ Second Edition by Tony Gaddis 6.1 Array Basics An array allows you to store a group of items of the same data type together in memory Why?

More information

Chapter 2 Basic Elements of C++

Chapter 2 Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

More information

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 3 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016 ENCM 339 Fall 2016 Slide Set 3 slide 2/46

More information

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

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) In this lecture, you will: Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of

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

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips Agenda / Learning Objectives: 1. Map out a plan to study for mid-term 2. 2. Review the C++ operators up to logical operators. 3. Read about the tips and pitfalls on using arrays (see below.) 4. Understand

More information

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept Arrays in C Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur 1 Basic Concept Many applications require multiple data items that have common characteristics.

More information

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng Slide Set 3 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2017 ENCM 339 Fall 2017 Section 01

More information

More Arrays. Last updated 2/6/19

More Arrays. Last updated 2/6/19 More Last updated 2/6/19 2 Dimensional Consider a table 1 2 3 4 5 6 5 4 3 2 12 11 13 14 15 19 17 16 3 1 4 rows x 5 columns 2 tj 2 Dimensional Consider a table 1 2 3 4 5 6 5 4 3 2 12 11 13 14 15 19 17 16

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

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

Review of Important Topics in CS1600. Functions Arrays C-strings Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why

More information

INTRODUCTION 1 AND REVIEW

INTRODUCTION 1 AND REVIEW INTRODUTION 1 AND REVIEW hapter SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN. Programming: Advanced Objectives You will learn: Program structure. Program statements. Datatypes. Pointers. Arrays. Structures.

More information

Pointers, Arrays and Parameters

Pointers, Arrays and Parameters Pointers, Arrays and Parameters This exercise is different from our usual exercises. You don t have so much a problem to solve by creating a program but rather some things to understand about the programming

More information

Procedural Programming

Procedural Programming Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Procedural Programming Session Five: Arrays Name: First Name: Tutor: Matriculation-Number: Group-Number: Date: Prof. Dr.Ing. Axel Hunger Dipl.-Ing.

More information

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty! Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns

More information

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS Programming Year 2017-2018 Industrial Technology Engineering Paula de Toledo Contents 1. Structured data types vs simple data types 2. Arrays (vectors and matrices)

More information

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER: Page 1 of 7 School of Computer Science 60-141-01 Introduction to Algorithms and Programming Winter 2014 Midterm Examination # 2 Wednesday, March 19, 2014 ANSWERS Duration of examination: 75 minutes STUDENT

More information

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as: 11. Arrays An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. That means that, for example,

More information

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014. Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example

More information

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

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. Week 3 Functions & Arrays Gaddis: Chapters 6 and 7 CS 5301 Fall 2015 Jill Seaman 1 Function Definitions! Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where

More information

Pointers and Dynamic Arrays

Pointers and Dynamic Arrays Pointers and Dynamic Arrays Pointers A pointer is the memory address of a variable Memory addresses can be used as names for variables If a variable is stored in three memory locations, the address of

More information

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9.

Chapter Overview. Pointers and Dynamic Arrays. Pointers. Pointers. Declaring Pointers. Pointers Tell Where To Find A Variable. 9. Chapter 9 Pointers and Dynamic Arrays Overview 9.1 Pointers 9.2 Dynamic Arrays Copyright 2011 Pearson Addison-Wesley. All rights reserved. Slide Revised by Zuoliu Ding at Fullerton College, Fall 2011 Slide

More information