Arrays Name, Index, Address. Arrays – Declaration and Initialization...... 70000 70004 70008 70012 70016 70020 70024 70028...... int x; y[0] y[1] y[2]

Slides:



Advertisements
Similar presentations
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Advertisements

More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
C Pointers Systems Programming Concepts. PointersPointers  Pointers and Addresses  Pointers  Using Pointers in Call by Reference  Swap – A Pointer.
Dr. Sajib Datta  We can also have arrays of arrays, also known as multidimensional arrays.  E.g., A two-dimensional array is an array of several.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on Arrays Using array elements as function arguments  Examples Using arrays as function.
Arrays Write a program that first reads in 20 integers and then asks the user whether they want to display all the even integers or all the odd integers.
1 Array Knowledge Understand the execute technique of array Skill Can write application program using one and two dimensional array.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on One-dimensional Arrays Using array elements as function arguments  Examples Using.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
Parameter Passing to Functions in C. C Parameter passing Review of by-value/by-reference.
Guidelines for working with Microsoft Visual Studio 6.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
Lecture 07 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 22, 2002.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
Pointers CSE 2451 Rong Shi.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
Computer Science 210 Computer Organization Arrays.
UNIT 14 Functions with Pointer Parameters.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
ICS103 Programming in C Lecture 11: Arrays I
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.
UNIT 10 Multidimensional Arrays.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
Pointers Value, Address, and Pointer. Values and Addresses int x, y, z; y x z values of x,
WEEK 8 Class Activities Lecturer’s slides.
Computer Programming for Engineers
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
ICS103: Programming in C 7: Arrays Muhamed F. Mudawar.
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
UNIT 9 Arrays.
Dr. Sajib Datta Feb 11,  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Array of pointers We can have an array whose members are pointers, in this example pointers-to-int. int* data[3]; int i; int x = 5; int y = 89; int z =
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
Arrays. Arrays are objects that help us organize large amounts of information.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Dr. Sajib Datta Sep 10,  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
 Recursion  Pointers and Arrays #include void print3(int n){ if (n==0)return; printf ("%d\n",n); print3(n-1); printf ("%d\n",n); } void main(){ print3(5);
Pointers. Pointer Arithmetic Since arrays consist of contiguous memory locations, we can increment (or decrement) the addresses to move through the array.
ARRAYS 2D ARRAY APPLICATIONS DYNAMIC ARRAYS
Functions and Pointers
Hassan Khosravi / Geoffrey Tien
CS1010 Programming Methodology
CS1010 Programming Methodology
Array 9/8/2018.
Visit for more Learning Resources
Functions and Pointers
Buy book Online -
CSC 270 – Survey of Programming Languages
Pointers.
EECE.2160 ECE Application Programming
CSCE 206 Lab Structured Programming in C
ICS103 Programming in C Lecture 12: Arrays I
Presentation transcript:

Arrays Name, Index, Address

Arrays – Declaration and Initialization int x; y[0] y[1] y[2] y[3] y[4] y[5] y[6] x addresses  &x is  &y[0] is  &y[1] is  …  &y[6] is int y[7]; x = 5; y[0] = 11; y[1] = 2 * y[0]; y[2] = y[0]; y[3] = y[0] + y[1] + y[2]; y means &y[0]

Arrays – Declaration and Initialization int x = 5; y[0] y[1] y[2] y[3] y[4] y[5] y[6] x int y[7] = { 11, 22, 11, 44, 33, 55, 105}; scanf(“%d”, &x); 10 scanf(“%d”, &y[5]); 20

Arrays – Declaration and Initialization Declare, but not initialize – double xarr[5]; Declare and initialize – double xarr[5] = {1.1, 2.2, 3.3, 4.4, 5.5} Declare, but initialize some – double xarr[5] = {1.1, 2.2} – 3 rd, 4 th, 5 th elements are initialized to 0.0 Declare and initialize without size – double xarr[] = {1.1, 2.2, 3.3, 4.4, 5.5}

Using Arrays Dr. Sadık EşmelioğluCENG 1145 x[0]x[1]x[2]x[3]x[4]x[5]x[6]x[7] double x[8]; x[3] = x[5] + 2; i = 4; printf(“%4.1f”, x[i]); printf(“%.1f + %.1f = %.1f”, x[0], x[6], x[0]+x[6]); i = 5; x[i] = x[i-1]; i = 2; printf(“%.1f”, x[--i]); i = 2; printf(“%.1f”, x[i--]); New x[3] = 7.5 Prints 2.5 New x[3] = 7.5 Prints 2.5 Prints = 11.9 New x[3] = 7.5 Prints 2.5 Prints = 11.9 New x[5] = x[4] = 2.5 New x[3] = 7.5 Prints 2.5 Prints = 11.9 New x[5] = x[4] = 2.5 Prints 2.1. New i is 1. New x[3] = 7.5 Prints 2.5 Prints = 11.9 New x[5] = x[4] = 2.5 Prints 2.1. New i is 1. Prints 5.2. New i is 1. x[0]x[1]x[2]x[3]x[4]x[5]x[6]x[7] x[0]x[1]x[2]x[3]x[4]x[5]x[6]x[7]

Example 1 #include int main(void) { int x[7]; x[0] = 5; x[1] = 10; x[2] = 15; x[3] = 20; x[4] = x[1] + x[2]; x[5] = x[0] + x[4]; x[6] = x[4] + x[5]; printf("The value of x[0] is: %3d\n", x[0]); printf("x[0] + x[6] = %3d\n", x[0] + x[6]); printf("x[1] * x[5] = %3d\n", x[1] * x[5]); printf("x[6] - x[2] = %3d\n", x[6] - x[2]); printf("\n"); printf("The address of array is: %d\n", x); printf("The address of x[0] is: %d\n", &x[0]); printf("The address of x[1] is: %d\n", &x[1]); printf("The address of x[2] is: %d\n", &x[2]); printf("The address of x[3] is: %d\n", &x[3]); printf("The address of x[4] is: %d\n", &x[4]); printf("The address of x[5] is: %d\n", &x[5]); printf("The address of x[6] is: %d\n", &x[6]); printf("The address of x[6] is: %d\n", &x[5] + 1); return(0); }

Arrays & Pointers #include void PrntArry(double a[], int n); int main(void) { double x[4] = {10.23, 20.12, 44.44, 12.22}; double *ptr; ptr = x; PrntArry(x, 4); ptr++; printf("Array Element %8.2f\n", *ptr); ptr++; printf("Array Element %8.2f\n", *ptr); printf("Array Element %8.2f\n", ++(*ptr)); printf("Array Element %8.2f\n", *(++ptr)); return(0); } void PrntArry(double a[], int n) { int i; for(i=0; i<n; i++) printf("[%d]: %8.2f\n", i, a[i]); }

Arrays & Pointers

Arrays & Loops int x[60]; /* 60 integers from x[0] to x[59] */ int i; /* Fill array */ for(i=0; i<60; i++) { printf("Enter array element %2d: ", i); scanf("%d", &x[i]); } /* Print array */ for(i=0; i<60; i++) { printf("Array Element %2d is %3d\n", i, x[i]); }

Arrays & Functions #include void FillArray(int arr[], int n); void PrntArray(int arr[], int n); int main(void) { int x[60]; /* Fill array */ FillArray(x, 60); /* Print array */ PrntArray(x, 60); return(0); } void FillArray(int arr[], int n) { int i; for(i=0; i<n; i++) { printf("Enter array element %2d: ", i); scanf("%d", &arr[i]); } void PrntArray(int arr[], int n) { int i; for(i=0; i<n; i++) { printf("Array Element %2d is %3d\n", i, arr[i]); }