PPSC CT-3

 SET -II

1. WRITE A C PROGRAM TO SPERATE ODD AND EVEN IN SPERATE ARRAY ? 

 #include <stdio.h>


void main()

 {

    int arr1[10], arr2[10], arr3[10];

    int i,j=0,k=0,n;

       printf("\n\nSeparate odd and even integers in separate arrays:\n");

       printf("------------------------------------------------------\n");


       printf("Input the number of elements to be stored in the array :");

       scanf("%d",&n);

   

       printf("Input %d elements in the array :\n",n);

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

            {

      printf("element - %d : ",i);

      scanf("%d",&arr1[i]);

    }


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

    {

if (arr1[i]%2 == 0)

{

   arr2[j] = arr1[i];

   j++;

}

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]);

    }

    printf("\n\n");

 }

2.  FIND THE SUM OF ELEMENTS IN ARRAY IN C PROGRAM 

#include<stdio.h>

int main()
{
    //let's assume the maximum array size as 100.
    //initialize sum as 0. Otherwise, it will take some garbage value.
    int arr[100], size, i, sum = 0;
    
    //Get size input from user
    printf("Enter array size\n");
    scanf("%d",&size);
    
    //Get all elements using for loop and store it in array
    printf("Enter array elements\n");
    for(i = 0; i < size; i++)
          scanf("%d",&arr[i]);
          
    //add all elements to the variable sum.
    for(i = 0; i < size; i++)
          sum = sum + arr[i]; // same as sum += arr[i];
    
    //print the result
    printf("Sum of the array = %d\n",sum);
    
    return 0;
}
SET-IV

1. WRITE A C PROGRAM TO SPERATE ODD AND EVEN IN SPERATE ARRAY ? 

 #include <stdio.h>


void main()

 {

    int arr1[10], arr2[10], arr3[10];

    int i,j=0,k=0,n;

       printf("\n\nSeparate odd and even integers in separate arrays:\n");

       printf("------------------------------------------------------\n");


       printf("Input the number of elements to be stored in the array :");

       scanf("%d",&n);

   

       printf("Input %d elements in the array :\n",n);

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

            {

      printf("element - %d : ",i);

      scanf("%d",&arr1[i]);

    }


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

    {

if (arr1[i]%2 == 0)

{

   arr2[j] = arr1[i];

   j++;

}

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]);

    }

    printf("\n\n");

 }


2. WRITE A PROGRAM TO SORT ELEMENT IN ARRAY 

  1. #include <stdio.h>    
  2.      
  3. int main()    
  4. {    
  5.     //Initialize array     
  6.     int arr[] = {5, 2, 8, 7, 1};     
  7.     int temp = 0;    
  8.         
  9.     //Calculate length of array arr    
  10.     int length = sizeof(arr)/sizeof(arr[0]);    
  11.         
  12.     //Displaying elements of original array    
  13.     printf("Elements of original array: \n");    
  14.     for (int i = 0; i < length; i++) {     
  15.         printf("%d ", arr[i]);     
  16.     }      
  17.         
  18.     //Sort the array in ascending order    
  19.     for (int i = 0; i < length; i++) {     
  20.         for (int j = i+1; j < length; j++) {     
  21.            if(arr[i] > arr[j]) {    
  22.                temp = arr[i];    
  23.                arr[i] = arr[j];    
  24.                arr[j] = temp;    
  25.            }     
  26.         }     
  27.     }    
  28.         
  29.     printf("\n");    
  30.         
  31.     //Displaying elements of array after sorting    
  32.     printf("Elements of array sorted in ascending order: \n");    
  33.     for (int i = 0; i < length; i++) {     
  34.         printf("%d ", arr[i]);    
  35.     }    
  36.     return 0;    
  37. }
SET- III
1. WRITE A C PROGRAM TO FIND FIRST  AND SECOND LARGEST NUMBER IN  ARRAY ?
#include<stdio.h>
 
int main()
{
int a[50],i,n,large,small;
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
 
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("The largest element is %d",large);
printf("\nThe smallest element is %d",small);
 
return 0;
} 

2. WRITE IS ARRAY ? HOW TO DECLARE THE ARRAY ?
An array in C/C++ or be it in any programming language is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using indices of an array.  They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type. To add to it, an array in C
// Array declaration by specifying size
int arr1[10];
int n = 10;
int arr2[n];

// Array declaration by initializing elements
int arr[] = { 10, 20, 30, 40 }
  
// Compiler creates an array of size 4.
// above is same as  "int arr[4] = {10, 20, 30, 40}"


// Array declaration by specifying size and initializing
// elements
int arr[6] = { 10, 20, 30, 40 }
  
// Compiler creates an array of size 6, initializes first
// 4 elements as specified by user and rest two elements as
// 0. above is same as  "int arr[] = {10, 20, 30, 40, 0, 0}

Advantages of an Array in C/C++: 

  1. Random access of elements using array index.
  2. Use of fewer 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 fewer line of code.

Disadvantages of an Array in C/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
SET NO - I 


1. WRITE A PROGRAM TO PRINT UNQIUE ELEMENTS IN C 


#include <stdio.h>
int main()
{
    int arr1[100], n,ctr=0;
    int i, j, k;
       printf("\n\nPrint all unique elements of an array:\n");
       printf("------------------------------------------\n");	
       printf("Input the number of elements to be stored in the array: ");
       scanf("%d",&n);
       printf("Input %d elements in the array :\n",n);
       for(i=0;i<n;i++)
            {
	      printf("element - %d : ",i);
	      scanf("%d",&arr1[i]);
	    }
    printf("\nThe unique elements found in the array are: \n");
    for(i=0; i<n; i++)
    {
        ctr=0;
        for(j=0,k=n; j<k+1; j++)
        {
            /*Increment the counter when the seaarch value is duplicate.*/
            if (i!=j)
            {
		       if(arr1[i]==arr1[j])
              {
                 ctr++;
               }
             }
        }
       if(ctr==0)
        {
          printf("%d ",arr1[i]);
        }
    }
       printf("\n\n");
}
2. WRITE IS ARRAY ? HOW TO DECLARE THE ARRAY ?
An array in C/C++ or be it in any programming language is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using indices of an array.  They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type. To add to it, an array in C
// Array declaration by specifying size
int arr1[10];
int n = 10;
int arr2[n];

// Array declaration by initializing elements
int arr[] = { 10, 20, 30, 40 }
  
// Compiler creates an array of size 4.
// above is same as  "int arr[4] = {10, 20, 30, 40}"


// Array declaration by specifying size and initializing
// elements
int arr[6] = { 10, 20, 30, 40 }
  
// Compiler creates an array of size 6, initializes first
// 4 elements as specified by user and rest two elements as
// 0. above is same as  "int arr[] = {10, 20, 30, 40, 0, 0}

Advantages of an Array in C/C++: 

  1. Random access of elements using array index.
  2. Use of fewer 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 fewer line of code.

Disadvantages of an Array in C/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





Previous Post Next Post