Week 8: Arrays and File I/O. BJ Furman 21OCT2009

Similar documents
Week 7: Pointers and Arrays. BJ Furman 02OCT2009

File I/O BJ Furman 23OCT2010

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Module 6: Array in C

File I/O. Arash Rafiey. November 7, 2017

CSCI 171 Chapter Outlines

File IO and command line input CSE 2451

Pointers. Memory. void foo() { }//return

ONE DIMENSIONAL ARRAYS

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

Today s class. Review of more C Operating system overview. Informationsteknologi

ARRAYS(II Unit Part II)

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Standard File Pointers

H192 Midterm 1 Review. Tom Zajdel

today cs3157-fall2002-sklar-lect05 1

C programming basics T3-1 -

A Crash Course in C. Steven Reeves

Principles of C and Memory Management

Preview from Notesale.co.uk Page 2 of 79

Input / Output Functions

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

CSI 402 Lecture 2 Working with Files (Text and Binary)

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

POINTER & REFERENCE VARIABLES

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

More about BOOLEAN issues

Programming Language B

Computers Programming Course 10. Iulian Năstac

Content. In this chapter, you will learn:

A Fast Review of C Essentials Part I

Tutorial 10 Pointers in C. Shuyue Hu

Lab # 4. Files & Queues in C

Engineering program development 7. Edited by Péter Vass

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

SBE201 Data Structures and Algorithms in C

A First Book of ANSI C Fourth Edition. Chapter 9 Character Strings

Special PRG Lecture No. 2. Professor David Brailsford Special PRG Lecture: Arrays

EL2310 Scientific Programming

Organization of a file

Chapter 10: File Input / Output

Variation of Pointers

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Today s Learning Objectives

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

File Input/Output. Similarly, for reading from files and writing to files, we'll use the functions

Homework #3 CS2255 Fall 2012

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

More Arrays. Last updated 2/6/19

Lesson 7. Reading and Writing a.k.a. Input and Output

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

Programming for Engineers Arrays

Contents. A Review of C language. Visual C Visual C++ 6.0

Summer May 18, 2010

Problem Solving and 'C' Programming

High Performance Programming Programming in C part 1

Arrays and Pointers (part 2) Be extra careful with pointers!

Language comparison. C has pointers. Java has references. C++ has pointers and references

High Performance Computing

Structured Programming. Dr. Mohamed Khedr Lecture 9

Example: Pointer Basics

Arrays and Pointers (part 2) Be extra careful with pointers!

CpSc 1111 Lab 4 Formatting and Flow Control

Introduction to Scientific Computing and Problem Solving

Multi-Dimensional arrays

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

by Pearson Education, Inc. All Rights Reserved.

Chapter 7 Array. Array. C++, How to Program

Computer Programming

Arrays and Pointers (part 1)

C introduction: part 1

Procedural Programming

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

COMP1917: 15 File IO

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

C Programming Language

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Data Types and Computer Storage Arrays and Pointers. K&R, chapter 5

CSI 402 Lecture 2 (More on Files) 2 1 / 20

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

ESC101N Fundamentals of Computing

Unit 7. Functions. Need of User Defined Functions

C Programming Review CSC 4320/6320

Dynamic Allocation of Memory Space

PROGRAMMAZIONE I A.A. 2017/2018

File I/O. Last updated 10/30/18

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

BITG 1113: Array (Part 1) LECTURE 8

Computer Programming Unit 3

Transcription:

Week 8: Arrays and File I/O BJ Furman 21OCT2009

The Plan for Today Arrays What is an array? How do you declare and initialize an array? How can you use an array? Array Examples Array Practice File I/O

Learning Objectives Explain what an array is Declare and initialize an array Use an array in a program How to open and close files for reading and writing

What is an Array? So far we've dealt with scalar variables contain just one value Arrays are collections of data of the same type that occupy contiguous memory locations Individual values in the collection are called elements of the array Need to declare before use: Format (1D array) type array_name [num_elements]; Ex. Array of 4 characters named, 'test' #include <stdio.h> int main() int i; char test[4]; test[0]='m'; test[1]='e'; test[2]='3'; test[3]='0'; for(i=0; i<4; i++) printf("test[%d]=%c",i,test[i]); printf(" @ 0x%p\n",&test[i]); return 0;

Accessing Array Elements Individual elements of an array are accessed by their index number ** Important Note** Array indexing starts at zero (take note of this, it is easy to forget!) char test[4]; Be careful! the first element is test[0] the fourth element is test[3] #include <stdio.h> int main() int i; char test[4]; test[0]='m'; test[1]='e'; test[2]='3'; test[3]='0'; for(i=0; i<4; i++) printf("test[%d]=%c",i,test[i]); printf(" @ 0x%p\n",&test[i]); return 0; The C compiler may not stop you from indexing beyond the boundary of your array. (What could happen?)

Initializing Array Elements Use braces to enclose the elements Separate elements by commas Can set number of elements in declaration: Explicit: int nums[5] Implicit: int imp[ ] = 1, 2; Read from the keyboard Ex. array_practice4.c /* array_practice3.c */ #include <stdio.h> int main() int i; int nums[5]=0,1,2,3,4; for(i=0; i<5; i++) printf("nums[%d]=%d",i,nums[i]); printf(" @ 0x%p\n",&nums[i]); return 0;

Arrays of Multiple Dimensions - 1 Arrays can have more than one dimension 2D matrices commonly used in linear algebra Declaration for 2D array: /* array_practice5.c */ #include <stdio.h> int main() int i,j; int my_array1[2][3]=0,1,2,3,4,5; for(i=0; i<2; i++) printf("\n"); for(j=0; j<3; j++) printf("%d ", array1[i][j]); return 0; int my_array1[number_of_rows][number_of_columns]; Enclose each row of initializers in its own set of braces Note method to print and access individual elements Can think of array1 as a "2-element array of 3 elements"

Arrays of Multiple Dimensions - 2 Arrays of more than one dimension, cont. Declaration for 3D array: int my_array2[n x ][n y ][n z ]; Enclose each row of initializers in its own set of braces Order of storage: far right subscript increments first my_array2[0][0][0] my_array2[0][0][1] my_array2[0][0][2] etc. to my_array2[n x -1][n y -1][n z -1] Find element number for my_array2[x][y][z]: x( JK) + y( K) + z + 1 Ex. array_practice6.c Initializes a 4x3x2 element array Can think of array2 as a "4-element array of a 3x2 element arrays"

Arrays - Practice 1 Declare: an array of 100 characters named char100 a 3x4 array of doubles named data1 Given int my_array1[2][3]=0,1,2,3,4,5; Find my_array1[1][2] my_array1[0][0] my_array1[0][3]

Notes on Arrays An array name that is not followed by a subscript is interpreted as a pointer to the first element of the array. &test[0] is equivalent to test pointer to first element of test test[0] is equivalent to *test first element of test #include <stdio.h> int main() int i=0; char test[]='m','e','3','0' printf("test[%d]==%c",i,test[i]); printf(" @ 0x%p\t",&test[i]); printf("test[%d]==%c", i, *test); printf(" @0x%p\n", test); for(i=1; i<4; i++) printf("test[%d]==%c",i,test[i]); printf(" @ 0x%p\n",&test[i]); return 0;

Notes on Arrays - 2 Pointer arithmetic array[n] is equivalent to *(array + n) Using just the name 'array' is equivalent to a pointer to the first element of array (i.e., base address) Dereferencing the expression, where an integer n is added to 'array', is equivalent to indexing the array to its element at subscript n array names cannot be changed, but pointer variables can be changed may not have: array_name = &var; the array name is a constant pointer and may not be changed

Array File I/O Arrays are often used with data consisting of many elements Too tedious to handle I/O by keyboard and monitor File I/O is used instead File I/O and array example

Opening a File Key concepts: Declare a pointer to FILE, e.g. FILE *fp; Use fopen function with path to the file and file mode as arguments fp = fopen( file_name.txt, r ); Some common modes (see reference below for others): r for reading from a file Returns NULL if the file does not exist w for writing to a file Creates the file if it does not exist, or wipes out contents of the file if it already exists a to append to a file Creates the file if it does not exist, or starts writing at the end of the file if it already exists Use the file pointer as an argument in functions like fscanf, fprintf, etc.) Good idea to test the file was opened without error Test the return value of fopen fopen will return NULL if there is an error opening the file Reference: http://www.cppreference.com/wiki/c/io/fopen

Closing a File Function header: int fclose(file *fp); Good idea to test the file was closed without error Test the return value of fclose fclose will return EOF if there is an error closing the file Make sure you close all files that you opened somewhere in your program Reference: http://www.cppreference.com/wiki/c/io/fclose

Review

References Jensen, T. (2003). A Tutorial on Pointers and Arrays in C, available from: http://home.netcom.com/~tjensen/ptr/point ers.htm.visited 02OCT2009. http://www.asciitable.com/. Visited 03OCT2009.