User Tools

Site Tools


user:acrowle1:portfolio:cprogproject3

Project: Squares

A project for CSCS1320S14 by Alana Whittier during the Spring Semester 2014.

This project was first begun, on or around, February, 20, 2014 and took approximately 1 week to complete. The program I first submitted is not incorrect, as it does compile and yields the expected output, although it is not necessarily the most elegant or efficient script. In that first program, a series of 'if' and 'else if' statements were used to ensure appropriate execution of the program. After some more consideration, I decided to try writing the program in an entirely different manner, using both the factor and the remainder to achieve the same output as produced in my first program. However, even this endeavor had it's challenges as I am still not sure how to conditionally tell the compiler for ALL values greater than 95, INCLUDING those that end in 5, to only return the error message.

Objectives

The purpose of this project is to become familiarized with using a pointer in order to use the scanf() feature for obtaining input from a user. Also, this project serves to introduce a new user of C programming to conditional statements in order to achieve the desired output. In order to successfully compile and execute the program using a mental math technique, a pointer must be applied appropriately, scanf() must be implemented to call for user input and communicate back to the compiler, and the use of conditional statements (if statements, etc.) must be used.

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences may be consulted/achieved:

Helpful Resources:

Experiences necessary:

  • ability to edit code
  • ability to use lab46 remotely
  • an understanding of the mental math technique to apply to the two digit integers ending in 5 to acquire the square of that number
  • a basic understanding of pointers and scanf() to obtain user input
  • ability to use conditional statements to achieve desired output

Background

This project implements a mental math technique to compute the square of any two-digit integer ending in 5. A program is written in a manner consistent with the technique described in the project assignment page.

See project assignment page on the provided link.

Project: MENTAL MATH (SQUARES OF 5)

This mental math technique, as well as many others, is part of a collection of what is commonly called Vedic Mathematics, methods to compute quickly, without the use of a calculator, or performing a lengthy multi-step multiplication process by hand.

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. A program is written in which the Vedic math technique is applied to compute the squares individually and output them for the user.

Attributes

State and justify the attributes you'd like to receive upon successful approval and completion of this project.

  • write program to successfully calculate the square of the two-digit integer ending in 5 using the Mental Math Technique
  • Obtain user input with a prompt by proper implementation of pointer and use of scanf().
  • Output both the user input value and the square of that value.

Procedure

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 technique. I then decided to implement the computation of the square in what I deemed more simplistic of an approach and then used a series of if, else 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.

Example 1 (My first submitted program):

#include <stdio.h> #include <stdlib.h>

int main() {

      
      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 <stdlib.h>

int main() {

      int i;
	int factor, R;
      int *p;
      p=&i;
      printf("type up to a two digit integer ending in 5;\n");
      scanf("%d", p);
	
	factor = i/10;
      R = i % 10;
	
if(R==5)
      printf("%.2d: %d\n",i, (factor*(factor+1))*100 + 5*5 );
if(i>95)
	printf("Error: Invalid Entry");
      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;

Compile with: gcc- - squares squares.c; Execute with: ./squares */

#include <stdio.h> #include <stdlib.h>

int main() {

      
      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);

}

Execution

lab46:~/src/cscs1320$ nano squares.c
lab46:~/src/cscs1320$ gcc -o squares squares.c
lab46:~/src/cscs1320$ ./squares
type up to a 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$

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 a 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.

References

In performing this project, the following resources were referenced:

user/acrowle1/portfolio/cprogproject3.txt · Last modified: 2014/03/09 14:53 by acrowle1