Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center.

Slides:



Advertisements
Similar presentations
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Advertisements

1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
1. 1. Introduction to Array 2. Arrays of Data 3. Array Declaration 4. Array Initialization 5. Operations on Array 6. Multidimensional Arrays 7. Index.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
Arrays. Arrays are objects that help us organize large amounts of information.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
EKT120 : Computer Programming
Arrays Declarations CSCI N305
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
INC 161 , CPE 100 Computer Programming
Module 2 Arrays and strings – example programs.
C-Programming, continued
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
CS1100 Computational Engineering
C Arrays Systems Programming.
Multidimensional Arrays
Programming and Data Structures
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Lecture 10 Arrays.
EKT150 : Computer Programming
Declaration, assignment & accessing
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
EKT120: Computer Programming
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
prepared by Senem Kumova Metin modified by İlker Korkmaz
2-d Arrays.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Introduction to Computing Lecture 10 Arrays (Part 1)
Arrays I Handling lists of data.
Array Data Structure Chapter 6
EECE.2160 ECE Application Programming
Arrays.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Dale Roberts, Lecturer IUPUI
CSCE 206 Lab Structured Programming in C
Arrays, Part 1 of 2 Topics Definition of a Data Structure
ICS103 Programming in C Lecture 12: Arrays I
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CSCE 206 Lab Structured Programming in C
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Introduction to Problem Solving and Programming
Presentation transcript:

Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center

Warming Up Problem Problem : Get 5 integer numbers using scanf function, and print the numbers in reverse order. Example Input : 3 10 5 7 2 Output : 2 7 5 10 3

Using array Memory structure #include <stdio.h> int main() { int x0, x1, x2, x3, x4; printf("Get 5 integers : "); scanf("%d", &x0); scanf("%d", &x1); scanf("%d", &x2); scanf("%d", &x3); scanf("%d", &x4); printf("Reverse Order : "); printf("%d ", x4); printf("%d ", x3); printf("%d ", x2); printf("%d ", x1); printf("%d ", x0); printf("\n"); return 0; } #include <stdio.h> int main() { int x[5], i; printf("Get 5 integers : "); for (i = 0; i < 5; ++i) scanf("%d", &x[i]); printf("Reverse order : "); for (i = 4; i >= 0; --i) printf("%d ", x[i]); printf("\n"); return 0; } Using array Memory structure

Array Variables? Array variable holds a sequence of multiple values with same data type int a[10]; There is an ordered method for extracting individual data items a[3]=7; Example If we want to store scores for exam1 , exam2 , exam3 , ... , exam10 First try, int exam1 = 90; int exam2 =85; … int exam100=93; sum = exam1 + exam2 + … + exam100; Using array int exam[100] = {90 , 85, … , 93}; for (i=0;i<100;i++) sum+=exam[i];

Array Variables Example In C, array index starts at zero. int id[3]; /* declaration of array id, which has space for three consecutive int variables */ id[0] = 101; id[1] = 232; id[2] = 231; Each piece of data in an array is called an element. Thus, array id has three elements.

Array Declaration, Initialization type arrayName[arraySize]; float a[5]; // declare a float type array with size 5 and int x, y, b[5]; Array Initialization int a[5] = {1, 3, 5, 7, 9}; int a[] = {1, 3, 5, 7, 9}; a[2] = 11; // a -> {1, 3, 11, 7, 9} a[5] = 20; // wrong!!!! Index can be between 0 and 4

example #include <stdio.h> int main() { int x[] = {1, 3, 5, 7, 9}; printf(" x = %8u\n", x); printf(" sizeof(x) = %8d\n", sizeof x); printf("sizeof(x[0]) = %8d\n", sizeof x[0]); return 0; } output x = 2280640 sizeof(x) = 20 sizeof(x[0]) = 4

Multi-Dimensional array Multi-dimensional arrays have two or more index values which are used to specify a particular element in the array. For this 2D array element, image[i][j] the first index value i specifies a row index, while j specifies a column index. Declaring multi-dimensional arrays is similar to the 1D case: int a[10]; /* declare 1D array */ float b[3][5]; /* declare 2D array */ double c[6][4][2]; /* declare 3D array */ Note that it is quite easy to allocate a large chunk of consecutive memory with multi-dimensional arrays. Array c contains 6x4x2=48 doubles. Initialization int age[2][3] = { {4,8,12} , {19,6,-1} };

Multi-Dimensional array Very commonly used for matrix int b[3][5]; for (i=0;i<3;i++) { for (j=0;j<5;j++) sum+=b[i][j]; }

Passing Array Argument Pass address of an array into function Example int getSum (int score[] , int arraySize) // int getSum (int* score , int arraySize) { int i; int sum=0; for (i=0;i<arraySize;i++) sum+=score[i]; return sum; } int main() int a[]={1,5,10}; printf(“a[0]=%d, a[1]=%d, a[2]=%d , sum=%d\n”,a[0],a[1],a[2],sum); sum = getSum(a,3); return 0;

Array Arguments Because we pass address of array to function, array element value which is modified in a function will remain modified even after the function call is finished. example int getSum (int score[] , int arraySize) { int i; int sum=0; for (i=0;i<arraySize;i++) { sum+=score[i]; score[i]=sum; } return sum; int main() int a[]={1,5,10}; printf(“a[0]=%d, a[1]=%d, a[2]=%d , sum=%d\n”,a[0],a[1],a[2],sum); sum = getSum(a,3); return 0;

Example : rcSum.c #include <stdio.h> #include <string.h> // strlen #define N 3 void readNxN(int a[N][N]); void sumNxN(const int a[N][N], int rSum[N], int cSum[N]); void printNxN(const int a[N][N], const int rSum[N], const int cSum[N]); int main() { int x[N][N]; int rSum[N] = {0}, cSum[N] = {0}; readNxN(x); sumNxN(x, rSum, cSum); printNxN(x, rSum, cSum); return 0; }

void readNxN(int a[N][N]) { int i, j; printf(“Input %d x %d integer matrix :\n", N, N); for (i = 0; i < N; ++i) for (j = 0; j < N; ++j) scanf("%d", & a[i][j]); } void sumNxN(const int a[N][N], int rSum[N], int cSum[N]) for (i = 0; i < N; ++i) { for (j = 0; j < N; ++j) { rSum[i] += a[i][j]; cSum[j] += a[i][j];

void printNxN(const int a[N][N], const int rSum[N], const int cSum[N]) { int i, j; const char *lseg = "-------"; const int width = strlen(lseg) - 1; for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) printf("%*d ", width, a[i][j]); printf("| %*d\n", width, rSum[i]); } for (i = 0; i < 3; ++i) printf("%s", lseg); printf("+\n"); printf("%*d ", width, cSum[i]); printf("\n");