Problem 1: Functions (20 points)
1.1 What is being printed by the following program (5 points)?

include

void fun1(int x, int *y)
{
x = x+(*y);
*y = 5*(*y);
x = (*y)*x;
}

int main(void)
{
int x = 2, y = 8;
fun1(x, y);
printf("%d\t%d\n", x, y);
return (0);
}



1.2 Write a C function called fun that computes the velocity and the displacement of a moving vehicle, given its initial velocity and acceleration. Use the following kinematic equations
a. v = v_0 + a * t.
b. d = v_0 * t + ½ a t2

Your function is called by the main as follows: (5 points).

int main(void)
{
float v_0, a, d, t, v;
printf("Enter the vehicle's initial velocity:");
scanf("%f", &v_0);
printf("Enter the vehicle's acceleration:");
scanf("%f", &a);
fun(10, a, v_0, &d, &v);
printf("The final velocity is %.1f.\nThe displacement is:%.1f\n",
v, d);
return (0);
}







Problem 2: Pointers (15 points)
2.1 What is the output of the following block of code (5 points)?
float var1, var2, *pt1;
float data[] = {10, 9, 8, 7, 5, 4, 2, 1, 0, 10};

var1 = 3.5;
pt1 = &var1;
data[7] = *pt1;
pt1 = &data[2];
printf(“%.2f, %.2f%, %d\n”, data[7], (*pt1 – data[4]), (&data[8]–pt1));




2.2 What is being returned by the following function (10 points)?
int *array_min (int *a, int n)
{ int *i, *r=a;

for ( i = a; i < a + n; i++)
{
if ( *i < *r)
r = i ;
}
return r;
}
when
(a) a = [2 7 9 4 5 8 6] and n = 7
(b) a = [5 9 -2 4 -6 10 7 0] and n = 8



Problem 3: Arrays (20 points)
3.1 Consider the following declarations:
#define SIZE 30

char a_list[SIZE], b_list[SIZE];
char a_char = 'v';
int nums[SIZE], vals[SIZE], i = 1;

For each statement below, answer TRUE if the statement is valid, and FALSE otherwise (5 points).
(a) a_list[30] = 'd';
(b) nums = vals + 1;
(c) a_list = b_list;
(d) for (i = 0; i <= SIZE - 1; ++i)
nums[SIZE - i] = i;
(e) nums[(a_char-‘a’)] = 10;


3.2 Write a function called median that receives as input an array of integers and the length of the array. The function returns the median value of the numbers contained within the array (15 points).

Recall that a median of a finite set of numbers is defined as the number for which half the numbers are larger and half the numbers are smaller. If the set has an even set of numbers, the median is defined as the mean of the two middle elements of the sorted list of the finite set of numbers.

Example: a = [4 5 2 7 9 3 6 8 12 19 1 14 16]  median = 7
a = [4 5 2 7 9 6 8 12 19 1 14 16]  median = (7+8)/2 = 7.5

Use the following declaration for your median function

float median(int *a, int len)

 

    • 10 years ago
    complete solution
    NOT RATED

    Purchase the answer to view it

    blurred-text
    • attachment
      c_functions.docx