Array Lesson 1 CS1313 Spring 2015 1 Array Lesson 1 Outline 1.Array Lesson 1 Outline 2.Mean of a List of Numbers 3.mean: Declarations 4.mean: Greeting,

Slides:



Advertisements
Similar presentations
Standard I/O Lesson Outline
Advertisements

Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
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:
File I/O Lesson CS1313 Spring File I/O Lesson Outline 1.File I/O Lesson Outline 2.File I/O Using Redirection #1 3.File I/O Using Redirection #2.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
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 of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
User Defined Functions Lesson 2 CS1313 Spring User Defined Functions 2 Outline 1.User Defined Functions 2 Outline 2.Argument Order When Passing.
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
For Loop Lesson 3 CS1313 Spring for Loop Lesson 3 Outline 1. for Loop Lesson 3 Outline 2. for Loop with a float Counter: BAD! 3. float Counter Example.
For Loop Lesson 2 CS1313 Spring for Loop Lesson 2 Outline 1. for Loop Lesson 2 Outline 2. for Loop Application 3. Factorial 4. Factorial Program.
Characters & Strings Lesson 2 CS1313 Spring Characters & Strings Lesson 2 Outline 1.Characters & Strings Lesson 2 Outline 2.Character String Declaration.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Numeric Data Types Lesson CS1313 Spring Numeric Data Types Outline 1.Numeric Data Types Outline 2.Data Types 3.Integers in Mathematics 4.Integers.
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Array Lesson 2 CS1313 Spring Array Lesson 2 Outline 1.Array Lesson 2 Outline 2.Reading Array Values Using for Loop #1 3.Reading Array Values Using.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
For Loop Lesson 1 CS1313 Spring for Loop Lesson 1 Outline 1. for Loop Lesson 1 Outline 2.A while Loop That Counts #1 3.A while Loop That Counts.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
Structures Lesson CS1313 Spring Structures Lesson Outline 1.Structures Lesson Outline 2.Beyond Arrays 3.A Company and Its Employees #1 4.A Company.
Variables & Constants Lesson 2 Outline
Boolean Data Lesson CS1313 Fall Boolean Data Outline 1.Boolean Data Outline 2.Data Types 3.C Boolean Data Type: char or int 4.C Built-In Boolean.
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.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
ICS103 Programming in C Lecture 11: Arrays I
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
User Defined Functions Lesson 1 CS1313 Spring User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
If Lesson 1 CS1313 Fall if Lesson 1 Outline 1.if Lesson 1 Outline 2.Absolute Value 3.Absolute Value Definition 4.Absolute Value Implementation 5.What.
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.
Idiotproofing Lesson CS1313 Spring Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.Idiotproofing Example.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Arrays. Arrays are objects that help us organize large amounts of information.
Sorting Lesson CS1313 Spring Sorting Lesson Outline 1.Sorting Lesson Outline 2.How to Sort an Array? 3.Many Sorting Algorithms 4.Bubble Sort #1.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
CS 108 Computing Fundamentals Notes for Thursday, February 18, 2016.
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.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
Boolean Data Lesson #1 Outline
Arrays in C.
Character Strings Lesson Outline
Boolean Data Lesson #1 Outline
Array Lesson 1 Outline Array Lesson 1 Array Lesson 1 Outline
Sorting Lesson Outline
User Defined Functions 2 Outline
EKT150 : Computer Programming
Lecture 18 Arrays and Pointer Arithmetic
for Loop Lesson 3 Outline
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Boolean Data Outline Boolean Data Lesson Boolean Data Outline
ICS103 Programming in C Lecture 12: Arrays I
Arithmetic Expressions Lesson #1 Outline
Getting Started With Coding
Presentation transcript:

Array Lesson 1 CS1313 Spring Array Lesson 1 Outline 1.Array Lesson 1 Outline 2.Mean of a List of Numbers 3.mean: Declarations 4.mean: Greeting, Input 5.mean: Calculation 6.mean: Output 7.mean: Compile, Run 8.mean: 6 Input Values 9.mean: 7 Input Values 10.mean: One Line Different 11.mean: Compile, Run for 6 12.mean: Compile, Run for 7 13.Scalars #1 14.Scalars #2 15.Another Scalar Example 16.A Similar Program, with Multiplication 17.A Similar Program, with a Twist 18.Arrays 19.Array Element Properties 20.Array Properties #1 21.Array Properties #2 22.Array Properties #3 23.Array Properties #4 24.Array Properties #5 25.Array Indices #1 26.Array Indices #2 27.Multidimensional Arrays & 1D Arrays 28.Array Declarations #1 29.Array Declarations #2 30.Array Declarations #3 31.Assigning a Value to an Array Element 32.Array Element Assignment Example 33.Getting Array Element Value with scanf 34.Array Element scanf Example #1 35.Array Element scanf Example #2 36.for Loops for Tasks on Arrays #1 37.for Loops for Tasks on Arrays #2 38.Another for/Array Example #1 39.Another for/Array Example #2 40.Another for/Array Example #3 41.Don’t Need to Use Entire Declared Length

Array Lesson 1 CS1313 Spring Mean of a List of Numbers Consider a list of real numbers of length n elements: x 1, x 2, x 3, …, x n The mean of this list is: (x 1 + x 2 + x 3 + … + x n ) / n

Array Lesson 1 CS1313 Spring mean: Declarations #include int main () { /* main */ const float initial_sum = 0.0; const int number_of_elements = 4; const int first_element = 0; const int program_success_code = 0; float input_value[number_of_elements]; float sum; float mean; int element;

Array Lesson 1 CS1313 Spring mean: Greeting, Input printf("I'm going to calculate the arithmetic\n"); printf(" mean of a list of length %d values.\n", number_of_elements); printf("What are the %d values of the list?\n", number_of_elements); for (element = first_element; element < number_of_elements; element++) { scanf("%f", &input_value[element]); } /* for element */

Array Lesson 1 CS1313 Spring mean: Calculation sum = initial_sum; for (element = first_element; element < number_of_elements; element++) { sum += input_value[element]; } /* for element */ mean = sum / number_of_elements;

Array Lesson 1 CS1313 Spring mean: Output printf("The %d input values of the list are:\n", number_of_elements); for (element = first_element; element < number_of_elements; element++) { printf("%f ", input_value[element]); } /* for element */ printf("\n"); printf("The mean of the %d values", number_of_elements); printf(" in the list is %f.\n", mean); return program_success_code; } /* main */

Array Lesson 1 CS1313 Spring mean: Compile, Run % gcc -o mean4 mean4.c % mean4 I'm going to calculate the arithmetic mean of a list of length 4 values. What are the 4 values of the list? The 4 input values of the list are: The mean of the 4 values in the list is

Array Lesson 1 CS1313 Spring mean: 4 Input Values #include int main () { /* main */ const float initial_sum = 0.0; const int number_of_elements = 4; const int first_element = 0; const int program_success_code = 0; float input_value[number_of_elements]; float sum; float mean; int element;

Array Lesson 1 CS1313 Spring mean: 7 Input Values #include int main () { /* main */ const float initial_sum = 0.0; const int number_of_elements = 7; const int first_element = 0; const int program_success_code = 0; float input_value[number_of_elements]; float sum; float mean; int element; The rest of the program is EXACTLY THE SAME!

Array Lesson 1 CS1313 Spring mean: One Line Different % diff mean4.c mean7.c 6c6 < const int number_of_elements = 4; --- > const int number_of_elements = 7; The diff Unix command compares two files of text and shows which lines are different. The only statement that differs between mean4.c and mean7.c is the declaration of number_of_elements.

Array Lesson 1 CS1313 Spring mean: Compile, Run for 4 % gcc -o mean4 mean4.c % mean4 I'm going to calculate the arithmetic mean of a list of length 4 values. What are the 4 values of the list? The 4 input values of the list are: The mean of the 4 values in the list is

Array Lesson 1 CS1313 Spring mean: Compile, Run for 7 % gcc -o mean7 mean7.c % mean7 I'm going to calculate the arithmetic mean of a list of length 7 values. What are the 7 values of the list? The 7 input values of the list are: The mean of the 7 values in the list is

Array Lesson 1 CS1313 Spring Scalars #1 % cat scalar_names.c #include int main () { /* main */ int b, c, d, e, f; b = 0; c = 2; d = 4; e = 6; f = 8; printf("b = %d\n", b); printf("c = %d\n", c); printf("d = %d\n", d); printf("e = %d\n", e); printf("f = %d\n", f); return 0; } /* main */ % gcc -o scalar_names \ scalar_names.c % scalar_names b = 0 c = 2 d = 4 e = 6 f = 8 Note that, in Unix, a backslash at the end of a Unix command line means: “continue this Unix command on the next line.”

Array Lesson 1 CS1313 Spring Scalars #2 % cat scalar_names.c #include int main () { /* main */ int b, c, d, e, f; b = 0; c = 2; d = 4; e = 6; f = 8; printf("b = %d\n", b); printf("c = %d\n", c); printf("d = %d\n", d); printf("e = %d\n", e); printf("f = %d\n", f); return 0; } /* main */ All of the variables in the program are simple int variables. Each of the individual int variables has a single name, a single address, a single data type and a single value. Such variables, whether their type is int, float, char or whatever, are referred to as scalar variables.

Array Lesson 1 CS1313 Spring Another Scalar Example % cat scalar_a.c #include int main () { /* main */ int a0, a1, a2, a3, a4; a0 = 0; a1 = 2; a2 = 4; a3 = 6; a4 = 8; printf("a0 = %d\n", a0); printf("a1 = %d\n", a1); printf("a2 = %d\n", a2); printf("a3 = %d\n", a3); printf("a4 = %d\n", a4); return 0; } /* main */ % gcc -o scalar_a \ scalar_a.c % scalar_a a0 = 0 a1 = 2 a2 = 4 a3 = 6 a4 = 8 The only difference between this program and the previous program is the names of the scalar variables (and therefore some of the output).

Array Lesson 1 CS1313 Spring A Similar Program, with Multiplication % cat scalar_mult.c #include int main () { /* main */ int a0, a1, a2, a3, a4; a0 = 0 * 2; a1 = 1 * 2; a2 = 2 * 2; a3 = 3 * 2; a4 = 4 * 2; printf("a0 = %d\n", a0); printf("a1 = %d\n", a1); printf("a2 = %d\n", a2); printf("a3 = %d\n", a3); printf("a4 = %d\n", a4); return 0; } /* main */ % gcc -o scalar_mult \ scalar_mult.c % scalar_mult a0 = 0 a1 = 2 a2 = 4 a3 = 6 a4 = 8 Notice that, in this program, the values of the scalar variables are obtained by multiplying a constant by the number associated with the scalar variable.

Array Lesson 1 CS1313 Spring A Similar Program, with a Twist % cat array_mult.c #include int main () { /* main */ int a[5]; a[0] = 0 * 2; a[1] = 1 * 2; a[2] = 2 * 2; a[3] = 3 * 2; a[4] = 4 * 2; printf("a[0] = %d\n", a[0]); printf("a[1] = %d\n", a[1]); printf("a[2] = %d\n", a[2]); printf("a[3] = %d\n", a[3]); printf("a[4] = %d\n", a[4]); return 0; } /* main */ % gcc -o array_mult \ array_mult.c % array_mult a[0] = 0 a[1] = 2 a[2] = 4 a[3] = 6 a[4] = 8 Huh?

Array Lesson 1 CS1313 Spring Arrays int a[5]; An array is a special kind of variable. Like a scalar variable, an array has: a name; an address; a data type. But instead of an array having exactly one single value, it can have multiple values. Each of these values is referred to as an element of the array. If you’re familiar with vectors in mathematics, you can think of an array as the equivalent idea, but in computing instead of in mathematics.

Array Lesson 1 CS1313 Spring Array Element Properties Each of the elements of an array is just about exactly like a scalar variable of the same data type. An element of an array has: 1.a name, which it shares with all of the other elements of the array that it belongs to; 2.an address, which we’ll learn about shortly; 3.a data type, which it shares with all of the other elements of the array that it belongs to; 4.a single value. But, an element of an array also has: 5.an index, which we’ll learn about shortly.

Array Lesson 1 CS1313 Spring Array Properties #1 int a[5]; An array as a whole has the following properties: 1.It has a data type, which is the data type of each of its elements; for example, int.

Array Lesson 1 CS1313 Spring Array Properties #2 int a[5]; An array as a whole has the following properties: 2.It as a dimension attribute, sometimes called its length, which describes the number of elements in the array; for example, [5].

Array Lesson 1 CS1313 Spring Array Properties #3 int a[5]; An array as a whole has the following properties: 3.It has exactly as many values as it has elements, and in fact each of its elements contains exactly one of its values.

Array Lesson 1 CS1313 Spring Array Properties #4 int a[5]; An array as a whole has the following properties: 4.Its elements are accessed via indexing with respect to the variable name; for example, a[2] = 7;

Array Lesson 1 CS1313 Spring Array Properties #5 int a[5]; An array as a whole has the following properties: 5.Its elements are contiguous in memory; for example, ? a[0] ? a[1] ? a[2] ? a[3] ? a[4] a[0] ???????? Address a[1] ???????? Address a[2] ???????? Address a[3] ???????? Address a[4] ???????? Address 12356

Array Lesson 1 CS1313 Spring Array Indices #1 int a[5]; We access a particular element of an array using index notation: a[2] This notation is pronounced “ a of 2 ” or “ a sub 2.” The number in square brackets – for example, the 2 in a[2] – is called the index or subscript of the array element. Array indices are exactly analogous to subscript numbers in mathematics: a 0, a 1, a 2, a 3, a 4

Array Lesson 1 CS1313 Spring Array Indices #2 int a[5]; An individual element of an array – for example, a[2] – has exactly the same properties as a scalar variable of the same data type – except for being accessed via indexing. Notice that the elements of an array are numbered from 0 through ( length - 1) ; in the above example, the elements of a are a[0], a[1], a[2], a[3], a[4]

Array Lesson 1 CS1313 Spring Multidimensional Arrays & 1D Arrays An array can have multiple dimensions: int array2d[8][5]; For now, we’re going to concentrate on arrays with only one dimension. A one-dimensional array is sometimes called a vector, because of the close relationship between arrays in computing and vectors in mathematics.

Array Lesson 1 CS1313 Spring Array Declarations #1 The general form of an array declaration is: type arrayname1[ dimension1 ], arrayname2[ dimension2 ],... ; For example: int a[8], b[4], c[9]; causes the compiler to set up three int arrays in memory.

Array Lesson 1 CS1313 Spring Array Declarations #2 int a[5], b[4], c[9]; causes the compiler to set up three int arrays in memory, like so: ? a[0] ? a[1] ? a[2] ? a[3] ? a[4] ? b[0] ? b[1] ? b[2] ? b[3] ? c[0] ? c[1] ? c[2] ? c[3] ? c[4] ? c[5] ? c[6] ? c[7] ? c[8]

Array Lesson 1 CS1313 Spring Array Declarations #3 int a[8], b[4], c[9]; In principle, these arrays could be remote from each other in memory (for example, a could start at address 12340, b could start at address and c could start at address ). In practice, they are usually contiguous or almost contiguous in memory; that is, the last byte of array a will typically be contiguous with the first byte of array b, and the last byte of array b will typically be contiguous with the first byte of array c. However, the compiler isn’t required to make the different arrays contiguous in memory. The only contiguity constraint is that, within each array, all of the elements are contiguous and sequential.

Array Lesson 1 CS1313 Spring Assigning a Value to an Array Element Because an individual array element is exactly analogous to a scalar variable, we can assign or input a value into it in exactly the same ways that we assign or input values into scalar variables. For example, we can use a scalar assignment for each individual element.

Array Lesson 1 CS1313 Spring Array Element Assignment Example % cat arrayeltassn.c #include int main () { /* main */ int a[3]; a[0] = 5; a[1] = 16; a[2] = -77; printf("a[0] = %d\n", a[0]); printf("a[1] = %d\n", a[1]); printf("a[2] = %d\n", a[2]); return 0; } /* main */ % gcc -o arrayeltassn \ arrayeltassn.c % arrayeltassn a[0] = 5 a[1] = 16 a[2] = -77

Array Lesson 1 CS1313 Spring Getting Array Element Value with scanf Just as we can assign a value to an individual array element, we can use scanf to obtain the value of each individual array element.

Array Lesson 1 CS1313 Spring Array Element scanf Example #1 #include int main () { /* main */ float a[3]; printf("Input a[0],a[1],a[2]:\n"); scanf("%f %f %f", &a[0], &a[1], &a[2]); printf("a[0] = %f\n", a[0]); printf("a[1] = %f\n", a[1]); printf("a[2] = %f\n", a[2]); return 0; } /* main */

Array Lesson 1 CS1313 Spring Array Element scanf Example #2 % gcc -o arrayeltread arrayeltread.c % arrayeltread Input a[0],a[1],a[2]: a[0] = a[1] = a[2] =

Array Lesson 1 CS1313 Spring for Loops for Tasks on Arrays #1 #include int main () { /* main */ const int a_length = 5; int a[a_length]; int count; for (count = 0; count < a_length; count++) { a[count] = 2 * count; } /* for count */ for (count = 0; count < a_length; count++) { printf("a[%2d] = %2d\n", count, a[count]); } /* for count */ return 0; } /* main */

Array Lesson 1 CS1313 Spring for Loops for Tasks on Arrays #2 % gcc -o array_for_mult array_for_mult.c % array_for_mult a[ 0] = 0 a[ 1] = 2 a[ 2] = 4 a[ 3] = 6 a[ 4] = 8

Array Lesson 1 CS1313 Spring Another for /Array Example #1 #include int main () { /* main */ const int minimum_a_length = 1; const int maximum_a_length = 15; const int program_failure_code = -1; const int program_success_code = 0; int a[maximum_a_length]; int a_length; int count; printf("How long will the array be (%d to %d)?\n", minimum_a_length, maximum_a_length); scanf("%d", &a_length); if ((a_length < minimum_a_length) || (a_length > maximum_a_length)) { printf("That’s not a valid array length!\n"); exit(program_failure_code); } /* if ((a_length < minimum_a_length) ||...) */

Array Lesson 1 CS1313 Spring Another for /Array Example #2 for (count = 0; count < a_length; count++) { a[count] = 2 * count; } /* for count */ for (count = 0; count < a_length; count++) { printf("a[%2d] = %2d\n", count, a[count]); } /* for count */ return program_success_code; } /* main */

Array Lesson 1 CS1313 Spring Another for /Array Example #3 % gcc -o array_for_mult_read array_for_mult_read.c % array_for_mult_read How long will the array be (1 to 15)? 0 That’s not a valid array length! % array_for_mult_read How long will the array be (1 to 15)? 16 That’s not a valid array length! % array_for_mult_read How long will the array be (1 to 15)? 5 a[ 0] = 0 a[ 1] = 2 a[ 2] = 4 a[ 3] = 6 a[ 4] = 8

Array Lesson 1 CS1313 Spring Don’t Need to Use Entire Declared Length #include int main () { /* main */ const int minimum_a_length = 1; const int maximum_a_length = 15; const int program_failure_code = -1; const int program_success_code = 0; int a[maximum_a_length];... } /* main */... % array_for_mult_read How long will the array be (1 to 15)? 5 a[ 0] = 0 a[ 1] = 2 a[ 2] = 4 a[ 3] = 6 a[ 4] = 8 Notice that we can declare an array to be larger than the portion of the array that we actually use, because RAM is cheap.