​Lab 2: Solving Simple Problems in C Solution

$30.00 $24.00

Objectives: Become familiar with writing simple C programs Solve problems involving simple arithmetic in C Work with C integer and floating-point math Introduce basic of C programming Starting Point: lab2_skel.c lab2_3.c lab2_5.c Turn-In: Submit a single PDF file on Canvas using the Lab 2 assignment link by the due date. Include all source code for…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Description

5/5 – (2 votes)

Objectives:

  • Become familiar with writing simple C programs

  • Solve problems involving simple arithmetic in C

  • Work with C integer and floating-point math

  • Introduce basic of C programming

Starting Point:

lab2_skel.c

lab2_3.c

lab2_5.c

Turn-In:

Submit a single PDF file on Canvas using the Lab 2 assignment link by the due date.

  1. Include all source code for this week’s lab in your lab report. Maintain formatting as best as you can. Include screenshots of code output in your lab report. Screenshots can be gathered using the snipping tool.

  1. Turn in a lab report describing your experiences with this lab. Follow the lab report formatand reference the sample lab report on BlackBoard.

Process:

Download the lab2_skel.c file. You will be using it as a template for the problems in this lab.

Problem 1: Creating Your Own Program

By now you should be familiar with the basics of the compiler as well as C program structure. Use Notepad++ to modify the lab2_skel.c file to display your full name as well as the course title and today’s date, each on a new line. Save it as lab2_1.c .

Be sure to include in your lab report a description of what problems you encountered in building your program.You will also need to capture the output of your programfor your lab report using Snipping Tool or screen shots.

Problem 2: A Simple Program with Input

Create another file, lab2_2_1.c, by copying your lab2_skel.c file and renaming it lab2_2_1.c.

This time, put the following code into your program:

Do not copy and paste this code, it will not compile correctly if you do. The quotations generally do not copy as unicode. You will have more luck and enjoy the lab more if you just retype the code.

int x, y;

printf(“Enter a width: ”);

scanf(“%d”,&x);

printf(“Enter a height: ”);

scanf(“%d”,&y);

printf(“A %d by %d rectangle’s area is %d\n”, x,y, x*y);

Compile, debug, and run lab2_2_1. Grab a screenshot.

Now, save lab2_2_1.c as lab2_2_2.c. Modify lab2_2_2.c so that it computes and prints the volume of an x by y by z rectangular prism (box) instead of a rectangle. You need to find the equation to calculate the volume on your own.

Run your program and test it on a 2 by 64 by 8 prism. Grab a screenshot.

Problem 3: Mysterious Output

  1. Download the example source code, lab2_3.c.

  1. Compile and run the program.

  1. Using the material that we reviewed in lecture, describe the reason behind each of the ‘incorrect’ program outputs. In each case, a unique error has been made when using the printf() display function. Pay close attention – these mistakes are often pitfalls in later labs!

For your lab report, show a corrected program and explain the erroneous output by placing comments wherever corrections were made.

Problem 4: Simple Arithmetic

  1. Write a program (lab2_4.c) to perform the following arithmetic operations and display the calculation and results to the screen. Pay attention to the sample

output format and sample code. All doubles must be displayed to two decimal places, and ints should be outputted as whole numbers. All calculations should be performed and assigned to an appropriate variable of the specified type. Do NOT alter the syntax of the equations.

  1. int a = 6427 + 1725

  1. int b = (6971 * 3925) – 95

  1. double c = 79 + 12 / 5

  1. double d = 3640.0 / 107.9

  1. int e = (22 / 3) * 3

  1. int f = 22 / (3 * 3)

  1. double g = 22 / (3 * 3)

  1. double h = 22 / 3* 3

  1. double i = (22.0 / 3) * 3.0

  1. int j = 22.0 / (3 * 3.0)

  1. double k = 22.0 / 3.0 * 3.0

Now develop equations to solve the following problems, put the code in lab2_4.c. Be sure to describe how you came up with the formula.

  1. Calculate the area of a circle with circumference 23.567

  1. Convert 14 feet to meters.

  1. Convert 76 degrees Fahrenheit to Centigrade.

(1 ft = 0.3048 m T= (T– 32) / 1.8)cF

Sample Program Output

1+2=3

45.00 + 2.00 = 47.00

1 ft = 0.30 m

Sample Code

int answer= 1+2 would be performed by the following piece of code:

int answer;

answer =1 +2;

  1. Compile and run your program.

  1. Review the output of your program paying particular attention to arithmetic problems (a.) through (k.).

For each case where the output differs from the normal arithmetic result, explain the C program output. Include the output and explanations in your lab report.

You do not need to correct the program.

  1. Capture the output into a text file by running the program using the redirection

operator you learned about in lab1 in the command line.

./lab2_3 > lab2_3_out.txt

Problem 5: Working with I/O – Pythagorean Theorem

You will create a program that finds the length of one side of a triangle using the pythagorean theorem. c2 = b2 + a2

  1. Download the lab2_5.c code, we have included a line of code to help you calculate the the square root of a value.

  1. Add two scanf statements, with accompanying printf statements, that prompt the user to enter a value for a and b.

  1. Using the scanned in values, calculate the value of c and print it out to the screen.

  1. Compile and test your program several times with different values for a and b.

  1. Run your program with a=5 and b=9, capture a screenshot of the output.

  2. Be sure to include your source code in your lab report.

Hints: Read your lab1 manual to see an example of how to use scanf() to scan value.

Note that the format specifier needs to be changed to %lf to work with a double.

Ending the Lab Session

  1. Leave your source code on your U: drive.

  1. Be sure to log off your workstation before leaving lab!

​Lab 2: Solving Simple Problems in C Solution
$30.00 $24.00