Looping and Function
Name: ______________________________________student id: _______________________ Group:___________
1. Write a program that will produce the output as below.
A B C DEEDCBA
A B C DDCBA
A B C CBA
A B B A
A A
2. Write the prototype for below functions.
a) Function wage that receives three double-precision floating point arguments, x, y, z, and returns a double-precision
floating point result.
b) Function area that uses two integers, a, b, and returns an integer.
c) Function average that returns a double-precision floating point result and has an unchangeable array of integers, t and
an integer s.
d) Function address that has a pointer integer, add and returns no value
3. What does this program print?
int main()
{
int w = 7;
int x = 3;
w = good(w,x);
printf("%d %d", w, good(w,x));
}
int good(int a, int b)
{
int z;
z = a + b;
z /= 2;
return z;
}
4. Consider the program below.
#include <stdlib.h> a) Which line defines a global variable?
int x; b) Which lines define local variables named x?
int main(void) c) Which variable is changed by the assignment in line 14?
{ d) This program defines two variables with the same name whose
x = 4; scope doesn’t overlap. What are they?
int s = m(x); e) Trace the output for this program.
printf("the output is %d", s);
return 0;
}
int m(int x)
{
int s =0;
int i;
for ( i=0; i<x; i++)
{
int x = i +1;
s = s+x;
}
return x;
}
5. What will be the result of the call mystery(4, 5)?
int mystery (int t, int u)
{
double result = (t + u) / (u - t);
return result;
}
6. What will be the output of function call a = f(2). Show your solution using tracing table.
double f(double x) { return g(x) + sqrt(h( x ) ) }; }
double g (double x ) { return 4 * h(x) ; }
double h(double x) { return x * x + k(x) -1; }
double k( double x) { return 2 * ( x + 1 ); }
7. Write a program that calculates the increment and new salary of an employee based on the average of three evaluation
points. Use the following functions:
In the main function:
Using for loop to get three evaluation points from the user. In this loop, calculate the total of evaluation points.
Calculate the average of three evaluation points.
Call function get_increment (…) and pass the average evaluation points to get the increment percentage.
Call function get_new_salary(…) and pass the increment percentage and employee salary to get the new salary.
Display the increment percentage and new salary.
In function get_increment(….):
Identify and return the increment percentage based on the following table.
Average Increment percentage
2.00 – 2.99 2%
3.00 – 3.99 4%
4.00 – 5.00 6%
In function get_new_salary(..):
Calculate and return the new salary.
8. What are main’s x and y at /*values here*/ in the following version?
int main(void)
{
x = 10, y = 11;
silly(&x);
silly(&y); /*values here*/
. . . . .
}
void silly (int *x)
{
int y;
y = *x + 2;
*x = 2 * *x;
}