CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Kernighan/Ritchie: Kelley/Pohl:
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
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.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Introduction to C Programming CE Lecture 9 Data Structures Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Array.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
A First Book of ANSI C Fourth Edition
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 Pearson Education, Inc. All rights reserved Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Arrays in C.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
WEEK 6 Class Activities Lecturer’s slides.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
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.
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.
Principle Prog Revision. Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int.
Computer Programming for Engineers
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
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.
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
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.
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.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Objectives You should be able to describe: One-Dimensional Arrays
Dynamic memory allocation and Intraprogram Communication.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
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.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
1-d Arrays.
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Declarations CSCI N305
Array 9/8/2018.
Module 2 Arrays and strings – example programs.
Arrays, For loop While loop Do while loop
Lecture 10 Arrays.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Presentation transcript:

CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1

Outline Working with arrays [chap 7 – Kochan] ◦The concept of array ◦Defining arrays ◦Initializing arrays ◦Character arrays ◦Multidimensional arrays ◦Variable length arrays

The concept of array Array: a set of ordered data items You can define a variable called x, which represents not a single value, but an entire set of values. Each element of the set can then be referenced by means of a number called an index number Mathematics: a indexed variable, x i, refers to the ith element x in a set C programming: the equivalent notation is x[i]

Declaring an array Declaring an array variable: ◦Declaring the type of elements that will be contained in the array—such as int, float, char, etc. ◦Declaring the maximum number of elements that will be stored inside the array. ◦The C compiler needs this information to determine how much memory space to reserve for the array.) ◦This must be a constant integer value The range for valid index values in C: ◦First element is at index 0 ◦Last element is at index [size-1] ◦It is the task of the programmer to make sure that array elements are referred by indexes that are in the valid range ! The compiler cannot verify this, and it comes to severe runtime errors !

Arrays - Example Example: int values[10]; Declares an array of 10 elements of type int Using Symbolic Constants for array size: #define N 10 … int values[N]; Valid indexes: values[0]=5; values[9]=7; Invalid indexes: values[10]=3; values[-1]=6; In memory: elements of an array are stored at consecutive locations

Arrays - Example #include int main () { int n[ 10 ]; /* n is an array of 10 integers */ int i,j; /* initialize elements of array n to 0 */ for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; /* set element at location i to i */ } /* output each array element's value */ for (j = 0; j < 10; j++ ) { printf("Element[%i] = %i\n", j, n[j] ); } return 0; }

Arrays - Example

#include #define N 6 int main (void) { int values[N]; int index; for ( index = 0; index < N; ++index ) { printf(“Enter value of element #%i \n”,index); scanf(“%i”, &values[index]); } for ( index = 0; index < N; ++index ) printf ("values[%i] = %i\n", index, values[index]); return 0; } Using symbolic constants for array size makes program more general Typical loop for processing all elements of an array

What goes wrong if an index goes out of range ? #include #define NA 4 #define NB 4 int main (void) { int b[NB],a[NA]; int index; for ( index = 0; index < NB; ++index ) b[index]=10+index; for ( index = 0; index < NA+2; ++index ) a[index]=index; for ( index = 0; index < NB; ++index ) printf ("b[%i] = %i ", index, b[index]); for ( index = 0; index < NA+2; ++index ) printf ("a[%i] = %i ", index, a[index]); printf("\n"); return 0; }

C program to find the sum marks of n students using arrays #include int main(){ int marks[10],i,n,sum=0; printf("Enter number of students: "); scanf("%d",&n); for(i=0;i<n;++i){ printf("Enter marks of student%d: ",i+1); scanf("%d",&marks[i]); sum+=marks[i]; } printf("Sum= %d",sum); return 0; }

C program to find the sum marks of n students using arrays

Initializing arrays int counters[5] = { 0, 0, 0, 0, 0 }; char letters[5] = { 'a', 'b', 'c', 'd', 'e' }; float sample_data[500] = { 100.0, 300.0, }; The C language allows you to define an array without specifying the number of elements. If this is done, the size of the array is determined automatically based on the number of initialization elements: int counters[] = { 0, 0, 0, 0, 0 };

Character arrays #include int main (void) { char word[] = { 'H', 'e', 'l', 'l', 'o', '!' }; int i; for ( i = 0; i < 6; ++i ) printf ("%c", word[i]); printf ("\n"); return 0; } a special case of character arrays: the character string type =>in a later chapter

Multidimensional arrays C language allows arrays of any number of dimensions Two-dimensional array: matrix int M[4][5]; // matrix, 4 rows, 5 columns M[i][j] – element at row i, column j int M[4][5] = { { 10, 5, -3, 17, 82 }, { 9, 0, 0, 8, -7 }, { 32, 20, 1, 0, 14 }, { 0, 0, 8, 7, 6 } }; int M[4][5] = { 10, 5, -3, 17, 82, 9, 0, 0, 8, -7, 32,20, 1, 0, 14, 0, 0, 8, 7, 6 };

Example: Typical matrix processing #define N 3 #define M 4 int main(void) { int a[N][M]; int i,j; /* read matrix elements */ for(i = 0; i < N; i++) for(j = 0; j< M; j++) { printf("a[%d][%d] = ", i, j); scanf("%d", &a[i][j]); } /* print matrix elements */ for(i = 0; i < N; i++) { for(j = 0; j< M; j++) printf("%5d", a[i][j]); printf("\n"); } return 0; }

Example: Dealing with variable numbers of elements #include #define NMAX 4 int main(void) { int a[NMAX]; int n; int i; printf("How many elements(maximum %d)?\n",NMAX); scanf("%d",&n); if (n>NMAX) { printf(“Number too big !\n”); return 1; } for(i = 0; i < n; i++) scanf("%d", &a[i]); for(i = 0; i < n; i++) printf("%5d", a[i]); printf("\n"); return 0; }

Variable length arrays A feature introduced by C99 It was NOT possible in ANSI C ! int a[n]; The array a is declared to contain n elements. This is called a variable length array because the size of the array is specified by a variable and not by a constant expression. The value of the variable must be known at runtime when the array is created => the array variable will be declared later in the block of the program Possible in C99: variables can be declared anywhere in a program, as long as the declaration occurs before the variable is first used. A similar effect of variable length array could be obtained in ANSI C using dynamic memory allocation to allocate space for arrays while a program is executing.

Example: Variable length arrays #include int main(void) { int n; int i; printf("How many elements do you have ? \n"); scanf("%d",&n); int a[n]; for(i = 0; i < n; i++) scanf("%d", &a[i]); for(i = 0; i < n; i++) printf("%5d", a[i]); printf("\n"); return 0; } Array a of size n created. Value of n must be set at runtime before arriving at the array declaration !

Example: variable length arrays Variable-length array does NOT mean that you can modify the length of the array after you create it ! Once created, a VLA keeps the same size ! #include int main(void) { int n; int i; n=7; int a[n]; for(i = 0; i < n; i++) a[i]=i n=20; for(i = 0; i < n; i++) a[i]=2*i; printf("\n"); return 0; } Wrong! Array a created of size 7