12 - ICS 103 Computer Programming in C Handout-12 Topic 1-D Array Objective To learn how to declare and access 1 D array To learn how to read and print

12 - ICS 103 Computer Programming in C Handout-12 Topic 1-D...

This preview shows page 1 - 3 out of 7 pages.

ICS 103: Computer Programming in C Handout-12 Topic: 1-D Array Objective: To learn how to declare and access 1 –D array. To learn how to read and print 1-D array elements. What is array? Why array are needed?: Arrays are very useful tools when we need to keep several values (e.g grades of students in an  exam) in memory.  Instead of using several variables, we use one array variable.  Specifying its  index then accesses individual element. Examples: 1. int a[5];     declares an array of size 5.           0 1 2 3 4 Note: In C language array index always starts from 0 2. a[0]=10;   a[1]=20;  a[2]=30;  a[3]=40;  a[4]=50;   Assigns values to the elements.  3.       Array can also be initialised at point of declaration: int a[]={10, 20, 30, 40, 50}; Note: if you specify a size but give fewer values, the remaining cells will be initialised to zero . Declaring and Referencing 1-D array : Till now to read n number of integers (or float, double, character) we took n number of variables.  For example, to read say 5 integers we used 5 integer variables say a, b, c, d, e. But with arrays we  can use a single variable name  to represent any  number of variables of the same data type . The  individual elements in the array can be accessed by supplying an index or subscript after the name  Page 1 of 7
Image of page 1
of the array variable in the square brackets. The array is declared like any simple variable but the  size of the array should be specified. The first element of the array is always referenced with the 0 index. Array elements are always   numbered from 0 to (size-1) . int    arri[10] ;          float  arrf[30] ; Here two arrays are declared. The array arri contains 10 integers  arri[0] to arri[9] and array arrf  contains 30 floating  values arrf[0] to arrf[29]. The first  elements of arri and arrf are referenced as  arri[0] and arrf[0]. The  last
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 7 pages?

  • Spring '10
  • zaman
  • Array, ARRI

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes