User Tools

Site Tools


haas:spring2011:cprog:tasks:task2


Corning Community College

C/C++ Programming


Task 2: Variables and Equations

~~TOC~~

Objective

To utilize variables and to create equations to solve problems.

Background

In class, we started writing some simple programs to play with input/output and manipulate variables.

What we're going to do is provide an opportunity to put those skills to use writing some programs.

Program 1: Average

Write a program that contains the following variables (choose the best fitting type for each variable):

  • score0
  • score1
  • score2
  • score3
  • score4
  • score5
  • score6
  • score7
  • sum
  • average

Have your program prompt the user for input (for each of the “score” variables), assume the user will put in a value in the range of 0-100.

Once you get through all 8 scores (score0 through score7), I want you to do the following:

  • come up with an equation to sum up all the scores and store the result in the variable called sum
  • once you have the sum, create an equation to calculate the average.
  • display the average (truncate at 2 decimal places).

Be sure to save a copy of your working source code as task2-average.c in your ~/src/submit/ directory, and add/commit it to your repository.

Program 2: Slope

Write a program that contains the following variables (choose the best fitting type for each variable):

  • x1
  • x2
  • y1
  • y2
  • slope (there'll be decimals)

Have your program prompt and get input for the starting x y and ending x y of a line.

Once you get the coordinates, I want you to do the following:

  • calculate the slope of the line, store it in the slope variable
  • display a message to the user showing the starting and ending x and y coordinates, and the slope

Be sure to save a copy of your working source code as task2-slope.c in your ~/src/submit/ directory, and add/commit it to your repository.

Questions

  1. What data type did you choose for your “average” variable?
  2. What is the equation used to calculate the average in your program?
  3. For the slope calculation, what sort of line would have a slope of 1?

Review of Compiling/Executing

Just to review the compilation/execution process for working with your source code, if we had a file, hello.c, that we wished to compile to a binary called hello, we'd first want to compile the code, as follows:

lab46:~/src/cprog$ gcc -o hello hello.c
lab46:~/src/cprog$ 

Assuming there are no syntax errors or warnings, and everything compiled correctly, you should just get your prompt back. In the event of problems, the compiler will be sure to tell you about them.

Conceptually, the arrangement is as follows:

gcc -o BINARY_FILE SOURCE_FILE

The BINARY_FILE comes immediately after the -o, and the SOURCE_FILE, must never immediately follow a -o. It can precede, and such is perfectly valid (especially if you feel that way more intuitive).

To execute your binary, we need to specify a path to it, so we use ./, which basically says “in the current directory”:

lab46:~/src/cprog$ ./hello
Hello, World!
lab46:~/src/cprog$ 

Copying files to your submit directory

As you write your code, hopefully you've developed the good habit of storing all your programs in your ~/src/cprog directory (and have added/committed them to your repository).

But, in order to complete your tasks, you've been requested to place it in your ~/src/submit directory instead.

What to do?!

We'll simply make a copy of your code! Assuming we're working with a source file called myprog.c in our ~/src/cprog directory, we'll copy it into ~/src/submit/ and give it a name of: taskX.c

To do that we use the cp command, and run it as follows:

lab46:~/src/cprog$ cp myprog.c ~/src/submit/taskX.c
lab46:~/src/cprog$ 

We can then hop over to our submit directory and add/commit it:

lab46:~/src/cprog$ cd ~/src/submit
lab46:~/src/submit$ ls
contact.info    taskU.c    taskV.c    taskW.c    taskX.c
lab46:~/src/submit$ svn add taskX.c
Added   taskX.c
lab46:~/src/submit$ svn commit -m "added taskX.c to the submit directory"
...

Submission

All questions in this assignment require an action or response. Please organize your responses into an easily readable format and submit the final results to your instructor per the appropriate methods.

Your assignment is expected to be performed and submitted in a clear and organized fashion- messy or unorganized assignments may have points deducted. Be sure to adhere to the submission policy.

When complete, questions requiring a response can be electronically submit using the following form:

<html><center><a href=“http://lab46.corning-cc.edu/haas/content/cprog/submit.php?task2”>http://lab46.corning-cc.edu/haas/content/cprog/submit.php?task2</a></center></html>

Additionally, the successful results of the following actions will be considered for evaluation:

  • placement of a functional task2-average.c in your ~/src/submit directory
  • placement of a functional task2-slope.c in your ~/src/submit directory
  • addition/commit of task2-average.c and task2-slope.c into your repository

As always, the class mailing list and class IRC channel are available for assistance, but not answers.

haas/spring2011/cprog/tasks/task2.txt · Last modified: 2011/02/07 01:46 by 127.0.0.1