C program to insert an element in an array

  • Program to insert an element in an array at a given position
  • C program to insert an element in an array without using function



Program #1: Write a c program to insert an element in array without using function


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   int insarray[100], pos, c, n, value;
  7.  
  8.   printf("Enter number of elements of array\n");
  9.    scanf("%d", &n);
  10.  
  11.    printf("Enter all %d elements\n", n);
  12.  
  13.    for (c = 0; c < n; c++)
  14.       scanf("%d", &insarray[c]);
  15.  
  16.    printf("Enter the specific position where you wish to insert an element in array\n");
  17.    scanf("%d", &pos);
  18.  
  19.    printf("Enter the value to insert\n");
  20.    scanf("%d", &value);
  21.  
  22.    for (c = n - 1; c >= pos - 1; c--)
  23.       insarray[c+1] = insarray[c];
  24.  
  25.    insarray[pos-1] = value;
  26.  
  27.    printf("Resultant array is\n");
  28.  
  29.    for (c = 0; c <= n; c++)
  30.       printf("%d\n", insarray[c]);
  31.  
  32. getch();
  33. }
Output:

insert element in array c

C program to delete an element in an array

  • Write a c program to insert and delete an element in array
  • Write a c program to delete an element in an array
  • C Program to Delete the Specified Integer from an Array without using function



Program #1: Write a c program to insert and delete an element in array


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   
  7.      int array[10];
  8.  
  9.     int i, n, position, element, exist = 0;
  10.  
  11.  
  12.     printf("Enter number of elements \n");
  13.  
  14.     scanf("%d", &n);
  15.  
  16.     printf("Enter the elements\n");
  17.  
  18.     for (i = 0; i < n; i++)
  19.  
  20.     {
  21.  
  22.         scanf("%d", &array[i]);
  23.  
  24.     }
  25.  
  26.     printf("Input array elements are\n");
  27.  
  28.     for (i = 0; i < n; i++)
  29.  
  30.     {
  31.  
  32.         printf("%d\n", array[i]);
  33.  
  34.     }
  35.  
  36.     printf("Enter the element to be deleted from array\n");
  37.  
  38.     scanf("%d", &element);
  39.  
  40.     for (i = 0; i < n; i++)
  41.  
  42.     {
  43.  
  44.         if (array[i] == element)
  45.  
  46.         {
  47.             exist = 1;
  48.  
  49.             position = i;
  50.  
  51.             break;
  52.  
  53.         }
  54.  
  55.     }
  56.  
  57.     if (exist == 1)
  58.  
  59.     {
  60.  
  61.         for (i = position; i <  n - 1; i++)
  62.  
  63.         {
  64.  
  65.             array[i] = array[i + 1];
  66.  
  67.         }
  68.  
  69.         printf("array after deletion  \n");
  70.  
  71.         for (i = 0; i < n - 1; i++)
  72.  
  73.         {
  74.  
  75.             printf("%d\n", array[i]);
  76.  
  77.         }
  78.  
  79.     }
  80.  
  81.     else
  82.  
  83.         printf("Given element %d is not exist in the array \n", element);
  84. getch();
  85. }
Output:


delete array element c

Apache Spark create rdd from an array java | convert an array in to RDD

  • RDD is the spark's core abstraction.
  • Full form of RDD is resilient distributed dataset.
  • Each RDD will split into multiple partitions which may be computed in different machines of cluster
  • We can create Apache spark RDD in two ways
    1. Parallelizing a collection
    2. Loading an external dataset.
     
  • So Creating RDD from an array comes in under Parallelizing a collection.
  • Let us see Apache spark an example program to convert an array into RDD.



Program #1: Write a Apache spark java example program to create simple RDD using parallelize method of JavaSparkContext. convert an array in to RDD


  1.  package com.instanceofjava.sparkInterview;
  2.  
  3. import java.util.Arrays;
  4.  
  5. import org.apache.spark.SparkConf;
  6. import org.apache.spark.api.java.JavaRDD;
  7. import org.apache.spark.api.java.JavaSparkContext;
  8.  
  9. /**
  10.  *  Apache spark examples:RDD in spark example program  
  11. *  converting an array to RDD
  12.  * @author www.instanceofjava.com
  13.  */
  14. public class SparkTest {
  15.     
  16.     public static void main(String[] args) {
  17.         
  18.         SparkConf conf = new
  19. SparkConf().setMaster("local[2]").setAppName("InstanceofjavaAPP");
  20.         JavaSparkContext sc = new JavaSparkContext(conf);
  21.         
  22.         String[] arrayStr={"convert array to rdd","convert array into rdd"};
  23.         
  24.         JavaRDD<String> strRdd=sc.parallelize(Arrays.asList(arrayStr));
  25.         System.out.println("apache spark rdd created: "+strRdd);
  26.         
  27.         /**
  28.          * Return the first element in this RDD.
  29.          */
  30.         System.out.println(strRdd.first());
  31.         
  32.     }
  33.  
  34. }
  35. }

Output:

  1. apache spark rdd created: ParallelCollectionRDD[0] at parallelize at SparkTest.java:24
  2. convert array to rdd



convert an array to rdd

How to create rdd in apache spark using java

  • RDD is the spark's core abstraction.
  • Full form of RDD is resilient distributed dataset.
  • That means it is immutable collection of objects.
  • Each RDD will split into multiple partitions which may be computed in different machines of cluster
  • We can create Apache spark RDD in two ways
    1. Parallelizing a collection
    2. Loading an external dataset.
  • Now we sill see an example program on creating RDD by parallelizing a collection.
  • In Apache spark JavaSparkContext  class providing parallelize() method.
  • Let us see the simple example program to create Apache spark RDD in java



Program #1: Write a Apache spark java example program to create simple RDD using parallelize method of JavaSparkContext.

  1. package com.instanceofjava.sparkInterview;
  2. import java.util.Arrays;
  3.  
  4. import org.apache.spark.SparkConf;
  5. import org.apache.spark.api.java.JavaRDD;
  6. import org.apache.spark.api.java.JavaSparkContext;
  7.  
  8. /**
  9.  *  Apache spark examples:RDD in spark example program
  10.  * @author www.instanceofjava.com
  11.  */
  12. public class SparkTest {
  13.     
  14.  public static void main(String[] args) {
  15.         
  16.  SparkConf conf = new SparkConf().setMaster("local[2]").setAppName("InstanceofjavaAPP");
  17.  JavaSparkContext sc = new JavaSparkContext(conf);
  18.         
  19.  JavaRDD<String> strRdd=sc.parallelize(Arrays.asList("apache spark element1","apache
  20. spark element2"));
  21.  System.out.println("apache spark rdd created: "+strRdd);
  22.         
  23. /**
  24.  * Return the first element in this RDD.
  25.  */
  26. System.out.println(strRdd.first());
  27.  }
  28.  
  29. }
  30.  
  31. }

Output:

  1. apache spark rdd created: ParallelCollectionRDD[0] at parallelize at SparkTest.java:21
  2. spark element1


create apache spark rdd java

Remove whitespace from string javascript

  • To remove white space in a string in JavaScript at the starting and ending. i.e both sides of a string we have trim() method in JavaScript.
  •  By Using .trim() method we can remove white spaces at both sides of a string.
  • var str = "       instanceofjava.com        ";
    alert(str.trim());
  • Will alert a popup with instanceofjava.com.
Javascript remove last character if comma 
  • If  we want to remove last character of a string then we need to use replace method in JavaScript.
  • str = str.replace(/,\s*$/, "");
  • We can also use slice method to remove last character.
remove white spaces in javascript

Remove specific characters from a string in Javascript


  • To remove specific character from string in java script we can use

  1. var string = 'www.Instanceofjava.com'; 
  2. string.replace(/^www.+/i, ''); 'Instanceofjava.com'

C program for addition subtraction multiplication and division using switch case

  • Write a C program to simulate a simple calculator to perform arithmetic operations like a and division only on integers. Error message should be reported if any attempt is made to divide by zero    
  • C program for addition subtraction multiplication and division using switch case
  • Write a c program to perform arithmetic operations using switch case
  • C program to add subtract multiply and divide two numbers using functions
Program #1: Write a c program to perform arithmetic operations using switch case



  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. int main()
  5. {
  6.     char oper;            /* oper is an operator to be selected */
  7.     float n1, n2, result;
  8.  
  9.     printf ("Simulation of a Simple Calculator\n\n");
  10.  
  11.     printf("Enter two numbers\n");
  12.     scanf ("%f %f", &n1, &n2);
  13.  
  14.     fflush (stdin);
  15.  
  16.     printf("Enter the operator [+,-,*,/]\n");
  17.     scanf ("%c", &oper);
  18.  
  19.     switch (oper)
  20.        {
  21.         case '+': result = n1 + n2;
  22.               break;
  23.         case '-': result = n1 - n2;
  24.               break;
  25.         case '*': result = n1 * n2;
  26.               break;
  27.         case '/': result = n1 / n2;
  28.               break;
  29.         default : printf ("Error in operation\n");
  30.               break;
  31.     }
  32.  
  33.     printf ("\n%5.2f %c %5.2f= %5.2f\n", n1,oper, n2, result);
  34. getch();
  35. }


Output:

algorithm for switch case in c


  • Output
    Simulation of Simple Calculator

    Enter two numbers
    3 5
    Enter the operator [+,-,*,/]
    +

     3.00 +  5.00=  8.00
     
  • RUN2
    Simulation of Simple Calculator

    Enter two numbers
    12.75
    8.45
    Enter the operator [+,-,*,/]
    -

    12.75 -  8.45=  4.30
  • RUN3
    Simulation of Simple Calculator

    Enter two numbers
    12 12
    Enter the operator [+,-,*,/]
    *

    12.00 * 12.00= 144.00
     
  • RUN4
    Simulation of Simple Calculator

    Enter two numbers
    5
    9
    Enter the operator [+,-,*,/]
    /

     5.00 /  9.00=  0.56
Select Menu