Anda di halaman 1dari 12

ASSINMENT NO.

Q.3.3 Explain in detail one dimensional and two dimensional array. One dimensional array :Array :An array is a collective name given to a group of similar type of elements (homogeneous). All these elements are of the same data type. It could be all integers, floats or characters. An array of character is called a string. Declaration of array :Data type name array (size) Array are defined in the same manner as ordinary variable, Accept that each array name must specify the size i.e. the no. of elements that the array can store. Accessing elements of array :# include (stdio.h) # include (conio.h) void main ( ) { int age [4] ; int i ; clrscr ( ) ;

for (i = 0 ; i < 4 ; i + +) { print f ( \n Enter an age of person%d: , i) ; scan ( % d, &age [i] ) ; } for (i = 0 ; i < 4 ; i + +) } print f (\n your entered age of person%d= % d , i, age [i]); } getch ( ) ; } In the above program first for loop gets the value from the user & places them in the array age, while the second loop reads them from the array and display them. In this program array declaration int age [4]; specifies that the name of array is that can store 4 elements of that integer. That means array is can store 4 int elements. int age [4]; size of array brackets delimit array size Name of array Data type of array Array Initialization:-

We can initialize an array while declaring it as follows. int age [4] = { 48, 18, 26, 10} int age [3] = {A, B, C} float age [2] = {12.00, 14.00} One dimensional Array [Algorithm, Flowchart & programme] 1. WAP to find out avg, rainfall at rainguage station for number of years. Algorithm Step 1 : Step 2 : Step 3 : Step 4 : Step 5 : Start Take declaration of variable Take float rain (25) Using for loop enter a rainfall & year Using for loop y = y + rain (i) Step 6 : Step 7 : arg = y / 10 Print

START Declaration of variable Take float main [10]


i =0 i<0 i =f

Print enter rainfall value & year Take value & year
i =0 i<0 i =f

y = y + rain (i)

Avg = y /10 STOP Manual calculation

Y1 = 56 Y2 = 60 Y3 = 59 Y4 = 77 Y5 = 88 yavg = =

Y6 = 45 Y7 = 96 Y8 = 80 Y9 = 70 Y10 = 55

[ y1 + y2 + y3 + y4 + y5 + y6 + y7 + y8 + y9 + y10] [ 56 + 60 + 59 + 77 + 88 + 45 + 96 + 80 + 70 + 55]

= 65.5999 The avg , rainfall value is 65.60

/*Name, Div. Roll No.*/ /*AVERAGE RAINFALL VALUE OF A STATION*/ #include<stdio.h> #include<conio.h> void main() { float rain[10],avg, y=0; int i; clrscr(); printf(Program To Calculate Avg. Rainfall for 7 Years); for(i=0;i<7;i++) { printf("\nENTER RAINFALL VALUE IN YEAR %d =",i+1); scanf("%f",&rain[i]); } for(i=0;i<7;i++) { y=y+rain[i]; } avg=y/7; printf("\nTHE AVERAGE RAINFALL VALUE IS=%0.2f",avg); getch(); }

OUTPUT: ENTER RAINFALL VALUE IN YEAR 1 =11 ENTER RAINFALL VALUE IN YEAR 2 =12 ENTER RAINFALL VALUE IN YEAR 3 =13

ENTER RAINFALL VALUE IN YEAR 4 =14 ENTER RAINFALL VALUE IN YEAR 5 =15 ENTER RAINFALL VALUE IN YEAR 6 =16 ENTER RAINFALL VALUE IN YEAR 7 =17 THE AVERAGE RAINFALL VALUE =14.00

Two dimensional array:Array can have two or more dimensions array having two dimensions is called as two dimensional array or 2 D array. The two dimensional array is called as matrix. For example Stud [4] [2] Here stud is a 2 D array having 4 rows & 2 columns. Declaration of 2D array:data type array name [ rows ] [columns] for example int stud [4] [2] columns rows name of array data type initialisation of 2D array :We can initialise 2D array as shown below int stud [4] [2] = { { 14, 20 } { 15, 25 } { 16, 29 } { 35, 30 } }

Q.

Write a program to store all roll no. & marks obtained by student side by side in a matrix.

# include (stdio.h) # include (conio.h) void main ( ) { int stud [4] [2] = { {12, 56}, {13, 26}, {14, 36}, {15, 40} ; int i, j ; clrscr ( ) ; print f ( \ n Array elements = \ n ) ; for (i = 0 ; i < 4 ; i + +) { for (j = 0 ; j < 2 ; J + +) { Print f ( % d , stud [i] [j] ) ; } } getch ( ) ; }

2)

Describe the array that is defined in each of the following statements indicate what value are assigned to individual array element. i) float C[8] = {2.0, 5.0, 3.0, -4.0, 12.0, 8.0, 9.0} This is an one dimensional array Float C [8] Bracket Size of array Name of array Data type of array Value assigned to array C[0] = 2.0 C[1] = 5.0 C[2] = 3.0 C[3] = -4.0 C[4] = 12.0 C[5] = 0.0 C[6] = 8.0 C[7] = 9.0

ii)

char flag [4] = { T , R , U , E} Char Flag [4] Bracket Size of array Name of array Data type of array Value assigned as Flag [0] = T Flag [1] = R Flag [2] = U Flag [3] = E

iii)

int P[2][4] = { 1, 1, 3, 3, 5, 5, 7, 7} P[0][0] = 1 P[0][1] = 1 P[0][2] = 3 P[0][3] = 3 P[1][0] = 5 P[1][1] = 5 P[1][2] = 7 P[1][3] = 7

It indicates 2D array of integer type which contains 8 element of integer type the letter P satisfies the 2 rows & 4 colums. iv) int C [2] [3] [4] ; {1, 2, 3} {4, 5} {6, 7, 8, 9} { {10, 11} { }

{12, 13, 14} }

C[0][0][0] = 1 C[0][1][0] = 4 C[0][2][0] = 6 C[1][0][0] = 10 C[1][1][0] = 0 C[1][2][0] = 12

C[0][0][1] = 2 C[0][1][1] = 5

C[0][2][1] = 7 C[1][0][1] =11 C[1][1][1] = 0 C[1][2][1] = 13

C[0][0][2] = 3 C[0][1][2] = 0 C[0][2][2] =8 C[1][0][2] = 0 C[1][1][2] = 0 C[1][2][2] = 19

C[0][0][3] = 0 C[0][1][3] = 0 C[0][2][3] = 9 C[1][0][3] = 0 C[1][0][3] = 0 C[1][1][3] = 0 C[1][2][3] = 0.

Anda mungkin juga menyukai