Arrays in Action-Programming-Lecture Slides, Slides of Computer Programming

This lecture was delivered by Prof. Varun Sahil to explain Programming concepts at Ankit Institute of Technology and Science. It includes: Arrays, Matrices, Action, Variables, Individual, Elements, Indexing, Accessing, Looping

Typology: Slides

2011/2012

Uploaded on 07/23/2012

paravi
paravi 🇮🇳

5

(1)

65 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Arrays in Action
are 2D arrays (matrices) of numbers:
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Arrays in Action-Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

Arrays in Action

are 2D arrays (matrices) of numbers:

Array

^ An array is a set of variables,represented by a single name. ^ The individual variables are calledelements and are identified by indexnumbers. ^ The following example declares anarray with ten elements.

int^

buf[10];

Accessing the Elements

^ To access an individual element in thearray, the index number follows thevariable name in square brackets. ^ The variable can then be treated likeany other variable in C. ^ The following example assigns a value6 to the first element in the array

buf.

buf[0]

=^ 16;

Reading and writing in an Array^ ^ The example prints the value of thethird element in array

buf.

printf("%d\n",

buf[2]);

^ The example uses the scanf function toread a value from the keyboard into thelast element of array

buf. scanf("%d",

&x[9]);

Looping through an Array  As the array is indexed sequentially, wecan use the for loop to display all thevalues of an array.  The example displays all the values ofan array.

Example

#include<stdio.h>void^

main^

(void) {

int^ x[10]={3,6,9,12,15,18,21,24,27,30};int^ i; for(i=0;

i<10;

i++) printf("x[%d]

=^ %2d\n",

i,^ x[i]);

}

Example

^ The program reads a line, stores it in abuffer, and prints its length (excludingthe newline at the end).

void main(void )

{

int n, c;char line[100];n = 0;while( (c=getchar( )) != '\n' ) {

if( n < 100 )

line[n] = c; n++; } printf("length = %d\n", n); }

Arrays of Different Types int^ ID[30];

/* Could be used to store the ID numbers of students in a class */ float

temperatures[31];^ /* Could be used to store the daily temperatures in a month */ char

name[20];^ /* Could be used to store a character string*/

Multidimensional Arrays  An array can have more than onedimension.  By allowing the array to have more thanone dimension provides greaterflexibility.  Spreadsheets are built on a twodimensional array; an array for therows, and an array for the columns.

#include

<stdio.h> void^ main(){ /*^

Declare

a^2 x^

5 multidimensional

array^

*/

int^ x[2][5]

=^ {^ {1,

2,^ 3,^

4,^ 5}, {2,^ 4,^

6,^ 8,^ 10}

};

int^ row,

column; for^ (row=0;

row<2;

row++) { for^

(column=0;

column<5;

column++) printf("%d\t",

x[row][column]); putchar('\n');} }

Storage in Memory

^ Since computer memory is essentiallyone-dimensional, with memorylocations running straight from 0 upthrough the highest location inmemory. ^ A multidimensional array cannot bestored in memory as a grid. Instead, thearray is dissected and stored in rows. ^ Consider the two-dimensional array.