LAB 01 QUESTIONS SOLUTION

$30.00 $24.00

Answer the questions below according to the lab specification. Write your answers directly in this text file and submit it to complete the lab. PROBLEM 1 `age.c’ ================= A ~ Examine the source code for `age.c’. Compile it using the following invocation of `gcc’ ,—- | gcc age.c `—- This should create the executable `a.out’.…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Description

5/5 – (2 votes)

Answer the questions below according to the lab specification. Write

your answers directly in this text file and submit it to complete the

lab.

PROBLEM 1 `age.c’

=================

A

~

Examine the source code for `age.c’. Compile it using the following

invocation of `gcc’

,—-

| gcc age.c

`—-

This should create the executable `a.out’. Run this program using the

invocation.

,—-

| ./a.out

`—-

Perform several runs of the program with the following values and

paste your output in the space below.

– Enter 16

– Enter 18

– Enter 25

– Enter 44

Enter your age in years:

16

You are 8409600 minutes old.

You’re too young to anything fun.

Enter your age in years:

18

You are 9460800 minutes old.

You can vote but cannot legally imbibe alcohol.

I bet that hasn’t stopped you from trying though…

Enter your age in years:

25

You are 13140000 minutes old.

You may imbibe alcohol but cannot be president

Enter your age in years:

44

You are 23126400 minutes old.

You may imbibe alcohol but cannot be president

B

~

Analyze the code for `age.c’ and describe a flaw in the conditional

structure towards the end that prevents the output:

,—-

| You can vote, drink, and be president.

| Try all three at once!

`—-

from being printed.

Alter the code to fix this bug so that for ages 35 and larger, the

above text is printed. Paste your fixed code for the conditional below

and test it by recompiling and showing a demo run.

else if( age_years >= 21 && age_years < 35 ){

printf(“You may imbibe alcohol but cannot be president\n”);

}

else { // does this print?

printf(“You can vote, drink, and be president.\nTry all three at once!\n”);

}

C

~

Attempt to enter some absurd ages for the age computation.

– Enter 5000

– Enter -5000

Describe anything strange that seems to be happening based your

understanding of how basic arithmetic is supposed to work.

We cannot be negative minutes old.

If you happen to know WHY this strangeness is happening, describe it

below. If not, you will find out soon.

+- 5000 * 525600 (minutes in a year) > maximum int range.

D

~

Describe which function is used to print information to the screen.

Describe how it seems to work to substitute values into output and

what *format specifier* indicates an integer should be substituted.

printf(). %d invokes an integer input into the print statement. format is as such:

printf(“a is: %d”,a)

E

~

Describe what function is used to read typed input interactively from

a user in the `age.c’ program. Describe anything that seems strange

about this function or its arguments.

We will learn in not long why this bit of strangeness is necessary.

scanf() is used. Its taking in the memory address of the input rather than than the input itself.

PROBLEM 2 Collatz

=================

A

~

Examine and compile the code associated with the collatz

program. There are three files associated with this program.

– `collatz_funcs.c’ which defines two utility functions for computing

the Collatz sequence

– `collatz_main.c’ which defines a `main()’ function to compute a

Collatz sequence

– `collatz.h’ header file which declares functions in

`collatz_funcs.c’ so that they are known to `collatz_main.c’

To compile the program, use the following invocation of `gcc’

,—-

| gcc -o collatz collatz_funcs.c collatz_main.c

`—-

This should create the program `collatz’ which can be run with

,—-

| ./collatz

`—-

Do so and enter inputs

– Starting integer 7

– Show output: 1

Paste the output below.

Enter the starting integer: 7

The next value in the Collatz sequence is 22

Show output of steps (0:NO, any other int: yes): 1

Step 0: 7

Step 1: 22

Step 2: 11

Step 3: 34

Step 4: 17

Step 5: 52

Step 6: 26

Step 7: 13

Step 8: 40

Step 9: 20

Step 10: 10

Step 11: 5

Step 12: 16

Step 13: 8

Step 14: 4

Step 15: 2

Step 16: 1

The starting value 7 converged to 1 in 16 steps

B

~

Determine what the “dash-O” option used above for `gcc -o’ does. For

example, what happens if one runs

,—-

| gcc -o GLIPGLOP collatz_funcs.c collatz_main.c

`—-

instead. You may wish to use the `ls’ command to list the files in

the current directory.

Describe what happens if you omit this option `-o’ when compiling as

in

,—-

| gcc collatz_funcs.c collatz_main.c

`—-

It names the file to run.

It is named ./a.out by default.

C

~

Attempt to compile only the file `collatz_main.c’ by doing

,—-

| gcc -o something collatz_main.c

`—-

This should result in an error. Show the output of that error and

determine why the compilation fails.

/tmp/cctiahZo.o: In function `main’:

collatz_main.c:(.text+0x4d): undefined reference to `collatz_next’

collatz_main.c:(.text+0x9e): undefined reference to `collatz_steps’

collect2: error: ld returned 1 exit status

Compilation failed because the other needed files were under different names.

D

~

Attempt to compile only the file `collatz_funcs.c’ by doing

,—-

| gcc -o something collatz_funcs.c

`—-

This should result in an error. Show the output of that error and

determine why the compilation fails.

/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start’:

(.text+0x20): undefined reference to `main’

collect2: error: ld returned 1 exit status

Compilation failed because the other needed files were under different names.

Review Course Syllabus

======================

Make sure to review the Course Syllabus to acquaint yourself with

course policies such as the following.

– The PRIME DIRECTIVE to preserve academic integrity

– Fair collaboration with other students

– Late submission policies on Assignments and Lab work

– Grading criteria and weighting on exams/assignments/labs.

LAB 01 QUESTIONS SOLUTION
$30.00 $24.00