Chapter 7 Working With Arrays

Similar documents
arrays C Programming Language - Arrays

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

Sources: On the Web: Slides will be available on:

Lecture 22: C Programming 4 Embedded Systems

Answers to Review Questions Chapter 7

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Beginner s Matlab Tutorial

Project 2: Bejeweled

Introduction to Data Structures

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

5 Arrays and Pointers

Chapter One Introduction to Programming

Informatica e Sistemi in Tempo Reale

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

The C Programming Language

CP Lab 2: Writing programs for simple arithmetic problems

Chapter 7: Additional Topics

Goals for This Lecture:

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

In this Chapter you ll learn:

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

Excel & Visual Basic for Applications (VBA)

Programming Database lectures for mathema

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Using C# for Graphics and GUIs Handout #2

CS 241 Data Organization Coding Standards

The C Programming Language course syllabus associate level

MATLAB Functions. function [Out_1,Out_2,,Out_N] = function_name(in_1,in_2,,in_m)

Solving Systems of Linear Equations Using Matrices

A Java array is kind of like a credit card holder Sleeves to put credit cards in

2+2 Just type and press enter and the answer comes up ans = 4

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

G563 Quantitative Paleontology. SQL databases. An introduction. Department of Geological Sciences Indiana University. (c) 2012, P.

VB.NET Programming Fundamentals

An Incomplete C++ Primer. University of Wyoming MA 5310

Beginning to Program Python

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Time Limit: X Flags: -std=gnu99 -w -O2 -fomitframe-pointer. Time Limit: X. Flags: -std=c++0x -w -O2 -fomit-frame-pointer - lm

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

C++ INTERVIEW QUESTIONS

Solving Problems Recursively

Common Beginner C++ Programming Mistakes

Darshan Institute of Engineering & Technology PL_SQL

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Lecture Notes on Linear Search

Parameter Passing in Pascal

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

Programming LEGO NXT Robots using NXC

C Programming Structure of a C18 Program

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

The Program Data Vector As an Aid to DATA step Reasoning Marianne Whitlock, Kennett Square, PA

Comp 255Q - 1M: Computer Organization Lab #3 - Machine Language Programs for the PDP-8

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Computer Science 2nd Year Solved Exercise. Programs 3/4/2013. Aumir Shabbir Pakistan School & College Muscat. Important. Chapter # 3.

(Eng. Hayam Reda Seireg) Sheet Java

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Chapter 07: Instruction Level Parallelism VLIW, Vector, Array and Multithreaded Processors. Lesson 05: Array Processors

13 Classes & Objects with Constructors/Destructors

Matrix Multiplication

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Factoring Quadratic Expressions

Object Oriented Software Design II

Introduction to Java

Repetition Using the End of File Condition

Tutorial on C Language Programming

C Programming. Charudatt Kadolkar. IIT Guwahati. C Programming p.1/34

Iteration CHAPTER 6. Topic Summary

CREATING A 12 TONE MATRIX: "My music is not modern, it is merely badly played." A. Schoenberg

Data Structures. Algorithm Performance and Big O Analysis

1 Description of The Simpletron

6.s096. Introduction to C and C++

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

Objectives. Overview of OpenMP. Structured blocks. Variable scope, work-sharing. Scheduling, synchronization

C++ Programming Language

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

The goal is to program the PLC and HMI to count with the following behaviors:

Computational Mathematics with Python

Passing 1D arrays to functions.

Data Structures using OOP C++ Lecture 1

Moving from CS 61A Scheme to CS 61B Java

Programmierpraktikum

Simple C Programs. Goals for this Lecture. Help you learn about:

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

CS1112 Spring 2014 Project 4. Objectives. 3 Pixelation for Identity Protection. due Thursday, 3/27, at 11pm

C++FA 5.1 PRACTICE MID-TERM EXAM

Arrays in Java. Working with Arrays

6. Standard Algorithms

Introduction to Matlab

9.4. The Scalar Product. Introduction. Prerequisites. Learning Style. Learning Outcomes

J a v a Quiz (Unit 3, Test 0 Practice)

Lecture 2 Mathcad Basics

Lab Experience 17. Programming Language Translation

HY345 Operating Systems

1 Abstract Data Types Information Hiding

Transcription:

Chapter 7 Working With Arrays Learning Objectives: Understand the Purpose of 1-D arrays and vectors Understand the differences between arrays and vectors Be able to declare 1-D arrays Be able to use 1-D arrays Going out of bounds with an array Swapping Values #define 1

What are Vectors and Arrays? Vectors and arrays are both single variables that can hold a list of values/objects. They have a name and index. Example of an array: double a[5]; //declare an array called a that can hold 5 double values for (int i = 0; i<5; i++) //this will read in 5 numbers, the first { //will be stored as a[0], 2 nd as a[1], etc printf("enter Number %i: ", i); scanf("%lf", &a[i]); } double sum=a[2] + a[4]; //this will add the 3 rd and 5 th //number Why 3 rd and 5 th, not 2 nd and 4 th? Array Index Starts with 0 2

Using Arrays Array elements can be used the same way any other variable is used: Example using the array called a: for (int n=0; n<5; n++) //This loop sums all values in a { } sum=sum+a[n]; The difference is that each time you use it, you need to specify which value in the array you are referring to. You do this with the index (value in square brackets). Why would you want to use arrays or vectors? You want to store and use a lot of values. You want to be able to loop through a series of values. 3

What are the main differences between an array and a vector? 1. You can change the size of a vector within the program. Once an array is declared, you can t change it s size. Some compilers allow you to use a variable size to start with, but once set you can t change it. 2. Vectors have functions associated with them, arrays don t. 3. Vectors can hold any data type, including other class objects, but arrays are limited to the original C data types. Once you declare a vector it works just like an array: a[ i ] could be an element from either a vector or from an array We will start with arrays then we ll cover vectors. 4

Declaring Arrays Syntax: type name[size]; Examples: int b[4]; What type of values does b hold? int How many values can you store in b? How do you refer to the 1 st element? 4 b[0] double r [1025]; How do you refer to the last element? r[1024] char letters[256]; What is the name of this array? letters How do you print the 8 th element? printf( %c, letters[7]); 5

Initializing Arrays Initializing arrays with values: int b[4]={1,3,2,-6}; //array where b[0] is 1, b[1] is 3, etc. int x[ ] = {1,3,2,-6}; //does the same thing with auto sizing Or, you can loop through each value and set it. For Example: int y[4]; for(int i=0; i<4; i++) { y[i] = -1; } What does this loop do? Sets all values in y to -1 Remember you can use an array element just like any other variable!! in expressions, printfs, scanfs, assignments, conditions, etc 6

Going out of bounds with an Array Example: int k = 4; int a[2]; printf( k = %i\n",k); a[0] = 9; Output: a[1] = 10; What happened to k? a[2] = 6; a[3] = 7; printf( k = %i\n",k); lgarriso@kec107 /cygdrive/h/cs101 $./scanfarray k = 4 k = 7 When we assigned a value to a[3], we overwrote the value in k s memory slot. Bottom Line: If you go out of bounds of your array, unpredictable things may happen. 7 Many times you ll get a Segmentation Fault Core Dump

Swapping Values in an Array Example: //This code swaps the values of a[0] and a[1] int a[2]; a[0] = 9; a[1] = 10; int temp = a[0]; a[0] = a[1]; a[1] = temp; Why did I need the variable temp? Because a[0] = a[1] overwrites a[0]. If I want to save the value of a[0], I need to store it somewhere. 8

Summary of 1-D Arrays Array = Variable that can hold more than one value Example declaration of a 1-D array: int x[4]; //declare an array called x that can hold 4 integer values indices 0 1 x memory slots 2 3 3 What would happen if you wrote the command: x[2] = 3; The index can be a number or a variable or an expression. Is this ok?: x[ i + 1] = 5; Yes, if i is an integer and has a value 9

#define Preprocessor Directive Notice that when using arrays, we usually also use loops and we loop through the size(s). Therefore, we find the size when we declare the array and every time we loop through it. If we want to change the size of the array in our program, we have to change it in multiple places. It is very easy to miss some. Instead, we can use #define for our array size(s). When you use #define, you create a name that represents a value that can be used throughout the program. It is like declaring a variable that can be used throughout the program, but can t be changed within the program. Whenever you use the name, the value you defined it with will be substituted. It is good for constants (like Pi) and for array sizes. 10

#define Preprocessor Directive syntax: #define name value Note that there is no semicolon. This goes under your #includes. Example (Note the convention is to make the name all-caps): #include <stdio.h> #define SIZE 7 int main() { } double a[size]; for(int i = 0; i < SIZE; i++) { } return 0; //other code goes here Note: you can have as many #defines as you want. For a 2-D array, you might want to have one for ROWS and one for COLUMNS. You could now change SIZE in just one place (at the top) 11and the array size and loop condition will also be changed.