KOCAELI UNIVERSITY
Department of Electronics & Telecommunications Engineering
MEH 112 Computer Programming: Spring 2014 Final Exam
10/06/2014
Name and Surname : CEVAP ANAHTARI 1 2 3 4 5 Total
Student Number : ……………………
Signature : ……………………
IMPORTANT NOTES:
1) Only the sentences written in English will be graded. Thus, do not write your answers in any other
language.
2) This is a 90 minutes closed books/notes exam.
QUESTIONS
Question-1 Write down the output of the following programs.
#include <stdio.h> void foo(char *src, char *dest) (10)
void foo(char*, char*); {
while(*src!= '\0')
abcdef
int main() {
{
char *string1, *string2; if(!(*src >= '0' && *src <='9'))
string1="ab12cd34ef23"; {
foo(string1, string2); *dest = *src;
printf("%s", string2); dest++;
printf("\n"); }
} src++;
}
*dest = '\0';
}
#include<stdio.h> int v=(x << 3); (5) 16
int main( void ) printf( "%d\n", p ); 25
{ printf( "%d\n", r );
9
int x=17; printf( "%d\n", s );
int y=24; printf( "%d\n", t ); 6
int p=(x & y); printf( "%d\n", v ); 136
int r=(x | y); return 0;
int s=(x ^ y); }
int t=(y >> 2);
#include<stdio.h> main() (10)
int x; {
int k;
5 7 7 3
int fn() k = x = 3;
{ printf("%d ", fn());
int *p; printf("%d ", fn());
p = &x; printf("%d %d", x, k);
*p = x+2; }
return(*p);
}
Question-2: Suppose we have a data file that stores students’ IDs and 4 hw grades. So we have 5
columns in each row.
a) Define a structure to store the students’ IDs and 4 hw grades (5p)
b) Write a program to divide the students’ records into two groups based on their average hw grade. (25p)
-If a student’s hw average is equal or greater than 30, then we will print his/her ID and "PASS" into
pass.txt file;
-Otherwise, we will print his/her ID and "FAIL" into fail.txt file;
For instance, your program should process the following input.txt and generate the corresponding
pass.txt and fail.txt files:
#include<stdio.h>
struct st {
int id;
int hw[4]; //5puan
} st_hw;
main()
{
int id , hw1 , hw2 , hw3 , hw4;
FILE *dosya1;
FILE *dosya2;
FILE *dosya3;
dosya1 = fopen( "input.txt" , "r" ); //5puan
dosya2 = fopen( "pass.txt" , "w" );
dosya3 = fopen( "fail.txt" , "w" );
while ( !feof( dosya1 ) )
{
fscanf(dosya1,"%d%d%d%d%d",&st_hw.id,&st_hw.hw[0],&st_hw.hw[1],&st_hw.hw[2],&st_hw.hw[3]);
//5 puan
printf( "%d %d %d %d %d\n" , st_hw. id, st_hw.hw[0], st_hw. hw[1], st_hw. hw[2], st_hw.hw[3] );
if ( ( ( st_hw.hw[0] + st_hw.hw[1] + st_hw.hw[2] + st_hw.hw[3] ) /4 ) >= 30 ) //15puan
fprintf( dosya2,"%d PASS\n" , st_hw.id );
else
fprintf( dosya3,"%d FAIL\n" , st_hw.id );
}
return 0;
}
Question-3 Write a recursive function that takes two inputs “base” and “exponent” and returns the value
of “baseexponent”. (Warning: Answers without recursion will not be graded!) (10p)
int power (int base, int exponent)
{
if (exponent < 0){
printf( "Negative exponent!\n" );
return -1;
}
if (exponent == 0)
return 1;
else
return base * power(base, exponent - 1);
Question-4: Write a function that takes two two-dimensional (2D) arrays as inputs and computes their
matrix multiplication. (Warning: Answers without a function will not be graded!) (20p)
void matrixmultiplication(int array1[SIZE1][SIZE2], int array2[SIZE3][SIZE4], int
array3[SIZE1][SIZE4])
{
int sum, i, j, k;
if (SIZE2 != SIZE3)
printf("Logic error!\n");
else {
for (i = 0; i < SIZE1; i++) {
for (k = 0; k < SIZE4; k++) {
sum = 0;
for (j = 0; j < SIZE2; j++) {
sum = sum + array1[i][j] * array2[j][k];
}
array3[i][k] = sum;
}
}
}
}
Question-5: Write a C program which takes two distances (in meters and centimeters) as inputs from the
user and adds them up in a function. To solve this program, make a structure. Pass two structure
variables (containing distance in meters and centimeters) to the “add” function (prototype given below)
and display the result in main function. (Warning: Answers which do not utilize the given function
prototype will not be graded!) (20p)
void add(struct distance d1,struct distance d2, struct distance *d3);
# include <stdio.h>
struct distance {
int meters;
int centimeters;
};
void add(struct distance d1,struct distance d2, struct distance *d3);
int main()
{
struct distance d1, d2, d3;
printf( "Enter meters for first distance\n" );
scanf( "%d" , &d1.meters );
printf( "Enter centimeters for first distance:\n" );
scanf( "%d" , &d1.centimeters );
printf( "Enter meters for first distance\n" );
scanf( "%d" , &d2.meters );
printf( "Enter centimeters for first distance:\n" );
scanf( "%d" , &d2.centimeters );
add( d1 , d2 , &d3 );
printf( "Summed distance is %d meters ", d3.meters);
printf( "and %d centimeters", d3.centimeters);
return 0;
}
void add(struct distance d1,struct distance d2, struct distance *d3)
{
d3->centimeters = ( d1.centimeters + d2.centimeters ) % 100;
d3->meters = d1.meters + d2.meters ;
d3->meters = d3->meters + (d1.centimeters + d2.centimeters) / 100;
}
Good luck.
Asst. Prof. Dr. Aysun TAŞYAPI ÇELEBİ
Asst. Prof. Dr. Alp ERTÜRK