0% found this document useful (0 votes)
32 views

Practical First

1. An array is a collection of similar data types stored in contiguous memory locations and accessed using an index. 2. Arrays allow for random access of elements, use less code than individual variables, and make sorting and traversing elements easy. 3. However, arrays have fixed sizes that are determined at declaration and inserting or removing elements can be costly.

Uploaded by

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

Practical First

1. An array is a collection of similar data types stored in contiguous memory locations and accessed using an index. 2. Arrays allow for random access of elements, use less code than individual variables, and make sorting and traversing elements easy. 3. However, arrays have fixed sizes that are determined at declaration and inserting or removing elements can be costly.

Uploaded by

Bhakti Binwade
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PRACTICAL 1

ARRAY
An array is defined as the collection of similar type of data items stored at contiguous memory
locations.

Arrays are the derived data type in C programming language which can store the primitive type of data
such as int, char, double, float, etc. C array is beneficial if you have to store similar elements.

Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements and the number of elements
required by an array as follows −
type arrayName [ arraySize ];

Advantages of an Array in C:

1. Random access of elements using array index.


2. Use of less line of code as it creates a single array of multiple elements.
3. Easy access to all the elements.
4. Traversal through the array becomes easy using a single loop.
5. Sorting becomes easy as it can be accomplished by writing less line of code.

Disadvantages of an Array in C:

1. Allows a fixed number of elements to be entered which is decided at the time of declaration.
Unlike a linked list, an array in C is not dynamic.
2. Insertion and deletion of elements can be costly since the elements are needed to be managed
in accordance with the new memory allocation.
PRACTICAL 1
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>

int main() {
int values[5];

printf("Enter 5 integers: ");

// taking input and storing it in an array


for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}

printf("Displaying integers: ");

// printing elements of an array


for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}

// Program to find the average of n numbers using arrays


#include <stdio.h>
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]);

// adding integers entered by the user to the sum variable


sum += marks[i];
}
average = sum/n;
printf("Average = %d", average);
return 0;
}
PRACTICAL 1
Program to read and display numbers
#include <stdio.h>
int main()
{
int i, n, arr[20];

printf("\n Enter the number of elements to store in the array : ");


scanf("%d", & n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d",& arr[i]);
}
printf("\n The array elements are ");
for(i=0;i<n;i++)
printf("\t %d", arr[i]);
return 0;
}

C Program to insert an element


// at a specific position in an Array

#include <stdio.h>
 
int main()
{
    int arr[100] = { 0 };
    int i, x, pos, n = 10;
 
    // initial array of size 10
    for (i = 0; i < 10; i++)
        arr[i] = i + 1;
 
    // print the original array
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
 
    // element to be inserted
    x = 50;
 
    // position at which element
    // is to be inserted
    pos = 5;
 
    // increase the size by 1
    n++;
 
PRACTICAL 1
    // shift elements forward
    for (i = n-1; i >= pos; i--)
        arr[i] = arr[i - 1];
 
    // insert x at pos
    arr[pos - 1] = x;
 
    // print the updated array
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
 
    return 0;
}

Remove element from array C program


#include <stdio.h>
int main()
{
   int array[100], position, c, n;

   printf("Enter number of elements in array\n");


   scanf("%d", &n);

   printf("Enter %d elements\n", n);

   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);

   printf("Enter the location where you wish to delete element\n");


   scanf("%d", &position);

   if (position >= n+1)
      printf("Deletion not possible.\n");
   else
   {
      for (c = position - 1; c < n - 1; c++)
         array[c] = array[c+1];

      printf("Resultant array:\n");

      for (c = 0; c < n - 1; c++)
         printf("%d\n", array[c]);
   }

   return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy