Selection sort is a sorting technique wherein at each iteration, a minimum element is selected from an unsorted array and compared with all other remaining elements. if an element is found that is smaller than already assumed minimum element, swapping would be performed.
It divides an array into two parts. One that is already sorted and another one which is still unsorted that need to be arranged.
From unsorted sublist, the algorithm finds the minimum element and adds on to sorted category. It ends when we get a single consolidated sorted sublist.
FOR i=0 to Array.Length minimum = i FOR j=0 to Array.Length if A[min]< A[j]) Swap A[min] with A[j] End IF END FOR END FOR
#include<conio.h> #include<stdio.h> #define size 5 void main() { int arr[size],i,j,temp,min; printf("Please enter elements: \n "); for(i=0;i<=size-1;i++) { scanf("%d",&arr[i]); } for(i=0;i<=size-1;i++) { min= i; for(j=0;j<=size-1;j++) { if(arr[min]<arr[j]){ temp = arr[j]; arr[j] =arr[min]; arr[min] = temp; } } } printf(" \n Entered Elements :"); for(i=0;i<=size-1;i++) { printf("\n %d ",arr[i]); } getch(); }
Lastly, it is an inefficient algorithm for the large dataset as it requires O(n2) comparisons in all cases worst, average and best case. It is simple and occasionally has an upper hand over other complicated algorithms.
]]>Program:
#include<graphics.h> #include<stdio.h> #include<conio.h> void main(){ int gd=DETECT,gm; initgraph(&gd,&gm,"C:\\TURBOC3\\BGI"); printf("\t\t ********* HUT ********"); line(150,100,50,200); line(150,100,350,100); line(150,100,300,200); line(300,200,500,200); line(350,100,500,200); line(50,200,300,200); rectangle(50,400,300,200); rectangle(300,200,500,400); rectangle(130,250,230,400); getch(); closegraph(); }
Output:
Program:
#include<stdio.h> #include<conio.h> #include<graphics.h> void shoot(); void shootagain(); void area(); void explode(int,int,int); void main() { int gm,gd=DETECT; initgraph(&gd,&gm,"C:\\TURBOC3\\BGI"); shoot(); getch(); closegraph(); } void shoot() { int i=0; int x=0,y=480,x1=15,y1=460; while(i<350) { area(); line(x+i,y-i,x1+i,y1-i); delay(50); i=i+10; cleardevice(); } explode(x+350,y-350,5); shootagain(); } void shootagain() { int i=0; int x=600,y=480,x1=585,y1=460; while(i<250) { setcolor(15); area(); line(x-i,y-i,x1-i,y1-i); delay(30); i=i+10; cleardevice(); } explode(x-300,y-300,5); } void explode(int x,int y,int r) { int k,j,interval=0; for(k=0;k<2;k++) { interval=interval+50; for(j=0;j<interval;j++) { area(); setcolor(BLACK); setcolor(rand()/15); // horizontal left and right circle(x+j,y,r+k); circle(x-j,y,r+k); //vertical up and down circle(x,y+j,r+k); circle(x,y-j,r+k); //slighten down circle(x+j,y+j,r+k); circle(x-j,y-j,r+k); //slighten up circle(x-j,y+j,r+k); circle(x+j,y-j,r+k); delay(30); cleardevice(); } } } void area() { //Area setcolor(15); line(0,350,600,350); rectangle(0,350,100,150); rectangle(40,350,60,300); rectangle(10,170,30,200); rectangle(70,170,90,200); rectangle(10,230,30,260); rectangle(70,230,90,260); rectangle(100,350,180,250); line(100,250,140,180); line(180,250,140,180); rectangle(110,280,130,300); rectangle(150,280,170,300); rectangle(130,350,160,330); rectangle(180,350,350,300); rectangle(190,310,220,330); rectangle(340,310,310,330); rectangle(370,350,440,150); rectangle(385,350,405,300); rectangle(380,170,400,200); rectangle(410,170,430,200); rectangle(380,230,400,260); rectangle(410,230,430,260); settextstyle(BOLD_FONT,HORIZ_DIR,1); outtextxy(110,50,"HAPPY NEW YEAR"); }
Output:
Both delete and free() are used for releasing the memory allocated at runtime. The main difference between them is, the delete operator deallocates memory for objects which are dynamically allocated through new keyword, whereas, the free() function deallocates memory that is dynamically allocated through malloc, calloc or realloc function.
Basis | free | delete |
---|---|---|
Refers to | A Library function | An operator |
Ideal for | Deallocating memory occupied using malloc,calloc and realloc function | Releasing memory which is allocated using new keyword |
Overriding | Not Possible | Possible |
Execution speed | slow | fast |
The main job of the free function is to release the memory which is allocated at the runtime. The free() function can be used in both C as well as C++ programming language.
It releases dynamically allocated memory created by calloc,realloc and calloc functions and provides extra reusable heap memory that can be used later. It takes pointer whose memory to be released as an argument.
void free(void *ptr)
The delete operator also has the same job like free function, that is, to release the memory which is allotted at the runtime. The delete operator is most commonly used in C++ programming.
It can defined as an operator which is used to release the memory allocated by new keyword such as objects and arrays. In C++, when delete is used with an object, it first calls the destructor of that particular object and then after deallocates the memory occupied by it. However, in the case of free(), no destructor is called of the respective object.
delete obj;
]]>
In this, we are using following formula directly in the code:
Geometric Mean :Average :
Harmonic mean :
Program:
#include<stdio.h> int main () { int i, size; float arr[10], arithmeticMean; float sum = 0; printf ("Please enter the number of elements = "); scanf ("%d", &size); printf ("Now, enter the elements \n"); for (i = 0; i < size; i++) { printf ("Element %d : ", i + 1); scanf ("%f", &arr[i]); } for (i = 0; i < size; i++) { sum = sum + arr[i]; } arithmeticMean = (float) (sum / size); printf ("\n Average or Arithmetic Mean = %.2f", arithmeticMean); return 0; }
Output
Please enter the number of elements = 9 Now, enter the elements Element 1 : 1 Element 2 : 2 Element 3 : 3 Element 4 : 4 Element 5 : 5 Element 6 : 6 Element 7 : 7 Element 8 : 8 Element 9 : 9 Average or Arithmetic Mean = 5.00
Program:
#include<stdio.h> int main (){ int i, size; float arr[10], oneNthValue = 0; float harmonicMean; printf ("Please enter the number of elements = "); scanf ("%d", &size); printf ("Now, enter the elements \n"); for (i = 0; i < size; i++) { printf ("Element %d : ", i + 1); scanf ("%f", &arr[i]); } for (i = 0; i < size; i++) { oneNthValue = oneNthValue + 1 / arr[i]; } harmonicMean = (float) size / oneNthValue; printf ("\n Harmonic mean= %.2f ", harmonicMean); return 0; }
Output:
Please enter the number of elements = 5 Now, enter the elements Element 1 : 2 Element 2 : 4 Element 3 : 6 Element 4 : 8 Element 5 : 10 Harmonic mean= 4.38
Program:
#include<stdio.h> #include<math.h> int main () { int i, size; double product = 1, root; float arr[10], oneNthValue = 0; float geometricMean; printf ("Please enter the number of elements = "); scanf ("%d", &size); printf ("Now, enter the elements \n"); for (i = 0; i < size; i++) { printf ("Element %d : ", i + 1); scanf ("%f", &arr[i]); } for (i = 0; i < size; i++) { product = (double) product *arr[i]; } root = (double) 1 / size; geometricMean = (float) pow (product, root); printf("Geometric mean = %.2f", geometricMean); return 0; }
Output:
Please enter the number of elements = 5 Now, enter the elements Element 1 : 12 Element 2 : 23 Element 3 : 14 Element 4 : 11 Element 5 : 17 Geometric mean = 14.85
Program:
#include<stdio.h> #include<math.h> /* C program to calculate arithmetic mean, harmonic mean and geometric mean */ int main () { int i, size; double product = 1, root; float arr[10], oneNthValue = 0; float harmonicMean, averageMean, geometricMean; float sum = 0; printf ("Please enter the number of elements = "); scanf ("%d", &size); printf ("Now, enter the elements \n"); for (i = 0; i < size; i++) { printf ("Element %d : ", i + 1); scanf ("%f", &arr[i]); } for (i = 0; i < size; i++) { oneNthValue = oneNthValue + 1 / arr[i]; sum = sum + arr[i]; product = (double) product *arr[i]; } root = (double) 1 / size; harmonicMean = (float) size / oneNthValue; averageMean = (float) (sum / size); geometricMean = (float) pow (product, root); printf ("\n Harmonic mean= %.2f \n Arithmetic mean = %.2f \n Geometric mean = %.2f", harmonicMean, averageMean, geometricMean); return 0; }
Output:
Please enter the number of elements = 5 Now, enter the elements Element 1 : 21 Element 2 : 3 Element 3 : 45 Element 4 : 76 Element 5 : 89 Harmonic mean= 11.69 Arithmetic mean = 46.80 Geometric mean = 28.61
Explanation:
The ASCII values are ranging from 0 to 128 that cover numbers from 0 to 9, uppercase letters and lowercase letters from a to z and some special characters are also the part of this system.
For example, ASCII value of 0 is 48.
or
Program:
#include <stdio.h> int main() { char value; printf("Enter a Value = "); scanf("%c", &value); printf("integer value of %c = %d\n", value, value); return 0; }
Output:
In case of number
Enter a Value = 0 ASCII integer value of 0 = 48
In case of uppercase letter
Enter a Value = A ASCII integer value of A = 65
In case of lowercase letter
Enter a Value = a ASCII integer value of a = 97
In case of special character
Enter a Value = @ ASCII integer value of @ = 64
Explanation:
It takes 5 arguments. First two arguments are the coordinates of a circle, its successive two arguments are starting angle and end angle and last one represents the radius of the circle.
The angle value of arc() function can be ranged from 0 to 360 degree.
Syntax:
void arc(coordinateX,coordinateY,startingAngle,closingAngle,radius)
Program:
#include<conio.h> #include<graphics.h> #include<stdio.h> #include<dos.h> int main() { int gd=DETECT,gm=0; initgraph(&gd,&gm,"c:\\tc\\bgi"); arc(100, 100, 135, 270, 100); getch(); closegraph(); }
Output:
A triangle can be broadly classified according to the length of its sides and the angle between the sides.
Based on the sides, there are three types of triangle
1. Equilateral Triangle
2. Isosceles Triangle
3. Scalene Triangle
Based on the angles, there are also 3 types:
1. Acute triangle
2. Right triangle
3. Obtuse triangle
In this article, we’ll be discussing triangles that are classified based on sides, and implementing the same in the program.
The program also implies Triangle Inequality Theorem to validate whether the given sides can be a triangle or not.
Program:
#include<conio.h> #include<stdio.h> #include<math.h> void main () { int a, b, c, flag = -1; printf (" Enter the values of a, b and c : = "); scanf ("%d %d %d", &a, &b, &c); if ((a >= 0 && a <= 10) && (b >= 0 && b <= 10) && (c >= 0 && c <= 10)){ // Triangle Inequality Theorem : every side's length should be shorter than sum of other two side if (((a + b) > c) && ((b + c) > a) && ((c + a) > b)){ flag = 1; if ((a == b) && (b == c)) printf ("\n It is an Equilatral Triangle"); else if ((a == b) || (b == c) || (c == a)) printf ("\n It is an isosceles Triangle"); else printf ("\n It is a Scalene Triangle"); } } if (flag == -1) printf ("Given side inputs, can't be a triangle "); getch (); }
Not a Triangle
Enter the values of a, b and c : = 11 5 5 Given side inputs, can't be a triangle
Equilateral triangle
Enter the values of a, b and c : = 4 4 4 It is an Equilateral Triangle
Isosceles Triangle
Enter the values of a, b and c : = 2 3 2 It is an isosceles Triangle
]]>
We need to generate a frequency array for 100 numbers between 0-19. Further, we need to create, add data and print a corresponding histogram for same.
Program:
/****************************************************************************** Program to print histogram from given array in C *******************************************************************************/ #include<stdio.h> #include <stdlib.h> #include<time.h> int main () { int arr[100], i, barSize, p, count; // Variables that are being used for printing histogram int firstBar = 0, secBar = 0, thirdBar = 0, fourthBar = 0, lowerRange = 0, upperRange = 5; // srand for creating random values between 0 to 20 srand (time (0)); for (i = 0; i < 100; i++){ arr[i] = rand () % 20; } // printing autogenerated array elements on output screen printf ("Array values for the histogram :: \n"); for (i = 0; i < 100; i++){ printf ("%d ", arr[i]); } // printing histogram in C by spliting bar in intervals of 5 i.e 0-5, 5-10 for (i = 0; i < 100; i++){ if (arr[i] < 5){ firstBar = firstBar + 1; } else if ((arr[i] < 10) && (arr[i] > 5)){ secBar = secBar + 1; } else if ((arr[i] < 15) && (arr[i] > 10)){ thirdBar = thirdBar + 1; } else{ fourthBar = fourthBar + 1; } } // Printing histogram in C printf ("\n\n*************** HISTOGRAM ***************\n\n"); i = 1; barSize = firstBar; while (i <= 4){ printf ("\n |\n%2d-%2d|", lowerRange, upperRange); p = 1; while (p <= barSize){ printf ("*"); p++; } printf ("\n |\n"); if (i == 2) barSize = secBar; if (i == 3) barSize = thirdBar; if (i == 4) barSize = fourthBar; lowerRange = lowerRange + 5; upperRange = upperRange + 5; i++; } return 0; }
Output:
Random data inputs are generated first:-
Array values for the histogram :: 6 18 0 9 16 16 17 6 3 10 6 18 13 6 5 15 7 5 17 9 15 6 16 7 15 14 0 0 12 8 12 18 18 12 7 14 0 17 0 15 7 19 13 12 5 10 0 4 15 17 14 2 15 10 9 10 17 2 2 1 2 14 0 0 18 19 14 19 16 6 14 15 5 8 0 2 10 0 19 6 17 13 0 13 15 2 15 4 4 18 6 6 4 18 6 3 9 12 14 18
Plotted to Histogram:
*************** HISTOGRAM *************** | 0- 5|************************ | | 5-10|************************ | | 10-15|******************** | | 15-20|**************** |
Explanation:
hours = minute / 60
Program:
#include<stdio.h> int main() { int minute; printf("\n\n\tEnter minutes = "); scanf("%d",&minute); printf("\n\t Entered minutes = %d minutes \n\t Which is equivalent to = %d hours and %d minutes",minute,minute/60,minute%60); return 0; }
Output:
Explanation: