C Program To Find Area Of Circle

C Program To Find Area Of Circle in C Programming Language ?


C Program To Find Area Of Circle, c program to find area of rectangle, c program to find area of triangle, c program to find area of circle using functions, c program to find area of circle and circumference, c program to find area of circle using pointers, c program to find area and perimeter of circle and rectangle, write a program to find out area of circle, c program to find area of square.
C Program To Find Area Of Circle


Solution For C Program To Find Area Of Circle
#include
#include
void main()
{
float radius,area;
clrscr();  // Clear Screen

printf("Enter the radius of Circle : ");
scanf("%d",&radius);

area = 3.14 * radius * radius;

printf("Area of Circle : %f",area);
getch();
}
Output :
Enter the radius of Circle : 2.0
Area of Circle : 6.14


Tags: C Program To Find Area Of Circle, c program to find area of rectangle, c program to find area of triangle, c program to find area of circle using functions, c program to find area of circle and circumference, c program to find area of circle using pointers, c program to find area and perimeter of circle and rectangle, write a program to find out area of circle, c program to find area of square.

C Program To Find Area of Right Angled Triangle

How to write to C Program To Find Area of Right Angled Triangle in C Programming Language ?


C Program To Find Area of Right Angled Triangle, area of right angled isosceles triangle, area of right angled triangle worksheet, area of right angled triangle in terms of inradius and circumradius, area of right angled triangle formulas, area of right angled triangle using hypotenuse, area of right angled triangle with angles are given, surface area of a right angled triangle, area of a right angled triangle calculator.
C Program To Find Area of Right Angled Triangle


Solution For C Program To Find Area of Right Angled Triangle
#include
#include
void main()
{
int base,height;
float area;
clrscr();  // Clear Screen

printf("Enter the base of Right Angle Triangle : ");
scanf("%d",&base);

printf("Enter the height of Right Angle Triangle : ");
scanf("%d",&height);

area = 0.5 * radius * radius;

printf("Area of Right Angle Triangle : %f",area);
getch();
}
Output :
Enter the base of Right Angle Triangle   : 4
Enter the height of Right Angle Triangle : 8
Area of Right Angle Triangle : 16.0

Tags: C Program To Find Area of Right Angled Triangle, area of right angled isosceles triangle, area of right angled triangle worksheet, area of right angled triangle in terms of inradius and circumradius, area of right angled triangle formulas, area of right angled triangle using hypotenuse, area of right angled triangle with angles are given, surface area of a right angled triangle, area of a right angled triangle calculator.

C Program To Find Area of Square

How to write a C Program To Find Area of Square in C Programming Language ?


C Program To Find Area of Square, c program to find area of rectangle, c program to find area of triangle, c program to find area of circle, c program to find square root, c program to find square root of a number using newton-raphson method, c program to find square root of a given number, c program to find square root of quadratic equation, c program to find square root of a number using function.
C Program To Find Area of Square


Solution For C Program To Find Area of Square:

#include<stdio.h>
#include<math.h>

int main()
{
int side;
float area,r_4;

r_4 = sqrt(3) / 4 ;

printf("Enter the Length of Side : ");
scanf("%d",&side);

area = r_4 * side * side ;

printf("Area of Equilateral Triangle : %f",area);
return(0);
}
Output :
Enter the Length of Side : 5
Area of Equilateral Triangle : 10.82

Tags: C Program To Find Area of Square, c program to find area of rectangle, c program to find area of triangle, c program to find area of circle, c program to find square root, c program to find square root of a number using newton-raphson method, c program to find square root of a given number, c program to find square root of quadratic equation, c program to find square root of a number using function.

C Program to Find Area of Scalene Triangle

How to write a C Program to Find Area of Scalene Triangle in C Programming Language ?


C Program to Find Area of Scalene Triangle how to find area of scalene triangle without height area of a scalene triangle formula area of a scalene triangle with 3 sides area of a scalene triangle given 3 sides area of scalene triangle heron's formula area of scalene triangle worksheet area of scalene triangle calculator area of a isosceles triangle
C Program to Find Area of Scalene Triangle

Solution For C Program to Find Area of Scalene Triangle:
#include<stdio.h>

int main()
{
int s1,s2,angle;
float area;

printf("nEnter Side1 : ");
scanf("%d",&s1);

printf("nEnter Side2 : ");
scanf("%d",&s2);

printf("nEnter included angle : ");
scanf("%d",&angle);

area = (s1 * s2 * sin((M_PI/180)*angle))/2;

printf("nArea of Scalene Triangle : %f",area);
return(0);
}

Output :

Enter Side1 : 3
Enter Side2 : 4
Enter included angle : 30
Area of Scalene Triangle : 3.000000

Tags: C Program to Find Area of Scalene Triangle, how to find area of scalene triangle without height
area of a scalene triangle formula, area of a scalene triangle with 3 sides, area of a scalene triangle given 3 sides, area of scalene triangle heron's formula, area of scalene triangle worksheet, area of scalene triangle calculator, area of a isosceles triangle.

C Program to Find Area and Circumference of Circle

How to write a C Program to Find Area and Circumference of Circle in C Programming Language ?


C Program to Find Area and Circumference of Circle, c program to find area and circumference of circle using function, write a c program to find area and circumference of circle, write a c program to calculate area and circumference of a circle, c program to find area and perimeter of circle and rectangle, c program to find area and perimeter of circle using pointers, formula to find area of circle write a program to calculate area of circle in c, program to find the simple interest.
C Program to Find Area and Circumference of Circle

Solution For 
#include<stdio.h>

int main()
{
int rad;
float PI=3.14,area,ci;

printf("Enter radius of circle: ");
scanf("%d",&rad);

area = PI * rad * rad;
printf("Area of circle : %f ",area);

ci = 2 * PI * rad;
printf("Circumference : %f ",ci);

return(0);
}

Output :

Enter radius of a circle : 1
Area of circle : 3.14
Circumference  : 6.28

Tags: C Program to Find Area and Circumference of Circle, c program to find area and circumference of circle using function, write a c program to find area and circumference of circle, write a c program to calculate area and circumference of a circle, c program to find area and perimeter of circle and rectangle, c program to find area and perimeter of circle using pointers, formula to find area of circle, write a program to calculate area of circle in c, program to find the simple interest.

C Program to Convert String to Integer

How to write a C Program to Convert String to Integer in C Programming Language ?


C Program to Convert String to Integer, c program to convert string to integer without using library functions, c program to convert string to integer and integer to string, c program to convert string to integer without using atoi function, java program to convert string to integer array, c programming convert int to string, c program to convert string to uppercase, string to integer conversion in c, c strtol.
 C Program to Convert String to Integer


Solution For C Program to Convert String to Integer:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int num;

// Variable marks is of Char Type
char marks[3] = "98";

printf("Please Enter Marks : ");
scanf("%s",marks);

num = atoi(marks);

printf("nMarks : %d",num);
}

Output :
Please Enter Marks : 76
Marks : 76

Tags: C Program to Convert String to Integer, c program to convert string to integer without using library functions, c program to convert string to integer and integer to string, c program to convert string to integer without using atoi function, java program to convert string to integer array, c programming convert int to string, c program to convert string to uppercase, string to integer conversion in c, c strtol.

C Program To Find The Square and Cube of Number

How to write a C Program To Find The Square and Cube of Number in C Programming Language ?


C Program To Find The Square and Cube of Number, c program to find square root of a number, c program to find square root of a number without using sqrt function, write a program in c to find the square root of a number, c program to find square root of a number using newton-raphson method, c program to find square root of a number without using math, c program to find square root of a number using for loop, c program to find square root of a number without using library function, c program to find square root of a given number.
C Program To Find The Square and Cube of Number

Solution For C Program To Find The Square and Cube of Number:

#include
#include
void main()
{
                 int no,square,cube;
                clrscr();
                 printf("enter the number ");
                 scanf("%d",&no);
                 square=no*no;
                 cube=no*no*no;
                 printf("the square is=%d",square);
                 printf("the cube is=%d",cube);
                 getch();
}

Tags: C Program To Find The Square and Cube of Number, c program to find square root of a number, c program to find square root of a number without using sqrt function, write a program in c to find the square root of a number, c program to find square root of a number using newton-raphson method, c program to find square root of a number without using math, c program to find square root of a number using for loop, c program to find square root of a number without using library function, c program to find square root of a given number.

C Program For Addition Of Two Numbers

How to write a C Program For Addition Of Two Numbers in C Programming Language ?

C Program For Addition Of Two Numbers c program for addition of two numbers using command line arguments c program for addition of two numbers using functions c program for addition of two numbers using pointers c program for addition of two numbers without using operator c program for addition of two numbers using bitwise operators' c program for addition of two matrices c program for subtraction of two numbers write a c program to add two numbers
C Program For Addition Of Two Numbers


Solution for C Program For Addition Of Two Numbers:

void main()
{
int num1=10,num2=20;
clrscr();
printf("Addition is %d",num1+num2);
getch();
}

Tags: C Program For Addition Of Two Numbers, c program for addition of two numbers using command line arguments, c program for addition of two numbers using functions, c program for addition of two numbers using pointers, c program for addition of two numbers without using operator, c program for addition of two numbers using bitwise operators, c program for addition of two matrices, c program for subtraction of two numbers, write a c program to add two numbers.

C Program To Take Random Number

How to write a C Program to take a random number in c programming language ?


Prints a random number in the range 0 to 99.

C Program to take random number, c program to generate random numbers, c program to generate random numbers without using rand function,c program to generate random numbers between 0 and 10,c program to generate random numbers between 0 and 1, c program to generate random numbers between 0 and 100,c program to generate random numbers without repetition,c program to generate random numbers with normal distribution,generate random number in c between two numbers.
C Program To Take Random Number


Solution:
/*C Program to take random number*/
#include<stdio.h>  
#include<stdlib.h> 
#include<time.h>
/* prints a random number in the range 0 to 99 */
int main(void)
{randomize();
printf(“Random number in the 0-99 range: %d\n”, random (100)); 
return 0;}

Tags: C Program to take random number, c program to generate random numbers, c program to generate random numbers without using rand function,c program to generate random numbers between 0 and 10,c program to generate random numbers between 0 and 1, c program to generate random numbers between 0 and 100,c program to generate random numbers without repetition,c program to generate random numbers with normal distribution,generate random number in c between two numbers.

C Program For Checking Arithmetic Expression Using Recursion

How to write a C Program For Checking Arithmetic Expression Using Recursion in C Programming Language ?


Solution:
/*C Program For Checking Arithmetic Expression Using Recursion*/

#include<stdio.h>
#include <conio.h>
#include <ctype.h>
void valid();
main()
{
 //clrscr();
 printf("\nEnter the arithmetic expression: ");
 valid();
 getch();
 }
 void valid()
 {
  char a,b;
  a=getche();
  if (isalpha(a))
  {
   b=getche();
   if (b=='\r')
    printf("valid expression");
   else if (b=='+'||b=='-'||b=='*'||b=='/')
    printf("valid expression");
   else
    printf("invalid expression");
  }
        else printf("invalid");
        }


Tags: C program for Checking Arithmetic Expression using recursion, c program to check the validity of an expression using stack, check for balanced parentheses in an expression in c, program for parenthesis matching using stack in c++, balancing parentheses using stack, recursion in c
c programming, online c compiler.

C Program To Convert Decimal To Binary Using Recursion

How to write a C Program To Convert Decimal To Binary Using Recursion in C Programming Language ?


Solution:
/*C Program To Convert Decimal To Binary Using Recursion*/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void binary(int);
main()
{
 int dno;
 //clrscr();
 printf("\nEnter the decimal number:");
 scanf("%d",&dno);
 printf("\nBinary equivalent:" );
 binary(dno);
 getch();
 }
void binary(int n)
{
 int q,r;
 if (n==0)
  return;
 r=n%2;
 q=n/2;
 binary(q);
 printf("%d",r);
 }

Tags: C program to Convert decimal to binary using recursion, write a c program to convert decimal to binary using recursion, c program to print binary equivalent of a decimal number, c program to convert decimal to hexadecimal, decimal to binary conversion in c using recursion, c program to convert decimal to binary using array, c program to convert decimal to binary using stack, c program to convert decimal to binary using function, c program to convert decimal to binary without using array

C Program To Find Factorial, Triangular, Fibonacci Number

How To write a C Program To Find Factorial, Triangular, Fibonacci Number in C Programming Language ?


Solution:
/*C Program To Find Factorial, Triangular, Fibonacci Number*/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int fact(int);
int triangular(int);
int fibo (int);
void main()
{
 int n,ch,i;
 clrscr();
 printf("Menu\n");
 printf("\n1.Factorial /n2.Triangular Number\n3.Nth term Fibonacci number \n4.Exit");
 printf("\nenter your choice:");
 scanf("%d",&ch);
 switch(ch)
 {
  case 1: printf("\nEnter the Number:");
   scanf("%d",&n);
   printf("factrial of %d=%d",n,fact(n));
   break;
  case 2: printf("\nEnter the Number:");
   scanf("%d",&n);
   printf("triangular number of%d=%d",n,triangular(n));
   break;
  case 3:printf("\nenter the ith term:");
   scanf("%d",&i);
   printf("the %dth term of fibbonaci series is%d ",i,fibo(i));
   break;
  case 4:exit(4);
  break;
   default:printf("invalid choice");
 }
 getch();
 }
int fact (int x)
{
 int p;
 if (x==0)
  return(1);
 p=x*fact(x-1);
 return(p);
 }
int triangular(int x)
{
 int p;
 if (x==0||x==1)
  return(x);
 p=x+triangular(x-1);
 return(p);
 }
int fibo(int i)
{
 int p;
 if (i==0||i==1)
  return(1);
 p=fibo(i-1)+fibo(i-2);
 return(p);
 }

Tags: C program to find Factorial , Triangular , Fibonacci number, fibonacci series program in c
factorial of a number in c, factorial program in c using recursion, factorial of a number in java
factorial program in c using function, factorial program in c using while loop, write a program to find the factorial of a number in c++, write a program to find the distance between 2 points using functions.

C Program To Multiply Two Numbers Using Recursion

How to write a C Program To Multiply Two Numbers Using Recursion in C Programming Language ?


Solution:
/*C Program To Multiply Two Numbers Using Recursion*/

#include<stdio.h>
#include <conio.h>
#include <stdlib.h>
int mul(int,int);
void main()
{
 int pro,a,b;
 clrscr();
 printf("enter two no:\n");
 scanf("%d%d",&a,&b);
 printf("product=%d",mul(a,b));
 getch();
 }
int mul(int x,int y)
{
 int p;
 if (y==1)
  return (x);
 p=x+mul(x,y-1);
 return(p);
 }

Tags: C program to Multiply two numbers using recursion, c program to find gcd of two numbers using recursion, c program to find lcm of two numbers using recursion, c program for factorial of a number using recursion, c program to reverse a number using recursion, c program to multiply two numbers using bitwise operators, c program to multiply two numbers without using arithmetic operator, c program to multiply two numbers using addition, c program to multiply two numbers using function.

C Program To Check The Postfix Expression Using Recursion

How to write a C Program To Check The Postfix Expression Using Recursion in C Programming Language ?


#include<stdio.h>
#include <conio.h>
#include <ctype.h>
void valid();
 main()
{
 //clrscr();
 printf("\nEnter the postfix expression: ");
 valid();
 getch();
 }
 void valid()
 {
  char b;
  b=getche();
  if (isalpha(b))
  {
   b=getche();
   if (b=='\r')
     printf("invalid expression");
   else if (isalpha(b))
   {
    b=getche();
    if (b=='+'||b=='-'||b=='*'||b=='/')
     printf("valid expression");
   }
   else printf("invalid expression");
  }
 }
Tags: C program to Check the Postfix Expression using recursion, c program to convert infix to postfix using stack array, c program for infix to postfix conversion using stack with output, infix to postfix conversion in c using stack examples, c program to convert infix to prefix using stack, c program to convert infix to postfix using stack linked list, c program to evaluate postfix expression, write a c program to convert infix to postfix, program to convert infix to postfix in c++.

C Program To Check The Prefix Expression

How to write a C Program To Check The Prefix Expression in C Programming Language ?


Solution:

#include<stdio.h>
#include <conio.h>
#include <ctype.h>
void valid();
main()
{
 //clrscr();
 printf("\nEnter the prefix expression: ");
 valid();
 getch();
 }
 void valid()
 {
  char b;
  b=getche();
  if (b=='+'||b=='-'||b=='*'||b=='/')
  {
   b=getche();
   if (b=='\r')
       printf("\ninvalid exppression");
   else if (isalpha(b))
    {
      b=getche();
      if(isalpha(b))
      {
      printf("\nvalid expression");
      valid();
      }
      }
   else printf("\ninvalid");
  }
        else printf("\nwainvalid");
        }

Tags:  C Program to convert Prefix Expression into Postfix, PROGRAM TO CONVERT A PREFIX EXPRESSION, C program to convert- 1.Postfix to Infix 2.Postfix to Prefix, Program to convert expression in postfix form to prefix, C program to check the prefix expression, prefix expression evaluation in c, prefix expression evaluation using stack in c, infix to prefix expression
prefix expression tree, prefix expression evaluation example, prefix expression evaluation algorithm, prefix expression calculator, prefix expression in data structure.

program realizuje sortowanie bÄ…belkow

program realizuje sortowanie bÄ…belkow

/* program realizuje sortowanie bÄ…belkow */

#include <stdio.h> //zmiana z <studio.h>
//#include <bubbles.h> // niepotrzebna biblioteka
#define MAXTAB 99  //zla deklaracja  = 99

int wczytaj_liczbe(int min, int max)
{
int liczba,
wynik;
do
{
fflush(stdin); // czyszczenie niepoprawnego strumienia stdout
fprintf(stdout, "podaj liczbe calkowita z zakresu <%d-%d>?", min, max);
wynik = fscanf(stdin, "%d", &liczba); //zmana z referencji &wynik na liczba, zły strumień
} while (wynik != 1 || liczba > max || liczba < min); // blad logiczny
return liczba;
}
void zamien_jezeli_wieksza(int * tab, int pos_mn, int pos_wk)
{
int tmp;
if (tab[pos_mn] > tab[pos_wk])
{
tmp = tab[pos_mn];
tab[pos_mn] = tab[pos_wk];
tab[pos_wk] = tmp; //poprawiona zamiana zmiennych
}
//tmp = tab[pos_mn]; tab[pos_mn] = tab[pos_wk]; tab[pos_wk] = tab[pos_mn]; // wszystkie powinny być w klamrze
}
int main(int argc, char* argv[])
{
int tablica[MAXTAB];
int ile, i, j;
printf("\n\nJestem program ""sortujacy\n\nile ""liczb chcesz sortowac\n");
ile = wczytaj_liczbe(0, MAXTAB);
for (i = 1; i<=ile; i++) // poprawiony warunek
{
printf("Wczytuje liczbe nr. %d \n", i);
tablica[i-1] = wczytaj_liczbe(0, 999); // poprawiona indeksacja
}
/*for (i = 0; i < ile;i++)
{
printf("\nliczba nr. %d : %d", i + 1, tablica[i]);
}*/
for (i = 0; i < ile;i++)  //; niepotrzebny srednik
for (j = 0; j < (ile - 1); j++) //{ niepotrzebna klamra , oraz warunek petli
zamien_jezeli_wieksza(tablica, j, j+1); //zamiana z i + 1 na j + 1

printf("\nPosortowane:");
for (i = 0; i < ile; i++)
printf("\nliczba nr. %d : %d", i+1 , tablica[i]); //poprawiona indeksacja
return 0;
}

C Program To Print Numbers Upto 5 Using Do-While Loop

How to write a C Program to print numbers upto 5 using do-while loop in C Programming language.

Solution -

/*Progam to print numbers upto 5 using do-while loop*/
#include<stdio.h>
void main()
{
     int num=1;    //initialisation phase
     do
     {
        printf("\t%d",num);
        num=num+1;    //increment phase
     }
     while(num<5);    //condition phase
}

Tags: C Program to print numbers upto 5 using do-while loop, c program to print n natural numbers using while loop, c program to print first 10 natural numbers, sum of first 10 natural numbers in c using while loop, c program to print 1 to 10 numbers using while loop, program to find sum of n natural numbers in c, c program to print natural numbers from 1 to 10 in reverse order, write a program to print first 10 natural numbers in java, do while loop in c programming example.

C Program Test Script For Moonbuggy Motor Control

How to write a C Program Test script for moonbuggy motor control in C Programming Language.

Solution -

/*
 * Test script for moonbuggy motor control
 */

const int throttle = A0;
const int field_PWM_pin = 2;
const int field_phase = 38;
const int arm_PWM_L = 5;
const int arm_PWM_R = 4;
const int phase_switch = 40;
int PHASE = 1;
int sensorValue = 0;
int throttleValue = 0;
int field_PWM = 0;


void setup() {
  pinMode(phase_switch, INPUT); //phase (direction) input
  pinMode(field_phase, OUTPUT); //phase (direction) output
  pinMode(field_PWM, OUTPUT); //field PWM
  pinMode(arm_PWM_L, OUTPUT); //armature PWM left
  pinMode(arm_PWM_R, OUTPUT); //armature PWM right
  delay(1000); //start up delay to allow charge pumps on motor controllers to stabilize
}
void loop() {
  PHASE = digitalRead(phase_switch); //read the phase input switch HIGH for forward and LOW for reverse
  sensorValue = analogRead(throttle); //input of raw throttle value
  throttleValue = map(sensorValue, 180, 818, 0, 255); //map min/max of throttle to 8bit integer

  digitalWrite(field_phase, PHASE); //set PHASE output to field controller
  if (throttleValue < 13){ //introducing a 5% dead zone at the low end of throttle due to unstable analog input
    analogWrite(field_PWM_pin, fiel_PWM); //set field voltage
    analogWrite(arm_PWM_L, throttleValue); //apply armature voltage
    analogWrite(arm_PWM_R, throttleValue); //apply armature voltage
  } else {
    analogWrite(field_PWM_pin, 0); //set field voltage
    analogWrite(arm_PWM_L, 0); //apply armature voltage
    analogWrite(arm_PWM_R, 0); //apply armature voltage
  }

}

C Program Loads a File Into Memory Dynamically allocated on heap and Stores address thereof in *content and length thereof in *length.

How to write a C Program to Loads a file into memory dynamically allocated on heap and Stores address thereof in *content and length thereof in *length in C Programming Language.


Solution -

/**
 * Loads a file into memory dynamically allocated on heap.
 * Stores address thereof in *content and length thereof in *length.
 */
bool load(FILE* file, BYTE** content, size_t* length)
{
    int index = 0;
    BYTE* buffer = malloc(1);
    int status = fread(buffer+index, 1, 1, file);
 
    while(status){
        index++;
        printf("About to ralloc %i\n", index);
        buffer = realloc(buffer, index);
        printf("Yay!\n");
        if(buffer != NULL){
            status = fread(buffer+index, 1, 1, file);
        }
        else{
            *length = 0;
            *content = NULL;
            return false;
        }
    }
    *content = buffer;
    *length = index;
    return true;
}

C Program To Compare Numbers

How to write a C Program To Compare Numbers in C Programming Language ?


Solution -

int compareNumbers(int arr1[], int arr2[])
{
int i = 0, res = 0;
while(i < {ARRAYS_LENGTH} && !res)
{
res = arr1[i] - arr2[i];
i++;
}
return res;
}

Tags: Compare Number in C Program, c program string compare, c program to compare two strings
c program to compare two strings using strcmp, c program to compare two strings using pointers, c program to compare two strings without using built in function, c program to compare two strings without using library function, c program to compare two strings without using strcpy, c program to compare two files.

C Program Determines If Any Given Year Is A Leap Year (or aborts.)

How to write a C Program determines if any given year is a leap year (or aborts) in C Programming Language ?


Solution:

/*C Program determines if any given year is a leap year (or aborts.)*/
#include <stdio.h>
#include <stdlib.h>
   #include <assert.h>

   int determineyear (int year);

    int main (int argc, char ** argv) {
      int year = 0;
       printf("Please enter the year that you are interested in.\n");
       scanf("%d", &year);
    determineyear (year);
      return EXIT_SUCCESS;
    }
          int determineyear (int year) {
       assert(year >= 1582);
      if (year%400 == 0) {
         printf ("%d is a leap year!\n", year);
      } else if (year%100 == 0) {
             printf ("%d is not a leap year!\n", year);
      } else if (year%4 == 0) {
          printf ("%d is a leap year!\n", year);
      } else printf ("%d is not a leap year!\n", year);
         return EXIT_SUCCESS;
      }

Tags: C Program Determines If Any Given Year Is A Leap Year (or aborts.), write a program in c to find leap year, leap year program in java, how to check leap year, c program to find leap year using conditional operator, c program to find leap year using logical operator, c program to find leap year using function, leap year program in c++, leap year condition.

C Program MaxHeap and MinHeap

How to write a C Program MaxHeap and MinHeap in C Programming Language ?


Solution:

#include <stdio.h>
#include <stdlib.h>
typedef struct heap {
    int dim;
    int *a;
}heap;
void inizializzaArray (int *a) {
    a[0] = 0;
}
void inizializzaArrayStruct (heap *h) {
    h->dim = 0;
}
void stampArray (int *a) {
    int i, k = 1;
    for (i=1; i<=a[0]; i++) {
        printf("(%d)%d   ", i, a[i]);
        if (i == k) {
            printf("\n");
            k = (i*2)+1;
        }
    }
    printf("\n\n");
}
void stampArrayStruct (heap *h) {
    int i, k = 1;
    for (i=1; i<=h->dim; i++){
        printf("(%d)%d   ", i, h->a[i]);
        if (i == k) {
            printf("\n");
            k = (i*2)+1;
        }
    }
    printf("\n\n");
}
void maxHeapify (int *a) {
    int i;
    for (i = (a[0]+1)/2; i>=1; i--) {
        int c = -1, aux, max = -1, t = i;
        while(t != c){
            c = t;
            max = a[c];
            if (( 2*c < (a[0]+1) ) && (a[c] < a[2*c])){
                t = 2*c;
                max = a[t];
            }
            if (( 2*c+1 < (a[0]+1) ) && (a[2*c + 1] > max))
                t = 2*c + 1;
            if ( c != t ){
                aux = a[c];
                a[c] = a[t];
                a[t] = aux;
            }
        }
    }
    printf("Bilanciamento eseguito con successo\nL'array che mi hai passato ora è un maxHeap!!\n");
}
void maxHeapifyStruct (heap *h) {
    int i;
    for (i = (h->dim+1)/2; i>=1; i--) {
        int c = -1, aux, max = -1, t = i;
        while(t != c){
            c = t;
            max = h->a[c];
            if (( 2*c < (h->dim+1) ) && (h->a[c] < h->a[2*c])){
                t = 2*c;
                max = h->a[t];
            }
            if (( 2*c+1 < (h->dim+1) ) && (h->a[2*c + 1] > max))
                t = 2*c + 1;
            if ( c != t ){
                aux = h->a[c];
                h->a[c] = h->a[t];
                h->a[t] = aux;
            }
        }
    }
    printf("Bilanciamento eseguito con successo\nLa struct che mi hai passato ora è un maxHeap!!\n");
}
void minHeapify (int *a) {
    int i;
    for (i = (a[0]+1)/2; i>=1; i--) {
        int c = -1, aux, max = -1, t = i;
        while(t != c){
            c = t;
            max = a[c];
            if (( 2*c < (a[0]+1) ) && (a[c] > a[2*c])){
                t = 2*c;
                max = a[t];
            }
            if (( 2*c+1 < (a[0]+1) ) && (a[2*c + 1] < max))
                t = 2*c + 1;
            if ( c != t ){
                aux = a[c];
                a[c] = a[t];
                a[t] = aux;
            }
        }
    }
    printf("Bilanciamento eseguito con successo\nL'array che mi hai passato ora è un minHeap!!\n");
}
void minHeapifyStruct (heap *h) {
    int i;
    for (i = (h->dim+1)/2; i>=1; i--) {
        int c = -1, aux, max = -1, t = i;
        while(t != c){
            c = t;
            max = h->a[c];
            if (( 2*c < (h->dim+1) ) && (h->a[c] > h->a[2*c])){
                t = 2*c;
                max = h->a[t];
            }
            if (( 2*c+1 < (h->dim+1) ) && (h->a[2*c + 1] < max))
                t = 2*c + 1;
            if ( c != t ){
                aux = h->a[c];
                h->a[c] = h->a[t];
                h->a[t] = aux;
            }
        }
    }
    printf("Bilanciamento eseguito con successo\nLa struct che mi hai passato ora è un minHeap!!\n");
}
void insertKeyInMaxHeap (int k, int *a) {  
    if (a[0] == 0) {
        a = realloc (a, 2*sizeof(int));
        a[0]++;
        a[a[0]] = k;
        printf("L'array che mi hai passato è vuoto.\nInserisco la radice..\n");
    }    
    else {
        a = realloc (a, (a[0]+2)*sizeof(int));
        a[0]++;
        a[a[0]] = k;
        int temp, i = a[0];
        while (i>1) {
            if (a[i]>a[i/2]) {
                temp = a[i];
                a[i] = a[i/2];
                a[i/2] = temp;
            }
            i /= 2;
        }
    }    
    printf("Inserimento in maxHeap eseguito con successo!\n");
}
void insertKeyInMaxHeapStruct (int k, heap *h) {
    if (h->dim == 0) {
        h->a = malloc (2*sizeof(int));
        h->a[1] = k;
        h->dim++;
        printf("La struct che mi hai passato è vuota.\nInserisco la radice..\n");
    }
    else {
        h->a = realloc (h->a, (h->dim+2)*sizeof(int));
        h->dim++;
        h->a[h->dim] = k;
        int temp, i = h->dim;
        while (i>1) {
            if (h->a[i]>h->a[i/2]) {
                temp = h->a[i];
                h->a[i] = h->a[i/2];
                h->a[i/2] = temp;
            }
            i /= 2;
        }
    }
    printf("Inserimento in maxHeapStruct eseguito con successo!\n");
}
void insertKeyInMinHeap (int k, int *a) {  
    if (a[0] == 0) {
        a = realloc (a, 2*sizeof(int));
        a[0]++;
        a[a[0]] = k;
        printf("L'array che mi hai passato è vuoto.\nInserisco la radice..\n");
    }    
    else {
        a = realloc (a, (a[0]+2)*sizeof(int));
        a[0]++;
        a[a[0]] = k;
        int temp, i = a[0];
        while (i>1) {
            if (a[i]<a[i/2]) {
                temp = a[i];
                a[i] = a[i/2];
                a[i/2] = temp;
            }
            i /= 2;
        }
    }    
    printf("Inserimento in maxHeap eseguito con successo!\n");
}
void insertKeyInMinHeapStruct (int k, heap *h) {
    if (h->dim == 0) {
        h->a = malloc (2*sizeof(int));
        h->a[1] = k;
        h->dim++;
        printf("La struct che mi hai passato è vuota.\nInserisco la radice..\n");
    }
    else {
        h->a = realloc (h->a, (h->dim+2)*sizeof(int));
        h->dim++;
        h->a[h->dim] = k;
        int temp, i = h->dim;
        while (i>1) {
            if (h->a[i]<h->a[i/2]) {
                temp = h->a[i];
                h->a[i] = h->a[i/2];
                h->a[i/2] = temp;
            }
            i /= 2;
        }
    }
    printf("Inserimento in minHeapStruct eseguito con successo!\n");
}
int extractKeyFromMaxHeapAndGetPosition (int k, int *a) {
    if (a[0] != 0) {
        int i, temp;
        _Bool trovato = 0;
        for (i = 1; i<=a[0] && !trovato; i++)
            if (a[i] == k) {
                temp = a[i];
                a[i] = a[a[0]];
                a[a[0]] = temp;
                a[0]--;
                trovato = 1;
                maxHeapify(a);
            }
        if (trovato) {
            printf("L'elemento %d è stato estratto con successo dall'array!!\n", k);
            return i-1;
        }
        printf("L'elemento %d non appartiene all'array!!\n", k);
    }
    else {
        printf("L'array che mi hai passato è vuoto, non posso estrarre!!\n");
        return -1;
    }
}
int extractKeyFromMaxHeapStructAndGetPosition (int k, heap *h) {
    if (h->dim != 0) {
        int i, temp;
        _Bool trovato = 0;
        for (i = 1; i<=h->dim && !trovato; i++)
            if (h->a[i] == k) {
                temp = h->a[i];
                h->a[i] = h->a[h->dim];
                h->a[h->dim] = temp;
                h->dim--;
                trovato = 1;
                maxHeapifyStruct(h);
            }
        if (trovato) {
            printf("L'elemento %d è stato estratto con successo dalla struct!!\n", k);
            return i-1;
        }
        printf("L'elemento %d non appartiene alla struct!!\n", k);
    }
    else {
        printf("La struct che mi hai passato è vuota, non posso estrarre!!\n");
        return -1;
    }
}
int extractKeyFromMinHeapAndGetPosition (int k, int *a) {
    if (a[0] != 0) {
        int i, temp;
        _Bool trovato = 0;
        for (i = 1; i<=a[0] && !trovato; i++)
            if (a[i] == k) {
                temp = a[i];
                a[i] = a[a[0]];
                a[a[0]] = temp;
                a[0]--;
                trovato = 1;
                minHeapify(a);
            }
        if (trovato) {
            printf("L'elemento %d è stato estratto con successo dall'array!!\n", k);
            return i-1;
        }
        printf("L'elemento %d non appartiene all'array!!\n", k);
    }
    else {
        printf("L'array che mi hai passato è vuoto, non posso estrarre!!\n");
        return -1;
    }
}
int extractKeyFromMinHeapStructAndGetPosition (int k, heap *h) {
    if (h->dim != 0) {
        int i, temp;
        _Bool trovato = 0;
        for (i = 1; i<=h->dim && !trovato; i++)
            if (h->a[i] == k) {
                temp = h->a[i];
                h->a[i] = h->a[h->dim];
                h->a[h->dim] = temp;
                h->dim--;
                trovato = 1;
                minHeapifyStruct(h);
            }
        if (trovato) {
            printf("L'elemento %d è stato estratto con successo dalla struct!!\n", k);
            return i-1;
        }
        printf("L'elemento %d non appartiene alla struct!!\n", k);
    }
    else {
        printf("La struct che mi hai passato è vuota, non posso estrarre!!\n");
        return -1;
    }
}
int main(int argc, char** argv) {
 
    int *a = malloc(1*sizeof(int));
    inizializzaArray(a);
 
    insertKeyInMaxHeap(20, a);
    insertKeyInMaxHeap(11, a);
    insertKeyInMaxHeap(0, a);
    insertKeyInMaxHeap(320, a);
    insertKeyInMaxHeap(67, a);
    insertKeyInMaxHeap(18, a);
    insertKeyInMaxHeap(45, a);
 
    stampArray(a);
 
    int t = extractKeyFromMaxHeapAndGetPosition(45, a);
 
    stampArray(a);
 
    printf("La posizione di 45 era: %d\n", t);
 
 
 
    heap h;
    inizializzaArrayStruct(&h);
 
    insertKeyInMaxHeapStruct(20, &h);
    insertKeyInMaxHeapStruct(11, &h);
    insertKeyInMaxHeapStruct(0, &h);
    insertKeyInMaxHeapStruct(320, &h);
    insertKeyInMaxHeapStruct(67, &h);
    insertKeyInMaxHeapStruct(18, &h);
    insertKeyInMaxHeapStruct(45, &h);
 
    stampArrayStruct(&h);
 
    int s = extractKeyFromMaxHeapStructAndGetPosition(45, &h);
 
    stampArrayStruct(&h);
 
    printf("La posizione di 45 era: %d\n", s);
 
    return (EXIT_SUCCESS);
}

Tags: C Program MaxHeap and MinHeap, max heap and min heap example, max heap and min heap in data structure, convert max heap to min heap, max heap sort program in c, build max heap program in c, max heap program in c++, min heap program in c, min and max heap tree code for c++.