0% found this document useful (0 votes)
6 views4 pages

PSCP History in Computer Programming Exam

The document is an exam paper for the MEH 112 Computer Programming course at Kocaeli University, dated Spring 2014. It includes various programming questions related to C language, such as output prediction for given code snippets, data structures for student grades, recursive functions, matrix multiplication, and distance addition using structures. The exam is closed book, lasts 90 minutes, and emphasizes the importance of writing answers in English.

Uploaded by

b.kircali55
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

PSCP History in Computer Programming Exam

The document is an exam paper for the MEH 112 Computer Programming course at Kocaeli University, dated Spring 2014. It includes various programming questions related to C language, such as output prediction for given code snippets, data structures for student grades, recursive functions, matrix multiplication, and distance addition using structures. The exam is closed book, lasts 90 minutes, and emphasizes the importance of writing answers in English.

Uploaded by

b.kircali55
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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
[Link] file;
-Otherwise, we will print his/her ID and "FAIL" into [Link] file;
For instance, your program should process the following [Link] and generate the corresponding
[Link] and [Link] 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( "[Link]" , "r" ); //5puan


dosya2 = fopen( "[Link]" , "w" );
dosya3 = fopen( "[Link]" , "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" , &[Link] );
printf( "Enter centimeters for first distance:\n" );
scanf( "%d" , &[Link] );
printf( "Enter meters for first distance\n" );
scanf( "%d" , &[Link] );
printf( "Enter centimeters for first distance:\n" );
scanf( "%d" , &[Link] );

add( d1 , d2 , &d3 );

printf( "Summed distance is %d meters ", [Link]);


printf( "and %d centimeters", [Link]);

return 0;
}

void add(struct distance d1,struct distance d2, struct distance *d3)


{
d3->centimeters = ( [Link] + [Link] ) % 100;
d3->meters = [Link] + [Link] ;
d3->meters = d3->meters + ([Link] + [Link]) / 100;
}

Good luck.
Asst. Prof. Dr. Aysun TAŞYAPI ÇELEBİ
Asst. Prof. Dr. Alp ERTÜRK

Common questions

Powered by AI

The function 'foo' iterates over the source string 'src', copies only the non-numeric characters to 'dest', and appends a null terminator at the end. As a result, 'dest' ends up containing only the alphabetic characters from the input string "ab12cd34ef23", which are "abcdef" .

The outputs of the bitwise operations are as follows: (1) 'p = (x & y)' evaluates to 16 because 17 & 24 results in 00010000 in binary, (2) 'r = (x | y)' evaluates to 25 because 17 | 24 results in 00011001, (3) 's = (x ^ y)' evaluates to 9 because 17 ^ 24 results in 00001001, (4) 't = (y >> 2)' evaluates to 6 because shifting 24 right by two bits results in 00000110, (5) 'v = (x << 3)' evaluates to 136 because shifting 17 left by three bits results in 10001000 .

The function 'matrixmultiplication' takes two matrices as inputs, along with a result matrix. It first checks if the number of columns in the first matrix equals the number of rows in the second matrix. If this condition is satisfied, it proceeds with the multiplication. It iterates over each element in the result matrix, calculating it by summing the products of corresponding elements from the rows of the first matrix and the columns of the second matrix .

The addition function calculates the total centimeters and uses the modulus operator to compute the remainder when dividing by 100, assigning this value to the centimeters of the resulting distance. Simultaneously, it divides the total centimeters by 100 to find how many full meters result, adding this value to the existing total of meters. This ensures proper conversion and accumulation of measurements .

The recursive function 'power' first checks if the exponent is negative, in which case it prints a warning and returns -1. If the exponent is zero, it returns 1 as any number to the power of zero is 1. Otherwise, it returns the base multiplied by the function itself, decreasing the exponent by one each time, effectively calculating base^exponent through repeated multiplication .

The logical error checked is whether the number of columns in the first matrix equals the number of rows in the second matrix. This condition is necessary for matrix multiplication to be valid. If this condition is not met, the function prints "Logic error!" and does not attempt to perform the multiplication, thereby preventing incorrect calculations or access violations .

The program opens three files: 'input.txt' for reading, 'pass.txt' for writing students who pass, and 'fail.txt' for those who fail. It reads student records from 'input.txt', calculating each student's average homework grade. If the average is 30 or greater, it writes the student ID and "PASS" to 'pass.txt'. Otherwise, it writes the student ID and "FAIL" to 'fail.txt'. This process utilizes `fscanf` for reading and `fprintf` for writing to files .

The average grade is calculated by summing the four homework grades and dividing by four. The program checks if this average is equal to or greater than 30. If so, the student's ID is written to 'pass.txt' with the word "PASS". Otherwise, it is written to 'fail.txt' with "FAIL". This division into pass and fail groups is based purely on average grade comparison .

The significance of checking for a negative exponent in the 'power' function is to ensure that the code handles invalid input cases gracefully. In this function, when the exponent is negative, the function prints a warning message and returns -1, as calculating powers with negative exponents isn't implemented. This prevents undefined behavior in operations that the function is not designed to handle .

The program defines a structure 'distance' with two fields: 'meters' and 'centimeters'. The 'add' function takes two 'distance' structures as input and a pointer to a 'distance' structure for output. It calculates the total centimeters and converts any overflow (every 100 centimeters makes 1 meter), then updates the meters and centimeters of the result structure accordingly. The final distance result is displayed in the main function .

You might also like