1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.

Slides:



Advertisements
Similar presentations
Chapter 7: Arrays In this chapter, you will learn about
Advertisements

The debugger Some programs may compile correctly, yet not produce the desirable results. These programs are valid and correct C programs, yet not the programs.
Kernighan/Ritchie: Kelley/Pohl:
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming.
1 Gentle Introduction to Programming Session 4: Arrays, Sorting, Efficiency.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 Arrays and Strings
Introduction to C Programming CE Lecture 9 Data Structures Arrays.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
1 Gentle Introduction to Programming Session 4: Arrays.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Chapter 8 Arrays and Strings
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Array (continue).
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
Computer Science 210 Computer Organization Arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Dr. Sajib Datta Feb 11,  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 More on Arrays. 2 Review: Passing Arrays to Functions Passing an array: what goes on in the memory? Why isn’t it the same as when passing primitives?
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Dr. Sajib Datta Sep 10,  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Consultation Hours. Mubashir: – Tuesday from 12:30 to 1:30 with ease of Students. Zohaib – Wednesday b/w 9:30 -10:30 Location: TA Room (next to HOD Office)
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
1-d Arrays.
Functions and Pointers
Lecture 7 Arrays 1. Concept of arrays Array and pointers
Array 9/8/2018.
INC 161 , CPE 100 Computer Programming
Functions and Pointers
C Passing arrays to a Function
Lecture 10 Arrays.
EKT150 : Computer Programming
Review of Arrays and Pointers
Data Structures (CS212D) Week # 2: Arrays.
Incremental operators
Exercise Arrays.
Presentation transcript:

1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays

2 Arrays Array: sequential block of memory that holds variables of the same type Array can be declared for any type Example: int A[10] is an array of 10 integers. Examples: list of students’ marks series of numbers entered by user vectors matrices

3 Arrays in Memory Sequence of variables of specified type The array variable itself holds the address in memory of beginning of sequence Example: double s[10]; The k-th element of array A is specified by A[k-1] (0 based) s ……

4 Arrays in Memory Access array’s content Change array’s content

5 Example – Find Minimum int i, min, array[10]; printf("please enter 10 numbers:\n"); for(i = 0; i < 10; ++i) scanf("%d", &array[i]); min = array[0]; for(i = 1; i < 10; ++i) { if (array[i] < min) min = array[i]; } printf("the minimum is: %d\n", min);

6 Define Magic Numbers (like 10 in the last example) in the program convey little information to the reader Hard to change in a systematic way #define defines a symbolic name During preprocessing phase, symbolic names are replaced by the replacement text

7 minimum with #define #include #define ARRAY_SIZE 10 int main(void) { int i, min, array[ARRAY_SIZE]; printf("please enter %d numbers:\n", ARRAY_SIZE); for(i = 0; i < ARRAY_SIZE; ++i) scanf("%d", &array[i]); min = array[0]; for(i = 1; i < ARRAY_SIZE; ++i) { if (array[i] < min) min = array[i]; } printf("the minimum is: %d\n", min); return 0; } Use capital letters

8 Initialization Can be initialized during declaration. the number of initializers cannot be more than the number of elements in the array but it can be less in which case, the remaining elements are initialized to 0 if you like, the array size can be inferred from the number of initializers (not recommended) by leaving the square brackets empty so these are identical declarations : int array1 [8] = {2, 4, 6, 8, 10, 12, 14, 16}; int array2 [] = {2, 4, 6, 8, 10, 12, 14, 16};

9 Exercise Write a program that gets 10 numbers from the user. It then accepts another number and checks to see if that number was one of the previous ones. Example 1: Please enter 10 numbers: Please enter a number to search for: 8 I found it! Example 2: Please enter 10 numbers: Please enter a number to search for: 30 Sorry, it’s not there.

10 Solution (simple_search.c) #include #define ARRAY_SIZE 10 int main(void) { int array[ARRAY_SIZE], i, num; printf("Please enter %d numbers:\n", ARRAY_SIZE); for (i = 0; i < ARRAY_SIZE; ++i) scanf("%d", &array[i]); printf("Please enter a number to search for:\n"); scanf("%d", &num); for (i = 0; i < ARRAY_SIZE; ++i) { if (array[i] == num) { printf("I found it!\n"); return 0; } printf("Sorry, it's not there\n"); return 0; }

11 Some Notes on Array Arrays versus basic variables Out of range Equality: do not use the operator ‘=‘ on arrays (until we learn otherwise…). Copy instead Size is always a constant (until we learn otherwise…)

12 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays

13 Arrays as function arguments Functions can accept arrays as arguments The array’s size also needs to be passed (why?)

14 Arrays as function arguments For example: int calc_sum(int arr[], int size); Within the function, arr is accessed in the usual way Changes to the array in the function change the original array! (why?)

15 Example (mult_all.c) void mult_all(int arr[], int size, int multiplier) { int i = 0; for (i = 0; i < size; ++i) arr[i] *= multiplier; } int main() { int i,array[]={1,2,3,4,5,6,7,8,9,10}; mult_all(array,10,5); printf(“array is now:\n”); for (i=0; i<10; i++) printf(“%d “,array[i]); printf(“\n”); return 0; }

16 Example - Sort We would like to sort the elements in an array in an ascending order sort

17 Example of Sort (Bubble Sort) (done)

18 Bubble Sort void sort(int a[], int size) { int i, j, temp; for (i = size - 1; i >= 0; --i) /* counting down */ { for (j = 0; j < i; ++j) /* bubbling up */ { if (a[j] > a[j+1]) /* if out of order... */ { /*... then swap */ temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; }

19 Using sort #include #define ARRAY_SIZE 5 void sort(int a[], int size); int main() { int array[ARRAY_SIZE] = {7, 2, 8, 5, 4}; int i = 0; sort(array, ARRAY_SIZE); /* print the sorted array */ for (i = 0; i < ARRAY_SIZE; ++i) printf("%d ", array[i]); return 0; }

20 Exercise home?) Implement a function that accepts two integer arrays and returns 1 if they are equal, 0 otherwise. The arrays are of the same size Write a program that accepts two arrays of integers from the user and checks for equality

21 Solution (compare_arrays.c) int compare_arrays(int arr1[], int arr2[], int size) { int i = 0; /* compare the elements one at a time */ for (i = 0; i < size; ++i) { if (arr1[i] != arr2[i]) return 0; } /* if we got here, both arrays are identical */ return 1; }

22 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays

23 Multi-dimensional arrays Array of arrays: int A[2][3] = { {1, 2, 3}, {4, 5, 6} }; Means an array of 2 integer arrays, each of length 3. Access: j-th element of the i-array is A[i][j]

24 Multi-dimensional arrays The size of the array can be determined by the compiler (not recommended): int B[][2] = {{1,2}, {2,3}, {3,4}}; Cannot skip this!!

25 Example: matrix addition #include #define SIZE 3 int main() { int A[SIZE][SIZE] = {{1,2,3}, {4,5,6}, {7,8,9}}; int B[SIZE][SIZE] = {{1,1,1}, {2,2,2}, {3,3,3}}; int C[SIZE][SIZE]; int i = 0, j = 0; for (i = 0; i < SIZE; ++i) for (j = 0; j < SIZE; ++j) C[i][j] = A[i][j] + B[i][j]; return 0; }

26 2D arrays as function arguments void print_matrix(int mat[3][3]) { int i, j; for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) printf("\t%d ", mat[i][j]); printf("\n"); } The second subscript must be specified and it must be constant

27 home Write a program that defines 3 matrices A,B,C of size 3x3 with float elements; initialize the first two matrices (A and B) Compute the matrix multiplication of A and B and store it in C (i.e. C = A*B) Matrix Multiplication: Print all the matrices on the screen

28 Solution mat_mul.c

29 Debugger (if time allows) Use the debugger on one of the above examples, see that you can follow execution step by step and see the variables values at each stage

30 More time (no chance)? Talk about the loops-related questions from 2 weeks ago