Czech version
logolink

< Back to the list of lessons

Basic Programming Constructions I

AlgortimyContent of the lesson:

  • Function JeVetsi to compare two inserted values
  • Procedure Setrid which sorts an array from the lowest value to the highest one
  • Function Maximum which returns the maximum value inside an array

Function JeVetsi to compare two inserted values

We want to create a procedure which will be able to compare two inserted numbers and will return 1 value of the variable AjeVetsiNezB (logical value of integer data type) in case that the first value is higher than the other one. Otherwise it will return 1.

You can get 3 possible results: a = b , a < b or a > b. You should compare the values using the operator IF (condition) and ELSE. The beginning and the end should be defined using the { and } commands. If the condition is not valid, the ELSE part is executed. You can see that there is no condition a > b. It is not defined because when none of the options a = b and a < b is valid, then the option a > b must be automatically valid.

Algorithm for comparing two values
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    int a, b;
    int AjeVetsiNezB;
    
    printf("Enter number a: ");
    scanf("%d", &a);
    
    printf("Enter number b: ");
    scanf("%d", &b);
    
    if (a == b) {
        AjeVetsiNezB = 0;
        printf("%d = %d => The value of A is equal to the value of B",a,b);
    }
    else
    {
        if (a < b) {
            AjeVetsiNezB = 0;
            printf("%d < %d => The value of A is lower than the value of B",a,b);
        }
        else
        {
            AjeVetsiNezB = 1;
            printf("%d < %d => The value of A is greater than the value of B",a,b);        
        }
    }
    
    printf("Press any key to end the program.");
    fflush(stdin);
    getchar();
    return 0;
}				

In case you want to specify what should happen in case that the condition is not valid, you can extend the IF command using the ELSE part which is executed when the condition is not valid.

Procedure Setrid which sorts an array from the lowest value to the highest one

If you want to sort an array, you have to create one at first. The constant n sets the number of items inside the array. If you want to adjust the number of items, you can easily change the value of n.

To fill the array you can use a simple FOR cycle which will fill the array using inserted values from user.

Initialization of the array
 const int n=10;
 int p[n];

  for (int i = 0; i < n; i++) {
    printf("Enter the %d value",i;
    scanf("%d", &p[i]);
   }

We will sort numbers by browsing the array from the left to the right end and swapping items in case that the previous one is greater than the following one. Using the same procedure we will browse the array from right to left end to speed up the algorithm. After the first pass we have the lowest value at the beginning of the array (left end) and the highest value at the end of the array (right end) so it is unnecessary to browse these values again (we declare a new variable named k - you have to set its value to 1 before the while cycle, otherwise the program would not function properly). We will browse the array until all items are sorted - until the value of variable serazene is 1.

In case that the following item is lower than the previous one, we swap these values. We save the first value inside the variable pom to be able to save the second value inside the first variable and then we save the value of variable pom inside the second variable.

BUBBLESORT
&  k=0;
  serazene=0;
  while (serazene==0) {
    serazene=1;
    for (int i = 0 + k; i < n - k; i++)
     {
      if (p[i]>p[i+1])
       {
        pom=p[i];
        p[i]=p[i+1];
        p[i+1]=pom;
        serazene=0;
       }
     }
     for (int i=(n-k-1); i > k+1; i--)
      {
       if (p[i] < p[i-1])
        {
         pom=p[i];
         p[i]=p[i-1];
         p[i-1]=pom;
         serazene=0;
        }
       }
    k=k+1;
   }

In every pass the value of k is increased by 1 - already sorted numbers will be ignored. At the beginning of the WHILE cycle you have to set the value of variable serazene to TRUE because you assume that the array is sorted. In case that items are not sorted and any of IFs is valid, the variable serazene is set back to FALSE and the WHILE cycle is repeated.

Algorithm for sorting numbers - BUBBLESORT
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
  const int n = 10;
  int p[n];
  int i, k, pom;
  int serazene;

  for (i = 0; i < n; i++) {
    printf("Enter the %d value',i;
    scanf("%d", &p[i]);
  }

  k=0;
  serazene=0;

  while (serazene==0) {
    serazene=1;
    for (int i = 0 + k; i < n - k; i++)
     {
      if (p[i]>p[i+1])
       {
        pom=p[i];
        p[i]=p[i+1];
        p[i+1]=pom;
        serazene=0;
       }
     }
     for (int i=(n-k-1); i > k+1; i--)
      {
       if (p[i] < p[i-1])
        {
         pom=p[i];
         p[i]=p[i-1];
         p[i-1]=pom;
         serazene=0;
        }
       }
    k=k+1;
   }

  for (i = 0; i < n; i++) {
    if (i==1) printf("Sorted values from the lowest one to the biggest one: %d ", p[i])
    else
     printf(", %d",p[i]);
  }

  printf("Press any key to end the program.");
  fflush(stdin);
  getchar();
  return 0;
}

Functions Maximum which returns the maximum value inside an array

You can find the maximum value while inserting values inside the array - the first inserted value should be inserted inside the variable max because it is empty at the beginning so there is nothing to compare the first inserted value to. We assume that the first value is the biggest one.

Then if you find a value which is higher than the value of max you should save it inside this variable.

Algorithm for computing maximum
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
 const int n=10;
 int p[n];
   for (int i = 0; i < n; i++)
    {
     printf("Enter the %d value: ", i);
     scanf("%d", &p[i]);
     if (i==0) max =p[0]
     else
      {
       if (p[i]>max) max=p[i];
      }
    }
   printf("\nThe maximum value is: %d\n",max);

  printf("Press any key to end the program.");
  fflush(stdin);
  getchar();
  return 0;
}
webdesign, xhtml, css, php - Mgr. Michal Mikláš