procedure bubbleSort(A : list of sortable items)
n := length(A)
repeat
swapped := false
for i := 1 to n-1 inclusive do
{ if this pair is out of order }
if A[i-1] > A[i] then
{ swap them and remember something changed }
swap(A[i-1], A[i])
swapped := true
end if
end for
until not swapped
end procedure
#include <stdio.h>
int main()
{
//!showSort(array)
int array[5], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
recursiveToThree(n)
print n + 1, "th"
if n < 3
r = recursiveToThree(n + 1)
n = r
return n
Function Main()
n = 0
n = recursiveToThree(0)
arr = [1, 2, 3]
ptr = address of arr[2]
set value at ptr to 5
d_arry = allocate memory for 3 integers
pd_arr[0] = allocate memory for 2 integers
pd_arr[1] = allocate memory for 2 integers
print "Hello, world!"
free memory at pd_arr[0]
open file "PLIVET.txt" for writing as fp
write "PLIVET" to fp
close fp
open file "PLIVET.txt" for reading as fp
buf = create buffer with size 7
while read up to 10 characters from fp into buf is not NULL
print buf
close fp
End
Function Main
Declare variables i, j as integers
Declare variable k as a pointer to integer
Read values for i and j from input
Print the values of i and j
If i is equal to 1 and j is equal to 2, then
Print the product of i and j
Else
Print the sum of i and j
Print the value pointed to by k (which is the value of j)
End
Function Main
Declare N, sum, i as integers
Declare iptr, tmp as integer pointers
Print "Enter value of N [1-10]: "
Read N from user
Allocate memory for iptr with size N * sizeof(int)
If iptr is NULL
Print "Unable to allocate memory space. Program terminated."
Return -1
Print "Enter N integer number(s)"
For i from 0 to N-1
Print "Enter #(i+1): "
Read input and store it in tmp
Add the value of tmp to sum
Print "Sum: ", sum
Free the memory allocated for iptr
End
#include <stdio.h>
int main(void) {
//!ShowMemory(start=1000)
// integer variables
int N = 0;
int sum = 0;
int i;
// integer pointer variables
int *iptr, *tmp;
// take user input
printf("Enter value of N [1-10]: ");
scanf("%d", &N);
// allocate memory
iptr = (int *) malloc (N * sizeof(int));
// check if memory allocated
if (iptr == NULL) {
printf("Unable to allocate memory space. Program terminated.\n");
return -1;
}
// take integers
printf("Enter %d integer number(s)\n", N);
for (i = 0, tmp = iptr; i < N; i++, tmp++) {
printf("Enter #%d: ", (i+1));
scanf("%d", tmp);
// compute the sum
sum += *tmp;
}
// display result
printf("Sum: %d\n", sum);
// free memory location
free(iptr);
return 0;
}
Enter value of N [1-10]: 5
Enter 5 integer number(s)
Enter #1: 10
Enter #2: 20
Enter #3: 30
Enter #4: 40
Enter #5: 50
Sum: 150
More Example:
//Example: 02
#include <stdlib.h>
int main() {
//! showMemory(start=1000)
int * p = malloc(4 * sizeof(int));
int * q = malloc(1 * sizeof(int));
free(q);
int * r = malloc(2 * sizeof(int));
return 0;
}
//Run this code in C Programming Manual Tracing
Function Main
Declare variables s1, s2, s3, s4, s5 as integers
Declare variable per as float
Declare variable grade as character
Print "Enter marks of five subjects"
Read s1, s2, s3, s4, s5
per = ((s1 + s2 + s3 + s4 + s5) * 100) / 500
Print per
Switch (per >= 90 and per < 100)
Case 1:
Print "Your grade is A"
Case 0:
Switch (per >= 80 and per < 90)
Case 1:
Print "Your grade is B"
Case 0:
Switch (per >= 70 and per < 80)
Case 1:
Print "Your grade is C"
Case 0:
Switch (per >= 60 and per < 70)
Case 1:
Print "Your grade is D"
Case 0:
Switch (per >= 50 and per < 60)
Case 1:
Print "Your grade is E"
Case 0:
Print "Your grade is F"
End