one dimentional array

11
1Dimansiona l ARRAY

Upload: sonya-rupa

Post on 16-Apr-2017

64 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: One Dimentional Array

1Dimansional ARRAY

Page 2: One Dimentional 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

Page 3: One Dimentional Array

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.

Page 4: One Dimentional Array

Declaring Arrays

¤ Declaration examples:int[] counts;double[] scores;String[] studentNames;

¤Syntax: Data type Array name [constant]

Page 5: One Dimentional Array

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];

Page 6: One Dimentional Array

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

Page 7: One Dimentional Array

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};

Page 8: One Dimentional Array

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];

Page 9: One Dimentional Array

#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

Page 10: One Dimentional 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

Page 11: One Dimentional Array

Thank you