Chapter 7 One-Dimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

Introduction to C Programming
The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
One Dimensional Arrays
Programming and Data Structure
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Chapter 9: Advanced Array Manipulation
Kernighan/Ritchie: Kelley/Pohl:
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Chapter 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
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.
Introduction to Programming with C++ Fourth Edition
C++ for Engineers and Scientists Third Edition
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
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
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
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Chapter 8 Arrays and Strings
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.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
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.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
© 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.
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.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
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)
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
An Introduction to Programming with C++ Sixth Edition
Chapter 10: Pointers 1.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
EKT150 : Computer Programming
Lecture 18 Arrays and Pointer Arithmetic
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 7 Arrays PROGRAMMING IN ANSI C.
Presentation transcript:

Chapter 7 One-Dimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Numeric Arrays 1-D Arrays and Printing Array Elements An array is a data structure in C A grouping of like-type data Topics Definition of arrays Characteristics of an array Using #define directive to define array size Printing array elements

1-D Arrays What is a one-dimension array and how do we declare it? element_type array_name [number_of_elements]; element_type specifies the type of the array’s elements array_name is the name of the array number_of_elements specifies the max number of elements that can be stored in the array.

1-D Arrays int a[2]; declares the name of the array is a the type of array elements is int the dimension is 1 (with one pair of brackets []) the number of elements or size is 2

1-D Arrays How to name an array? Array names are classified as identifiers. c[15], f3_rack[4] (legal identifiers) 12abc[15], y4#[23] (illegal identifiers) How to distinguish between a 1-D, 2-D, or 3-D arrays? int a[2]; int b[2][3]; int c[2][3][2];

1-D Array What is the first index (also called subscript) of an array in C? In C, by default, the first index or subscript is 0. int a[2]; Since C begins at 0, a[0] and a[1] are used in the program body. We should not use a[2] in the program body. With a[2], the program may execute completely and give answers (which very likely will be wrong).

1-D Array How to print an array element using printf? Treat an array element like we treat a single variable printf(“a[0] = %3d, a[1] = %3d”, a[0], a[1]); printf(“b[3] = %8.2f, b[6] = %8.2f”, b[3], b[6]); What types of variables can be used for array indices? Integer type variables such as int char with their modifiers signed, unsigned, short, and long

/* Program for Lesson 6_1 */ #define N 10 void main(void) { int a[2]; double b[N]; a[0]=11; a[1]=22; b[3]=777.7; b[6]=888.8; printf("a[0] = %3d, a[1] = %3d\n", a[0],a[1]); printf("b[3] = %8.2lf, b[6] = %8.2lf \n", b[3],b[6]); printf("b[2] = %lf\n", b[2]); printf("a[3] = %d\n", a[3]); } The results make no sense.

Array Initialization Topics Methods for initializing array elements in declarations Initialing array elements using scanf Initialing array elements using assignment statements in loops

Initialization What are two ways to initialize the elements of a 1-D array in a declaration? 1. type name [number_of_elements] = {value_0, …, value_n}  int a[3] = {11, 22};  a[0] = 11, a[1] = 22, and a[3] = type name [] = {value_0, …, value_n} int b[] = {44, 55, 66}; Advantages: unnecessary to change the number of elements values and count the number of array elements Disadvantages: may lead to out-of-range errors

Initialization How else to initialize an array? Use a read function, such as scanf double x[2]; scanf(“%lf %lf”, &x[0], &x[1]); Use an assignment statement double y[10]; for (i=0; i<10; i++) { y[i] = i*100.0; }

Initialization Would the declaration “int b[2]={44, 55, 66}” have caused an error in C? Yes! Since b has only 2 elements, we listed three. Although C will not detect out-of-range errors with arrays, it will detect this error.

One-Dimensional Arrays The simple built-in data types of the C language - int, float, double, and char - are not sufficient to represent complex data such as lists, tables, vectors, and matrices. A one-dimensional array is a data type derived from the basic built-in data types. In scientific and engineering computations, there are vectors, matrices, and determinants.

The flight path of a space shuttle, projectile or orbiters, as well as pressure measurements under the sea floor, are all recorded and stored as lists using one-dimensional arrays. Operations such as sorting and searching require an entire list of data to be stored in memory. This is accomplished by storing the data in a one- dimensional array.

7.1 One-Dimensional Arrays Subscripts and Subscripted Variables Declaration of Arrays Initialization of Arrays

array An array is a named sequence of memory locations that is used to store data of a homogeneous data type. Each of the named memory locations is called an element of the array. The elements of an array are identified by a subscript or an index. Here are some practical applications: altitude, depth, pressure, velocity, and temperature measurements may be stored in one-dimensional arrays.

Subscripts and Subscripted Variables

Declaration of Arrays

Initialization of Arrays

7.2 Input of One-Dimensional Arrays Array Input The input of data to a one-dimensional array may use indexing, or may use array pointers or dynamic pointers which will be presented in Chapter 10. Input of Parallel Arrays

Array Input

Input of Parallel Arrays

7.3 Output of One-Dimensional Arrays Array Output Output may be displayed on a monitor, printed using a printer, or written to an output data file. Output of Parallel Arrays

Array Output

Output of Parallel Arrays

Manipulation of Arrays Arithmetic, relational, and logical operations can be performed on array elements. An array can be added to another array and the result can be stored in a third. All these operations are performed element-by- element using for loops.

7.4 Manipulation of Arrays Array Assignment Array Arithmetic

Array Assignment

Array Arithmetic

Passing Arrays to Functions Arrays can be passed to functions as though they were single variables; but rather than being a set of values, an array name is an address constant. An entire array is effectively passed by using the array name as an argument, which passes the address of the array. If an array is passed element-by-element as single variables, the elements may be passed by value or explicitly by pointer. It is a normal practice to pass arrays by name into input functions, thus the input values from the function are passed back to the storage allocated in the calling function.

7.5 Passing Arrays to Functions Passing Fixed-Sized Arrays Passing Array Elements

Functions and 1-D Arrays Topics Passing individual array elements to functions Passing entire arrays to functions Passing entire arrays to functions with a restriction Illustrate how values and address of array elements are passed

Function Call How to pass a single array element to a function? We treat a single element like a simple variable. If we want to change the value of the array element in the function, we use the “address of” operator, &, before the array element in the function call. If we want to pass element without having it changed in the function, we simple put the array element in the parameter list. function1(&a[5], a[8]);

Function Prototype When receiving an address, we must use a pointer variable (indicated in the declaration by *). When receiving a value, we use a simple variable. void function1(int *d, int e); function1(&a[5], a[8]); void function1(int *d, int e) Value passed to simple variable Address passed to pointer variable *d = e;

Function Call and Prototype How to pass the ability to access an entire 1-D array to a function? Pass the address of the first element of the array With the address of the first element, C can internally calculate the address of any element in the array. The address of the first element of an array is indicated by the array name with no brackets following it. &c[0] and c are equivalent.

Function Call and Prototype function2(c, 5) Pass the address of the first element of the array c[ ] to function2, which gives function2 the ability to modify the array c[ ]. The prototype for the function must indicate that it is receiving an address. Use * in the declaration void function2 (double *b, int num_elem); Use brackets [ ] void function2 (double b[ ], int num_elem);

Function Call and Prototype function2(c, 5) void function2 (double b[ ], int num_elem); Number of array elements passed as a simple variable Address of the first element passed to pointer variable indicated with brackets

Function Definition Within a function that has received an array’s address, how to work with the array? Must be cognizant of the number of elements that the array contains. void function2 (double b[ ], int num_elem) void function2 (double b[5]) This is correct – A separate parameter is used to transfer the number of elements. This is incorrect – It does not indicate that b[ ] has only five elements.

Passing Fixed-Sized Arrays

Output of an Array in a Function

Manipulation of Arrays in a Function

Passing Array Elements

Sample Programs Reynolds Numbers Stress and Strain Standard Deviation Maximum and Minimum Values Sorting Searching Inventory of an Engineering Sales Company

Bubble Sort What concept underlines a bubble sort? Want to sort the 1-D array, b[4] = {33, 44, 11, 22}, in ascending order. First compare b[0] and b[1]; if b[0] > b[1], swap the values; otherwise, no rearrangement. Perform the same action for b[1] and b[2]. Continue this with the last pair, b[2] and b[3]. This concludes the first round. The largest element, 44, bubbles down and stops in the last element of the array.

Bubble Sort Bubble sort Round 1 b[3] is the largest of the four values Round 2 b[2] is the largest of the three values Round 3 b[1] is the larger of the two values Round 4 b[0] is automatically the smallest Swapping the values of array elements temp = b[1]; copy the value of b[1] into temp b[1] = b[2]; copy the value of b[2] into b[1] b[2] = temp; copy the value of temp into b[2]

#include #define START 0 #define END 4 #define SIZE 10 void main(void) { int i, j, k, b[SIZE], c[SIZE], d[SIZE]; int temp, max, wheremax=END-1, min, wheremin=START; int a[END]={33,44,11,22}; /************************************************ ** INITIALIZE ARRAYS b, c, AND d ************************************************/ for (i=START; i<END ;i++) b[i]=c[i]=d[i]=a[i]; /************************************************ ** BUBBLE SORT ************************************************/ for (i=START;i <END;i++) { for (j=START; j <END-i-1;j++) { if (b[j] > b[j+1] ) { temp=b[j+1]; b[j+1]=b[j] ; b[j]=temp; } Each time through loop is one round for the bubble sort The swap is performed Beginning and ending of portion of array to be sorted

Exchange Maximum Sort Exchange maximum sort Assign the last array element to the variable max. Compare max with the rest of the array elements; if the max is smaller than any array element, then replace max and use the variable wheremax to relocate the new maximum. Continue the process until the max in the array is found. Put the found max at the last element of the array. This completes the first round. Eliminate the last element from the search group. Repeat steps 2, 3, and 4 until the array is in ascending order.

/******************************************* ** EXCHANGE MAXIMUM SORT *******************************************/ for (i = END-1; i >= START; i--) { max = c[i]; for (j = i; j >= START; j--) { if (max <= c[j]) { max = c[j]; wheremax = j; } c[wheremax] = c[i]; c[i] = max; } Each time through loop is one round for the exchange maximum sort The swap is performed Compare max with the rest of the array elements; if the max is smaller than any array element, then replace max and use the variable wheremax to relocate the new maximum. Put the found max at the last element of the array. Eliminate the last element from the search group

Exchange Minimum Sort Similar to the Exchange Maximum Sort Finds the min value and puts it in the first loation In ascending order

/******************************************** ** EXCHANGE MINIMUM SORT ********************************************/ for (i=START;i <END;i++) { min=d[i]; for (j=i;j <END;j++) { if (min >= d[j]) { min=d[j]; wheremin=j; } d[wheremin]=d[i]; d[i]=min; }