Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

GoTo Statement

The C goto statement is a jump statement which is sometimes also referred to as an


unconditional jump statement. The goto statement can be used to jump from anywhere to
anywhere within a function.

Syntax

goto label;

In the above syntax, the first line tells the compiler to go to or jump to the statement
marked as a label. Here, the label is a user-defined identifier that indicates the target
statement.
#include <stdio.h>
int main() {

int i;
int number, average, sum = 0;

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


printf("Enter a number: ");
scanf("%d", &number);
if (number < 10) {
goto jump;
}
}

jump:
printf("Jump called");
return 0;
}
Unit 3
Arrays
The variables that we have used till now are capable of storing only one value at a time.
Consider a situation when we want to store and display the age of 100 employees.

For this we have to do the following

1.· Declare 100 different variables to store the age of employees.

2. Assign a value to each variable.

3. Display the value of each variable.


An array in C is a fixed-size collection of similar data items stored in contiguous memory
locations and each data item is called an element of the array. The data type of the
elements may be any valid data type like char, int or float. The elements of array share the
same variable name but each element has a different index number known as subscript.
Arrays can be single dimensional or multidimensional. The number of subscripts
determines the dimensions of array.

A one-dimensional array has one subscript, two dimensional array has two subscripts
and on.

The one-dimensional arrays are known as vectors and two-dimensional arrays are known
as matrices.
Properties of Arrays

• Each element of an array is of same data type and carries the same size, i.e., int = 4
bytes.

• Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.

• Elements of the array can be randomly accessed since we can calculate the address
of each element of the array with the given base address and the size of the data
element.
Advantage of C Array

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

2) Ease of traversing: By using the for loop, we can retrieve the elements of an array
easily.

3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.

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

Disadvantage of C Array

1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't
exceed the limit.
One Dimensional Array
Declaration of I-D Array
We can declare an array by specifying its name, the type of its elements, and the size of its
dimensions.

When we declare an array in C, the compiler allocates the memory block of the specified size
to the array name.
data_type array_name[size];
int age[100];
float sal[15];
char grade[20];
Accessing I-D Array Element
The elements of an array can be accessed by specifying the array name followed by subscript
in brackets.
Array Initialization with Declaration

In this method, we initialize the array along with its declaration. We use an initializer list
to initialize multiple elements of the array. An initializer list is the list of values enclosed
within braces { } separated b a comma.

data_type array_name [size] = {value1, value2, ... valueN};

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


Array Initialization with Declaration without Size

we can skip declaring the size of the array as the compiler can automatically deduce
the size of the array in these cases.

data_type array_name[] = {1,2,3,4,5};

Int a[ ] = {1,3,5,6,7};
Array Initialization after Declaration

Int arr[10] is an array of int type-


Reading values in arr[10]

int arr[10], i;
for( i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
Displaying values of arr[ 10]
for( i = 0; i < 10; i++)
printf(%d ", arr[i])
Access Array Elements

We can access any element of an array in C using the array subscript operator [ ] and
the index value of the element.

array_name [index]; int main()


{

int a[5] = { 15, 25, 35, 45, 55 };

printf("Element at a[2]: %d\n", a[2]);

printf("Element at a[0]: %d", a[0]);

return 0;
}
Update Array Element

We can update the value of an element at the given index i in a similar way to accessing
an element by using the array subscript operator [ ] and assignment operator =.

array_name[i] = new_value;

a[3] = 200;
Array Traversal

#include<stdio.h>
int main(){
int i=0;
int marks[5]={20,30,40,50,60};
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}
return 0;
}
Calculating the Length of an Array

The length of an array is given by the number of elements stored in it. The general
formula to calculate the length of an array is

Length = upper_bound – lower_bound + 1

where upper_bound is the index of the last element and lower_bound is the index of the
first element in the array

Let Age[5] be an array of integers such that Age[0] = 2, Age[1] = 5, Age[2] = 3, Age[3] = 1,
Age[4] = 7
Calculating the Address of Array Elements
array name is a symbolic reference to the address of the first byte of the array. When we use
the array name, we are actually referring to the first byte of the array. Since an array stores
all its data elements in consecutive memory locations, storing just the base address, that is
the address of the first element in the array, is sufficient. The address of other data elements
can simply be calculated using the base address. The formula to perform this calculation is
Address of data element, A[k] = BA(A) + w(k – lower_bound)
Here, A is the array, k is the index of the element of which we have to calculate the address,
BA is the base address of the array A, and w is the size of one element in memory, for
example, size of int is 2

Given an array int marks[]={99,67,78,56,88,90,34,85}, calculate the address of marks[4] if


the base address = 1000.
OPERATIONS ON ARRAY

•Traversing an array : Traversing an array means accessing each and every


element of the array for a specific purpose.

• Inserting an element in an array

•Searching an element in an array

•Deleting an element from an array

• Merging two arrays

•Sorting an array in ascending or descending order


Find the average of n numbers using arrays
Int main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]
sum = sum+marks[i];
}

average = sum/n;

printf("Average = %d", average);


}
void main() search for an item in the array
{
int num, i, item, found = 0, array[num];
printf("Enter the number of elements "); if (found == 1)
scanf("%d", &num); printf("Element is present in the array at
printf("Enter the elements of array \n"); position %d",i+1);
for (i = 0; i < num; i++) else
{ printf("Element is not present in the array\n");
scanf("%d", &array[i]); }
}
printf("Enter the element to be searched ");
scanf("%d", &item);
for (i = 0; i < num ; i++)
{
if (item == array[i] )
{
found = 1;
break;
}}
Write a C program to count the positive negative and zeros in given array.
int main()
{
int a[5], n, i, positive=0, negative=0, zero=0;
printf("Enter 5 elements: ");
for(i=0; i<5; i++)
{
scanf("%d", &a[i]);
if(a[i]>0)
positive++;
else if(a[i]==0)
zero++;
else
negative++;
}

printf("%d%d%d", positive, zero,negative);


}
Write a program to find no. of duplicate
elements in array for (i = 0; i < n; i++)
{
#include <stdio.h> for (j = i + 1; j < n; j++)
int main() {
{ if (arr[i] == arr[j])
int arr[100]; {
int n,mm=1,ctr=0; ctr++;
int i, j; break;
printf(“Enter the no of Elements :"); }
scanf("%d",&n); }
printf("Input %d elements in the array :\n",n); }
for(i=0;i<n;i++) printf(“Number of duplicate
{ elements: %d\n", ctr);
printf("element - %d : ",i); return 0;
scanf("%d",&arr[i]); }
}
Write a program in C to separate odd and even integers into separate arrays.

void main() for(i=0;i<n;i++)


{ {
int arr1[10], arr2[10], arr3[10]; if (arr1[i]%2 == 0)
int i,j=0,k=0,n; {
arr2[j] = arr1[i];
for(i=0;i<10;i++) j++;
{ }
scanf("%d",&arr1[i]); else
} {
arr3[k] = arr1[i];
k++;
}
}
printf("\nThe Even elements are : \n");
for(i=0;i<j;i++)
{
printf("%d ",arr2[i]);
}

printf("\nThe Odd elements are :\n");


for(i=0;i<k;i++)
{
printf("%d ", arr3[i]);
}

}
Write a program in C to read n number of values in an array and display them in reverse
order
printf("\n\nThe values store into the
#include <stdio.h>
array in reverse are :\n");
int main()
for(i=n-1;i>=0;i--)
{
{
int i,n,a[10];
printf("%d",a[i]);
}
n=10;
}
printf("Input %d number of elements in the
array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
Write a program in C to print all unique elements in an array.

Write a Program to count no of occurrences of character ‘b’ in an array.

Write a program in C to sort elements of an array in ascending order.

Write a program in C to copy the elements of one array into another array.

You might also like