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 SET1

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

"C Programming MIT Problem Set 1" 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 6 – Solutions
Part 2: Function pointers, hash table

Out: Thursday, January 21, 2010. Due: Friday, January 22, 2010.


Problem 6.1
In this problem, we will use and create function that utilize function pointers. The file ’call­
back.c’ contains an array of records consisting of a fictitious class of celebrities. Each record consists
of the firstname, lastname and age of the student. Write code to do the following:

• Sort the records based on first name. To achieve this, you will be using the qsort() function
provided by the standard library: void qsort(void∗ arr,int num,int size,int (∗fp)(void∗ pa,void∗pb)).
The function takes a pointer to the start of the array ’arr’, the number of elements ’num’ and
size of each element. In addition it takes a function pointer ’fp’ that takes two arguments.
The function fp is used to compare two elements within the array. Similar to strcmp(), it
is required to return a negative quantity,zero or a positive quantity dependeing on whether
element pointed to by ’pa’ is ”less” than, equal to or ”greater” the element pointed to by
’pb’. You are required to write the appropriate callback function.

• Now sort the records based on last name. Write the appropriate callback function.

• The function void apply (...) iterates through the elements of the array calling a function for
each element of the array. Write a function isolder() that prints the record if the age of
the student is greater 20 and does nothing otherwise.




1

, Answer: Here’s one possible implementation:
#include <s t d i o . h>
#include <s t r i n g . h>
#include < s t d l i b . h>

#define MAX STUDENTS 10
struct s t u d e n t
{
char fname [ 1 0 0 ] ;
char lname [ 1 0 0 ] ;
int year ;
i n t age ;
};

struct s t u d e n t c l a s s [ ] = {
" Sean " , " Penn " , 2 , 2 1 ,
" Sean " , " Connery " , 4 , 2 5 ,
" Angelina " , " Jolie " , 3 , 2 2 ,
" Meryl " , " Streep " , 4 , 2 9 ,
" Robin " , " Williams " , 3 , 3 2 ,
" Bill " , " Gates " , 3 , 1 7 ,
" Jodie " , " Foster " , 4 , 2 5 ,
" John " , " Travolta " , 1 , 1 7 ,
" Isaac " , " Newton " , 2 , 1 9 ,
" Sarah " , " Palin " , 2 , 1 9
};


/∗
@function compare first name
@desc compares f i r s t name o f two r e c o r d s .
∗/
i n t c o m p a r e f i r s t n a m e ( const void ∗ a , const void ∗ b )
{
struct s t u d e n t ∗ s a =( struct s t u d e n t ∗ ) a ;
struct s t u d e n t ∗ sb=( struct s t u d e n t ∗ ) b ;
return strcmp ( sa−>fname , sb−>fname ) ;
}

/∗
@ f u n c t i o n compare lname name
@desc compares l a s t name o f two r e c o r d s .
∗/
i n t c o m p a r e l a s t n a m e ( const void ∗ a , const void ∗ b )
{
struct s t u d e n t ∗ s a =( struct s t u d e n t ∗ ) a ;
struct s t u d e n t ∗ sb=( struct s t u d e n t ∗ ) b ;
return strcmp ( sa−>lname , sb−>lname ) ;
}

/∗ !
@ f u n c t i o n app l y
@desc applies
∗/
void a p p l y ( struct s t u d e n t ∗ s a r r , i n t nrec , void ( ∗ f p ) ( void ∗ prec , void ∗ a r g ) , void ∗ a r g )
{
i n t i =0;
f o r ( i =0; i <n r e c ; i ++)



2

Written for

Course

Document information

Uploaded on
October 19, 2023
Number of pages
8
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