User Tools

Site Tools


user:lburzyns:portfolio:project1

Project: Exploring Arithmetic Expressions

A project for CPROG by Lauren Burzynski during the Fall 2011.

This project was begun on November 1, 2011 and was finished on November 6, 2011.

Objectives

The purpose of the project is to master arithmetic operators, learn how they are used properly within a program, and to write a code with output based on user input.

Prerequisites

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

  • ability to log into lab46
  • ability to edit text files
  • ability to compile C source code
  • knowledge of basic arithmetic operators in C programming
  • abilitiy to create a basic program in C programming
  • knowledge of basic arithmetic

Background

The purpose of this project is to explore arithmetic expressions, learning how to create a program with addition, subtraction, multiplication, and division with the correct result. I am attempting to pursue a basic understanding about how to write arithmetic operators within a line of code.

In my Opus part 2, experiment 1 was a good jumping off point for this project (see link below): http://lab46.corning-cc.edu/opus/fall2011/lburzyns/start

I took what I learned from that experiment, which basically entailed whether or not I would receive a negative number when I subtracted 20 from 10. The answer turned out to be yes, I did receive a negative number. The small code I used to determined that was the basis for this project, only I expanded on it to encompass all of the arithmetic operators. Generally, what is going to happen is I will write a code that allows the user to input 2 different values for 2 different integers to be added, subtracted, multiplied, and divided. The user is not limited to only 2 values for all of these operations, the program will prompt the user for each value each time a new operator is called on by the program. The result will look something like this:

10 + 10 = 20

The results vary given the input, as well as the operator called on at the time. After this result is given, then the program will prompt the user for more values to use in the next arithmetic expression, and will continue on through all four operators.

Scope

This project will be exploring the nature of arithmetic operators available in C programming language. How to write a program with arithmetic expressions that allows the user to input numbers as variables instead of assigning values to the variables at the beginning of the code.

A program will be written that will display all of the arithmetic operators being used properly.

The arithmetic operators being used include:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)

Attributes

  • variables: A variable will be assigned for each input number and the values will be determined by user input
  • arithmetic operators: Arithmetic operators will be used within the program and clearly defined

Procedure

To accomplish this project, I first had to to some research on how to neatly and properly write a whole program where the user was inputing the values for the integers being added, subtracted, multiplied, and divided. When I first wrote the program, I forgot to use the “scanf” command, which was a big oversight on my part. I quickly realized that the program was not going anywhere without that command in the program. I received an error when I tried to compile and run the program, and after some minor troubleshooting I quickly realized where I went wrong, and re-wrote the program. My second attempt at writing the program was almost successful - the only thing I adjusted the second time around was to add a “\” sign in the lines that contained “ = %d\n”. This allowed there to be a space in between arithmetic expressions, and made the program look a lot neater and more organized. After that final adjustment, I was able to walk away with a successful program.

Code

#include <stdio.h>
 
int main ()
{
        int firstNum, secondNum, delta = 0;
        printf("Enter 1st number to be added(x): ");
        scanf("%d", &firstNum);
        printf("Enter 2nd number to be added(y): ");
        scanf("%d", &secondNum);
        delta = firstNum + secondNum;
        printf("x + y = %d\n", delta);
 
        printf("Enter 1st number to be subtracted(x): ");
        scanf("%d", &firstNum);
        printf("Enter 2nd number to be subtracted(y): ");
        scanf("%d", &secondNum);
        delta = firstNum - secondNum;
        printf("x - y = %d\n", delta);
 
        printf("Enter 1st number to be multiplied(x): ");
        scanf("%d", &firstNum);                
        printf("Enter 2nd number to be multiplied(y): ");
        scanf("%d", &secondNum);
        delta = firstNum * secondNum;
        printf("x * y = %d\n", delta);
 
        printf("Enter 1st number to be divided(x): ");
        scanf("%d", &firstNum);
        printf("Enter 2nd number to be divided(y): ");
        scanf("%d", &secondNum);
        delta = firstNum / secondNum;
        printf("x / y = %d\n", delta);
 
return 0;
}

Execution

lab46:~/src/cprog$ ./project1
Enter 1st number to be added(x): 5
Enter 2nd number to be added(y): 5
x + y = 10
Enter 1st number to be subtracted(x): 5
Enter 2nd number to be subtracted(y): 5
x - y = 0
Enter 1st number to be multiplied(x): 5
Enter 2nd number to be multiplied(y): 5
x * y = 25
Enter 1st number to be divided(x): 5
Enter 2nd number to be divided(y): 5
x / y = 1
lab46:~/src/cprog$

Reflection

After I successfully completed this project, I felt accomplished and proud. It is always a thrill for me to be able to do something correctly, especially when I find something to be difficult to conquor. This also overcame my fear of projects, feeling that they came off more daunting than they really are. I feel that I definitely mastered the “scanf” command, as well as completely explored arithmetic operations. Throughout my 3 attempts (as noted under the 'procedure' heading), I learned a lot from what a little character symbol can do to change the output of a whole program. As I went, I made notes for the future so as not to make the same mistakes again, and if I do, I have the solution. Doing this project was very informative and a good learning experience.

References

In performing this project, the following resources were referenced:

This website helped me properly write and execute a program based on arithmetic operators. This website showed simple, easy to understand examples of how each operator is used.

This website was helpful because it posts a short program that shows the basics behind arithmetic operators. The difference in my program was that I wanted the user to input the values. This program has assigned values already in place. Still, it was helpful for me to reference and it was a good jumping off point.

This website was useful because it lists in the “comment” column of the chart what to type when it came to the last line of code for each operator. This was essential in creating a successful program.

This website was useful in the early stages. This was helpful in explaining, in plain english, what each expression and operator does, and how it is used. There are also some small examples that were useful.

user/lburzyns/portfolio/project1.txt · Last modified: 2011/11/07 01:11 by lburzyns