User Tools

Site Tools


user:acrowle1:portfolio:cprogproject3

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
user:acrowle1:portfolio:cprogproject3 [2014/03/02 16:45] – [Scope] acrowle1user:acrowle1:portfolio:cprogproject3 [2014/03/09 14:53] (current) – [Project: dayofweek] acrowle1
Line 1: Line 1:
-======Project: squares======+======Project: Squares======
  
 A project for CSCS1320S14 by Alana Whittier during the Spring Semester 2014. A project for CSCS1320S14 by Alana Whittier during the Spring Semester 2014.
Line 40: Line 40:
  
 =====Scope===== =====Scope=====
-The motivation behind this project is to become familiarized with applying a pointer in order to use the Scanf() feature to acquire user input for computing the squares of two-digit integers ending in 5.  +The motivation behind this project is to become familiarized with applying a pointer in order to use the Scanf() feature to acquire user input for computing the squares of two-digit integers ending in 5. A program is written in which the Vedic math technique is applied to compute the squares individually and output them for the user. 
- +
-A program is written to+
 =====Attributes===== =====Attributes=====
 State and justify the attributes you'd like to receive upon successful approval and completion of this project. State and justify the attributes you'd like to receive upon successful approval and completion of this project.
  
-  * ability to edit code: this project will help immensely since the data types must be declared and specified correctly. +  * write program to successfully calculate the square of the two-digit integer ending in 5 using the Mental Math Technique 
-  * ability to convert to hexadecimal: this is requirement to use the bitwise AND/OR logical operators per data type +  * Obtain user input with prompt by proper implementation of pointer and use of scanf().  
-  * ability to obtain the low (negative values) for the signed data types.+  * Output both the user input value and the square of that value
 =====Procedure===== =====Procedure=====
-The actual steps taken to accomplish the projectInclude imagescode snippets, command-line excerpts; whatever is useful for intuitively communicating important information for accomplishing the project.+My first attempt at writing the program was using the modulus operator, %, although abandoned that effort quickly as it was not clear what to do with the factor in order to implement the mental math techniqueI then decided to implement the computation of the square in what I deemed more simplistic of an approach and then used a series of ifelse if statements to output **//only//** the values I wanted the user to input and obtain (two-digit integers ending in 5), and  finally an else statement in which "Error: Invalid Entry" is displayed for all values greater than 95 and for all values that do not end in 5
  
-=====Code===== +Example 1 (My first submitted program): 
-<code c> +
-''/*datatypes.c - A Program to derive and display information; +
-                for the signed and unsigned data types in C.;+
  
  
-written by: Alana Whittier for CSCS1320S14;+#include <stdio.h> 
 +#include <stdlib.h>
  
-on February 14, 2014; 
  
-Compile withgcc -o datatypes datatypes.c+int main() 
-Execute with: ./datatypes +
-*/+         
 +        int i; 
 +        int *p; 
 +        p=&i; 
 + 
 +        printf("type up to a two digit integer ending in 5;\n"); 
 +        scanf("%d", p); 
 +        if(i==15){ 
 +                printf("%.2d%d\n",i, (1*(1+1))*100 + 5*5);} 
 +    else if(i==25){ 
 +                printf("%.2d: %d\n",i, (2*(2+1))*100 +5*5);} 
 + else if(i==35){ 
 +                printf("%.2d%d\n",i, (3*(3+1))*100 +5*5);} 
 +        else if(i==45){ 
 +                printf("%.2d: %d\n",i, (4*(4+1))*100 +5*5); 
 +        else if(i==55){ 
 +                printf("%.2d: %d\n",i, (5*(5+1))*100 +5*5);} 
 +        else if(i==65){ 
 +                printf("%.2d: %d\n",i, (6*(6+1))*100 +5*5);}  
 +        else if(i==75){ 
 +                printf("%.2d: %d\n",i, (7*(7+1))*100 +5*5);         
 +        else if(i==85){ 
 +                printf("%.2d: %d\n",i, (8*(8+1))*100 +5*5);} 
 +        else if(i=95){ 
 +                printf("%.2d: %d\n",i, (9*(9+1))*100 +5*5);  
 + else  
 + printf("Error: Invalid Entry\n"); 
 + 
 + 
 + 
 +        return(0); 
 +}   
 + 
 +After more consideration, I decided to try again writing the program using the modulus operator, which I called R and the factor which I declared a variable. This code is substantially more efficient as it does not require so many lines of code to obtain the squared values. However, I am uncertain how to completely limit the computation and output, since this approach also works for 3 and 4 digit integers ending in five.  
 + 
 +Example 2 (The second program using modulus operator and factor):  
  
 #include <stdio.h> #include <stdio.h>
 +#include <stdlib.h>
  
 int main() int main()
 { {
-    unsigned char uchr = 0;  //unsigned char code +   
- fprintf(stdout, "TYPE: %15s, ", "unsigned char")//returns string +        int i
- fprintf(stdout"bytes: %lu, ", sizeof(uchr))//returns number of bytes for unsigned char + int factorR
- fprintf(stdout, "low: %hhu, ", (uchr & 0X00))//returns low value for unsigned char +        int *p
- fprintf(stdout, "high: %hhu, ", (uchr | 0XFF)); //returns high value for unsigned char +        p=&i;
- uchr uchr -1//decrement +
- fprintf(stdout, "qty: %hu\n", (uchr+1)); //performs increment first, then returns qty value+
  
-    signed char schr = 0;  //signed char code +        printf("type up to a two digit integer ending in 5;\n"); 
-    fprintf(stdout, "TYPE: %15s, ", "signed char"); //returns string +        scanf("%d", p); 
- fprintf(stdout, "bytes: %lu, ", sizeof(schr)); //returns number of bytes for signed char +  
- fprintf(stdout, "low: %hhd, ", (schr | -0X80)); //returns low value for signed char (need to type cast AND change to bitwise OR) + factor i/10
- fprintf(stdout, "high: %hhd, ", (schr | 0X7F));//returns high value for signed char +        10
- schr = schr -1; //decrement +  
- fprintf(stdout, "qty: %hu\n", (unsigned char) (schr+1)); //type cast to display negative values, perform increment, then display qty + if(R==5
-     +        printf("%.2d: %d\n",i, (factor*(factor+1))*100 5*5 ); 
-    unsigned short int usi = 0;  //unsigned short int code + if(i>95
-    fprintf(stdout, "TYPE: %18s, ", "unsigned short int");//returns string + printf("ErrorInvalid Entry");
- fprintf(stdout, "bytes: %lu, ", sizeof(usi)); //returns number of bytes for unsigned short int +
- fprintf(stdout, "low: %hu, ", (usi & 0X00)); //returns low value for unsigned short int +
- fprintf(stdout, "high: %hu, ", (usi | 0XFFFF));//returns high value for unsigned short int +
- usi usi -1; //decrement +
- fprintf(stdout, "qty: %hu\n", (usi+1))//performs increment first, then returns qty value +
-     +
-    signed short int ssi 0;  //signed short int code +
-    fprintf(stdout, "TYPE: %18s, ", "signed short int")//returns string +
- fprintf(stdout, "bytes: %lu, ", sizeof(usi)); //returns number of bytes for signed short int +
- fprintf(stdout, "low: %hd, ", (ssi | -0X8000); //returns low value for signed short int (need to type cast AND change to bitwise OR) +
- fprintf(stdout, "high: %hd, ", (ssi | 0X7FFF)); //returns high value for signed short int +
- ssi ssi -1; //decrement +
- fprintf(stdout, "qty: %hu\n", (unsigned short int) (ssi+1)); //type cast to display negative value, perform increment, then display qty +
-     +
-    unsigned int ui 0;  //unsigned int code +
-    fprintf(stdout, "TYPE: %18s, ", "unsigned int"); //return string +
-    fprintf(stdout, "bytes: %lu, ", sizeof(ui)); //return number of bytes for unsigned int +
- fprintf(stdout, "low: %u, ", (ui & 0X00)); //returns low value for unsigned int  +
- fprintf(stdout, "high: %u, ", (ui | 0XFFFFFFFF)); //returns high value for unsigned int +
- ui = ui -1; //decrement +
- fprintf(stdout, "qty: %u\n", (ui+1)); //performs increment firstthen returns qty value +
-     +
-    signed int si = 0;  //signed int code +
-    fprintf(stdout, "TYPE: %18s, ", "signed int"); //returns string +
-    fprintf(stdout, "bytes: %lu, ", sizeof(si)); //returns number of bytes for signed int +
- fprintf(stdout, "low: %d, ", (si | -0X80000000); //returns low value for signed int (need to type cast AND change to bitwise OR) +
- fprintf(stdout, "high: %d, ", (si | 0X7FFFFFFF)); //returns high value for signed int +
- si = si -1; +
- fprintf(stdout, "qty: %u\n", (unsigned int) (si+1)); //type cast to display negative value, perform increment, then display qty +
-  +
-    unsigned long int uli = 0;  //unsigned long int code +
-    fprintf(stdout, "TYPE: %18s, ", "unsigned long int"); //returns string +
-    fprintf(stdout, "bytes: %lu, ", sizeof(uli)); //returns number of bytes for unsigned long int +
- fprintf(stdout, "low: %lu, ", (uli & 0X00)); // returns low value for unsigned long int +
- fprintf(stdout, "high: %lu, ", (uli | 0XFFFFFFFFFFFFFFFF)); //returns high value for unsigned long int +
- uli = uli -1; //decrement +
- fprintf(stdout, "qty: %lu\n", (uli+1)); //performs increment first, then displays qty +
-     +
-    signed long int sli = 0;  //signed long int code +
-    fprintf(stdout, "TYPE: %18s, ", "signed long int"); //returns string +
-    fprintf(stdout, "bytes%lu, ", sizeof(sli)); //returns number of bytes for signed long int +
- fprintf(stdout, "low: %ld, ", (sli | -0X8000000000000000)); //returns low value for signed long int (need to type cast AND change to bitwise OR) +
- fprintf(stdout, "high: %ld, ", (sli | 0X7FFFFFFFFFFFFFFF)); //returns high value for signed long int +
- sli = sli -1; //decrement +
- fprintf(stdout, "qty: %lu\n", (unsigned long int) (sli+1)); //type cast to display negative value, perform increment, then display qty+
  
-    unsigned long long int ulli = 0;  //unsigned long long int code 
-    fprintf(stdout, "TYPE: %18s, ", "unsigned long long int"); //returns string 
-    fprintf(stdout, "bytes: %llu, ", sizeof(uli)); //returns number of bytes for unsigned long long int 
- fprintf(stdout, "low: %llu, ", (uli & 0X00)); //returns low value for unsigned long long int 
- fprintf(stdout, "high: %llu, ", (uli | 0XFFFFFFFFFFFFFFFF)); //returns high value for unsigned long long int 
- ulli = ulli -1; //decrement 
- fprintf(stdout, "qty: %llu\n", (uli+1)); //performs increment first, then displays qty 
-     
-    signed long long int slli = 0;  //signed long long int code 
-    fprintf(stdout, "TYPE: %18s, ", "signed long long int"); //returns string 
-    fprintf(stdout, "bytes: %lu, ", sizeof(slli)); //returns number of bytes for signed long long int 
- fprintf(stdout, "low: %lld, ", (slli | -0X8000000000000000)); //returns low value for signed long long int (need to type cast AND chage to bitwise OR) 
- fprintf(stdout, "high: %lld, ", (slli | 0X7FFFFFFFFFFFFFFF)); //returns high value for signed long long int 
- slli = slli -1; //decrement 
- fprintf(stdout, "qty: %lu\n", (unsigned long long int) (slli+1)); //type cast to display negative value, perform increment, then display qty 
   
- }+       
  
 +        return(0);
 +
 +
 +
 +=====Code=====
 +<code c>
 +/*Squares.c - A program which implements a mental math;
 +              technique for computing the square of any;
 +   two digit integer ending in 5, given by
 +   user input;
 +   
 +Written by: Alana Whittier for CSCS1320S14 on February 26, 2014;
  
-=====Execution=====+Compile with: gcc- - squares squares.c; 
 +Execute with: ./squares 
 +*/
  
-<cli+#include <stdio.h
-lab46:~/src/cscs1320$ nano datatypesM-D.+#include <stdlib.h>
-lab46:~/src/cscs1320$ gcc -o datatypesM-D datatypesM-D.c +
-lab46:~/src/cscs1320$ ./datatypesM-D +
-TYPE: unsigned char, bytes: 1, low: 0, high: 255, qty: 256 +
-TYPE:   signed char, bytes: 1, low: -128, high: 127, qty: 256 +
-TYPE: unsigned short int, bytes: 2, low: 0, high: 65535, qty: 0 +
-TYPE:   signed short int, bytes: 2, low: -32768, high: 32767, qty: 0 +
-TYPE: unsigned int, bytes: 4, low: 0, high: 4294967295, qty: 0 +
-TYPE:   signed int, bytes: 4, low: -2147483648, high: 2147483647, qty: 0 +
-TYPE: unsigned long int, bytes: 8, low: 0, high: 18446744073709551615, qty: 0 +
-TYPE:   signed long int, bytes: 8, low: -9223372036854775808, high: 9223372036854775807, qty: 0 +
-TYPE: unsigned long long int, bytes: 8, low: 0, high: 18446744073709551615, qty: 0 +
-TYPE:   signed long long int, bytes: 8, low: -9223372036854775808, high: 9223372036854775807, qty: 0 +
-</cli>+
  
-=====Reflection===== 
-Considering the difficulties I encountered during the process of writing this program, it was as rewarding as it was frustrating. It forced me to delve deeper into more of the computer fundamentals to successfully execute the program. Since I have never taken a digital logic type course and this was my first programming course, binary was a foreign concept to me. Furthermore, converting from decimal to binary or hex was even more foreign. I have learned everything from two's and one's complement, to format specifiers, to manipulating code in order to obtain the negative values in the range for the signed data types. In order to do this, I changed from bitwise AND to bitwise OR, as well as type cast to the unsigned counterpart of the data type. This was a surprise, as I happened upon changing from bitwise AND to OR, only in desperation to achieve what I knew the low values in the range were supposed to be. I kept second guessing MY logic, as well as the computer logic used in completing the assignment.  
  
-**Observations**+int main() 
 +
 +         
 +        int i; 
 +        int *p; 
 +        p=&i;
  
-The long and long long int (signed and unsignedappear the sameThis is because they are both 64 bit and that is the most the compiler can handle.+        printf("type up to a two digit integer ending in 5;\n")
 +        scanf("%d", p); 
 +        if(i==15){ 
 +                printf("%.2d: %d\n",i, (1*(1+1))*100 + 5*5);} 
 +    else if(i==25){ 
 +                printf("%.2d: %d\n",i, (2*(2+1))*100 +5*5);} 
 + else if(i==35){ 
 +                printf("%.2d: %d\n",i, (3*(3+1))*100 +5*5);} 
 +        else if(i==45){ 
 +                printf("%.2d: %d\n",i, (4*(4+1))*100 +5*5);}  
 +        else if(i==55){ 
 +                printf("%.2d: %d\n",i, (5*(5+1))*100 +5*5);} 
 +        else if(i==65){ 
 +                printf("%.2d: %d\n",i, (6*(6+1))*100 +5*5);}  
 +        else if(i==75){ 
 +                printf("%.2d: %d\n",i, (7*(7+1))*100 +5*5);         
 +        else if(i==85){ 
 +                printf("%.2d: %d\n",i, (8*(8+1))*100 +5*5);} 
 +        else if(i=95){ 
 +                printf("%.2d: %d\n",i, (9*(9+1))*100 +5*5);  
 + else  
 + printf("Error: Invalid Entry\n");
  
-printf() and fprintf() basically do the same thing. The difference being that printf can only print on the monitor, has the default stream of STDOUT, while fprintf can print to a user defined stream (or file). In our project, fprintf uses the STDOUT to the screen AS if it were a file.  
  
-STDOUT is by default printed to the screen unless user specified.  
  
-%s is the format specifier used to print a string of characters, %hhu is the format specifier for half half unsigned char, % hu is the format specifier for unsigned short int. +        return(0); 
 +}   
 +  
 +=====Execution=====
  
-The difference between %u and %d are that %u denotes an unsigned int type, while %d denotes signed int type. +<cli> 
 +lab46:~/src/cscs1320$ nano squares.c 
 +lab46:~/src/cscs1320$ gcc -o squares squares.c 
 +lab46:~/src/cscs1320$ ./squares 
 +type up to two digit integer ending in 5; 
 +25 
 +25: 625 
 +lab46:~/src/cscs1320$ ./squares 
 +type up to a two digit integer ending in 5; 
 +55 
 +55: 3025 
 +lab46:~/src/cscs1320$ ./squares 
 +type up to a two digit integer ending in 5; 
 +60 
 +Error: Invalid Entry 
 +lab46:~/src/cscs1320$ ./squares 
 +type up to a two digit integer ending in 5; 
 +95 
 +95: 9025 
 +lab46:~/src/cscs1320$ ./squares 
 +type up to a two digit integer ending in 5; 
 +100 
 +Error: Invalid Entry 
 +lab46:~/src/cscs1320$
  
-Considering the 13 in %13 in the first stanza for unsigned char in the program, this just specifies the number of characters in the string, including spaces to be printed for "TYPE"+</cli>
  
-If sign is left unspecified, it is assumed unsigned by default.+=====Reflection===== 
 +In writing this program, I encountered some initial difficulty with the use of if, else if, and else statements. I was not understanding why the program was not doing as I expected. After series of trial and error attempts, it was observed that my code would work if I used the correct syntax. First mistake: the semi-colon following the if, else if, else conditions that I tried to impose. Removing them helped significantly. 
 +Second mistake: The way I used the curly braces. Essentially, I was embedding several else if statements within the initial if. By correcting this my code worked flawlessly. 
 + 
 +Since my initial attempt at writing this program included the modulus operator, which had been quickly abandoned, I decided to revisit that since I now had a working program written in a manner I felt was more simplistic. What I realized that I was missing from my initial program was the factor (the number of times that 10 went into the integer). For example, if I declared the factor to be i/10 and R= i%10, then for an integer value of 25, R=5 and the factor=2, since 10 can go into 25 twice, with a remainder of 5. With this program written this way, the same mental math technique works for 3 and 4 digit integers ending in 5 as well, to compute the squares. This code is shown above in Example 2 of the Procedure section   
  
-The & and | operators are the bitwise logic operators, which in our case took the hex representation of our data types to help us to obtain the appropriate high/low values within our ranges.  
  
-I experienced some difficulty in initial attempts to obtain the low values for the signed data types. I later learned that not only did I need to change the expression for the "low" values to bitwise OR, but I also needed to type cast in the final line of the signed data type stanza. 
  
-Based on my program's output, the total bits allocated per the following data types are as follows: 
-   * signed char = 8 bits 
-   * unsigned short int = 16 bits 
-   * unsigned int = 32 bits 
-   * signed int = 32 bits 
-   * signed long long int = 64 bits 
  
-However, due to the decrementing and incrementing per data type, only the unsigned char actually stored ANY memory at all and stored a total of 16 bits! 
  
  
Line 213: Line 227:
 In performing this project, the following resources were referenced: In performing this project, the following resources were referenced:
  
-  * http://en.cppreference.com/w/cpp/language/types1 +  * http://wildaboutmath.com/2007/11/11/impress-your-friends-with-mental-math-tricks/comment-page-6
-  * http://www.youtube.com/watch?v=SXAr35BiqK8 +  * http://saurabhg.hubpages.com/hub/Vedic-Mathematics---Quick-multiplication-techniques---Part-1 
-  * http://www.binaryhexconverter.com/decimal-to-hex-converter +  * Kernighan, Ritchie //The C Programming Language// Second Edition, AT&T Bell Laboratories, 1988. Print. 
-  * http://en.wikipedia.org/wiki/Signed_number_representations +  * email consultations and guidance from Matt Haas
user/acrowle1/portfolio/cprogproject3.1393778734.txt.gz · Last modified: 2014/03/02 16:45 by acrowle1