C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
Chapter 9: Arrays and Strings
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Chapter 9 Introduction to Arrays
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Java Unit 9: Arrays Declaring and Processing Arrays.
A First Book of ANSI C Fourth Edition
Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Chapter 8 Arrays and Strings
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
COP 3275: Chapter 09 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
COP 3275: Chapter 08 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Sorting –Bubble sort –Merge sort –Quick sort –Selection.
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.
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
Chapter 9: Functions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Chapter 9: Functions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Chapter 12: Pointers and Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Windows Programming Lecture 03. Pointers and Arrays.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
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.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Computer Programming BCT 1113
C programming---Arrays
Today’s Material Arrays Definition Declaration Initialization
9. FUNCTIONS.
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company. 1
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Lecture 14: Problems with Lots of Similar Data
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Presentation transcript:

C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates in C: Arrays Structures An array is a data structure containing a number of data values, all of which have the same type.

Arrays One-Dimensional Arrays The simplest kind of arrays The elements, the values of the items, of a one- dimensional array are conceptually arranged one after another in a single row. int a[8]; a #define N 10 …… int a[N];

Arrays Subscripting Array elements are always numbered starting from 0, so the elements of an array of length n are indexed from 0 to n-1 a[0]a[1] a[8-1] a[1] = 9; printf(“%d\n”, a[5]); ++a[i];

Arrays and For Loop Arrays and for loops go hand-in-hand Idioms: for(i = 0; i < N; i++) a[i] = 0; for(i = 0; i < N; i++) scanf(“%d”, &a[i]); for(i = 0; i < N; i++) sum += a[i];

Arrays and Its Indexing C doesn’t require that subscript bounds be checked. If a subscript goes out of range, the program’s behavior is undefined. int a[10], i; for(i = 1; i <= 10; i++) a[i] = 0;

Array subscript may be any integer expression a[i+j*10] i = 0; while(i < N) a[i++] = 0; i = 0; while(i < N) a[i] = b[i++];

An Example reverse.c

Array Initialization Always initialize array when you declare it int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int a[10] = {1, 2, 3, 4, 5, 6}; /* {1, 2, 3, 4, 5, 0, 0, 0, 0, 0} */ int a[10] = {0}; /* {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} */ int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; /* {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} */

An Example repdigit.c

Using the sizeof Operater with arrays The sizeof operator can determine the size of an array (in bytes). int a[10]; sizeof(a) = 40 (assuming each integer requires 4bytes) Measure the size of an array: Sizeof(a) / sizeof(a[0]); #define SIZE ((int) (sizeof(a) / sizeof(a[0])))

Multidimensional Arrays int m[5][9]; // 5 rows 9 columns M[i, j] != M[i][j] M[i, j] == M[j]

How Multidimensional Array Stored in Memory #define N 10 double ident[N][N]; int row, col; for(row = 0; row < N; row++) for(col = 0; col < N; col++) if(row == col) ident[row][col] = 1.0; else ident[row][col] = 0.0;

Initializing a Multidimensional Array int m[5][9] = { {1,1,1,1,1,1,1,1,1}, {0,1,0,0,0,1,1,0,1}, {0,0,0,0,0,0,0,0,0}, {1,1,1,1,1,1,1,1,1}, {1,0,1,0,1,0,1,0,1}} int m[5][9] = { {1,1,1,1,1,1,1,1,1}, {0,1,0,0,0,1,1,0,1}, {1,0,1,0,1,0,1,0,1}} int m[5][9] = { {1,1,1,1,1,1,1,1,1}, {0,1,0,0,0,1,1,0}, {0,0,0,0,0,0,0,0,0}, {1,1,1,1,1,1,1}, {1,0,1,0,1,0,1,0,1}} int m[5][9] = { 1,1,1,1,1,1,1,1,1, 0,1,0,0,0,1,1,0,1, 0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,1,1,1, 1,0,1,0,1,0,1,0,1}

Functions Functions are the building blocks of C programs. Each function is essentially a small program, with its own declarations and statements. Using functions, programs are divided in to small pieces that are easier for us as well as others to understand and modify. Functions can take some of the tedium out of programming by allowing us to avoid duplicating code that’s used more than once.

Defining and Calling Functions double average(double a, double b) { return (a + b) / 2; } avg = average(x/2, y*2); printf(“Average: %g\n”, average(x, y));

Defining and Calling Functions return-type function-name ( parameters ) { declarations statements } Functions may not return arrays; Specifying that the return type is void indicates that the function doesn’t return a value double average(double a, b) /** WRONG **/

An Example prime.c

Function Declarations Declare your functions before you use them double average(double x, double y) { ……….. } int main() { ……….. average(x, y); }

Functions Declarations double average(double x, double y); int main() { ……….. average(x, y); } double average(double x, double y) { ……….. }

Arguments Parameters appear in function definitions: they are dummy names that represent values to be supplied when the function is called Arguments are expressions that appear in function calls.

Arguments --- passed by value When a function is called, each argument is evaluated and its value assigned to the corresponding parameter. Each parameter behaves like a variable that’s been initialized to the value of the matching argument

Arguments int power(int x, int n) { int i, result = 1; for (i = 1; i <= n; i++) result = result * x; return result; } int power(int x, int n) { int result = 1; while(n-- > 0) result *= x; return result; }

Array Arguments int f(int a[]) { …… } int f(int a[]) { int len = sizeof(a) / sizeof(a[0]); /** WRONG **/ ……. }

Array Arguments int f(int a[], int n) { …… }

The return Statement return expression; return 0; return status; return n >= 0 ? n : 0 return;

Program Termination Using return in main is one way to exit the program Calling exit has the same meaning exit belongs to exit(0); exit(EXIT_SUCCESS); exit(EXIT_FAILURE);

Recursion A function is recursive if it calls itself. int fact(int n) { if(n <= 1) return 1; else return n * fact(n-1); }

An Example --- Quicksort