hamming code

If you wish to download:

Hamming Code.exe
Hamming Code.c – Windows Version


 
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define SIZE 700

//This is the word encoder
void wordEncoder(int uncoded[][8],int ROWS,int COLS){
     
     int i, j, k, s, t;
     int J[8][8];
     int I[8][8];
     int c[SIZE][COLS];
     int W[SIZE][2*COLS];
     i = 0;
     j = 1;
     k = 1;
     s = 5;
     J[0][0] = 0;
    
//This generates a J matrix 
       while( i < COLS ){
        k = 1;
         if(i < 4){
           while( j < COLS ){
             J[i][j] = k;
               if(s == j){
                  k = 0;
               }else if(s > j){
                  k = 1;
               }
             j++;
           }
           j = 0; 
          s--;
         }else{
           J[i][6] = 1;
           while(j < 6){
            J[i][j] = k;
               if(s < j){
                  k = 1;
               }else if(s > j){
                  k = 0;
               }
             j++;
           }
           j = 0;
          s++;
         }
         i++;
        } 
       i = j = 0;
       k = 0;
//End of generation of J matrix
//This generates an I matrix       
       while(i < COLS){
         while(j < COLS){
          if(j != k){
            I[i][j] = 0;
          }else{
            I[i][j] = 1;
          }
          j++;
         }
        k++;       
        j = 0;
        i++;
       }
//End of generation of I matrix     
     
    printf("\nThe generated A matrix [I|J] is : \n\n"); 
     for ( i=0; i < COLS; i++){
       printf("[");
        for (j=0; j < COLS; j++){
          printf("%d", J[i][j]);
        }
        printf(" ");
        for (j=0; j < COLS; j++){
          printf("%d", I[i][j]);
        }
       printf("]\n");
      }
  
//The encoding process AC = w
  printf("\n\nThe encoded text (A*C[i]) is: ");
   k = i = j = s = t = 0;
  while(i < ROWS){
    t++;
    printf("\n\nInitial value for C[%d]: ", t);
    
       while(k < COLS){
          printf("%d", uncoded[i][k]);
         k++;
       }
    printf("\nThe new number is w[%d]: ", t);
//multiplies each row of uncoded (C), by each column of A
    while(s < 14){         
      if(s < 7){
           while(j < COLS){
             W[i][s] += (uncoded[i][j] * I[j][s]);
             j++;
           }  
      }else{
           while(j < COLS){
             W[i][s] += (uncoded[i][j] * J[j][s-7]);
             j++;
           }
      }
       W[i][s] = (W[i][s] % 2);
       printf("%d", W[i][s]);
       s++;
       j = 0;
    }
    
   k = 0;
   s = 0;
   i++;
  } 
//End of encoding  
return;
}

//This is the word decoder
void decoder(){ 
  int i, j, k, s, t;
  int COLS = 7;
  int ROWS;
  int J[8][8];
  int I[8][8];
  int c[SIZE][2*COLS];
  int W[SIZE][2*COLS];
  i = 0;
  j = 0;
  k = 1;
  s = 5;
  t = 0;
  int ch;
  char ascii;
  
  printf("\n\n");
  printf("Please type 1 if you would like to use the generated J, or 2 to type your own:\n");
  scanf("%d", &k);
    if(k == 1){
//This generates the J matrix from above
       while( i < COLS ){
        k = 1;
         if(i < 4){
           while( j < COLS ){
             J[i][j] = k;
               if(s == j){
                  k = 0;
               }else if(s > j){
                  k = 1;
               }
             j++;
           }
           j = 0; 
          s--;
         }else{
           J[i][6] = 1;
           while(j < 6){
            J[i][j] = k;
               if(s < j){
                  k = 1;
               }else if(s > j){
                  k = 0;
               }
             j++;
           }
           j = 0;
          s++;
         }
         i++;
       } 
//This is the end of the generation of the J matrix
    }else if(k == 2){ 
     printf("\nPlease enter your J matrix (must be a 7x7 matrix)\n"); 
     while(i < 7){
       printf("\nJ[%d]: ", i);
         while((ch = getchar()) != '\n'){
             if(49 == ch){
                ch = 1;
             }else if(48 == ch){
                ch = 0;
             }
           J[i][j] = ch;
           j++;
         }
       i++;
       j = 0;
     }
    }else{
     printf("\nUnkown option, failure!\n");
     return;
   }
//This generates and I matrix
  printf("\nGenerating your I matrix for you!\n");
   i = j = k = 0;
      while(i < COLS){
        while(j < COLS){
          if(j != k){
            I[i][j] = 0;
          }else{
            I[i][j] = 1;
          }
         j++;
        }
       i++;
       j = 0;
       k++;
      }
//End of I matrix generation

//Prints the A* matrix 
   printf("\nThe generated A* matrix [J/I] is : \n\n"); 
     for ( i=0; i < COLS; i++){
       printf("[");
        for (j=0; j < COLS; j++){
          printf("%d", J[i][j]);
        }
       printf("]\n");
     }
        printf("\n");
     for ( i=0; i < COLS; i++){
       printf("[");   
        for (j=0; j < COLS; j++){
          printf("%d", I[i][j]);
        }
       printf("]\n");
      }
   
      
   printf("\n\nPlease enter how many characters you have in your sentance?\n");
   printf("Please include puncuation and spaces: ");
   scanf("%d", &ROWS);
   i = 0;
   t = 1;
   j = 0;    
   k = getchar(); //Because I used scanf I have to return processor use to getchar();
//Collects each character in binary  
    while(i < ROWS){

      printf("\nPlease type value for W[%d]: ", t); 
           while((ch = getchar()) != '\n'){
             if(49 == ch){
                ch = 1;
             }else if(48 == ch){
                ch = 0;
             }
             W[i][j] = ch;
            j++;
           }
         t++;
         i++;
         j = 0;
        }
//Decodes each letter          
   printf("\n\nThe Decoded Text is: ");         
   k = i = j = s = t = 0;
   
   while(i < ROWS){
    t++;
    k = 0;
    printf("\n\nInitial value for W[%d]: ", t);
    
       while(k <= 2*COLS){
          printf("%d", W[i][k]);
         k++;
       }
    printf("\nThe new number is c[%d]: ", t);
    j = 0;
    while(j < 7){
        printf("%d", W[i][j]);
        ++j;
    }
    /*multiplies each row of encoded (W), by each column of A*
    I am removing this because it's a computer and doesn't need error checking...
    I don't have time to make it purposly add errors.
    while(s < 7){ 
        while(j < 2*COLS){
          if(j < 7){
           c[i][s] += (W[i][j] * J[s][j]);
           j++;
          }else{
           c[i][s] += (W[i][j] * I[s][j]);
           j++;
          }
        }
     c[i][s] = (c[i][s] % 2);
     printf("%d", c[i][s]);
     s++;
     j = 0;
    }
    */
   i++;
  }
  
  i = 0;
  ch = 0;
  
  printf("\n\nYoure sentence was: ");
//Converts binary to letter!
  while(i < ROWS){
    
    if(W[i][0] == 1){
       ch += 64;
    }if(W[i][1] == 1){
       ch += 32; 
    }if(W[i][2] == 1){
       ch += 16;
    }if(W[i][3] == 1){
       ch += 8;
    }if(W[i][4] == 1){
       ch += 4;
    }if(W[i][5] == 1){
       ch += 2;
    }if(W[i][6] == 1){
       ch += 1;
    }
    
    ascii = ch;

    printf("%c", ascii);
    ch = 0;
    i++;
  }
//End of conversion!  
  
return;
}
      


// This is the word collector
void wordCollector(){
     
  char word[SIZE];
  char ch;
  int i = 0;
  int str_len;
  int k,num;
  int j = 7;
  int binary[SIZE][8];
  
  k = getchar();
  printf("\n***After you type your sentence, type enter**\n");
  printf("Please type the word you wish to encode: ");
  while((ch = getchar()) != '\n'){
      j = 6;
      word[i] = ch;
      
    for(k = 0; k < 8; k++){
        binary[i][k] = 0;
    }
    
    for(k = 0; k < 1; k++){
        num = ch;//num = *(p+i);
        printf("\n\nANSI value for '%c' is %d", ch, num);
        while(num != 0){
            binary[i][j--] = num % 2;
            num /= 2;
        }
    }
    
    printf("\nConverted to Binary: ");
    for(k = 0; k < 7; k++){
        printf("%d", binary[i][k]);
    }
   i++; 
  } 
  
  word[i] = '\0';
  i = 0;
  printf("\n\nYour sentence was: \n");
       while (word[i] != '\0'){
            putchar(word[i]);
            printf(" [");
              for(k = 0; k < 7; k++){
                  printf("%d", binary[i][k]);
              }
        printf("]\n");
       i++;       
       }
       
  wordEncoder(binary, i, 7);
  
return;
}

//This is the main function
int main(void){
 
 int option = 1;
 while(option != 51){
    printf("\n\nAre you encoding(type 1), decoding(type 2) or do you wish to exit(type 3)?: ");
    option = getchar();
  if(option == 49){ 
    wordCollector();
  }else if(option == 50){
    decoder();
  }else if(option == 51){
  }else{
    printf("\nInvalid Choice, Choose Another!");
  }
 }
 printf("\n\n");    
       
 system("pause");
 return 0;
}

EEA and MEA in C

The following are programs for the Extended Euclidean Algorithm and Modular Exponentiation Algorithm.

EEA.c
EEA.exe

MEA.c
MEA.exe

RSA in C

RSA Program in C language!

The following program can encipher and decipher via the RSA method (using a public and private key). This program is basic and only allows for 27 possible characters (sorry!), but it gives the general idea of how the RSA method of encryption works.

RSA Coder.exe
RSAProgram.PDF
RSA Coder.c (windows)
RSAcoder.c (linux)

The listed goals:

Seller does the following:

  1. Pick 2 prime numbers p and q.
  2. Find n = pq
  3. Find b = (p – 1)(q – 1)
  4. Pick an exponent E such that 0 < E < n and gcd(E, b) = 1. (Use the EEA.)
  5. Calculate D, the smallest positive solution x to the congruence . (Use the EEA.)
  6. Share n and E with buyers.
Buyer does the following:

  1. Type up a plaintext message.
  2. Change each letter into a 2-digit number using a table.
  3. Line these numbers up to make a long string.
  4. Break up the sting into groups of three numbers each a Pi.
  5. Encode each of the three-digit numbers into a Ci. (Use the MEA.)
  6. Send the string of Ci to the seller.
Seller does the following:

  1. Change the Ci to Pi.  (Use the MEA.)
  2. Line these three digit numbers into a string.
  3. Break the string into groups of 2 digits.
  4. Look up the letters corresponding to each 2-digit number.
Read the plain text.

#include
#include
#include
#define SIZE 50
#define BIG 10000

//The Menu
int Menu(){

  int choice = 0;
     printf("\n\n____________________________________________________________________");
     printf("\n\nPlease type:\n\n");
     printf("  1. For seller looking to create a public and secret key.\n");
     printf("  2. For buyer looking to cipher their data.\n");
     printf("  3. For seller looking to decipher an encoded message.\n");
     printf("  4. To Exit\n");
     printf("\nChoice: ");
     scanf("%d", &choice);

     if(choice == 1){
        printf("\nHello Seller!\n");
        printf("\nType 1 to create a key, type 3 if you already have one: ");
        scanf("%d", &choice);
     }
  return choice;
}

// To find GCD
int GCD(int num1, int num2)
{
    int temp;
    while(num2!=0)
    {
        temp = num2;
        num2 = num1 % num2;
        num1 = temp;
    }
    return num1;
}

// The Extended Euclidean Algorithim
int EEA(int *m, int *n){

int i = 1;
int L[5][500];
int D;

L[0][0] = -1; // i
L[1][1] =  0; // Q
L[2][0] = *m; // ri
L[2][1] = *n; // ri
L[3][0] =  1; // x
L[3][1] =  0; // x
L[4][0] =  0; // y
L[4][1] =  1; // y

    while(L[2][i] > 0){
        ++i;
        L[0][i-2] = L[0][i-2] + 1;

         L[2][i] = L[2][i-2] % L[2][i-1];
         L[1][i] = L[2][i-2] / (L[2][i-1] - L[1][i]);

        L[3][i] = L[3][i-2] - (L[1][i] * L[3][i-1]);
        L[4][i] = L[4][i-2] - (L[1][i] * L[4][i-1]);
    }
      if(L[3][i-1] > 0){
         D = L[3][i-1];
      }else if(L[3][i-1] < 0){
         D = (L[3][i-1] + *n);
      }

 return D;
}

// The Modular Exponentiation Algorithm
int MEA(int p, int e, int n){

int r2 = 1;
int r1 = 0;
int Q = 0;
int R = 0;

  while( e != 0 ){
     R = (e % 2);
     Q = ((e - R) / 2);

     r1 = ((p * p) % n);

       if(R == 1){
          r2 = ((r2 * p) % n);
       }
     p = r1;
     e = Q;
  }
return r2;
}

//word collector
void wordCollector(){

  char word[SIZE];
  char ch;
  int i = 0;

  ch = getchar();
  printf("\n*After you type your word type enter\n");
  printf("Please type the word you wish to encode: ");
  while((ch = getchar()) != '\n'){
      word[i++] = ch;
  }
  word[i] = '\0';
  i = 0;
       while (word[i] != '\0'){
            putchar(word[i++]);
       }
  printf("\n\n");

  wordEncoder(word, i);

return;
}

//word Encoder
wordEncoder(char word[], int j){
 int i = 0;
 int k = -2;
 int t = 0;
 int P, n, E;
 int cipher[j];
 int plain[(j*2) + 2];
 int triple[(j*2) + 2];

     printf("\nIn oder to encipher the word we require an n and E.");
     printf("\nPlease enter the sellers n value: ");
     scanf("%d", &n);
     printf("\nPlease enter the sellers E value: ");
     scanf("%d", &E);
       i = 0;
         while( i < j ){
         k += 2;
           switch( word[i] ){
               case ' ':
                    P = 0;
                    plain[k] = 0;
                    plain[k + 1] = 0;
                   break;
               case 'A':
               case 'a':
                    P = 1;
                    plain[k] = 0;
                    plain[k + 1] = 1;
                  break;
               case 'B':
               case 'b':
                    P = 02;
                    plain[k] = 0;
                    plain[k + 1] = 2;
                  break;
               case 'C':
               case 'c':
                    P = 03;
                    plain[k] = 0;
                    plain[k + 1] = 3;
                  break;
               case 'D':
               case 'd':
                    P = 04;
                    plain[k] = 0;
                    plain[k + 1] = 4;
                  break;
               case 'E':
               case 'e':
                    P = 05;
                    plain[k] = 0;
                    plain[k + 1] = 5;
                  break;
               case 'F':
               case 'f':
                    P = 06;
                    plain[k] = 0;
                    plain[k + 1] = 6;
                  break;
               case 'G':
               case 'g':
                    P = 07;
                    plain[k] = 0;
                    plain[k + 1] = 7;
                  break;
               case 'H':
               case 'h':
                    P = 8;
                    plain[k] = 0;
                    plain[k + 1] = 8;
                  break;
               case 'I':
               case 'i':
                    P = 9;
                    plain[k] = 0;
                    plain[k + 1] = 9;
                  break;
               case 'J':
               case 'j':
                    P = 10;
                    plain[k] = 1;
                    plain[k + 1] = 0;
                  break;
               case 'K':
               case 'k':
                    P = 11;
                    plain[k] = 1;
                    plain[k + 1] = 1;
                  break;
               case 'L':
               case 'l':
                    P = 12;
                    plain[k] = 1;
                    plain[k + 1] = 2;
                   break;
               case 'M':
               case 'm':
                    P = 13;
                    plain[k] = 1;
                    plain[k + 1] = 3;
                   break;
               case 'N':
               case 'n':
                    P = 14;
                    plain[k] = 1;
                    plain[k + 1] = 4;
                   break;
               case 'O':
               case 'o':
                    P = 15;
                    plain[k] = 1;
                    plain[k + 1] = 5;
                   break;
               case 'P':
               case 'p':
                    P = 16;
                    plain[k] = 1;
                    plain[k + 1] = 6;
                   break;
               case 'Q':
               case 'q':
                    P = 17;
                    plain[k] = 1;
                    plain[k + 1] = 7;
                   break;
               case 'R':
               case 'r':
                    P = 18;
                    plain[k] = 1;
                    plain[k + 1] = 8;
                   break;
               case 'S':
               case 's':
                    P = 19;
                    plain[k] = 1;
                    plain[k + 1] = 9;
                   break;
               case 'T':
               case 't':
                    P = 20;
                    plain[k] = 2;
                    plain[k + 1] = 0;
                   break;
               case 'U':
               case 'u':
                    P = 21;
                    plain[k] = 2;
                    plain[k + 1] = 1;
                   break;
               case 'V':
               case 'v':
                    P = 22;
                    plain[k] = 2;
                    plain[k + 1] = 2;
                   break;
               case 'W':
               case 'w':
                    P = 23;
                    plain[k] = 2;
                    plain[k + 1] = 3;
                   break;
               case 'X':
               case 'x':
                    P = 24;
                    plain[k] = 2;
                    plain[k + 1] = 4;
                   break;
               case 'Y':
               case 'y':
                    P = 25;
                    plain[k] = 2;
                    plain[k + 1] = 5;
                   break;
               case 'Z':
               case 'z':
                    P = 26;
                    plain[k] = 2;
                    plain[k + 1] = 6;
                   break;
           }
          i += 1;
          printf("\nP%-2d is:%3d", i, P);
         }
         k += 2;
 // This part ensures that there are groups of 3
     if(k % 3 == 2){
          plain[k] = 0;
     }else if(k % 3 == 1){
          plain[k] = 0;
          plain[k + 1] = 0;
     }
       k = 0;
  // Prints the data in groups of three.
     printf("\n\nYour string of numbers are: ");
     while (k < (2*j) ){
        printf("%d%d%d ", plain[k], plain[k+1], plain[k+2]);
        k += 3;
     }
     printf("\n");
     k = 0;
     i = 1;
 //This part ensures that the loop for ciphering stops appropriately.
        t = (j / 3);
 //ciphers the groups of three.
     while (i < j + 1 - t){       
       printf("\nYour ciphered text C%-2d is: ", i);
       triple[i - 1] = (plain[k]*100) + (plain[k + 1] * 10) + (plain[k + 2]);         
         k += 3;       
         P = triple[i - 1];       
         cipher[i - 1] = MEA(P, E, n);         
         printf("%3d", cipher[i - 1]);         
        i++;       
     }       
  printf("\n\n");   
return;  
}  

//Decipher  
void Decipher(){     
int i, k, D, n, t;     
int j = 1;              
  
      printf("\nHow many ciphered number packets do you have? ");          
      scanf("%d", &i);           

int cipher[i];     
int triple[i];     
int plain[i * 3];               

      printf("\nPlease enter the D value: ");         
      scanf("%d", &D);               
      printf("\nPlease enter the n value: ");          
      scanf("%d", &n);          
      printf("\n");                
       while(i > j - 1){
          printf("Please enter the C%d digit: ", j);
          scanf("%d", &cipher[j - 1]);
          triple[j - 1] = MEA( cipher[j - 1], D, n );
          j++;
       }

     j = 1;
     printf("\nYour string is: ");

       while(i > j - 1){
          printf("%d ", triple[j - 1]);
          j++;
       }
       printf("\n\n");

   j = 0;
   k = 0;
   t = 1;
   int s = 0;
   int q = 0;
   int plain2[i*2];

     while(i > j){

         while((3 * i) > k){
              plain[k + 2] = triple[j] % 10;
              triple[j]    = triple[j] / 10;
              plain[k + 1] = triple[j] % 10;
              triple[j]    = triple[j] / 10;
              plain[k]     = triple[j] % 10;
              triple[j]    = triple[j] / 10;
         j++;
            if(s == 0){

              printf("\nP%d is %d%d", t, plain[k], plain[k + 1]);
              plain2[q] = (plain[k]*10) + plain[k + 1];
              t++;

              printf("\nP%d is %d", t, plain[k + 2]);
              plain2[q + 1] = plain[k+2] * 10;
              t++;
              s = 1;

            }else if(s == 1){

              printf("%d", plain[k]);
              plain2[q + 1] += plain[k];

              printf("\nP%d is %d%d", t, plain[k + 1], plain[k + 2]);
              plain2[q + 2] = (plain[k + 1] * 10) + plain[k + 2];
              t++;
              s = 0;
              q += 3;
            }

         k += 3;
        }
     }
   j = 0;
   char word[i];
   char c = ' ';
    printf("\n");
     while( q > j){
           switch( plain2[j] ){
               case 0:
                    word[j] = ' ';
                   break;
               case 1:
                    if(j < 1){
                       word[j] = 'A';
                    }else{
                       word[j] = 'a';
                    }
                  break;
               case 2:
                    if(j < 1){
                       word[j] = 'B';
                    }else{
                       word[j] = 'b';
                    }
                  break;
               case 3:
                    if(j < 1){
                       word[j] = 'C';
                    }else{
                       word[j] = 'c';
                    }
                  break;
               case 4:
                    if(j < 1){
                       word[j] = 'D';
                    }else{
                       word[j] = 'd';
                    }
                  break;
               case 5:
                    if(j < 1){
                       word[j] = 'E';
                    }else{
                       word[j] = 'e';
                    }
                  break;
               case 6:
                    if(j < 1){
                       word[j] = 'F';
                    }else{
                       word[j] = 'f';
                    }
                  break;
               case 7:
                    if(j < 1){
                       word[j] = 'G';
                    }else{
                       word[j] = 'g';
                    }
                  break;
               case 8:
                    if(j < 1){
                       word[j] = 'H';
                    }else{
                       word[j] = 'h';
                    }
                  break;
               case 9:
                    if(j < 1){
                       word[j] = 'I';
                    }else{
                       word[j] = 'i';
                    }
                  break;
               case 10:

                    if(j < 1){
                       word[j] = 'J';
                    }else{
                       word[j] = 'j';
                    }
                  break;
               case 11:
                    if(j < 1){
                       word[j] = 'K';
                    }else{
                       word[j] = 'k';
                    }
                  break;
               case 12:
                    if(j < 1){
                       word[j] = 'L';
                    }else{
                       word[j] = 'l';
                    }
                   break;
               case 13:
                    if(j < 1){
                       word[j] = 'M';
                    }else{
                       word[j] = 'm';
                    }
                   break;
               case 14:
                    if(j < 1){
                       word[j] = 'N';
                    }else{
                       word[j] = 'n';
                    }
                   break;
               case 15:
                    if(j < 1){
                       word[j] = 'O';
                    }else{
                       word[j] = 'o';
                    }
                   break;
               case 16:
                    if(j < 1){
                       word[j] = 'P';
                    }else{
                       word[j] = 'p';
                    }
                   break;
               case 17:
                    if(j < 1){
                       word[j] = 'Q';
                    }else{
                       word[j] = 'q';
                    }
                   break;
               case 18:
                    if(j < 1){
                       word[j] = 'R';
                    }else{
                       word[j] = 'r';
                    }
                   break;
               case 19:
                    if(j < 1){
                       word[j] = 'S';
                    }else{
                       word[j] = 's';
                    }
                   break;
               case 20:
                    if(j < 1){
                       word[j] = 'T';
                    }else{
                       word[j] = 't';
                    }
                   break;
               case 21:
                    if(j < 1){
                       word[j] = 'U';
                    }else{
                       word[j] = 'u';
                    }
                   break;
               case 22:
                    if(j < 1){
                       word[j] = 'V';
                    }else{
                       word[j] = 'v';
                    }
                   break;
               case 23:
                    if(j < 1){
                       word[j] = 'W';
                    }else{
                       word[j] = 'w';
                    }
                   break;
               case 24:
                    if(j < 1){
                       word[j] = 'X';
                    }else{
                       word[j] = 'x';
                    }
                   break;
               case 25:
                    if(j < 1){
                       word[j] = 'Y';
                    }else{
                       word[j] = 'y';
                    }
                   break;
               case 26:
                    if(j < 1){                                                
                       word[j] = 'Z';                                          
                    }else{                                                
                       word[j] = 'z';
                    }                                        
                   break;                         
            }                  
        j++;             
       }                
  j = 0;   
                
     printf("\n\nYour deciphered word is: "); 
        
   while(q > j){
         printf("%c", word[j]);
         j++;
   }
    printf("\n\n");

  return;
}

int main(void){

int prime1;
int prime2;
int choice, n, b, D, j, E;

 printf("\nWelcome to the RSA Enciphering - Deciphering Program!");

   choice = Menu();
 while(choice != 4){
  if(choice == 1){
       printf("\nPlease enter prime number 1: ");
       scanf("%d", &prime1);
       printf("\nPlease enter prime number 2: ");
       scanf("%d", &prime2);
       n = prime1 * prime2;
       printf("\nn is equal to %d\n", n);
       b = (prime1 - 1)*(prime2 - 1);
       printf("\nb is equal to %d\n", b);
       printf("\nPlease pick an E: ");
       scanf("%d", &E);
       j = GCD(E, b);
     while(j != 1){
            printf("\nSorry that E is incompatible!\n");
            printf("\nPlease pick an E: ");
            scanf("%d", &E);
            j = GCD(E, b);
     }

      D = EEA(&E, &b);
      printf("\nD is equal to %d\n\n", D);

      printf("If you would like to give a someone (or group)\n");
      printf(" the public key please do so now.\n\n");

  }else if(choice == 2){
    wordCollector();
  }else if(choice == 3){
     Decipher(&n);
  }else{
    printf("\n\nSorry, perhaps next time you'll hit a real option!\n\n");
  }
  choice = Menu();
 }

system("pause");
return 0;
}

C++ – Alphabetizer

Programming assignment found online.

Write a program that stores lists of names (the last name first) and ages in parallel arrays and sorts the names into alphabetical order keeping the ages with the correct names. The original arrays of names and ages should remain no changes. Therefore, you need to create an array of character pointers to store the addresses of the names in the name array initially. Apply the selection sort to this array of pointers so that the corresponding names are in alphabetical order.

Compiled Program: Student Alphabetizer

Header5.h

Read more

Operation TRAMP

TRAMP:

T – Transportable

R – Remote

A – Access

M – Monetarily

P – Practical

The Big Idea

Read more

Class Average Organizer

The goal of this program was to allow for a number of students (max 50), to have their class averages calculated when there are only three tests.

#include <stdio.h>
#include <string.h>
#define SIZE 50

typedef struct{
    char first[SIZE];
    char last[SIZE];
    int E1;
    int E2;
    int E3;
    double avg;
} student_t;

/* Gathers data */
void Collect(int *i, student_t student[]){
 
   while(scanf("%s%s", &student[*i].first, &student[*i].last) != EOF){
     printf("Enter student's three exams: ");
     scanf("%d", &student[*i].E1);
     scanf("%d", &student[*i].E2);
     scanf("%d", &student[*i].E3);
     ++*i;
     printf("\nEnter student name (Ctrl-Z to stop): ");
   }
        
return;
} 

/* Function Calculates and Displays Data */
void CalcDisplay(int *i, student_t student[]){

int j = 0;
double ClassAvg = 0;
char str[SIZE];

  while(j < *i){
      student[j].avg = (double)((student[j].E1 + student[j].E2 + student[j].E3) / 3);  
      ClassAvg = ClassAvg + student[j].avg;
    ++j;
  }
   
   ClassAvg /= (double)(j);
   j = 0;
   
      printf("\n\nName                       Exam 1    Exam 2    Exam 3     Average");
  while(j < *i){             
      sprintf(str, "\n%s %s", student[j].first, student[j].last); 
      printf("%-20.20s%14d%10d%10d%12.1f\n", str, student[j].E1, student[j].E2, student[j].E3, student[j].avg);
    ++j;
  }
    printf("\n\nThe class average is %3.1f\n", ClassAvg);
      
return;
}

/* Main */
int main(void){
    
student_t student[SIZE];
int i = 0;

  printf("\nEnter student name (Ctrl-Z to stop): "); 
     
     Collect(&i, student);
     CalcDisplay(&i, student);
    
system("pause");
return 0;
}

Why?

This poor soul just doesn’t understand.

Read more

Recursion Practice

Write a recursive function named two_ele_subs with one character string argument. The function will print all of the two-element subsets of a given set of letters. Write a main function with a loop to test the two_ele_subs function with different input character strings.

To leave the loop press ‘^’ and ‘z’ key while holding down the control key:

‘control’ + ‘^’ then ‘control’ + ‘z’

Iterative Version:

 
#include <stdio.h>
#include <string.h>
 
#define max_length 50
 
void two_ele_subs(char * str){
      
 int n = strlen(str);
 int i = 0;
 int j = 1;

     while(i < n){
         while(j < n){
            printf("[%c,%c]\n", str[i], str[j]);
              ++j;
         }
      ++i;
      j = i + 1; 
    }
 printf("\n\nEnter your string: ");
}

int main(void){
    
    char str[max_length];
    
      printf("Enter your string: ");
    while(scanf("%s", &str) != EOF){
      two_ele_subs(str);
    }
    
system("pause");
return 0;
}

Recursive Version:

 #include <stdio.h>
 #include <string.h>
 
 #define max_length 50
 
 void two_ele_subs(char * str){
      
 int n = strlen(str);
 int i = 0;
 int j = 1;
     if(n > 1){
         while(j < n){
             printf("[%c,%c]\n", str[i], str[j]);
             ++j;          
         }if(i < n){
            ++i;
         }
      two_ele_subs(str+1);
     }                      
}

int main(void){
    
char str[max_length];
    
    printf("Enter your string: ");
  while(scanf("%s", &str) != EOF){
     two_ele_subs(str);
     printf("\n\nEnter your string: ");
 }
     
system("pause");
return 0;
}

Bearded Dragon

Array Practice

Problem Description:

Each year the Department of Traffic Accidents receives accident count reports from a number of cities and towns across the country. To summarize these reports, the department provides a frequency distribution printout that gives the number of cities reporting accident counts in the following ranges:

0 – 99
100 – 199
200 – 299
300 – 399
400 – 499
500 or above

The department needs a computer program to take the number of accidents for each reporting city or town and add one to the count for the appropriate accident range. After all the data have been processed, the resulting frequency counts are to be displayed.

Traffic Accident Addition (.exe)

 
#include <stdio.h>

#define size 6

void updateRange (int *i, int *a_range){
     *i = 0;  
   while(*i >= 0){
        printf("Enter an accident count (negative to end): ");
        scanf("%d", &*i);
                if (*i <= 0){
                }else if (*i <= 99){
                   a_range[0]++;
                }else if (*i <= 199){
                   a_range[1]++;
                }else if (*i <= 299){
                   a_range[2]++;
                }else if (*i <= 399){
                   a_range[3]++;
                }else if (*i <= 499){
                   a_range[4]++;
                }else if (*i >= 500){
                   a_range[5]++; 
                } 
         }              
}
    
void displayRange (int *a_range){
  int y = 0;
  int numRange1 = 0;
  int numRange2 = 99;
    printf("\n   Range  Frequency\n\n" ); 
    printf("%4d-%3d%10d\n ",numRange1, numRange2, a_range[y]);
    ++y;
        while(y < 5){         
          numRange1 += 100;
          numRange2 += 100;
            printf("%d-%d%10d\n ", numRange1, numRange2, a_range[y]);
            ++y;
        }
    numRange1 += 100;
    printf("%d or above%5d\n\n",numRange1, a_range[y]);
}
       
int main(void){
  int i = 0;
  int a_range[size];
      while (i < size){
          a_range[i] = 0;
        ++i;
      }    
  updateRange(&i, &*a_range);           
  displayRange(&*a_range);
system("pause");
return 0;
}

Return top
  • Copyright ©  2011-2012  Lettergram
  • Powered by Lettergram!