Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Introduction to C Programming
Pointers Pointer is a variable that contains the address of a variable Here P is sahd to point to the variable C C 7 34……
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
One Dimensional Arrays
Programming and Data Structure
Structures Spring 2013Programming and Data Structure1.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on Arrays Using array elements as function arguments  Examples Using arrays as function.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on One-dimensional Arrays Using array elements as function arguments  Examples Using.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Arrays. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Declare 14 double variables? What about.
Multiple-Subscripted Array
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.
Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
VB Arrays Chapter 8 Dr. John P. Abraham Professor UTPA.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
CSC Programming for Science Lecture 36: Structures.
Pointers *, &, array similarities, functions, sizeof.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
More Array Access Examples Here is an example showing array access logic: const int MAXSTUDENTS = 100; int Test[MAXSTUDENTS]; int numStudents = 0;... //
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
ICS103: Programming in C 7: Arrays Muhamed F. Mudawar.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
UNIT 8 Pointers.
Array of pointers We can have an array whose members are pointers, in this example pointers-to-int. int* data[3]; int i; int x = 5; int y = 89; int z =
1 CSC103: Introduction to Computer and Programming Lecture No 17.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Windows Programming Lecture 03. Pointers and Arrays.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
CS1010 Programming Methodology
Arrays Low level collections.
Pointers & Arrays.
Formatted Input/Output
Array An “average.cpp” program
Formatted Input/Output
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Lecture 18 Arrays and Pointer Arithmetic
INC 161 , CPE 100 Computer Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Initializing variables
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CISC181 Introduction to Computer Science Dr
Arrays I Handling lists of data.
Pointers & Arrays.
Presentation transcript:

Arrays

Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about next semester?

Arrays Declare a list of variables – all with the same type double scores[14]; name[size];

Example //declare the array scores:

Example //declare the array double scores[3]; //put 90.5 in the first box scores:

Example //declare the array double scores[3]; //put 90.5 in the first box scores[0] = 90.5; 90.5scores:

Example //declare the array double scores[3]; //put 90.5 in the first box scores[0] = 90.5; scores[1] = 73; scores[3] = 87; scores:

Alternative double scores[3] = {90.5, 73, 87}; Initializes elements of the array to values given when array is created

Subscripts Subscript describes which box of the array you are dealing with Array of size N has subscripts –0, 1, 2, … (n-1) –array of size 3 – 0, 1, 2 –subscript added to base address of array Subscript can be a variable or expression which produces an integer –scores[i]; –scores[i+5]; If subscript specified does not exist – runtime error

Example scores[1]; scores[3]; //assume i = -2; scores[i+2]; sum = scores[1] + scores[2]; sum = scores[1] + scores[i*8]; scores[2] = scores[1] + 6; scores:

Accessing Array Elements Print all elements of the array scores –Use only one printf statement

Accessing Array Elements Print all elements of the array scores –Use only one printf statement #include #define MAX_SCORES 3 int main(void) { int i; double scores[MAX_SCORES] = {90.5, 72, 87}; for(i = 0; i < MAX_SCORES; i++) { printf("Score %d: %lf\n", i, scores[i]); } return(0); }

Passing Array Elements Write a function to add two elements of an array

Passing Array Elements Write a function to add two elements of an array –How is the function called? double add(double num1, double num2) { return (num1 + num2); }

Passing Array Elements Write a function to add two elements of an array –How is the function called? add(scores[0], scores[1]); … double add(double num1, double num2) { return (num1 + num2); }

Passing Array Elements Write a function to swap two elements of an array

Passing Array Elements Write a function to swap two elements of an array –How do you call swap? void swap(double *num1, double *num2) { double tmp = *num1; *num1 = *num2; *num2 = tmp; }

Passing Array Elements Write a function to swap two elements of an array –How do you call swap? swap(&scores[0], &scores[1]); … void swap(double *num1, double *num2) { double tmp = *num1; *num1 = *num2; *num2 = tmp; }

Passing Arrays Would like to pass an entire array into a function –Sum all elements, prompt user for values Array name is treated as a pointer (to first element of array)

Example add_one(scores, MAX_SCORES); void add_one(double scores_to_add[], int numscores) { int i; for(i = 0; i < MAX_SCORES; i++) { scores_to_add[i] = scores_to_add[i] + 1; }

Misc Parallel Arrays Partially filled arrays