CSC 107 - Programming for Science Lecture 26: Arrays.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
1 C++ Syntax and Semantics The Development Process.
CSC Programming for Science Lecture 5: Actual Programming.
CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Variables and Constants
CSC Programming for Science Lecture 30: Pointers.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
CSC 107 – Programming For Science. Today’s Goal  Learn how pointers really used with arrays  Exploit the similarity between pointers & arrays  Take.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
CSC 107 – Programming For Science. Announcements.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
Lecture 2 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout Data types.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Primitive Variables.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. The Week’s Goal.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Primitive Variables.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
CSC 107 – Programming For Science. Announcements.
CSC 212 – Data Structures Lecture 26: Hash Tables.
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
CSC Programming for Science Lecture 4: Beginning Programming.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
Lecture 3 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout from Lecture 1 Background.
PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
CSC Programming for Science Lecture 8: Character Functions.
Welcome to AP Computer Science A We use the Java language, but this class is much more rigorous than Intro to Java More programming, but also more theory.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
CSC Programming for Science Lecture 28: Multi-dimensional Arrays.
CSC Programming for Science Lecture 34: Dynamic Pointers.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
CSC Programming for Science Lecture 37 : Course Review.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CSC Programming for Science Lecture 27: Arrays as Parameters, Searching, & Sorting.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
CSC Programming for Science Lecture 5: Actual Programming.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
User Interaction and Variables
Revision Lecture
Type Conversion, Constants, and the String Object
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
C++ Data Types Data Type
Chapter # 2 Part 2 Programs And data
Variables and Computer Memory
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
CSCE 206 Lab Structured Programming in C
Variables and Constants
Getting Started With Coding
Presentation transcript:

CSC Programming for Science Lecture 26: Arrays

Problem of the Day Two English words change their pronunciation when their first letter is capitalized. What are they? Polish/polish Reading/reading

Today’s Goal After lecture should be familiar with 1- dimensional arrays  Creating an array variable  Assigning data inside the array  Using the values stored in the array Arrays in functions on Wednesday Higher dimension arrays on Friday

Variables Names memory location for program’s use  Initial value is unknown  Value updated via assignments  Value used whenever program uses variable (Should be) Declared within a function Only usable by the declaring function

Data Types Each variable also has data type  Specifies how program treats variable’s value 6(+1) numeric types exist  Integer types: short, int, long  Decimal types: float, double, long double  char holds character, but these are numbers C will warn on bad assignments  Assignments that may lose information

Problem with Variables Each variable stores single value  Assignment overwrites prior value Storing 2 values at once needs 2 variables Storing 100 values needs 100 variables Images usually contain millions of values  Good luck with all that typing!

Arrays Variable holding multiple pieces of data  Really contains many different locations  Locations are numbered sequentially from 0 Each location is like an individual variable  Initial value is unknown  New value set at assignment But location is not independent of array  Can only be used by accessing array variable

Declaring an Array Variable Must declare array variable before use Declaration must include type, name, and size  Size should be literal value  Name follows same rules as any other variable  Variable is an array of the requested type  Each location, however, holds data of that type int sampleArray[10]; float armada[200]; FILE* inputs[3];

Working With Arrays Only creates location between 0 & size-1 C does not make this process easy  Cannot find what an array’s size is  Will not give you any sort of warning If you do make an illegal access…  Program may crash with “Segmentation Fault”  May see problems with other variable  May not notice any problems  (Major source of computer hacking attempts)

Initializing an Array Can set locations’ initial values Must specify value for all locations  Can initialize all locations to same value…  …or can initialize each location separately double taxrate[3]={0.15, 0.25, 0.3}; char list[5]={‘h’,‘e’,‘l’,‘l’,‘o’}; int vector[100]={0}; /*all set to 0*/ int s[]={5,0,-5}; /*a’s size is 3*/

Using An Array Each array location used like a variable  But can access only through array variable  Locations are accessed using ‘[’ & ‘]’ Example computing Fibonacci numbers: int i, fib[20]; fib[0] = 1; fib[1] = 1; for (i = 2; i < 20; i++) { fib[i] = fib[i – 1] + fib[i – 2]; }

More Uses of An Array #define MAX_SIZE 80 char name[MAX_SIZE]; int inLoc, outLoc; printf(“What is your name? ”); for (inLoc = 0; inLoc < MAX_SIZE; inLoc++) { name[inLoc] = getchar(); if (name[inLoc] == ‘\n’) { break; } } printf(“Hello ”); for (outLoc = 0; outLoc < inLoc; outLoc++) { printf(“%c”, name[outLoc]); }

Your Turn Get back into groups and complete the daily activity

For Next Lecture Work on weekly assignment #11 Continue on Programming Assignment #2 Keep up on your reading  Do not need to understand all the details  But important knowing what is not understood