ARRAYS ..

Slides:



Advertisements
Similar presentations
One Dimensional Arrays
Advertisements

Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
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.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
Kernighan/Ritchie: Kelley/Pohl:
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
Arrays Ethan Cerami New York University Today n Array Basics (Review) n Random Number Example n Passing Arrays to Functions n Strings.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Introduction to C Programming CE Lecture 9 Data Structures Arrays.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Functions A Problem Suppose we are writing a program that displays messages on the screen. We want to display rows of ======================== =======
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
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.
Computer Programming for Engineers
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
ARRAYS Lecture void reverse (int x[], int size) { int i; for (i=0; i< (size/2); i++) temp = x[size-i-1] ; x[size-1-1] = x[i] ; x[i] = temp;
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
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.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
142 M -1 Arrays Chapter 8 Motivation: to deal with large amounts of data e.g sorting values: Input: 10, 15, 4, 15, 17, 3, 12, 36, 48, 32, 9, 21 Want the.
Department of Computer Science Western Michigan University
ARRAYS.
1-d Arrays.
Computer Science 210 Computer Organization
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
C Programming Tutorial – Part I
Module 2 Arrays and strings – example programs.
Arrays in C.
CSCE 210 Data Structures and Algorithms
Computer Science 210 Computer Organization
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
ARRAYS Lecture
CNG 140 C Programming (Lecture set 8)
ICS103 Programming in C Lecture 13: Arrays II
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 1 of 2 Topics Definition of a Data Structure
Functions with arrays.
2-d Arrays.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 16 Pointers and Arrays
Arrays, Part 1 of 2 Topics Definition of a Data Structure
EECE.2160 ECE Application Programming
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
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
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
Presentation transcript:

ARRAYS .

What is an array? A linear arrangement of data of the same type of elements An array has to be declared first with the maximum number of elements it can store int marks[100]; char name[20];

How is an array stored? Starting from a given memory location, the successive array elements are allocated space in consecutive memory locations. x: starting address of the array in memory k: number of bytes allocated per array element a[i]  is allocated memory location at address Array a x + i*k

C Array bounds are not checked Index Rule An array index must evaluate to an int between 0 and n-1 where n is the number of elements in the array. marks[76] marks[i*2+k] // provided i*2+k is between 0 1nd 99 C Array bounds are not checked if (0<=i && i<100) marks[i] = 10; else printf (“Array index %d out of range”, i); #define S 100 marks[S] = 10;

Use An array element can be used wherever a simple variable of the same type can be used. Examples : scanf (“%d”, &marks[i]); marks[i] = (int) (sqrt(21.089));

Things you can and can’t do You can not use = to assign one array variable to another use == to directly compare array variables directly scanf or printf arrays But you can do these things on array elements. You can write functions to do them.

Averaging marks #define CLASS_SIZE 50 double marks[CLASS_SIZE] ; double total=0.0; int i; printf (“Enter %d grades \n”, CLASS_SIZE); for (i=0; i<CLASS_SIZE; i++) scanf(“%f”, &marks[i]); for (i=0; i<CLASS_SIZE; i++) { printf (“ %d . %f\n”,i, marks[i]); total += marks[i] ; } printf (“Average = %f\n”, total / (double) CLASS_SIZE) ;

Are Arrays necessary to solve the above problem ? What about this problem : read student marks, print all marks above average only.

#define MtWeight 0.3 #define FinalWeight 0.7 #define MaxStudents 100 int NumStudents; int midterm[MaxStudents]; int final[MaxStudents]; double score[MaxStudents];

Parallel Arrays midterm final score /* Suppose we have input the value of NumStudents, read student i’s grades for midterm and final, and stored them in midterm[i] and final[i] Store a weighted average in the array score */ if (NumStudents < MaxStudents) for (i=0; i<NumStudents; i++) { score[i] = MtWeight* (double) midterm[i] + FinalWeight* (double) final[i]; }

Reading Array Elements /* Read in student midterm and final grades and store them in two arrays */ #define MaxStudents 100 int midterm[MaxStudents], final[MaxStudents]; int NumStudents ; /* actual no of students */ int i, done, Smidterm, Sfinal; printf (“Input no of students :”); scanf(“%d”, &NumStudents) ; if (NumStudents > MaxStudents) printf (“Too many students”) ; else for (i=0; i<NumStudents; i++) scanf(“%d%d”, &midterm[i], &final[i]);

Reading Arrays - II done=FALSE; NumStudents=0; while (!done) { /* Read in student midterm and final grades and store them in 2 arrays */ #define MaxStudents 100 int midterm[MaxStudents], final[MaxStudents]; int NumStudents ; /* actual no of students */ int i, done, Smidterm, Sfinal; done=FALSE; NumStudents=0; while (!done) { scanf(“%d%d”, &Smidterm, &Sfinal); if (Smidterm !=-1 || NumStudents>=MaxStudents) done = TRUE; else { midterm[NumStudents] = Smidterm; final[NumStudents] = Sfinal; NumStudents++; }

Size of an array How do you keep track of the number of elements in the array ? 1. Use an integer to store the current size of the array. #define MAX 100 int size; float cost[MAX] ; 2. Use a special value to mark the last element in an array. If 10 values are stored, keep the values in cost[0], ... , cost[9], have cost[10] = -1 3. Use the 0th array element to store the size (cost[0]), and store the values in cost[1], ... , cost[cost[0]]

Add an element to an array 1. cost[size] = newval; size++; 2. for (i=0; cost[i] != -1; i++) ; cost[i] = newval; cost[i+1] = -1; 3. cost[0]++; cost[cost[0]] = newval;

Arrays as parameters of functions An array passed as a parameter is not copied

Array operations #define MAXS 100 int insert (int[], int, int, int) ; int delete (int[], int, int) ; int getelement (int[], int, int) ; int readarray (int[], int) ; main () { int a[MAXS]; int size; size = readarray (a, 10) ; size = insert (a, size, 4, 7) ; x = getelement (a, size, 3) ; size = delete (a, size, 3) ; }

Array operations } int readarray (int x[], int size) { int i; for (i=0; i<size; i++) scanf(“%d”, &x[i]) ; return size; } #define MAXS 100 int insert (int[], int, int, int) ; int delete (int[], int, int) ; int getelement (int[], int, int) ; int readarray (int[], int) ; main () { int a[MAXS]; int size; size = readarray (a, 10) ; size = insert (a, size, 4, 7) ; x = getelement (a, size, 3) ; size = delete (a, size, 3) ; } int getelement (int x[], int size, int pos){ if (pos <size) return x[pos] ; return -1; } int insert (int x[], int size, int pos. int val){ for (k=size; k>pos; k--) x[k] = x[k-1] ; x[pos] = val ; return size+1; }

int findmax (int x[], int size) { void reverse (int x[], int size) { } int findmax (int x[], int size) { }

void reverse (int x[], int size) { int i; for (i=0; i< (size/2); i++) temp = x[size-i-1] ; x[size-1-1] = x[i] ; x[i] = temp; } int findmax (int x[], int size) { int i, max; max = x[0]; for (i=1; i< size; i++) if (x[i] > max) max = x[i] ; return max; }

Strings Strings are 1-dimensional arrays of type char. By convention, a string in C is terminated by the end-of-string sentinel \0, or null character. String constant : “abc” is a character array of size 4, with the last element being the null chaaracter \0. char s[] = “abc” ; a b c \0