Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

C Programming Part 10

VARDHAMAN COLLEGE OF ENGINEERING - C programming

Uploaded by

molocof324
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

C Programming Part 10

VARDHAMAN COLLEGE OF ENGINEERING - C programming

Uploaded by

molocof324
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Example

#include <stdio.h>

#include <conio.h>

void main(){

int i=1;//initializing a local variable

clrscr();

//starting a loop from 1 to 10

for(i=1;i<=10;i++){

C PROGRAMMING Page 91
printf("%d \n",i);

if(i==5){//if value of i is equal to 5, it will break the loop

break;

}//end of for loop

getch();

Output

12345

Continue Statement

Continue is keyword exactly opposite to break. The continue statement is used for continuing
next iteration of loop statements. When it occurs in the loop it does not terminate, but it skips
some statements inside the loop / the statements after this statement. . The continue statement is
used/ associated with decision making statement such as if ,if-else.

Syntax of continue Statement

continue;

Flowchart of continue Statement

C PROGRAMMING Page 92
How continue statement works?

Example

C PROGRAMMING Page 93
1. #include <stdio.h>
2. #include <conio.h>
3. void main(){
4. int i=1;//initializing a local variable
5. clrscr();
6. //starting a loop from 1 to 10
7. for(i=1;i<=10;i++){
8. if(i==5){//if value of i is equal to 5, it will continue the loop
9. continue;
10. }
11. printf("%d \n",i);
12. }//end of for loop
13. getch();
14. }

Output
1234678910

Comparision between break and continue statements

Break Continue

1 : break statement takes the control to the 1 :continue statement takes the control to
ouside of the loop the beginning of the loop..

2 : it is also used in switch statement. 2 : This can be used only in loop


statements.

3 : Always associated with if condition in 3 : This is also associated with if


loops. condition.

ARRAYS
Using Arrays in C

C PROGRAMMING Page 94
C supports a derived data type known as array that can be used to handle large amounts of data
(multiple values) at a time.

Definition:

An array is a group (or collection) of same data types.

Or

An array is a collection of data that holds fixed number of values of same type.

Or

Array is a collection or group of elements (data). All the elements of array


are homogeneous (similar). It has contiguous memory location.

Or

An array is a data structured that can store a fixed size sequential collection of elements of same
data type.

What‟s the need of an array?

Suppose you have to store marks of 50 students, one way to do this is allotting 50
variables. So it will be typical and hard to manage. For example we can not access the
value of these variables with only 1 or 2 lines of code.

Another way to do this is array. By using array, we can access the elements easily. Only
few lines of code is required to access the elements of array.

Where arrays are used

 to store list of Employee or Student names,


 to store marks of a students,
 or to store list of numbers or characters etc.

Advantage of C Array

1) Code Optimization: Less code to the access the data.

2) Easy to traverse data: By using the for loop, we can retrieve the elements of an array easily.

C PROGRAMMING Page 95
3) Easy to sort data: To sort the elements of array, we need a few lines of code only.

4) Random Access: We can access any element randomly using the array.

Disadvantage of Array

Fixed Size: Whatever size, we define at the time of declaration of array, we can't exceed the
limit. So, it doesn't grow the size dynamically like LinkedList

Declaration of an Array
data-type variable-name[size/length of array];

For example:

int arr[10];

int arr[ 5];

Here int is the data type, arr is the name of the array and 10 is the size of array. It means
array arr can only contain 10 elements of int type. Index of an array starts from 0 to size-1 i.e
first element of arr array will be stored at arr[0] address and last element will occupy arr[9].

Initialization of an Array

C PROGRAMMING Page 96
After an array is declared it must be initialized. Otherwise, it will contain garbage value(any
random value). An array can be initialized at either compile time or at runtime.

Compile time Array initialization

Compile time initializtion of array elements is same as ordinary variable initialization.

Syntax : data_type array_name[size]={v1,v2,…vn/list of values ;

Example

int age[5]={22,25,30,32,35};

int marks[4]={ 67, 87, 56, 77 }; //integer array initialization

float area[5]={ 23.4, 6.8, 5.5 }; //float array initialization

int marks[4]={ 67, 87, 56, 77, 59 }; //Compile time error

Different ways of initializing arrays :

1 : Initilizing all specified memory locations

2 : Partial array initialization.

3 : Intilization without size.

4 : String initialization.

1 : Initilizing all specified memory locations : If the number of values to be initialized is equal
to size of array. Arrays can be initialized at the time of declaration. Array elements can be
initialized with data items of type int,float,char, etc.

Ex : consider integer initialization

int a[5]={10,20,30,40,50};

C PROGRAMMING Page 97
During compilation, 5 contiguous memory locations are reserved by the compiler for the
variable a and all these locations are initialized.

The array a is initialized as

a[0] a[1] a[2] a[3] a[4]

10 20 30 40 50

1000 1002 1004 1006 1008

If the size of integer is 2 bytes, 10 bytes will be allocated for the variable a.

Ex : consider character initialization

char b[8] = {„C‟,‟O‟,‟M‟,‟P‟,‟U‟,‟T‟,‟E‟,‟R‟};

The array b is initialized as

b[0] b[1] b[2] b[3] b[4] b[5] b[6] b[7]

C O M P U T E R

Other Examples : char b[5]={„J‟,‟B‟,‟R‟,‟E‟,‟C‟,‟B‟};

//error : number of initial values are more than the size of array.

Other Example : int a[5]={10,20,30,40,50,60};

C PROGRAMMING Page 98
//error : Number of initial values are more than the size of array.

2 : Partial Array Initilization : partial array initialization is possible in C language. If the


number of values to be initialized is less than the size of the array, then the elements are
initialized in the order from 0th location. The remaining locations will be initialized to zero
automatically.

Ex : Consider the partial initilization

int a[5]={10,15};

Eventhough compiler allocates 5 memory locations, using this declaration


statement, the compiler initializes first two locations with 10 and 15, the next set of memory
locations are automatically initialized to zero.

The array a is partial initialization as

a[0] a[1] a[2] a[3] a[4]

10 15 0 0 0

1000 1002 1004 1006 1008

How to access the elements of an array?


You can access elements of an array by indices/index. You can use array subscript (or index) to
access any element stored in array. Subscript starts with 0, which means array_name[0] would be
used to access first element in an array.

In general array_name[n-1] can be used to access nth element of an array. where n is any integer
number.

Example

float mark[5];

Suppose you declared an array mark as above. The first element is mark[0], second element
is mark[1] and so on.

C PROGRAMMING Page 99
Few key notes:

 Arrays have 0 as the first index not 1. In this example, mark[0]


 If the size of an array is n, to access the last element, (n-1) index is used. In this
example, mark[4]
 Suppose the starting address of mark[0] is 2120d. Then, the next address, a[1], will be
2124d, address of a[2] will be 2128d and so on. It's because the size of a float is 4 bytes.

Input data into array

As you can see, in above example that I have used „for loop‟ and „scanf statement‟ to enter data
into array. You can use any loop for data input.

Code:

for (x=0; x<=19;x++)

printf("enter the integer number %d\n", x);

scanf("%d", &num[x]);

Reading out data from an array

For example you want to read and display array elements, you can do it just by using any
loop. Suppose array is mydata[20].

for (int i=0; i<20; i++)

printf("%d\n", mydata[x]);

C PROGRAMMING Page 100

You might also like