AN INTRODUCTION TO C++ "PAM" STYLE OF PROGRAM CODING

 I am working right now with what I may consider as an 'ambitious' project that could, in a way give more ADDITIONAL security to a program that requires an AUTHORIZED USER to only memorize the sequence of entering inputs, rather than the conventional way of having a form to fill out the necessary information such an example below;

                             EMPLOYEES MONTHLY SALARY  UPDATE

  Name of the Employee: ____________________________
  Employee ID: _____________
  For the Month of: _______________
  No. of Days: ______________
  Amount of Salary (in Dollar): $ ____________________
  

What if all you CAN ONLY SEE on your monitor screen is the title (Employees Monthly Salary Update) and just followed by a 'blinking cursor' (in a C++ program) and nothing more? This will certainly put a  'hacker' to think twice before pressing any key as only an AUTHORIZED personnel knows the sequence by let's say entering first an employee's ID, then separated by a 'comma' or any other special character (?, !, $, &, *, etc) to be able to fill out the other information and update a monthly salary record. Of course, there could be instances that  a user might accidentally hit a wrong key, so the design allows a user three attempts or the system will 'lock out' the program and sounds an alarm or shows a 'warning'.

This is how I think PAM (POINTER-ARRAY MANIPULATION) Style or Method will have the BEST USE or application. Like most C++ beginners, I also got confused about Array and Pointer and as a first impression, doesn't really cares nor appreciate their importance. 

Somehow, I realized then that MASTERING how to manipulate pointers and arrays gave me the option to design programs that is, 'wonderfully impressive'.

One of them, I think most beginners not yet discovered (maybe at this point), is the option not to repeatedly create containers of similar kind just to 'accommodate' numerous inputs, even in hundreds. (Please watch these video presentations)



The next thing I discovered as C++ beginners should also take note, is that GOLDEN RULE that datatype of different 'kinds' cannot directly 'mingle' which each other, unless you do some kind of a 'SYNCHRONIZATION' method, the way I did it. (As a guide, please watch this video presentation)


But to fully understand what I'm 'pointing out' about "synchronizing" datatypes - I will post here, a simple program (with 'source code') that will translate a worded phrase like "ONE PLUS ONE" into integers and give the result (sum) and for kids, to translate back the 'sum' into English word 'TWO'. 

Finally, I uploaded my video presentation that 'highlights' the C++ program coding 'SHOWING' the practical application of PAM technique. (As a guide, please watch this video presentation):


If you are eager to know the source code... Here it is. (Take note, that for those who are curious about the details, there is a section below that will explain them):

program designed by sirjon

01   #include<iostream>
02   using namespace std;
03
04   int  goAhead (string Entry, string Entry1, string Entry2)
05  { 
06     float x=0;
07     float y=0;
08     float sum=0;
09     float difference=0;
10     float product=0;
11     float quotient=0;
12
13     string Wsum;
14     string Wdifference;
15
16     string wordedNumbers[16]={"NEGATIVE FIVE","NEGATIVE FOUR","NEGATIVE            
17                   THREE","NEGATIVE TWO","NEGATIVE ONE","ZERO","ONE","TWO","THREE", 
18                  "FOUR", "FIVE","SIX","SEVEN","EIGHT","NINE","TEN"};
19     string *q;
20     q=&wordedNumbers[0];
21
22     float realNumbers[16]={-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10};
23     float *p;
24     p=&realNumbers[0];
25
26         for (int i=0; i<16; i++)
27            {if (*(q+i)==Entry)
28               {x=*(p+i);}
29            }  
30
21         for (int i=0;i<16;i++)
22            {if (*(q+i)==Entry2)
23                {y=*(p+i);}
24            } 
25
26          sum=x+y;
27          difference=x-y;
28          product=x*y;
29          quotient=x/y;
30                    
31          for (int i=0;i<16;i++)
32             {if (*(p+i)==sum)
33                 {Wsum=*(q+i);}
34              } 
35                      
36          for (int i=0;i<16;i++) 
37              {if (*(p+i)==difference)
38                 {Wdifference=*(q+i);}
39              }
40          if (Entry1=="PLUS")
41            {cout<<x<<"+"<<y<<"= "<<sum<<"\n";
42              cout<<Entry<<" PLUS "<<Entry2<<" IS "<<Wsum<<"\n";}
43                     
44          if (Entry1=="MINUS") 
45            {cout<<x<<"-"<<y<<"= "<<difference<<"\n";
46              cout<<Entry<<" MINUS "<<Entry2<<" IS "<<Wdifference<<"\n";}
47                     
48          if (Entry1=="TIMES")
49            {cout<<x<<"x"<<y<<"= "<<product;}
50                     
51          if (Entry1=="DIVIDED_BY")
52            {cout<<x<<"/"<<y<<"= "<<quotient;}
53
54    return 0;
55   }
56
57    int main ()
58   { 
59      int proceed;
60      string starter;
61      string Word;
62      string Word1;
63      string Word2;
64      cout<<"DO MATH IN ENGLISH WORDS \n";
65      cout<<"Enter numbers from 'ONE' to 'FIVE' ONLY! (in common English words)\n";
66      cout<<"Direction:\n"; 
67      cout<<"  Use PLUS to Add two numbers,\n";
68      cout<<"  Use MINUS to Subtract,\n";
69      cout<<"  Use TIMES to Multiply,\n";
70      cout<<"  Use DIVIDED_BY to Divide.\n";
71      cout<<"Follow this format: ONE PLUS ONE \n";
72      cout<<"Important: ALL IN UPPER-CASE LETTERS \n";
73      cout<<"\n";
74  
75          cin>>Word>>Word1>>Word2;
76          proceed= goAhead(Word,Word1,Word2);
77    return 0;
78   }

DETAILS ABOUT THIS C++ PROGRAM

THIS program design is divided into TWO FUNCTIONS:

1) The "int main ()" - THE DEFAULT FUNCTION
2) and the "int goAhead (  )" as the SECOND FUNCTION

The DEFAULT FUNCTION consists mainly of WHAT-To-DO
dialogs that is represented by the cout<< DISPLAY command code:

          cout<<"DO MATH IN ENGLISH WORDS \n";
          cout<<"Enter numbers from 'ONE' to 'FIVE'ONLY   (in common English words)\n";
          cout<<"Direction:\n"; 
          cout<<"  Use PLUS to Add two numbers,\n";
          cout<<"  Use MINUS to Subtract,\n";
          cout<<"  Use TIMES to Multiply,\n";
          cout<<"  Use DIVIDED_BY to Divide.\n";
          cout<<"Follow this format: ONE PLUS ONE \n";
          cout<<"Important: ALL IN UPPER-CASE LETTERS \n";

The importance of these DIALOGS is to give the user an idea of what program it is about, any instructions or directions to follow and what to enter.

Of course, as BEST practice, create first CONTAINERS before writing the DIALOG on what to enter...

         string Word;
         string Word1;
         string Word2;

(Disregard the container "string starter", I just put it there for others to check if they truly understand how this program works!)

NEXT, the method on HOW TO ENTER the inputs:

                                          cin>>Word>>Word1>>Word2;

Notice that there's only one cin>> INPUT COMMAND code and it is followed by >> for each 'ENTRIES", which is possible to do in C++.

It will be represented by a BLINKING CURSOR.

The first entry's destination is store the INPUT to the container 'Word', then the second to 'Word1' and the third to Word2.

The FIRST CONTAINER will 'only contain' English words (restricted) - "ONE, TWO, THREE, FOUR or FIVE" as given, instructed as part of the RULE.

The SECOND CONTAINER is restricted to recognize only the words "PLUS, MINUS, TIMES or DIVIDED_BY". Notice that the word 'DIVIDED_BY is a continous word separated only by the UNDERSCORE sign as we use cin>> INPUT COMMAND code which only recognize 'single word entry'.

The THIRD CONTAINER, like the first container, also - WILL ONLY contain English words - "ONE, TWO, THREE, FOUR or FIVE".

IT IS IMPORTANT RULE for user to 'HIT THE SPACE BAR' every time it is finished to enter the INPUT FOR EACH CONTAINER and then 'HIT ENTER' once completed the three entries.

NEXT, a need to put the other program coding in a separate section...

               int proceed;
               proceed= goAhead(Word,Word1,Word2);

Of course, a need to create first, a container labeled 'proceed' witht datatype 'int'. The reason I prefer the FUNCTION's datatype as'integer' is that, it is easier to simply use the "return 0;" notation at the end of the Function program (also as a standard procedure). 

Then a syntax to be able to transfer the values inside containers Word, Word1 and Word2 to the next FUNCTION...

           goAhead(Word,Word1,Word2) 
           
As for beginners who have no idea about C++ FUNCTION, I recommend you to read articles about it (Search the Internet).

NOW, THE 'SECOND FUNCTION'...

      int goAhead(string Entry,string Entry1, string Entry2)


The type of PAM that was introduced here shows how to coordinate the   ARRAY OF STRINGS to the ARRAY OF INTEGERS (or to be specific, "ARRAY OF FLOATING NUMBERS").

Like the 'best practice of declaring first -'datatype' and label names for 'variables' and sometimes to give 'values' to either 'initialize values' or to 'store permanent values' in creating CONTAINERS...

There's a need to establish first an ARRAY that will store permanently, the values we INTENDED to be manipulated. See the example below:

  string wordedNumbers[16] = {"NEGATIVE FIVE","NEGATIVE FOUR","NEGATIVE THREE, 
                                             "NEGATIVE TWO","NEGATIVE ONE","ZERO","ONE","TWO", 
                                             "THREE", 'FOUR',"FIVE", "SIX", "SEVEN", "EIGHT","NINE", TEN"};
      
Maybe, you're wondering why we need 16 ELEMENTS (or VALUES), started from "NEGATIVE FIVE UP TO TO TEN"?

Well, in this C++ program - we intended to display the result of either the SUM or DIFFERENCE in English Words, as part of the design, the reason there are 16 elements.

Imagine, how much more elements do we need to include if we intend to display the result of the 'product' in English Word ( as it seems, it will become complicated if we include the quotient in English Word).

To make the Array flexible, there's a need to create a pointer and a pointer, like any conventional container, need to be declared. Using the 'asterisk sign', we can able to differentiate a pointer from a container . See example below:

                                                              string *q;  

After declaring a pointer, it is a strict requirement to set a pointer to a 'reference point' as its STARTING POINT. A need to include the AMPERSAND sign to achieve it:

                                                      q=&wordedNumbers[0];

NEXT, of course, is to create a corresponding Array using the datatype 'float', including declaring the pointer and a reference point:


                         float realNumbers[16]={-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10};
                         float *p;
                         p=&realNumbers[0];

If you will notice, both pointers are with SYNCHRONIZED STARTING POINT: An English word = 
 NEGATIVE FIVE, corresponding to a real number equivalent  =  -5 .

The COMBINATION of 'FOR-LOOP' and 'IF'statements ACTS like a 'DETECTOR'.

                         for(int i=0; i<16; i++)
                          {if (*(q+i) = = Entry)
                             {x = *(p+i);}
                           }

The FOR-LOOP 'COUNTER i' - will keep on searching for a 'MATCH'inside the STRING ARRAY and compare it to the value stored inside container 'Entry'. The 'COUNTER i' changes the position where pointer 'q' is pointing as:

                         *(q+0) = "NEGATIVE FIVE"
                         *(q+1) = "NEGATIVE FOUR"
                         *(q+2) = "NEGATIVE THREE" and so on...

The condition that if 'the pointer 'q' is pointing to an Element that has an exact "string of Letters" exactly the same as what the container ENTRY has, then it will 'get' the exact 'i COUNTER' value (counterpart) that will correspond to what the "pointer 'p' is pointing and then will be stored in the container with the label 'x'.

It will have the similar process for Entry2, except the corresponding value will be stored in a container labeled  'y':

                         for (int i=0; i<16; i++)
                          {if (*(q+i)= = Entry2)
                              {y = *(p+i);}
              }  

NOW that both English words for numbers are converted to real numbers, it is now possible to do 'math operations':


                                     sum = x+y;
                                     difference = x-y;
                                     product = x*y;
                                     quotient = x/y;

A second batch of FOR-LOOP/IF STATEMENTS are included after a certain 'math operation': 

                                    for (int i=0;i<16;i++)
                                     {if (*(p+i) = = sum)
                                       {Wsum = *(q+i);}
                                      } 
                      
                                    for (int i=0;i<16;i++) 
                                     {if (*(p+i) = = difference)
                                       {Wdifference = *(q+i);}

These are located after the math operation (applicable only for ADDITION and SUBTRACTION), as there's a need first to determine the value for the SUM or DIFFERENCE, that the 'p' pointer is pointing, before it will correspond to the STRING ARRAY's pointer 'q' to point it on its corresponding English word for that number. 


ANY 'RESULT' of the math operations can be displayed by including 'IF STATEMENTS' that will determine what 'math operation' a user wishes to display. 

                                           if (Entry1= = "PLUS")
                                              {cout<<x<<"+"<<y<<"= "<<sum<<"\n";
                                                cout<<Entry<<" PLUS "<<Entry2<<" IS "<<Wsum<<"\n";}
                      
                                           if (Entry1= = "MINUS") 
                                              {cout<<x<<"-"<<y<<" = "<<difference<<"\n";
                                               cout<<Entry<<" MINUS "<<Entry2<<" IS "<<Wdifference<<"\n";           
                     
                                           if  (Entry1= = "TIMES")
                                              {cout<<x<<"x"<<y<<" = "<<product;}
                     
                                           if (Entry1= = "DIVIDED_BY")
                                              {cout<<x<<"/"<<y<<" = "<<quotient;}


TAKE NOTE, for every NEW VARIABLE that will exist during program design, there must be a corresponding container for each one of them...

                                               float x=0;
                                               float y=0;
                                               float sum=0;
                                               float difference=0;
                                               float product=0;
                                               float quotient=0;
            
                                               string Wsum ;
                                               string Wdifference ;

Comments

Popular posts from this blog

C++ SOURCE CODE FOR SIRJON'S "MICRO" TIC TAC TOE GAME FOR FUN!

ENTER CORRECT PASSWORD, THREE ATTEMPTS ONLY USING C++ WHILE-IF COMBINATION

C++ SOURCE CODE FOR SIRJON'S "MICRO" TIC TAC TOE GAME FOR LAPTOPS