Lab Exercise: Writing Code and Formatting Output in C, Lab Reports of Computer Science

A lab exercise for cps 196, a computer science course, from the fall 2007 semester. The lab focuses on writing small sections of code by hand, formatting output using printf, and using functions from standard libraries. The exercise includes various tasks such as writing if statements for tax calculation based on salary, determining if a year is a leap year, and formatting output with printf. The document also includes instructions for using visual studio for coding and debugging.

Typology: Lab Reports

Pre 2010

Uploaded on 09/17/2009

koofers-user-n20
koofers-user-n20 🇺🇸

9 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPS 196 Lab 9 Fall 2007
Name: ________________________________________________
1. Writing small sections of code by hand. In the following problem descriptions, assume that
there are variables declared as:
double salary, taxes;
double lowrate, medrate, highrate;
and assume that they have been given values in the early part of a program.
A. In the space provided, write an if statement such that if the salary is less than 20000. , assign the
taxes to be lowrate times the salary, but otherwise assign the taxes to be the highrate times the
salary.
You do not have to run this program, but if you did, what values of salary would you use in the
testing? Evaluate your statement in your head to see if it works.
B. In the space provided, write an if statement such that if the salary is less than 20000., assign the
taxes to be the lowrate times the salary, but if it is between 20000. and 40000., assign the taxes to be
the medrate times the salary, and if it is higher than 40000., assign the taxes to be the highrate times
the salary.
You do not have to run this program, but if you did, what values of salary would you use in the
testing? Evaluate your statement in your head to see if it works.
pf3
pf4
pf5

Partial preview of the text

Download Lab Exercise: Writing Code and Formatting Output in C and more Lab Reports Computer Science in PDF only on Docsity!

CPS 196 Lab 9 Fall 2007

Name: ________________________________________________

1. Writing small sections of code by hand. In the following problem descriptions, assume that

there are variables declared as:

double salary, taxes;

double lowrate, medrate, highrate;

and assume that they have been given values in the early part of a program.

A. In the space provided, write an if statement such that if the salary is less than 20000. , assign the

taxes to be lowrate times the salary, but otherwise assign the taxes to be the highrate times the

salary.

You do not have to run this program, but if you did, what values of salary would you use in the

testing? Evaluate your statement in your head to see if it works.

B. In the space provided, write an if statement such that if the salary is less than 20000., assign the

taxes to be the lowrate times the salary, but if it is between 20000. and 40000., assign the taxes to be

the medrate times the salary, and if it is higher than 40000., assign the taxes to be the highrate times

the salary.

You do not have to run this program, but if you did, what values of salary would you use in the

testing? Evaluate your statement in your head to see if it works.

2. Writing small sections of code by hand. In the next problem description, assume that this

variable is declared:

int year;

and has been suitably initialized.

A. In the space provided, write an if statement that decides if the year is a leap year or not by the

following algorithm (abbreviated from the real life algorithm): If the year is evenly divisible by 4,

then it is a leap year, unless it is also evenly divisible by 100, then it is not a leap year. The if

statement should print a message that either says the year is a leap year, or says it is not a leap year.

You do not have to run this program, but if you did, what values of year would you use in the

testing? Evaluate your statement in your head to see if it works.

4. Using functions from standard libraries. Make a new program in Visual Studio and copy/paste

this program there.

/functions.c Using some built in functions. Using the locals window to observe values. / #include<stdio.h> /scanf, printf/ #include<stdlib.h> /STANDARD LIBRARY abs(int)/ #include<math.h> /MATH FUNCTIONS: ceil(double), cos(double), exp(double), fabs(double), floor(double), log(double), log10(double), pow(double,double), sin(double), sqrt(double), tan(double), and more / int main() { /declare variables/ int m = 4, n = -3, k = 0; double pi = 3.14159, e = 2.71828, two = 2.0, x; double z;

/absolute value for integers and for doubles 2/ k=abs(m); k=abs(n); z=fabs(pi); z=fabs(pi-10.0);

/ceiling and floor return the integer part of the number (as a double)/ z=ceil(pi); z=ceil(e); z=floor(pi); z=floor(e);

/* functions to computer power (exponent) and squareroot/ z=twotwo*two; z=pow(two,3.0); z=pow(two,-3.0); z=sqrt(4.0); z=sqrt(two);

return 0;

Using Debug Step Into | Step Over, step through this program and write down the value of either the

variable k or the variable z for each statement in the program, depending on which one is assigned

to.

5. Working with a nested for loop. Suppose that we want to print a design that has an empty box

with a border around it. It should look like this:

______

______

The program should ask the user how many spaces should be left in the interior of the box – the

example above has 4 horizontal space and 4 vertical spaces (lines) in the interior. We will discuss

this program to print such boxes:

/* printbox.c / / Given a size from the user, print a box with that many spaces in

  • each direction (left to right), (top to bottom) in the interior of the box.
  • Use underbar for the top and bottom borders of the box.
  • Use vertical bar for the left and right borders of the box. / / gives examples of output formats / #include<stdio.h> int main() { int i,j; / loop index variables / int size; / input: the size of the interior of the box */

/* Get the input size from the user */ printf("Type in the size of the interior of the box in each dimension> \n"); scanf("%i", &size); printf("\n");

/* print the top border with a loop of size + 2 underbars / for (j = 1; j <= (size+2); j++) { printf("_"); } / go to next line */ printf("\n");

/* print the interior lines of the box / / first loop over the number of lines, which is size / for (i = 1; i <= size; i++) { / print the left border character / printf("|"); / now loop over the size of the interior, printing spaces / for (j = 1; j <= size; j++) { printf(" "); } / print the right border character and go to the next line */ printf("|\n"); }

/* print the bottom border with a loop of size + 2 underbars */