Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Exam (elaborations)

C Programming MIT Problem SET3

Rating
-
Sold
-
Pages
7
Grade
A
Uploaded on
19-10-2023
Written in
2023/2024

"C Programming MIT Problem Set 3" is a valuable educational resource designed for individuals interested in mastering the intricacies of the C programming language, particularly from a problem-solving perspective. Developed by the Massachusetts Institute of Technology (MIT), this problem set offers a series of challenging exercises and tasks aimed at enhancing one's understanding of C programming. Whether you are a student looking to strengthen your programming skills or an enthusiast interested in solving complex problems, this resource provides an excellent platform for honing your C programming abilities. By engaging with these MIT problem sets, you can sharpen your problem-solving skills, gain a deeper understanding of C, and broaden your horizons in the world of computer science.

Show more Read less
Institution
Course

Content preview

Massachusetts Institute of Technology
Department of Electrical Engineering and Computer Science

6.087: Practical Programming in C

IAP 2010

Problem Set 3
Control flow. Functions. Variable scope. Static and global variables. I/O: printf and scanf. File
I/O. Character arrays. Error handling. Labels and goto.
Out: Wednesday, January 13, 2010. Due: Friday, January 15, 2010.

Problem 3.1
Code profiling and registers. In this problem, we will use some basic code profiling to examine
the effects of explicitly declaring variables as registers. Consider the fibonacci sequence generating
function fibonacci in prob1.c, which is reproduced at the end of this problem set (and can be
downloaded from Stellar). The main() function handles the code profiling, calling fibonacci()
many times and measuring the average processor time.
(a) First, to get a baseline (without any explicitly declared registers), compile and run prob1.c.
Code profiling is one of the rare cases where using a debugger like gdb is discouraged, because
the debugger’s overhead can impact the execution time. Also, we want to turn off compiler
optimization. Please use the following commands to compile and run the program:
dweller@dwellerpc:~$ gcc -O0 -Wall prob1.c -o prob1.o
dweller@dwellerpc:~$ ./prob1.o
Avg. execution time: 0.000109 msec ← example output
dweller@dwellerpc:~$
How long does a single iteration take to execute (on average)?
(b) Now, modify the fibonacci() function by making the variables a, b, and c register variables.
Recompile and run the code. How long does a single iteration take now, on average? Turn
in a printout of your modified code (the fibonacci() function itself would suffice).
(c) Modify the fibonacci() function one more time by making the variable n also a register
variable. Recompile and run the code once more. How long does a single iteration take with
all four variables as register variables?
(d) Comment on your observed results. What can you conclude about using registers in your
code?

Problem 3.2
We are writing a simple searchable dictionary using modular programming. First, the program
reads a file containing words and their definitions into an easily searchable data structure. Then,
the user can type a word, and the program will search the dictionary, and assuming the word is
found, outputs the definition. The program proceeds until the user chooses to quit.

We split the code into several files: main.c, dict.c, and dict.h. The contents of these files are
described briefly below.


1

, main.c: dict.c: dict.h:
#include <stdio.h> #include "dict.h" /* data structure
#include <stdlib.h> for the dictionary */
#include "dict.h" /* data structure char * the dictionary[1000];
for the dictionary */
int main() { char * the dictionary[1000]; /* declarations */
... void load dictionary();
} void load dictionary() {
char * lookup(char []);
...
}

char * lookup(char []) {
...
}


Answer the following questions based on the above program structure.

(a) In implementing this program, you want to access the global variable the dictionary from
main.c, as well as from dict.c. However, due to the header file’s inclusion in both source
documents, the variable gets declared in both places, creating an ambiguity. How would you
resolve this ambiguity?

(b) Now, suppose you want to restrict the dictionary data structure to be accessible only from
functions in dict.c. You remove the declaration from dict.h. Is it still possible to directly
access or modify the variable from main.c, even without the declaration in dict.h? If so,
how would you ensure the data structure variable remains private?

(c) Congratulations! You’re done and ready to compile your code. Write the command line
that you should use to compile this code (using gcc). Let’s call the desired output program
dictionary.o.

Problem 3.3
Both the for loop and the do-while loop can be transformed into a simple while loop. For each
of the following examples, write equivalent code using a while loop instead.

(a) i n t f a c t o r i a l ( i n t n ) {
int i , r e t = 1 ;
f o r ( i = 2 ; i <= n ; i ++)
r e t ∗= i ;
return r e t ;
}


(b) #include < s t d l i b . h>

double r a n d d o u b l e ( ) {
/∗ g e n e r a t e random number i n [ 0 , 1 ) ∗/
double r e t = ( double ) rand ( ) ;
return r e t / (RAND MAX+1);
}

i n t s a m p l e g e o m e t r i c r v ( double p ) {


2

Written for

Course

Document information

Uploaded on
October 19, 2023
Number of pages
7
Written in
2023/2024
Type
Exam (elaborations)
Contains
Only questions

Subjects

$99.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
shubhjaiswal

Get to know the seller

Seller avatar
shubhjaiswal Teachme2-tutor
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
48
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions