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.

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

Introduction to C Programming
One Dimensional Arrays
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to 2-D Arrays.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
Friday, December 29, 2006 Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. - Stan Kelly-Bootle.
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.
Lab 11 rArrays rExercises Note: Read the whole Chapter 8.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
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.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Chapter 7 One-Dimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
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.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Arrays- Part 2 Spring 2013Programming and Data Structure1.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 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.
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.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
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.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Computer Programming for Engineers
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Arrays. Arrays are objects that help us organize large amounts of information.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used 50 minutes 4 or 5 questions write full programs.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
Objectives You should be able to describe: One-Dimensional Arrays
Computer Programming BCT 1113
Lecture 7 Arrays 1. Concept of arrays Array and pointers
© 2016 Pearson Education, Ltd. All rights reserved.
Lecture 10 Arrays.
Arrays.
Arrays and Matrices.
Presentation transcript:

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 of an array is accessed using the array name and an index or subscript The name of the array is the address of the first element and the subscript is the offset In C, the subscripts always start with 0 and increment by 1

Definition and Initialization An array is defined using a declaration statement. data type array_name[size]; –allocates memory for size elements –subscript of first element is 0 –subscript of last element is size-1 –size must be a constant

Example int list[10]; –allocates memory for 10 integer variables –subscript of first element is 0 –subscript of last element is 9 –C does not perform any bounds checking on arrays list[0] list[1] list[9]

Initializing Arrays Arrays can be initialized at the time they are declared. Examples: double taxrate[3] ={0.15, 0.25, 0.3}; char list[5] = {‘h’,’e’,’l’,’l’,’o’}; double vector[100] = {0.0}; /*assigns zero to all 100 elements*/ int s[] = {5,0,-5}; /*the size of a s is 3 */

Assigning values to an array for loops are often used to assign values to an array Example: int list[10], i; for(i=0; i<10; i++) { list[i] = i; }

Input from a data file Arrays are often used to store information from a data file Example int k; double time[10], motion[10]; FILE *sensor3; sensor3 = fopen(“sensor3.txt”, “r”); for(k=0; k<10; k++) { fscanf(sensor3, “%lf %lf”,&time[k], &motion[k]); }

Practice! Show the contents of the arrays defined in each of the following sets of statements. a)int x[10] = {-5, 4, 3}; b)char letters[] = {'a', 'b', 'c'}; c)double z[4]; ……… z[1]=-5.5; z[2]=z[3]=fabs(z[1]); d)int k; double time[9]; ……….. for(k=0;k<=8;k++) time[k]=(k-4)*0.1;

Practice Assume that the variable k and the array s have been defined with the following statement: int k, s[]={3,8,15,21,30,41}; Using a hand calculation, determine the output for each of the following sets of statements: 1. for(k=0;k<=5;k+=2) printf(“%d %d \n”,s[k],s[k+1]); 2. for(k=0;k<=5;k++) if(s[k]%2==0) printf(“%d “,s[k]); printf(“\n”);

Function Arguments When the information in an array is passed to a function, two parameters are usually used. One parameter specifies a particular array. Other parameter specifies the number of elements used in the array.

Passing Entire Arrays as Arguments to Functions Arrays are always pass by reference The array name is the address of the first element The maximum size of the array must be specified at the time the array is declared. The actual number of array elements that are used will vary, so the actual size of the array is usually passed as another argument to the function

Example int main(void) { /* Declare variables and functions */ FILE *exp1; double max (double array[], int actual_size); double x[100]; int count=0; exp1 = fopen(“exp1.txt”, “r”); while((fscanf(exp1, “%f”, &x[count])) == 1) { count++; } printf(“Maximum value: %f \n”, max(x, count)); fclose(exp1); return 0; }//end main

Selection Sort Example: Ascending algorithm Find the minimum value Exchanging the minimum value with the value in the first position in the array Find the next minimum value begin with second element and exchange this minimum value with the second position in the array Continue until the last element

Two-Dimensional Arrays

Definition and Initialization int x[rows][columns]; Example, int x[4][3]; int x[4][3]={{2,3,-1},{0,-3,5},{2,6,3},{-2,10,4}}; int x[][3]={{2,3,-1},{0,-3,5},{2,6,3},{-2,10,4}}; If the initializing sequence is shorter than the array, the rest of the values are initialized to zero.

Nested ‘for’ loop Arrays can also be initialized with program statements. Two nested ‘for ‘ loops are usually required to initialize an array. /* Declare variables */ int i, j, t[5][4]; …………. ………… /* Initialize array. */ for(i=0;i<=4;i++) for(j=0;j<=3;j++) t[i][j]=i;

Example #include int main() { int i,j; int data[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}; for (i=0;i<3;i++) { for(j=0;j<4;j++) printf("%2d ",data[i][j]); printf("\n"); } return 0; }

Matrices A matrix is a set of number arranged in a grid with rows and columns. A matrix is defined using a type declaration statement. –data type array_name[row_size][column_size]; –int matrix[3][4]; row[0] row[1] row[2] in memory row0row1row2

Accessing Array Elements int matrix[3][4]; –matrix has 12 integer elements –matrix[0][0]element in first row, first column –matrix[2][3]element in last row, last column –matrix is the address of the first element –matrix[1] is the address of the second row

Practice! int i, j, g[3][3]={{0,0,0},{1,1,1},{2,2,2}}; Give value of sum after each statements is executed: 1) sum=0; for (i=0;i<=2; i++) for(j=0;j<=2;j++) sum += g[i][j]; 2) sum=1; for(i=1;i<=2;i++) for(j=0;j<=1;j++) sum *= g[i][j]; 3) sum=0; for(j=0;j<=2;j++) sum -= g[2][j]; 4) sum=0; for(i=0;i<=2;i++) sum += g[i][1];

2-Dimensional Arrays as Arguments to Functions Example: void transpose(int b[NROWS][NCOLS], int bt[NCOLS][NROWS]) { /* Declare Variables. */ int i, j; /* Transfer values to the transpose matrix. */ for(i=0; i<NROWS; i++) { for(j=0; j<NCOLS; j++) { bt[j][i] = b[i][j]; } return; }

Character Functions Character I/O Example 1, printf and scanf functions- print and read characters using %c specifier. Example 2, putchar- takes one integer argument, returns an integer value. putchar(‘a’); putchar(‘b’); putchar(‘\n’); putchar(‘c’); same as: putchar(97); putchar(98); putchar(10); putchar(99);

getchar function-reads character from the keyboard and returns the integer value of the character. Example, int c; …. putchar(‘\n’); c=getchar(); putchar(c); putchar(‘\n’);