0% found this document useful (0 votes)
45 views

Name: ID: Bil-200 Final

The document contains instructions for 5 programming problems: 1) Write a recursive function called "gibonacci" that returns the nth number in the Gibonacci sequence where each number is the sum of the previous 3 numbers. 2) Analyze a C code sample and identify at least 5 errors. 3) Write a function that takes a file name, opens the file, and returns the number of lines in the file. 4) Analyze a C program and determine its output. The program takes a string, splits it into individual strings, and prints each substring. 5) Write code in a main function that allocates memory for a user-specified number of "family" structures. It prompts

Uploaded by

Mustafa Yılmaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Name: ID: Bil-200 Final

The document contains instructions for 5 programming problems: 1) Write a recursive function called "gibonacci" that returns the nth number in the Gibonacci sequence where each number is the sum of the previous 3 numbers. 2) Analyze a C code sample and identify at least 5 errors. 3) Write a function that takes a file name, opens the file, and returns the number of lines in the file. 4) Analyze a C program and determine its output. The program takes a string, splits it into individual strings, and prints each substring. 5) Write code in a main function that allocates memory for a user-specified number of "family" structures. It prompts

Uploaded by

Mustafa Yılmaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Name:

ID:
BIL-200 FINAL (120 min.)
1) (20) In class, we have seen an example about the Fibonacci sequence, in which the first two
numbers are defined to be 1, and each additional number is defined to be the sum of the two
previous numbers. The first several numbers of the Fibonacci sequence are therefore:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
Now we are going to make up the Gibonacci sequence, in which the first three numbers are
defined to be 1, and each additional number is defined to be the sum of the three previous
numbers. The first several numbers of the Gibonacci sequence are therefore:
1, 1, 1, 3, 5, 9, 17, 31, 57, 105, ...
Write a function called gibonacci which accepts, as a parameter, an integer n (assumed
to be positive) and returns the nth Gibonacci number.
HINT: Use recursion.

2) (15) The following code has at least 5 errors. Determine and indicate these errors:
#include <stdio.h>
int main(void)
{
FILE *fpOut;
fpOut = fopen(output.txt, r);
if (fpOut = NULL)
{
printf(Could not create output.txt!\n);
return 1;
}
c = getchar();
while (c != NULL)
{
putchar(c, fpOut);
c = getchar();
}
fclose(fpOut);
return 0;
}

3) (20) Write a function that takes the name of a text file as its input argument, opens the
file for reading, determines and returns the number of lines (separated by <enter>s) in that
text file.

4) (20) Analyze and determine the output of the following program:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char **f(char *str, int *len)
{
char **ret;
int i;
*len=strlen(str);
ret=(char **)malloc(*len*sizeof(char *));
for(i=0;i<*len;i++)
ret[i]=(char *)malloc(*len*sizeof(char));
for(i=0;i<*len;i++)
strcpy(ret[i],str+i);
return ret;
}
void main(void)
{
char str[10]="merhaba", **strs;
int i=0, len;
strs=f(str,&len);
while(i<len)
puts(strs[i++]);
}

5) (25) Assume that you have the following structs:


struct person {
int age;
char name[100];
};
struct family {
int size,
struct person *member;
char address[256];
}

Using this type, you define the following pointer variable inside your main program:
void main(void){
struct family *families;
int i,....
....
....

Continue the main program by writing a code which:

asks the user how many families he/she would enter (say N),
allocates memory for N families, then
one by one asks N family entries from the user by,
a. first asking how many member persons are there in the family, then
b. allocating the member field of that family according to this number, then
c. asking the age and name of each member, and
d. asking the common address of the family,
and finally displays the whole N families including their members.

You might also like