5-1D Arrays

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 32

ARRAYS

Case
You are required to store the following data for the Health Ministry Department of India: Name of states and union territories along with capitals Population of each Literacy rate of each

Compute and display the state with highest population and literacy rate.

Notion of an array
Array Homogeneous collection of variables of same type. Group of consecutive memory locations. Linear and indexed data structure. To refer to an element, specify Array name Position number ( Index )

Single Dimension Arrays

Array Format
Elements of an array can be integers, floats, characters etc. All the elements share a common name with an index called subscript. In an array of n elements:
[0] [1] [2] [3] [n-1]

Memory Layout

Declaration
When declaring arrays, specify Data type of array ( integers, floats , characters..) Name of the array. Size: number of elements array_type array_name[ size ] ; Example: int student[ 10 ] ; float my_array [ 300 ] ;

Example
For example, int x [ 4 ] ;
Square bracket

memory addresses

1004
1006

An array of integers of 4 elements. Note that the starting memory address is determined by the operating system (just like that of simple variable). Contiguous memory locations are allocated.

1008 1010 1012

(x[0]) (x[1]) (x[2])

1014
1016 1018

(x[3])

Examples
Integer array of 20 elements: int array_1 [ 20 ]; Character array of 50 elements: char array_2 [ 50 ]; Float array of 100 elements: float array_3 [ 100 ];

sizeof()
The amount of storage required to hold an array is directly related to its data type and the size. Example Total size in bytes for a 1 D array: total bytes = sizeof( data type) * size of array int a [ 7 ] ; total_bytes = 2 * 7 = 14 bytes in memory

Exercise
int main( void) { float f1[10] ; char c1[10] ; printf(%d, sizeof(f1)); printf(%d, sizeof(c1)); return 0; }

Output:
40 10

Example 1
Write a program to read 10 integers from the user and display them. int main( void) { int digit[ 10 ] , t ; printf( \n Enter the value of 10 integers ) ; / * for reading 10 integers from the user using scanf * / for( t = 0, t < 10 ; t + + ) scanf( %d, &digit [ t ] ); /* for displaying the integers using printf */ for( t = 0, t < 10 ; t + + ) printf( %d, digit [ t ] ); return 0;}

Example 2
Write a program to read 10 integers and add 5 to odd elements and 10 to even elements.

Exercise 1
In an organization 100 employee are there. Write a program to store the employee id and salary of all the employees. Then input the employee id, and display the salary of the employee.

Solution
Steps Declare two arrays int emp_id[100] and float emp_sal[100] Input the value of both the arrays from the user.
0
1 2

Emp_id
0
1 2

emp_sal

Input the emp_id for which salary is to be displayed.


Search the above id in array emp_id[ ] and get the index if match found In the second array display the salary for the same index.

100

100

ARRAY Example
Grade lists Sporting event times/scores Telephone numbers Voting tallies Digitized images

Arrays - Why?
One example:

Employees salaries in a company. Each salary is a float. All the salaries could be stored as an array of floats. Can then do things like: Change an employees salary Find out an employees salary Get the total salary bill
and do it more easily than representing each salary as a single float (convenience of representation)

Arrays - Why? - Another Example

main() { float sal1,sal2,sal3;

scanf (%f,&sal1);
scanf (%f,&sal2); scanf (%f,&sal3);

main() { int sal[3], i=0; while (i < 3) { scanf (%f,&sal[i]; } }

Imagine having 100 variables...

Initializing Integer arrays


Another way of initializing the elements of the array is as follows: int array [ ] = { 2, 20, -30, 10, 100 } ;

The array size need not be specified explicitly when initial values are included as a part of array declaration.
Array size will automatically be set equal to the number of initial values specified with in definition.

int digits [ 10 ] = { 3, 30, 300} ;

All individual array elements that are not assigned explicit initial values will automatically be set to zero.
digits [ 0 ] = 3 digits [ 1 ] = 30 digits [ 3 ] = 0 digits [ 4 ]= 0 digits [ 7 ] = 0 digits [ 8 ] =0 digits [ 2 ]= 300 digits[ 5 ]= 0 digits [ 9 ] =0

Initializing Arrays
The second way is to Initialize each array element separately
id[0]=1234; id[2]=2883; id[3]=2322; id[4]=8888; id[5]=8237;

Basics of character array


If you are required to store a group of character like your name, city , or your college name, or any word or text you need to define a array of characters. A char variable can hold a SINGLE character only like char c = A ; char c1=B; What if you need to store Sachin Tendulkar or MUMBAI a string.

Character Arrays
To hold a single string you need to declare a single dimension character array char str [ 11 ] ; When declaring a character array to hold a string( group of characters), one need to declare the array to be one character longer than the largest string that it will hold

Example above array str[ 11 ] will hold 10 characters and a NULL character ( \0) at the end

How To Enter value ?


/* program to read users name and display it */ int main( ) { char str [10 ] ; printf( Enter your name ); scanf( %s , str); No need to put & printf( Your name is : %s, str); return 0; }

In case of character arrays Name of the array is the starting address of the array char str[10]; &str [ 0 ] => str

Disadvantage of %s
While reading a string using %s format specifier, it does not scan the string after the space bar. For example Schin Tendulkar It will scan only Sachin . It will IGNORE any space or tab space

How to scan a complete text of words?

gets( )
gets( argument string) : Collects a string of characters terminated by new line character \n from the standard input stream ( stdin ). It allows to input spaces, tabs and copies all the character till new line into the argument string and append a NULL character \0 at the end of it.

For example char str [ 20 ] ; gets( str ) ;

puts( )
puts( argument string ): It displays the string of characters from argument string to standard output till NULL character and appends a new line character at the end. For example puts ( str ) ;

EXAMPLE: Input ten numbers into an array, using values of 0 to 99, and print out all numbers except for the largest number.. /* to accept 10 values in the range 0 to 99 */ int size=10; int value[size], i; for (i=0; i< size; ++i) { scanf (%d,&value[i]); if (value[i] > 99 || value[i] <0) { printf ( enter only values within 0 -99 ); i--; } }

/* to find the greatest among the list */ int maximum= 0; for (i=0; i< size; i++) { if (maximum < value[i] ) maximum = value[i]; }

/* to print all the values other than the greatest */ for (i=0; i< size; i++) { if (value[i] != maximum ) printf (%d, value[i]; }

Home Assignment
Write a program to read a text from console and display the number of words and lines in the text.

You might also like