Unformatted text preview:

October 2002 10.001 Introduction to Computer MethodsArrays and Pointers. Lecture Plan. • Intro into arrays.definition and syntaxdeclaration & initializationmajor advantagesmultidimensional arraysexamples• Intro into pointers.address and indirection operatorsdefinition of pointerspointers and arrays – comparisonpointer arithmeticOctober 2002 10.001 Introduction to Computer MethodsArrays and PointersArray is a group of elements that share a common name, and that are different from one another by their positions within the array. C syntax: x[1]=3.14;x[2]=5.2;x[3]=6347;Declaration: int x[5];type name sizeSets aside memory for the arrayArray indexOctober 2002 10.001 Introduction to Computer MethodsArrays and PointersInitialization: int grade[]={100,99,85}; int grade[3]={100,99,85};int grade[100]={1,3,5,7};– grade[4]-grade[99] will be zeros.grade[36] = 87;Multidimensionality:Scalar variable aVector variable (1D) a0, a1, a2,...Matrix variable (2D) a00, a01,a02,...a10,a11,a12,...a20,a21,a22,......October 2002 10.001 Introduction to Computer MethodsArrays and PointersDeclaration: int L=100, M=100, N=100;float a[L][M][N];Initialization: alpha[2][2]={1,2,3,4};alpha[2][2]={{1,2},{3,3}};alpha[0][1]=3;alpha[1][1]=2;NB: Array size is fixed at declaration.#define L 100#define M 100#define N 100...int a[L][M][N]October 2002 10.001 Introduction to Computer MethodsArrays and PointersNB: If x[5] is accessed, no error will result!Utility: simplify programming of repetitive operationsimprove clarityimprove modularityimprove flexibilityNB: In C numbers of array elements start form zero:x[0], x[1], x[2], x[3], x[4]. There is no x[5].October 2002 10.001 Introduction to Computer MethodsArrays and PointersExample: a program to compute the class average of the midterm.Scalar form:int main(void){float average;int sum=0,grade1,grade2,..;scanf(“%d”,&grade1);scanf(“%d”,&grade2);...sum += grade1;sum += grade2;...average = sum/95.0;}Vector (array) form:int main(void){float average;int i,n,sum=0,grade[100];scanf(“%d”,&n);for(i=0;i<n,&n;i++){scanf(“%d”,&grade[i]);sum += grade[i];} ...average = (float)sum/n;}October 2002 10.001 Introduction to Computer MethodsArrays and PointersExample: Integration using Composite Trapezoid Rule[]∑∑=−=−++=+=NiNiiiixfbfafhxfxfhI1111)(2)()()()(2Continuous function f(x), x belongs to [a,b] a set of discrete values f(xi), xibelong to [a,b].∫=badxxfI )(October 2002 10.001 Introduction to Computer MethodsArrays and PointersGiven a function y=f(x) to integrate form x=a to x=b:int main(void) {...h=(b-a)/n;integral =0.5*(func(a)+func(b));for(i=1;i<n;i++)integral += func(a+i*h);integral *=h;...return(0);}October 2002 10.001 Introduction to Computer MethodsArrays and PointersGiven discrete data yi= f(xi) integrate form x=a to x=b:int main(void) {...for (i=0; i<=n; i++)scanf(“%f”,&y[i]); /*reading f(xi)*/integral =0.5*(y[0]+y[n]);for(i=1; i<n; i++){scanf(“%f”,&y); /*summing f(x[i])*/integral += y;}scanf(“%f”, &a)scanf(“%f”, &b)integral *= (b-a)/n;...return(0);}October 2002 10.001 Introduction to Computer MethodsArrays and PointersCalculating the average. Version 1. /*No arrays.*/#include <stdio.h>int main(void){float ave;int sum=0;int data1, data2, data3;scanf(“%d”, &data1);scanf(“%d”, &data2);scanf(“%d”, &data3);sum == data1;sum += data2;sum += data3;ave = sum/3.0;...}• inefficient coding• only works for a fixed number of data pointsOctober 2002 10.001 Introduction to Computer MethodsArrays and PointersCalculating the average. Version 2. /* no arrays, scalar “for” loop */#include <stdio.h>int main(void){float ave;int i, n, datai, sum=0;scanf(“%d”, &n);for (i=0;i<n;i++){scanf(“%d”, &datai);sum += datai;}ave = (float) sum/n;... }October 2002 10.001 Introduction to Computer MethodsArrays and PointersCalculating the average. Version 3. /* with arrays */#include <stdio.h>#include <math.h>#define NMAX 100int main(void){float ave;int i, n, data[NMAX], sum=0;scanf(“%d”, &n);if(n>NMAX) printf(“number of pts > NMAX);for (i=0; i<n; i++)scanf(“%d”, &data[i]);sum += data[i];}ave = float(sum)/n;... }• array size is fixed at declaration• use #define to have some flexibilityOctober 2002 10.001 Introduction to Computer MethodsArrays, Summing up•The name identifies the location in memory, big enough to store the whole array.• a[k] refers to the k-th element of the array, the indexing starting from 0.• The memory allocation happens when the array is declared: use # to set the dimensions.• Advantages: clear and compact coding, better modularity, take advantage of loops for repetitive operations.October 2002 10.001 Introduction to Computer MethodsArrays and PointersIntro into pointers.& - address operator, unary, right to left precedencev – variable &v – location (address) of v in the memoryThe special type of variable to operate with the address is needed: POINTER pv = &v;Identifier vpvMemory address 1776 1997Value51776October 2002 10.001 Introduction to Computer MethodsArrays and PointersDeclaration: int *p; p – pointer to integer variable.Value range: zero or NULL address and a set of positive integers.Assignment: p=0; p=NULL; p=&i; p=(int *)1776;address of icast as “pointer to int”Indirection (dereferencing) operator * - “inverse” to &.Gives the value of the variable pointed to by the pointer.p = &i; i = *p; We can access any variable, if know the variable’s address!&i = p; illegal, addresses are allocated by declarations.p = &3; p = &(i+j); illegal: constants and expressions do not have addresses.October 2002 10.001 Introduction to Computer MethodsArrays and PointersRelationship between arrays and pointers:• Array name is a pointer constant, it’s value is the address of the first element of the array.• Pointers can be subscribeda[i] = *(a + i) a – address of a[0](base address or the array)a[i] = *(p + i) points to i-th element of the arrayNB: a is a constant pointer, a=p, ++a, &aare illegal.October 2002 10.001 Introduction to Computer MethodsArrays and PointersPointer arithmetic is equivalent to array indexing:p = a + 1 p = &a[1]p = a + m p = &a[m]Summing the array using pointers:for (p = a; p < &a[N]; ++p)sum += *p;orfor (i = 0; i < N; ++i)sum += *(a + i);October 2002 10.001 Introduction to Computer MethodsArrays and PointersPointer arithmetic:p + 1 ++p p + i p += iHowever, pointers and numbers are not quite the


View Full Document

MIT 10 001 - Arrays and Pointers

Download Arrays and Pointers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays and Pointers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays and Pointers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?