










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 18
This page cannot be seen from the preview
Don't miss anything!











are 2D arrays (matrices) of numbers:
^ 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];
^ 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]
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]);
#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]);
}
^ 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); }
/* 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*/
#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');} }
^ 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.