one dimentional array
TRANSCRIPT
1Dimansional ARRAY
Title:One Dimentional Array
Presented By:Sonya Akter Rupa
ID:3151610098th batch
Department of CSE
Presented To: Md:Abdul Mukib
LecturerDepartment of
CSE
HAMDARD UNIVERSITY BANGLADESH
One Dimensional Array♣Structured collection of components.♣Structure given a single name.♣Individual elements accessed by
index indicating relative position in collection.
♣Type of elements stored in an array can be “just about” anything.
♣Index of an array must be an integer.
Declaring Arrays
¤ Declaration examples:int[] counts;double[] scores;String[] studentNames;
¤Syntax: Data type Array name [constant]
Array Allocation
Arrays are allocated using the Java new operatorThe syntax is:
new type[size];Examples:
counts = new int[10];scores = new double[15];studentNames = new String[10];
Array Organization
counts
Each box is an int variable The numbers on top are each variable’s
subscript or index An array of size 10 has subscripts 0 to 9
0 1 2 3 4 5 6 7 8 9
Array Initialization
• Arrays can be initialized by giving a list of their elements• If your list contains n elements the subscripts will range from 0 to n – 1• Do not need to allocate the array explicitly after it is initialized• Example:int [] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
Arrays of Objects
•Arrays of objects are declared in the same manner as arrays of primitive variables•Assuming that a class Student was declared elsewhere a client application could declare and allocate an array of 10 students using
Student[ ] students;students = new Student[10];
#include<stdio.h>#include<string.h>int main(){ char s1[80],s2[80]; gets (s1); gets (s2); puts(s1); puts(s2); printf("length:%d %d\n",strlen(s1),strlen(s2)); return 0;}
Example of 1D ARRAY
#include <stdio.h>int main(){ int i, n; float a[100],sum=0,sum_pos=0,sum_neg=0,avg; printf("enter the value:"); scanf("%d",&n); for(i=0;i<n; i++) { scanf("%f",&a[i]); } for(i=0;i<n; i++) { if(a[i]>0) sum_pos=sum_pos+a[i]; } for(i=0;i<n; i++) { if(a[i]<0) sum_neg=sum_neg+a[i]; } sum=sum_pos+sum_neg; avg=sum/n; printf("%f\n",sum); printf("%f\n",avg);
return 0;}
Example of 1D ARRAY
Thank you