Langsung ke konten utama

How to Create Bubble Sort, Sequential Search, Binary Search, Mean Value Source Code (Pascal & C++ Programming)

Simple Program to read the number 1-4 only in C language

 

Here is the source code for this program.

#include<stdio.h>
#include<conio.h>
/*question: make a program to read a number from 1 to 4 then you print
the name of that number. ex: if you input 1 then it will print ONE.*/
void main(){
   char num1[]="'ONE'", num2[]="'TWO'", num3[]="'THREE'", num4[]="'FOUR'";
   int num;
   clrscr();
   do{
      printf(" Input a number[1-4 only] = ");scanf("%d",&num);}
      while (num > 4 || num < 1);
      if(num == 1){ printf(" %d is %s",num,num1);}
      else if(num == 2){ printf(" %d is %s",num,num2);}
      else if(num == 3){ printf(" %d is %s",num,num3);}
      else if(num == 4){ printf(" %d is %s",num,num4);}
   getch();
}
 

Simple printf() C++ Program

This is the source code for a simple program in C++ using printf().

#include<stdio.h>
#include<conio.h>
//this is just a simple program in c++
int main(){
int age;
char name[30];

printf("------------------------------------\n");
printf(" What is your name: ");gets(name);
printf(" How old are you : ");scanf("%d",&age);
printf("------------------------------------");
printf("\n Your name is %s.",name);
printf("\n You are %d years old.",age);
printf("\n------------------------------------");
printf("\n Nice to meet you!");
printf("\n------------------------------------");

return 0;
}

cpp1

 
 

Bubble Sort in C++

Bubble sort is a simple method of sorting. Here is the source code.

#include <stdio.h>
int main(){
int a[10];
int temp;

printf("-----------------------------------\n");
printf(" BUBBLE SORT PROGRAM\n");
printf("-----------------------------------\n");

for(int i=0;i<10;i++){
printf(" Value number-%d= ",i+1);scanf("%d",&a[i]);
}
for(int j=0;j<9;j++){
for(int k=j+1;k<10;k++){
if(a[j]>a[k]){
temp=a[k];
a[k]=a[j];
a[j]=temp;
}
}
}

printf("-----------------------------------\n");
printf(" The elements after sorting\n");
printf("-----------------------------------\n");
for(int i=0;i<10;i++){
printf(" %d|",a[i]);
}
printf("\n-----------------------------------");

return 0;
}

cpp4


 

Sequential Search Example in C++

Here is the source code of sequential search in a simple way.

#include <stdio.h>
int main()
{
int data [8]={10,900,-9,5,25,16,233,1};
int search;
int flag=0;
printf("----------------------------------\n");
printf(" The data you want to search= ");scanf("%d",&search);
for(int i=0;i<8;i++){
if(data[i]==search)
flag=1;
}
printf("----------------------------------\n");
if(flag==1) printf(" You have the data!\n");
else printf(" You don't have the data!\n");
printf("----------------------------------");
return 0;
}
cpp2
cpp22.png
 
 

Binary Search in C++ (Source Code)

This is the source code for binary search in C++.

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int data[9]={3,9,11,12,15,17,23,31,35};
  int l,r,m;
  int n=9;
  int search;
  printf("-------------------------\n");
  printf(" Search data= ");scanf("%d",&search);
  printf("-------------------------\n");
  l=0;
  r=n-1;
  int f=0;
  while(l<=r && f==0){
    m=(l+r)/2;
    printf(" mid data: %d\n",m);
    if(data[m]==search)f=1;
    else if(search<data[m]){
       printf(" Search on the left\n");
       r=m-1;
    }
    else{
       l=m+1;
       printf(" Search on the right\n");
    }
  }
  printf("-------------------------\n");
  if(f==1)printf(" You have the data!\n");
  else printf(" You don't have the data!\n");
  printf("-------------------------\n");
  return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(){
int data[9]={3,9,11,12,15,17,23,31,35};
int l,r,m;
int n=9;
int search;
printf("-------------------------\n");
printf(" Search data= ");scanf("%d",&search);
printf("-------------------------\n");
l=0;
r=n-1;
int f=0;
while(l<=r && f==0){
m=(l+r)/2;
printf(" mid data: %d\n",m);
if(data[m]==search)f=1;
else if(search<data[m]){
printf(" Search on the left\n");
r=m-1;
}
else{
l=m+1;
printf(" Search on the right\n");
}
}
printf("-------------------------\n");
if(f==1)printf(" You have the data!\n");
else printf(" You don't have the data!\n");
printf("-------------------------\n");
return 0;
}

cpp33.pngcpp3.png

 
 

Simple Mean Value of 5 Numbers using Array in Pascal

Now we use array to search the mean value of 5 numbers.

program array3;
uses crt;
type
nilai=array [1..5] of real;
var
l:byte; mean:real;
n:nilai;
begin
clrscr;
writeln('===========================');
for l:= 1 to 5 do
begin
write(' Value number-',l,': '); readln(n[l]);
end;
writeln('===========================');
mean:=(n[1]+n[2]+n[3]+n[4]+n[5])/5;
writeln(' Mean Value : ',mean:5:3);

readln;
end.

pascal9