Author Archive

Predicting Randomness

The Back Story

About six months ago (December 2012) a friend and I were discussing ideas for beginning a business. Our methodology was simple, throw out as many ideas as we could, weed out the silly ones and then form a few models for the remaining ideas in hopes one seems like it has a fair possibility of success (hopefully with minimum risk). Turns out, although we had several interesting ideas, the most interesting part of the whole event was determining how to determine what would be best.

My friend had recently read a book titled: The Black Swan: The Impact of the Highly Improbable by Nassim Taleb and had been discussing it with me. In the book the author discusses the his ideology which explains that the most successful endeavors are the one that no one ever saw coming, although in retrospect most people believe it a certainty that that event would happen. In other words, looking at the past it seems likely that historical events do not seem to be random, but rather follow a logical track. Unfortunately, when in the present we cannot (even with the aid of computers) determine an event before it happens, even though the evidence certainly should exist. The author reasons that this is because as humans our brains simply don’t work like that, we attempt to find patterns and unfortunately, we simply don’t have a large enough sample size and perhaps never can.

These two discussions (about the book and starting a business) have been troubling me ever since. Clearly, there is an issue. Starting a company obviously is coupled with risk, however the idea that the most successful companies form from seemingly improbable ideas is troubling. Then what about the other side of the spectrum? What if I attempt to start a company that produces similar goods to another (better established) company with similar technology? Then I am out of luck. Leading to the conclusion that all highly successful companies must form from an idea which has currently not been predicted or created. That being the case, it would be beneficial to predict such cases would it not?

The Point

Not only would the ability to predict seemingly random events be beneficial in business, it would be beneficial in all aspects of life. Imagine the ability to predict traffic accidents, political upheavals, terrorist activities, economic downturns, inflation rates, perhaps even crimes.

The simple fact is, we cannot predict nearly as much as we could with the data we have at hand and although there are some serious challenges  that would have to be over come I have no idea why we have no model to predict the seemingly “random” of history.

My Naive Solution

According to the author of the before mentioned book (The Black Swan: The Impact of the Highly Improbable) one major reason that we (as humans) have an issue predicting random events is our brains simply are not set up that way. Sure we can think, but clearly our evolution favored reflexes, strength, and adaptability over prediction what we needed to adapt to. An example would be, when tossed into the water we initially try to splash around stay afloat, we don’t (normally) consider how to swim before we are tossed in. The reasoning is simple. We have complex and fast computers which can do massive amounts of computations and have companies such as Google which can organize data efficiently. It should then be possible to rank current and semi-historical events similar to PageRank in order to predict likely outcomes. The trouble with this is that human history has trillions of important events and even more minor events which lead to outcomes. That being said, the issue according to the author (as well as inductive reasoning) points out that because most historians claim that their is a logical chain of events that lead to outcomes, history should lead to specific outcomes based on those facts. The problem with that chain of thought is that historians (as well as businessmen, economists, stock traders, etc) all look at ALL of history rather than each situation as a new one.

Therefore, if somehow we could determine a time-frame for which most seemingly “random” events or ideas occur we could therefore make accurate predictions about the short term future. For example, if in this randomness program there is a high likelihood that a company will develop a technology which can increase cell tower reception by 100% and it would be successful it would be a good idea to invest in that sector of the market. Similarly, if the program could determine a possible housing bubble fiasco it would be beneficial to fix take money out of the market. Obviously, there are many MANY clear issues, however the idea that I was attempting to convey was that although random events of today seem to be explosive and almost unpredictable there is a solution which on the surface seems possible.

And perhaps creating something which can predict the seemingly random, seems like a good business venture…

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

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