Computer Science II
Computer Science II
net/publication/349862592
Computer Science II
CITATIONS
0
1 author:
Ali al Khansa
Orange Labs
6 PUBLICATIONS 4 CITATIONS
SEE PROFILE
All content following this page was uploaded by Ali al Khansa on 07 March 2021.
0Ali
| PAl-Khansa
age 2021 Semester II
Table of Contents
Table of Contents .......................................................................................................................................... 1
Introduction .................................................................................................................................................. 2
Acknowledgements....................................................................................................................................... 2
Partial Exam 2020 Solutions.......................................................................................................................... 4
Partial Exam 2019 Solutions.......................................................................................................................... 4
Partial Exam 2018 Solutions........................................................................................................................ 10
Partial Exam 2017 Solutions........................................................................................................................ 13
Partial Exam 2016 Solutions........................................................................................................................ 16
Partial Exam 2015 Solutions........................................................................................................................ 20
Partial Exam 2014 Solutions........................................................................................................................ 23
Partial Exam 2013 Solutions........................................................................................................................ 26
Partial Exam 2012 Solutions........................................................................................................................ 30
Partial Exam 2011 Solutions........................................................................................................................ 35
Partial Exam 2010 Solutions........................................................................................................................ 39
Final Exam 2020 Solutions .......................................................................................................................... 42
Final Exam 2019 Solutions .......................................................................................................................... 46
Final Exam 2018 Solutions .......................................................................................................................... 49
Final Exam 2017 Solutions .......................................................................................................................... 53
Final Exam 2016 Solutions .......................................................................................................................... 57
Final Exam 2015 Solutions .......................................................................................................................... 62
Final Exam 2014 Solutions .......................................................................................................................... 66
Final Exam 2013 Solutions .......................................................................................................................... 70
Final Exam 2012 Solutions .......................................................................................................................... 75
Final Exam 2011 Solutions .......................................................................................................................... 80
Final Exam 2010 Solutions .......................................................................................................................... 85
Page 1 of 87 | A.KH
Introduction
This book includes the solution manual of Computer Science II -in the Lebanese university faculty of
engineering 3rd branch-. partial and final exams.
- In the solutions, the “int” type before the “main” function can be replaced with the “void” type.
- The “conio.h” library can be included in every program, and is necessary if you wanted to use the
getch() function.
- You are not responsible for the “fflush(stdin)” used before scanning a character or a string
- Anything after “//” is a remark and the compiler does not read it
- Anything between “/*” and “*/” is a remark and the compiler does not read it
- For any comment: Gmail: [email protected]
Acknowledgements
Many thanks go to my friend Mohammad Abbass for reviewing the solutions.
Page 2 of 87 | A.KH
Page 3 of 87 | A.KH
Partial Exam 2020 Solutions
NO Partial exam
Exercise 1
#include<stdio.h>
//part 1
float ABS(float X)
{
if (X < 0)
return -X;
return X;
}
//part2
Page 4 of 87 | A.KH
//part 3
float SQUAR_ROOT(float X)
{
float A=1, B;
do
{
B=A;
A = ((X/B)+B)/2;
}while(ABS(A-B)>0.00001);
return A;
}
//part 4
if (D<0)
return 0;
else
{
if(D == 0)
{
*X1 = *X2 = (-b)/(2*a);
return 1;
}
else
{
*X1 = (-b-SQUAR_ROOT(D))/(2*a);
*X2 = (-b+SQUAR_ROOT(D))/(2*a);
return 2;
}
}
}
}
Page 5 of 87 | A.KH
//part 5
int main()
{
float a,b,c,X1,X2;
int n;
do
{
puts("Please enter the coefficients of the quadratic equation");
scanf("%f%f%f",&a,&b,&c);
}while(a<=0);
n=QUAD(a,b,c,&X1,&X2);
printf("Number of roots=%d\n",n);
if(n==1)
printf("X=%f\n",X1);
if(n>1)
printf("X1=%f\tX2=%f\n",X1,X2);
}
Exercise 2
//part 1
typedef struct moment
{
int Hour, Minute, Second;
}moment;
//part 2
int DIFF_TIME(moment T1, moment T2)
{
int count=0;
if ( (T1.Hour > T2.Hour) || (T1.Hour == T2.Hour && T1.Minute > T2.Minute) || (T1.Hour ==
T2.Hour && T1.Minute == T2.Minute && T1.Second > T2.Second))
count = 3600*(T1.Hour - T2.Hour) + 60*(T1.Minute - T2.Minute) + T1.Second -
T2.Second;
else
count = 3600*(T2.Hour - T1.Hour) + 60*(T2.Minute - T1.Minute) + T2.Second -
T1.Second;
return count;
}
Page 6 of 87 | A.KH
Exercise 3
#include<stdio.h>
// Part 1
typedef struct assessment
{
int Group, Individual, Product;
float Grade1, Grade2, Grade3, Total;
}assessment;
// Part 2
void READ(assessment *A)
{
printf("Please enter the grades of product %d, group %d, and individual %d\n", A-
>Product,A->Group,A->Individual);
scanf("%f%f%f",&A->Grade1,&A->Grade2,&A->Grade3);
A->Total = (A->Grade1 + A->Grade2 + A->Grade3)/3;
}
// Part 3
// Note that following the parameters of the given questions, we have 120 assessements (4 * 6 * 5)
void FILL(assessment A[120])
{
int i,j,k;
for(i=0;i<4;i++) // loop of products
for(j=0;j<6;j++) // loop of groups
for(k=0;k<5;k++) // loop of individuals
{
A[i*30+j*5+k].Product=i+1; // we added 1 only to start from 1 and not from zero
A[i*30+j*5+k].Group=j+1;
A[i*30+j*5+k].Individual=k+1;
READ(&A[i*30+j*5+k]);
}
}
// Part 4
/* Note that we can use same assessment element, but it is more suitable to create a new structure
with only the needed elements */
Page 7 of 87 | A.KH
/* Remark: It seems to be more logical here to make a loop over all group integers, but following
the question, we took the group number within the parameters of the loop, and we will do the
loop in the main function. */
// part 5
/* Note that we can use same assessment element, but it is more suitable to create a new structure
with only the needed elements. */
Page 8 of 87 | A.KH
// part 6
int main()
{
int i,j, temp1;
float temp2;
assessment A[120];
assessment_2 B[24];
assessment_3 C[4];
FILL(A);
for(i=0;i<6;i++) // loop on all producnts
CALCULATE_GROUP(A,i+1,B);
CALCULATE_PRODUCT(B,C);
for(i=0;i<4;i++)
{
for(j=i+1;j<4;j++)
{
if(C[i].Total < C[j].Total)
{
temp2 = C[i].Total;
C[i].Total = C[j].Total;
C[j].Total = temp2;
temp1 = C[i].Product;
C[i].Product = C[j].Product;
C[j].Product = temp1;
}
}
}
for(i=0;i<4;i++)
printf("Product %d\t%f\n",C[i].Product,C[i].Total);
}
Page 9 of 87 | A.KH
Partial Exam 2018 Solutions
Exercise 1
5
13 14
13
Exercise 2
#include<stdio.h>
//part 1
//part 2
//part 3
int main()
{
double a,b;
puts("input two numbers");
scanf("%lf%lf",&a,&b);
printf("biggest integer quotient of (%lf,%lf)=%lf\n",a,b,FQUOT(a,b));
printf("remainder of the division of (%lf,%lf)=%lf\n",a,b,FMOD(a,b));
}
Exercise 3
#include<stdio.h>
//part 1
Page 10 of 87 | A.KH
{
char Name[50];
ADDRESS BA,SA;
}CLIENT;
//part 2
ADDRESS READ_ADDRESS()
{
ADDRESS A;
puts("input house number");
scanf("%d",&A.HN);
puts("input street name");
fflush(stdin);
gets(A.SN);
puts("input Zip code");
scanf("%d",&A.ZC);
puts("input country name");
fflush(stdin);
gets(A.Country);
return A;
}
//part 3
//part 4
Page 11 of 87 | A.KH
//part 5
printf("Name: ");
puts(C[i].Name);
//part 6
int main()
{
int i,ZC;
CLIENT C[50];
FILL_CLIENTS(C);
puts("input the Zip code");
scanf("%d",&ZC);
FIND_CLIENTS(ZC,C);
}
Page 12 of 87 | A.KH
Partial Exam 2017 Solutions
Exercise 1
//part 1
int ISXDIGIT(char c)
{
if((c>='0'&&c<='9')||(c>='A'&&c<='F'))
return 1;
return 0;
}
//part 2
int TOASCII(char c)
{
return c;
}
Exercise 2
//part 1
float ABS(float x)
{
if(x<0)return -x;
return x;
}
//part 2
float Cubic_Root(float A)
{
float B=1,C=1;
do
{
B=C;
C=B+(A/(B*B)-B)/3;
}while(ABS(C-B)>0.00001);
return C;
}
//part 3
int main()
{
float A;
puts("input a real number");
scanf("%f",&A);
printf("Cubic root of %f=%f\n",A,Cubic_Root(A));
Page 13 of 87 | A.KH
puts("input a real number");
scanf("%f",&A);
while(A!=0)
{
printf("Cubic root of %f=%f\n",A,Cubic_Root(A));
puts("input a real number");
scanf("%f",&A);
}
}
Exercise 3
#include<stdio.h>
//Note: The question can be solved in another way, if part 1(structure) is written in different way.
//part 1
typedef struct CHEF
{
int nb;
float grades[10][4],AVERAGES[10],FINAL_GRADE;
}CHEF;
//part 2
float AVERAGE(float a,float b,float c,float d)
{
return (a+b+c+d)/4;
}
//part 3
void READ_GRADES(int nb,int Jnb,CHEF C[])
{
puts("input grade for originality");
scanf("%f",&C[nb-1].grades[Jnb-1][0]);
puts("input grade for hygiene");
scanf("%f",&C[nb-1].grades[Jnb-1][1]);
puts("input grade for taste");
scanf("%f",&C[nb-1].grades[Jnb-1][2]);
puts("input grade for presentation");
scanf("%f",&C[nb-1].grades[Jnb-1][3]);
C[nb-1].AVERAGES[Jnb-1]=AVERAGE(C[nb-1].grades[Jnb-1][0],C[nb-1].grades[Jnb-
1][1],C[nb-1].grades[Jnb-1][2],C[nb-1].grades[Jnb-1][3]);
}
//part 4
void FILL(CHEF C[])
{
int i,j;
for(i=0;i<25;i++)
for(j=0;j<10;j++)
READ_GRADES(i+1,j+1,C);
}
Page 14 of 87 | A.KH
//part 5
float CALCULATE(float grades[][4],int nb,CHEF C[])
{
int i;
float SUM=0;
for(i=0;i<10;i++)
SUM=SUM+C[nb-1].AVERAGES[i];
return SUM;
}
//part 6
void FINAL(CHEF C[])
{
int i;
for(i=0;i<25;i++)
C[i].FINAL_GRADE=CALCULATE(C[i].grades,i+1,C);
}
//part 7
int main()
{
CHEF C[3];
int i,j,k,mx;
float max;
FILL(C);
FINAL(C);
mx=1;max=C[0].FINAL_GRADE;
for(i=1;i<3;i++)
{
if(C[i].FINAL_GRADE>max)
{
mx=i+1;
max=C[i].FINAL_GRADE;
}
}
printf("The number of the winning chef is:%d\n",mx);
}
Page 15 of 87 | A.KH
Partial Exam 2016 Solutions
Exercise 1
𝒊
Exercise 2
#include<stdio.h>
//part 1
float BMI(float m,float h)
{
return m/(h*h);
}
//part 2
int main()
{
float h,m;
puts("input mass and height");
scanf("%f%f",&m,&h);
while(h>0&&m>0)
{
printf("%f\n",BMI(m,h));
puts("input mass and height");
scanf("%f%f",&m,&h);
}
}
Exercise 3
#include<stdio.h>
//part 1
typedef struct element
{
int nb;
char d;
}element;
//part 2
void Convert(char traj[],element E[100])
{
int i=0,k=0;
while(traj[i]!='\0')
{
E[k].d=traj[i];
E[k].nb=1;
if(traj[i]==traj[i+1])
{
while(traj[i]==traj[i+1])
Page 16 of 87 | A.KH
{
E[k].nb++;
i++;
}
k++;
i++;
}
else
{
i++;
k++;
}
}
E[k].nb=-1;
E[k].d=traj[i];
}
//part 3
void print(element E[100])
{
int i=0;
while(E[i].nb!=-1)
{
printf("%d%c",E[i].nb,E[i].d);
i++;
}
printf("%d%c",E[i].nb,E[i].d);
}
int main()
{
char traj[100];
element E[100];
puts("fill the trajectory");
gets(traj);
Convert(traj,E);
print(E);
}
Exercise 4
#include<stdio.h>
//part 1
int ReadN()
{
int n;
do
{
puts("input an integer between 1 and 10");
scanf("%d",&n);
}while(n<1||n>10);
Page 17 of 87 | A.KH
return n;
}
//part 2
void Print(int A[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{printf("%d\t",A[i][j]);}
printf("\n");
}
}
//part 3
void NextCell(int A[10][10],int n,int *r,int *c,int *D)
{
if((*D)==1)
{
if((*c)<n-1&&A[*r][*c+1]==0)
{
(*c)++;
}
else
{
(*r)++;
(*D)=2;
}
}
else
{
if((*D)==2)
{
if((*r)<n-1&&A[*r+1][*c]==0)
{
(*r)++;
}
else
{
(*D)=3;
(*c)--;
}
}
else
{
if((*D)==3)
{
if((*c)!=0&&A[*r][*c-1]==0)
Page 18 of 87 | A.KH
{
(*c)--;
}
else
{
(*D)=4;
(*r)--;
}
}
else
{
if((*D)==4)
{
if(A[*r-1][*c]==0)
{
(*r)--;
}
else
{
(*D)=1;
(*c)++;
}
}
}
}
}
}
//part 4
int main()
{
int A[10][10],v,n,d,i=0,j=0;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
A[i][j]=0;
n=ReadN();
v=1;i=0;j=0;d=1;
for(v=1;v<=(n*n);v++)
{
A[i][j]=v;
NextCell(A,n,&i,&j,&d);
}
Print(A,n);
}
Page 19 of 87 | A.KH
Partial Exam 2015 Solutions
Exercise 1
//part 1
float Frac(float x)
{
return x-(int)x;
}
//part 2
int Int_Div(float x,float y)
{
return (int)x/y;
}
//part 3
/*
X/Y= (integer Part) + (Fractional Part)
X= (integer Part)*Y + (Fractional Part)*Y
X= Quotient * Y + Remainder
So Quotient= Integer part
Remiander= (Fractional Part)*Y
*/
float Int_Remainder(float x,float y)
{
return Frac(x/y)*y;
}
//part 4
void Fraction(float x,float y, int *q,float *r)
{
*q=Int_Div(x,y);
*r=Int_Remainder(x,y);
}
//part 5
float ABS(float x)
{
return(x<0)?-x:x;
}
//part 6
float SQ(float x)
{
float u=1,v;
do
{
v=u;
u=(x/v+v)/2;
}while(ABS(u-v)>0.00001);
Page 20 of 87 | A.KH
return u;
}
Exercise 2
#include<stdio.h>
#include<math.h>
//part 1
typedef struct point
{
char name;
float x,y;
}point;
//part 2
point find_point(point x[],int d,char n)
{
point p={' ',-1,-1};
int i;
for(i=0;i<d;i++)
if(x[i].name==n)
return x[i];
return p;
}
//part 3
int main()
{
point p[10],a,b;
int i;
float l,w;
for(i=0;i<10;i++)
{
puts("input name");
fflush(stdin);
scanf("%c",&p[i].name);
puts("coordinates");
fflush(stdin);
scanf("%f%f",&p[i].x,&p[i].y);
}
do
{
puts("input point 1");
fflush(stdin);
scanf("%c",&a.name);
a=find_point(p,10,a.name);
}while(a.name==' ');
do
{
puts("input point 2");
fflush(stdin);
Page 21 of 87 | A.KH
scanf("%c",&b.name);
b=find_point(p,10,b.name);
}while(b.name==' ');
l=fabs(b.x-a.x);w=fabs(b.y-a.y);
printf("perimeter=%f\n",2*l+2*w);
printf("area=%f\n",l*w);
}
Exercise 3
#include<stdio.h>
// part 1
typedef struct number
{
int v,r;
}number;
//part 2
number read_number()
{
number n;
puts("input an integer");
scanf("%d",&n.v);
n.r=1;
return n;
}
//part 3
void fill_array(number x[],int d)
{
int i;
for(i=0;i<d;i++)
x[i]=read_number();
}
//part 4
void rank_number(number x[],int d,number *n)
{
int i;
for(i=0;i<d;i++)
if(x[i].v<n->v)
n->r++;
}
//part 5
int main()
{
number a[10];int i;
fill_array(a,10);
for(i=0;i<10;i++)
rank_number(a,10,&a[i]);}
Page 22 of 87 | A.KH
Partial Exam 2014 Solutions
Exercise 1
//part 1
int Is_w(char x)
{
if(x==' '||x=='\t'||x=='\n')
return 1;else return 0;
}
//part 2
int Is_even(int x)
{
if(x%2==0)
return 1;else return 0;
//part 3
int Is_odd(int x)
{
if(Is_even(x)==0)
return 1;else return 0;
}
Exercise 2
#include<stdio.h>
#include<math.h>
//part 1
void read(float a[],int n)
{
int i;
for(i=0;i<n;i++)
{
puts("input a nb");
scanf("%f",&a[i]);
}
}
//part 2
float sum(float a[],int n)
{
int i;float s=0;
for(i=0;i<n;i++)
s+=a[i];
return s;
}
Page 23 of 87 | A.KH
//part 3
float average(float a[],int n)
{
return(sum(a,n)/n);
}
//part 4
void dif(float x[],int n,float y[])
{
int i;
for(i=0;i<n;i++)
y[i]=pow(x[i]-average(x,n),2);
}
//part 5
int main()
{
float a[15],b[15],v;
int i;
read(a,15);
dif(a,15,b);
for(i=0;i<15;i++)
v+=b[i];
v/=15;
printf("variance=%f\n",v);
}
Exercise 3
#include<stdio.h>
//part 1
typedef struct cont
{
int id,nbsent,nbrec;
char name[31],onl;
}cont;
//part 2
void new_cont(cont *c)
{
puts("input a number");
scanf("%d",&c->id);
puts("input name");
fflush(stdin);
gets(c->name);
c->onl='y';
c->nbsent=0;
c->nbrec=0;
}
Page 24 of 87 | A.KH
//part 3
void change(cont *c)
{
if(c->onl=='y') c->onl='n';
else c->onl='y';
}
//part 4
int online_array(cont a[],int n)
{
int i,s=0;
for(i=0;i<n;i++)
{
if(a[i].onl=='y')
s++;
}
return s;
}
//part 5
void blabla(cont a[],int n,int *idmax1,int *idmax2)
{
int i,max1,max2;
max1=a[0].nbsent;*idmax1=a[0].id;
for(i=1;i<n;i++)
if(a[i].nbsent>max1)
{
max1=a[i].nbsent;
*idmax1=a[i].id;
}
max2=a[0].nbrec;*idmax2=a[0].id;
for(i=1;i<n;i++)
if(a[i].nbrec>max2)
{
max1=a[i].nbrec;
*idmax2=a[i].id;
}
}
Page 25 of 87 | A.KH
Partial Exam 2013 Solutions
Exercise 1
//part 1
int Is_integer(float x)
{
int d;
d=(int)x;
if(d==x)
return 1;
else
return 0;
}
//part 2
void conc(char s[],char s1[],char s2[])
{
int i=0,j=0;
while(s1[i]!='\0')
{
s[i]=s1[i];
i++;
}
while(s2[j]!='\0')
{
s[i]=s2[j];
i++;j++;
}
s[i]='\0';
}
//part 3
int lcm(int x,int y)
{
int i;
for(i=1;;i++)
if(i%x==0&&i%y==0)
break;
return i;
}
//part 4
int gcd(int x,int y)
{
return((x*y)/lcm(x,y));
}
Exercise 2
#include<stdio.h>
Page 26 of 87 | A.KH
//part 1
void Read_Byte(int A[8])
{
int i;
for(i=7;i>=0;i--)
do{
puts("input a number");
scanf("%d",&A[i]);
}while(A[i]!=1&&A[i]!=0);
}
//part 2
int CTO1(int A[8])
{
int i;
for(i=0;i<8;i++)
{
if(A[i]==0)
A[i]=1;
else
A[i]=0;
}
}
//part 3
int DEC(int A[8])
{
int i,j=1,s=0;
if(A[0]==0)
{
for(i=7;i>=0;i--)
{
s+=(A[i]*j);
j=j*2;
}
}
else
{
CTO1(A);
for(i=7;i>=0;i--)
{
s+=(A[i]*j);
j=j*2;
}
s++;
s=-s;
}
return s;
}
Page 27 of 87 | A.KH
//part 4
int main()
{
int A[8],AC[8],i;
Read_Byte(A);
for(i=7;i>=0;i--)
AC[i]=A[i];
CTO1(A);
puts("real byte");
for(i=0;i<8;i++)
printf("%d",AC[i]);
printf("\n");
printf("decimal=%d\n",DEC(AC));
puts("ones complement");
for(i=0;i<8;i++)
printf("%d",A[i]);
}
Exercise 3
#include<stdio.h>
//part 1
typedef struct exp
{
double lo,ro,val;
char oper;
int err;
}exp;
//part 2
exp read_exp()
{
exp i;
puts("input left operand");
scanf("%lf",&i.lo);
do
{
puts("input operator");
fflush(stdin);
scanf("%c",&i.oper);
}while(i.oper!='*'&&i.oper!='/'&&i.oper!='+'&&i.oper!='-');
puts("input right operand");
scanf("%lf",&i.ro);
i.val=0;
i.err=-1;
return i;
}
Page 28 of 87 | A.KH
//part 3
void eval_exp(exp *i)
{
if(i->oper=='/')
{
if(i->ro==0)
{i->err=1;i->val=0;}
else
i->val=(i->lo/i->ro);
}
else
{
i->err=0;
if(i->oper=='*')
i->val=(i->lo*i->ro);
if(i->oper=='+')
i->val=(i->lo+i->ro);
if(i->oper=='-')
i->val=(i->lo-i->ro);
}
}
//part 4
void eval_array(exp a[10])
{
int i;
for(i=0;i<10;i++)
eval_exp(&a[i]);
}
//part 5
int main()
{
exp a[10];int i;
for(i=0;i<10;i++)
{
a[i]=read_exp();
}
eval_array(a);
for(i=0;i<10;i++)
{
if(a[i].err==1)
puts("error");
else
printf("%lf%c%lf=%lf\n",a[i].lo,a[i].oper,a[i].ro,a[i].val);
}
}
Page 29 of 87 | A.KH
Partial Exam 2012 Solutions
Exercise 1
//part 1
typedef struct cand
{
int id;
char fname[10],lname[10];
float judges[8],score;
}cand;
//part 2
void READ(cand C[10])
{
int i,j;
for(i=0;i<10;i++)
{
puts("input id");
scanf("%d",&C[i].id);
puts("input first name");
fflush(stdin);
gets(C[i].fname);
puts("input last name");
fflush(stdin);
gets(C[i].lname);
for(j=0;j<8;j++)
{
do
{
puts("input judge");
scanf("%f",&C[i].judges[j]);
}while(C[i].judges[j]<0||C[i].judges[j]>10);
}
}
}
//part 3
void MAXMIN(cand C,int *max,int *min)
{
int i;
float mx=C.judges[0],mn=C.judges[0];
*max=0;
*min=0;
for(i=1;i<8;i++)
{
if(C.judges[i]>mx)
{
Page 30 of 87 | A.KH
mx=C.judges[i];
*max=i;
}
if(C.judges[i]<mn)
{
mn=C.judges[i];
*min=i;
}
}
}
//part 4
void TOTALSCORE(cand C[10])
{
int max,min,i,j;
for(i=0;i<10;i++)
{
MAXMIN(C[i],&max,&min);
C[i].score=0;
for(j=0;j<8;j++)
{
if(j!=max&&j!=min)
C[i].score+=C[i].judges[j];
}
}
}
//part 5
int WINNER(cand C[10])
{
int i,win;
float max;
TOTALSCORE(C);
win=0;
max=C[0].score;
for(i=1;i<10;i++)
{
if(C[i].score>max)
{
max=C[i].score;
win=i;
}
}
return win;
}
Page 31 of 87 | A.KH
//part 6
void PRINT(cand C[10],int win)
{
int i,j;
for(i=0;i<10;i++)
{
If(i==win)
puts(“WINNER:”);
printf("%d\t",C[i].id);
puts(C[i].fname);
puts(C[i].lname);
for(j=0;j<8;j++)
printf("%f\t",C[i].judges[j]);
printf("final score=%f\n",C[i].score);
}
Exercise 2
#include<stdio.h>
//part 1
int READ()
{
int N;
do
{
puts("input N");
scanf("%d",&N);
}while(N<0||N%2==0||N>9);
return N;
}
//part 2
//lines and columns starts from zero till N-1(N terms)
void NEXTCELL_1(int N,int i,int j,int *r,int *c)
{
if(i==0)
*r=N-1;
else
*r=i-1;
if(j==N-1)
*c=0;
else
*c=j+1;
}
Page 32 of 87 | A.KH
//part 3
void NEXTCELL_2(int N,int i,int j,int *r,int *c)
{
if(i==N-1)
*r=0;
else
*r=i+1;
*c=j;
}
//part 4
void FILL(int A[9][9],int N)
{
int i,j,r,c,q;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
A[i][j]=0;
i=0;
j=(int)N/2;
A[i][j]=1;
for(q=2;q<=N*N;q++)
{
NEXTCELL_1(N,i,j,&r,&c);
if(A[r][c]==0)
{
A[r][c]=q;
i=r;
j=c;
}
else
{
NEXTCELL_2(N,i,j,&r,&c);
A[r][c]=q;
i=r;
j=c;
}
}
}
//part 5
void DISPLAY(int A[9][9],int N)
{
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
printf("%d\t",A[i][j]);
printf("\n");
}}
Page 33 of 87 | A.KH
//part 6
int main()
{
int N,A[9][9];//A[9][9] bcz N is maximum 9
N=READ();
FILL(A,N);
DISPLAY(A,N);
}
Page 34 of 87 | A.KH
Partial Exam 2011 Solutions
Exercise 1
void COUNT(char T[],int *U,int *l)
{
int i;
(*U)=0;
(*l)=0;
for(i=0;i<strlen(T);i++)
{
if(T[i]>='A'&&T[i]<='Z')
(*U)++;
if(T[i]>='a'&&T[i]<='z')
(*l)++;
}
}
Exercise 2
#include<stdio.h>
void INIT(float A[],int n)
{
int i;
for(i=0;i<n;i++)
{
if(i<n/2)
A[i]=i*i;
else
A[i]=3*i;
}
}
int main()
{
float A[50];
int i;
INIT(A,50);
//he didn't ask for printing the array..
for(i=0;i<50;i++)
printf("%f\n",A[i]);
}
Exercise 3
#include<stdio.h>
//part 1
int FIND(int A[10][2],int x)
{
int i;
for(i=0;i<10;i++)
Page 35 of 87 | A.KH
if(A[i][0]==x)
return i;
return -1;
}
//part 2
//this fnxn will return nb and will fill B
int FREQ(int A[10],int B[10][2])
{
int i,j;
int nb=10,pos;
//get nb
for(i=0;i<10;i++)
for(j=i+1;j<10;j++)
if(A[i]==A[j])
{
nb--;
break;
}
i=0;
for(j=0;j<10;j++)
{
pos=FIND(B,A[j]);
if(pos==-1)
{
B[i][0]=A[j];
B[i][1]=1;
i++;
}
else
B[pos][1]++;
}
return nb;
}
//part 3
int main()
{
int A[10],B[10][2],i,nb;
for(i=0;i<10;i++)
{
puts("input nb");
scanf("%d",&A[i]);
}
nb=FREQ(A,B);
printf("number\t");
for(i=0;i<nb;i++)
{
printf("%d\t",B[i][0]);
Page 36 of 87 | A.KH
}
printf("\n");
printf("repet\t");
for(i=0;i<nb;i++)
{
printf("%d\t",B[i][1]);
}
Exercise 4
#include<stdio.h>
#include<math.h>
int reverseDigits(int x)
{
int r=0,y=fabs(x),rem;
while(y!=0)
{
rem=y%10;
r=r*10+rem;
y=y/10;
}
if(x>0)return r;
else return -r;
}
int main()
{
int x,y;
puts("input integer");
scanf("%d",&x);
y=reverseDigits(x);
printf("%d\n",y);
}
Exercise 5
//part 1
//remark we can use bool for res instead of int
typedef struct table
{
int nb,chair,pers,res;
}table;
//part 2
int FIND(table T[100],int nb)
{
int i=0;
for(i=0;i<100;i++)
{
Page 37 of 87 | A.KH
if(T[i].nb==nb)
return i;
}
return -1;
}
//part 3
int RESERVE(table T[100],int nb,int pers)
{
int i;
i=FIND(T,nb);
if(i==-1){puts("table does not exist");return -99999;}
else
{
if(T[i].res==1)
return 0;
else
{
if(T[i].chair<pers)
return -1;
else
{
T[i].pers=pers;
T[i].res=1;
return 1;
}
}
}
}
//part 4
int PERSONS(table T[100],int nb)
{
int i;
i=FIND(T,nb);
if(i==-1)return -1;
return T[i].pers;
}
//part 5
int TOTAL(table T[100])
{
int i,s=0;
for(i=0;i<100;i++)
s=s+T[i].pers;
return s;
}
Page 38 of 87 | A.KH
Partial Exam 2010 Solutions
Exercise 1
//course!
Exercise 2
#include<stdio.h>
//part a
typedef struct student
{
char name[20];
int grade;
}student;
//part b
typedef struct exam
{
int code;
student S[50];
}exam;
int main()
{
//part c
exam examination[10];
//part d
int i,j;
for(i=0;i<10;i++)
{
examination[i].code=0;
for(j=0;j<50;j++)
examination[i].S[j].grade=0;
}
}
Page 39 of 87 | A.KH
Exercise 3
int COMPARE(int A[],int dim,int x)
{
int s=0,i;
for(i=0;i<dim;i++)
{
if(A[i]<x)s++;
}
return s;
}
Exercise 4
int VOWEL(char c)
{
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='y'||c=='A'||c=='E'||c=='I'||c
=='O'||c=='U'||c=='Y')
return 1;
else return 0;
}
Exercise 5
int MERGE_ARRAY(int A[],int dimA,int B[],int dimB,int C[])
{
int i;
for(i=0;i<dimA;i++)
C[i]=A[i];
for(i=0;i<dimB;i++)
C[i+dimA]=B[i];
}
Page 40 of 87 | A.KH
Page 41 of 87 | A.KH
Final Exam 2020 Solutions
Exercise 1
#include<stdio.h>
// part 1
// part 2
Page 42 of 87 | A.KH
// part 3
int PPCM(int a, int b)
{
int x,t;
if (b>a)
{
x=b;
b=a;
a=x;
}
if (a%b==0)
return a;
else
{
t=(a*b);
while(b != 0)
{
x = a%b;
a = b;
b = x;
}
return t/a;
}
}
// part 4
int PGCD(int a, int b)
{
return a*b/PPCM(a,b);
}
Page 43 of 87 | A.KH
Exercise 2
#include<stdio.h>
// part 1
typedef struct expression
{
double G, D, Val;
char Oprtr;
int Err;
}expression;
// part 2
expression Read_Expression()
{
expression E;
puts("Enter the left operand and the right operand");
scanf("%lf%lf",&E.G,&E.D);
puts("Enter the operator");
fflush(stdin);
scanf("%c",&E.Oprtr);
E.Val = 0.0;
E.Err = -1;
return E;
}
// part 3
void EVAL_EXP(expression *E)
{
if (E->Oprtr != '+' && E->Oprtr != '-' && E->Oprtr != '*' && E->Oprtr != '/')
{
E->Val = 0;
E->Err = 1;
}
else
{
if (E->Oprtr == '+')
{
E->Val = E->G + E->D;
E->Err = 0;
}
if (E->Oprtr == '-')
{
E->Val = E->G - E->D;
E->Err = 0;
}
if (E->Oprtr == '*')
{
Page 44 of 87 | A.KH
E->Val = E->G * E->D;
E->Err = 0;
}
if (E->Oprtr == '/')
{
if (E->D == 0) // check if there is a division by zero
{
E->Val = 0;
E->Err = 1;
}
else
{
E->Val = E->G / E->D;
E->Err = 0;
}
}
}
}
// part 4
void EVAL_ARRAY(expression E[10])
{
int i;
for (i = 0; i < 10; i++)
EVAL_EXP(&E[i]);
}
// part 5
int main()
{
expression E[10];
int i;
Page 45 of 87 | A.KH
Final Exam 2019 Solutions
Exercise 1
int POSITION(int A[50], int a, int counter)
{
if(counter==50) return -1;
if(A[counter]==a) return counter;
return POSITION(A,a,counter+1);
}
Exercise 2
//part a
// Remark: I added the parameter "size" in case the array is not fully added
// We can simply defince size = 100 within the function if we are sure the array is full
int iterativeDichotomic(int A[100],int size,int element)
{
int start=0,mid,end=size-1;
while(start<=end)
{
mid=(start+end)/2;
if(A[mid]==element)
return mid;
else
if(element<A[mid])
end = mid-1;
else
start = mid+1;
}
return -1;
}
// part b
// "start" is initialized by zero and "end" by the size of the array-1
int recursiveDichotomic(int A[100], int start, int end, int element)
{
int mid;
if (end>=start)
{
mid = (start+end)/2;
if (A[mid]==element)
return mid;
if (A[mid]>element)
return recursiveDichotomic(A,start,mid-1,element);
return recursiveDichotomic(A,mid+1,end,element);
}
return -1;
}
Page 46 of 87 | A.KH
Exercise 3
// part a
typedef struct FilmActor
{
int ID;
char name[50];
}FilmActor;
// part b
FilmActor Search(int ID,char FName[])
{
FilmActor FA;
FILE *h;
h=fopen(FName,"r");
fread(&FA,sizeof(FilmActor),1,h);
while(!feof(h))
{
if(FA.ID==ID)
{
fclose(h);
return FA;
}
fread(&FA,sizeof(FilmActor),1,h);
}
fclose(h);
FA.ID=-1;
return FA;
}
Page 47 of 87 | A.KH
// part c
float Display(int FilmID)
{
FILE *h;
Cost C;
FilmActor F;
FilmActor A;
float count=0;
F=Search(FilmID,"Film.dta");
printf("%s\n",F.name);
h=fopen("Cost.dta","r");
fread(&C,sizeof(Cost),1,h);
while(!feof(h))
{
if(C.FilmID==FilmID)
{
count=count+C.C;
A=Search(C.ActorID,"Actor.dta");
printf("%s\t%f\n",A.name,C.C);
}
fread(&C,sizeof(Cost),1,h);
}
fclose(h);
printf("Total\t%f\n",count);
return count;
}
// part d
#include<stdio.h>
int main()
{
int ID;
puts("Please enter a film id");
scanf("%d",&ID);
Display(ID);
}
// copy here parts b and c
Page 48 of 87 | A.KH
Final Exam 2018 Solutions
Exercise 1
#include<stdio.h>
//part 1
int Cube(int a)
{
return a*a*a;
}
//part 2
int SomCube(int a)
{
if(a==0) return 0;
return Cube(a%10)+SomCube(a/10);
}
//part 3
int main()
{
int i;
for(i=1;i<5000;i++)
if(SomCube(i)==i)
printf("%d\t",i);
}
Exercise 2
typedef struct Section
{
int n;
char name[50];
}Section;
void Merge()
{
Section A,B;
FILE *f,*g,*h;
f=fopen("SectionA.txt","r");
g=fopen("SectionB.txt","r");
h=fopen("Class.txt","w");
fread (&A, sizeof (Section), 1, f);
fread (&B, sizeof (Section), 1, g);
while(!feof(f)&&!feof(g))
{
if(A.n<B.n)
{
Page 49 of 87 | A.KH
fwrite(&A, sizeof(Section), 1,h);
fread (&A, sizeof (Section), 1, f);
}
else
{
fwrite(&B, sizeof(Section), 1,h);
fread (&B, sizeof (Section), 1, g);
}
}
if(feof(f))
{
while(!feof(g))
{
fwrite(&B, sizeof(Section), 1,h);
fread (&B, sizeof (Section), 1, g);
}
}
if(feof(g))
{
while(!feof(f))
{
fwrite(&A, sizeof(Section), 1,h);
fread (&A, sizeof (Section), 1, f);
}
}
fclose(f);
fclose(g);
fclose(h);
}
Exercise 3
#include<stdio.h>
//part 1
typedef struct PERSON
{
int nb,Fnb,Mnb;
char Name[50],FName[50],Gender;
}PERSON;
//part 2
PERSON GET_INFO(int nb)
{
PERSON P;
FILE *h;
h=fopen("PERSON.DTA","r");
fread(&P,sizeof(PERSON),1,h);
while(!feof(h))
Page 50 of 87 | A.KH
{
if(P.nb==nb)
{
fclose(h);
return P;
}
fread(&P,sizeof(PERSON),1,h);
}
fclose(h);
P.nb=0;
return P;
}
//part 3
void Ancestors(int nb)
{
PERSON P,Father,Gfather;
P=GET_INFO(nb);
Father=GET_INFO(P.Fnb);
Gfather=GET_INFO(Father.Fnb);
if(Gfather.nb!=0)
{
Ancestors(P.Fnb);
printf("%s %s\n",Gfather.FName,Gfather.Name);
}
}
//part 4
void Print_Family(int nb)
{
FILE *h;
PERSON Q,P,Father,Mother;
P=GET_INFO(nb);Father=GET_INFO(P.Fnb);Mother=GET_INFO(P.Mnb);
printf("Person is %s %s\n",P.FName,P.Name);
puts("Ancestors are:");
Ancestors(nb);
printf("Father's name is %s %s\n",Father.FName,Father.Name);
printf("Mother's name is %s %s\n",Mother.FName,Mother.Name);
h=fopen("PERSON.DTA","r");
fread(&Q,sizeof(PERSON),1,h);
while(!feof(h))
{
if((Q.Fnb==P.Fnb||Q.Mnb==P.Mnb)&&Q.nb!=P.nb)
{
if(Q.Gender=='M')
Page 51 of 87 | A.KH
printf("Brother: %s %s\n",Q.FName,Q.Name);
else
printf("Sister: %s %s\n",Q.FName,Q.Name);
}
fread(&Q,sizeof(PERSON),1,h);
}
fclose(h);
}
//part 5
int main()
{
int nb;
puts("input a number");
scanf("%d",&nb);
Print_Family(nb);
}
Page 52 of 87 | A.KH
Final Exam 2017 Solutions
Exercise 1
int int_div(float A,float B)
{
if(B>A)return 0;
else return 1+int_div(A-B,B);
}
Exercise 2
int common()
{
int nb1,nb2;
FILE *f,*g;
f=fopen("F1.dta","r");
g=fopen("F2.dta","r");
fscanf(f,"%d",&nb1);
fscanf(g,"%d",&nb2);
while(!feof(f)&&!feof(g))
{
if(nb1==nb2)
{
fclose(f);
fclose(g);
return nb1;
}
if(nb1<nb2)
fscanf(f,"%d",&nb1);
else
fscanf(g,"%d",&nb2);
}
fclose(f);
fclose(g);
return 0;
}
Exercise 3
//part a
typedef struct AUTH
{
int code;
char name[50];
}AUTH;
Page 53 of 87 | A.KH
//part b
typedef struct BOOKS
{
int code,Acode,copies;
char title[50],subject[50];
}BOOKS;
//part c
AUTH AUTHOR(int code)
{
AUTH A;
FILE *h;
h=fopen("AUTHOR.DAT","r");
fread(&A,sizeof(AUTH),1,h);
while(!feof(h))
{
if(A.code==code)
{
fclose(h);
return A;
}
fread(&A,sizeof(AUTH),1,h);
}
fclose(h);
A.code=-1;
return A;
}
//part d
BOOKS BOOK(int code)
{
BOOKS B;
FILE *h;
h=fopen("BOOK.DAT","r");
fread(&B,sizeof(BOOKS),1,h);
while(!feof(h))
{
if(B.code==code)
{
fclose(h);
return B;
}
fread(&B,sizeof(BOOKS),1,h);
}
fclose(h);
B.code=-1;
return B;
}
Page 54 of 87 | A.KH
//part e
void PRINT(int code)
{
BOOKS B;
AUTH A;
B=BOOK(code);
A=AUTHOR(B.Acode);
if((B.code)!=-1)
{
printf("books code: %d\n",B.code);
printf("Title: ");
puts(B.title);
printf("Subject: ");
puts(B.subject);
printf("auth name: ");
puts(A.name);
printf("nb of copies: %d\n",B. copies);
}
else
puts(“BOOK NOT FOUND”);
}
//part f
//We suppose it is a new copy
void ADD(BOOKS B)
{
FILE *h;
h=fopen("BOOK.DAT","a");
fwrite(&B,sizeof(BOOKS),1,h);
fclose(h);
}
//part g
void BORROW(int code)
{
FILE *f,*t;
BOOKS B;
B=BOOK(code);
if(B.code==-1)puts("BOOK NOT FOUND");
else
{
f=fopen("BOOK.DAT","r");
t=fopen("temp.temp","w");
fread(&B,sizeof(BOOKS),1,f);
while (!feof (f))
{
if (B.code == code)
Page 55 of 87 | A.KH
{
if (B.copies > 1)
{
B.copies--;
puts ("BOOK FOUND");
}
else
if(B.copies==1)
{puts ("BOOK FOUND LAST COPY");B.copies--;}
else
puts("BOOK FOUND BUT NO AVAILABLE COPIES");
}
fwrite (&B, sizeof (BOOKS), 1, t);
fread (&B, sizeof (BOOKS), 1, f);
}
fclose(f);
fclose(t);
f=fopen("BOOK.DAT","w");
t=fopen("temp.temp","r");
fread(&B,sizeof(BOOKS),1,t);
while(!feof(t))
{
fwrite(&B,sizeof(BOOKS),1,f);
fread(&B,sizeof(BOOKS),1,t);
}
fclose(f);
fclose(t);
}
}
Page 56 of 87 | A.KH
Final Exam 2016 Solutions
Exercise 1
//part 1
//part 2
-for A=9,B=5
F1(9,5)
return F1(8,4)
return F1(7,3)
return F1(6,2)
return F1(5,1)
return F1(4,0)
return 4
-for A=15,B=20
F1(15,20)
return F1(14,19)
return F1(13,18)
return F1(12,17)
return F1(11,16)
return F1(10,15)
return F1(9,14)
return F1(8,13)
return F1(7,12)
return F1(6,11)
return F1(5,10)
return F1(4,9)
return F1(3,8)
return F1(2,7)
Page 57 of 87 | A.KH
return F1(1,6)
return F1(0,5)
return 5
//part 3
F1 computes the absolute value of the difference of A and B=|A-B|
//part 4
int F1_iterative(int A,int B)
{
if(A>B)
return A-B;
return B-A;
}
Exercise 2
//part 1
//i starts from sqrt(N);i=sqrt(N) in the first call
int HDIV(int N,int i)
{
if(N%i==0)return i;
else return HDIV(N,i-1);
}
//part 2
void DISPLAY(int N)
{
int i=(int)sqrt(N);
do
{
printf("(%d,%d)\t",HDIV(N,i),N/HDIV(N,i));
i=HDIV(N,i)-1;
}while(N/HDIV(N,i)!=N);
printf("(%d,%d)\t",1,N);
}
//part 3
#include<stdio.h>
#include<math.h>
int HDIV(int,int);
void DISPLAY(int);
int main()
{
int N;
do
{
puts("input a positive non zero integer");
scanf("%d",&N);
Page 58 of 87 | A.KH
}while(N<1);
DISPLAY(N);
}
//(we should write part 1 and 2 here)
//Part 1
//Part 2
/*
trace for N=100
DISPLAY(100)
.print(HDIV(100,10),100/HDIV(100,10))
=print(10,100/10)
=print(10,10)
.print (HDIV(100,9),100/HDIV(100,9))
print (HDIV(100,8),100/HDIV(100,8))
print (HDIV(100,7),100/HDIV(100,7))
print (HDIV(100,6),100/HDIV(100,6))
print (HDIV(100,5),100/HDIV(100,5))
print (5,100/5)
print (5,20)
.print (HDIV(100,4),100/HDIV(100,4))
print (4,100/4)
print(4,25)
.print (HDIV(100,3),100/HDIV(100,3))
print (HDIV(100,2),100/HDIV(100,2))
print (2,100/2)
print(2,50)
.print(1,100)
*/
Exercise 3
//part 1
typedef struct application
{
int id,cat,dev;
char name[25];
float price;
}application;
Page 59 of 87 | A.KH
typedef struct developer
{
int id;
char name[25];
}developer;
//part 2
category Find_Category(int id)
{
category c;
FILE *f=NULL;
f=fopen("Category.dta","r");
fscanf(f,"%d%s",&c.id,&c.name);
while(!feof(f))
{
if(id==c.id)
{
fclose(f);
return c;
}
fscanf(f,"%d%s",&c.id,&c.name);
}
c.id=-1;
c.name[0]='\0';
fclose(f);
return c;
}
//part 3
void UPLOAD(application app)
{
int j=0;
FILE *f=NULL,*g=NULL;
application A;
category c;
c=Find_Category(app.cat);
if(c.id<0)puts("ERROR!!");
else
{
f=fopen("Application.dta","r");
g=fopen("temp","w");
fscanf(f,"%d%s%d%f%d",&A.id,&A.name,&A.cat,&A.price,&A.dev);
while(!feof(f))
{
if(A.cat==app.cat&&A.id>app.id&&j==0)
{
Page 60 of 87 | A.KH
fprintf(g,"%d\t%s\t%d\t%f\t%d\n",app.id,app.name,app.cat,app.price,app.dev);
j++;
}
fprintf(g,"%d\t%s\t%d\t%f\t%d\n",A.id,A.name,A.cat,A.price,A.dev);
fscanf(f,"%d%s%d%f%d",&A.id,&A.name,&A.cat,&A.price,&A.dev);
}
fclose(f);
fclose(g);
f=fopen("Application.dta","w");
g=fopen("temp","r");
fscanf(g,"%d%s%d%f%d",&A.id,&A.name,&A.cat,&A.price,&A.dev);
while(!feof(g))
{
fprintf(f,"%d\t%s\t%d\t%f\t%d\n",A.id,A.name,A.cat,A.price,A.dev);
fscanf(g,"%d%s%d%f%d",&A.id,&A.name,&A.cat,&A.price,&A.dev);
}
fclose(f);
fclose(g);
}
}
//part 4
void Show_Category(int id)
{
FILE *f=NULL;
category c;
application A;
c=Find_Category(id);
if(c.id<0)puts("ERROR!!");
else
{
f=fopen("Application.dta","r");
fscanf(f,"%d%s%d%f%d",&A.id,&A.name,&A.cat,&A.price,&A.dev);
while(!feof(f))
{
if(A.cat==id)
printf("%d\t%s\t%d\t%f\t%d\n",A.id,A.name,A.cat,A.price,A.dev);
fscanf(f,"%d%s%d%f%d",&A.id,&A.name,&A.cat,&A.price,&A.dev);
}
fclose(f);
}
}
Page 61 of 87 | A.KH
Final Exam 2015 Solutions
Exercise 1
#include<stdio.h>
#include<string.h>
void RPUTS(char A[],int i)
{
if(strlen(A)>i)
{
RPUTS(A,i+1);
printf("%c",A[i]);
}
}
int main()
{
char A[999999];
puts("input a sentence");
fflush(stdin);
gets(A);
RPUTS(A,0);
}
Exercise 2
#include<stdio.h>
int EXTRACT(int *N,int B)
{
int C=B,i=0;
while(*N-C>=0)
{
C=C*B;
i++;
}
if(*N>=B)
{
*N=*N-C/B;
return i;
}
else
return 0;
}
int main()
{
int N,B,i;
puts("input a number N and a base B");
scanf("%d%d",&N,&B);
i=EXTRACT(&N,B);
if(i!=0)
Page 62 of 87 | A.KH
printf("%d+%d^%d",N,B,i);
else
printf("NO POSSIBLE EXTRACTION!!");
}
Exercise 3
#include<stdio.h>
#include<math.h>
//part 1
int SN(int n)
{
if(n==0)return 0;
return n%10+SN(n/10);
}
//part 2
int ROOT(int n)
{
if(n/10==0)return n;
return ROOT(SN(n));
}
//part 3
int main()
{
int n;
puts("input nb");
scanf("%d",&n);
printf("%d\n",ROOT(n));
}
Exercise 4
void INSERT(int n)
{
int a;
FILE *f,*g;
f=fopen("Data.Dta","r");
g=fopen("temp.temp","w");
fscanf(f,"%d",&a);
while(!feof(f)&&n>a)
{
fprintf(g,"%d\n",a);
fscanf(f,"%d",&a);
}
fprintf(g,"%d\n",n);
if(!feof(f))
{
Page 63 of 87 | A.KH
fprintf(g,"%d\n",a);
fscanf(f,"%d",&a);
while(!feof(f))
{
fprintf(g,"%d\n",a);
fscanf(f,"%d",&a);
}
}
fclose(f);
fclose(g);
f=fopen("Data.dta","w");
g=fopen("temp.temp","r");
fscanf(g,"%d",&a);
while(!feof(g))
{
fprintf(f,"%d\n",a);
fscanf(g,"%d",&a);
}
fclose(f);fclose(g);
}
Exercise 5
//part 1
typedef struct place
{
int id,pid;
char name[20],nature;
}place;
//part 2
place Search(int id)
{
place p;
FILE *f;
f=fopen("Places.Dta","r");
fread(&p,sizeof(place),1,f);
while(!feof(f))
{
if(p.id==id)
{
fclose(f);
return p;
}
fread(&p,sizeof(place),1,f);
}
fclose(f);
p.id=-1;
return p;
}
Page 64 of 87 | A.KH
//part 3
void PRINT(int id)
{
place p;
p=Search(id);
if(p.id>0)
{
printf("%s\t",p.name);
if(p.pid!=0)
PRINT(p.pid);
}
}
Page 65 of 87 | A.KH
Final Exam 2014 Solutions
Exercise 1
//part 1
//i must be entered two……….v must entered one……….w must entered one
float rec_seq(float A[1000],float a,float v,float w,int i)
{
float q;
if(i<1000)
{
q=((a/(v*v))+(1/w))/3;
A[i]=q;
if(fabs(q-v)>0.00001)
return rec_seq(A,a,q,v,i+1);
}
A[0]=1;
A[1]=1;
return q;
}
int main ()
{
float A[1000],x,v=1,w=1;
puts("input x");
scanf("%f",&x);
x=rec_seq(A,x,v,w,2);
}
//part 2
//i must be entered zero
void pr_tr(float A[1000],int i,float un)
{
if (i<1000)
{
if(A[i]==un)
printf("%f\n",A[i]);
else
{
printf("%f\n",A[i]);
pr_tr(A,i+1,un);
}
}
}
Page 66 of 87 | A.KH
Exercise 2
int common()
{
int nb1,nb2;
FILE *f,*g;
f=fopen("F1.dta","r");
g=fopen("F2.dta","r");
fscanf(f,"%d",&nb1);
fscanf(g,"%d",&nb2);
while(!feof(f)&&!feof(g))
{
if(nb1==nb2)
{
fclose(f);
fclose(g);
return nb1;
}
if(nb1<nb2)
fscanf(f,"%d",&nb1);
else
fscanf(g,"%d",&nb2);
}
fclose(f);
fclose(g);
return 0;
}
Exercise 3
#include<stdio.h>
//part a
typedef struct PF
{
int n;
char Fname[25],Lname[25],G;
int fn,mn;
}PF;
//part b
PF Get_Info(int id)
{
FILE *f;
PF P;
f=fopen("PERSON.DTA","r");
fread(&P,sizeof(PF),1,f);
while(!feof(f))
Page 67 of 87 | A.KH
{
if(P.n==id)
{
fclose(f);
return P;
}
fread(&P,sizeof(PF),1,f);
}
P.n=0;
fclose(f);
return P;
}
//part c
void Create_Family(int id)
{
PF P,t;
FILE *f,*g;
P=Get_Info(id);
f=fopen("FAMILY.DTA","w");
t=Get_Info(P.fn);
fwrite(&t,sizeof(PF),1,f);
t=Get_Info(P.mn);
fwrite(&t,sizeof(PF),1,f);
fwrite(&P,sizeof(PF),1,f);
g=fopen("PERSON.DTA","r");
fread(&t,sizeof(PF),1,g);
while(!feof(g))
{
if((t.fn==P.fn||t.mn==P.mn)&&t.n!=id)
fwrite(&t,sizeof(PF),1,f);
fread(&t,sizeof(PF),1,g);
}
fclose(f);
fclose(g);
}
//part d
void Print_Info()
{
FILE *f;
PF P;
f=fopen("FAMILY.DTA","r");
fread(&P,sizeof(PF),1,f);
printf("father:%s %s\n",P.Fname,P.Lname);
fread(&P,sizeof(PF),1,f);
printf("Mother:%s %s\n",P.Fname,P.Lname);
fread(&P,sizeof(PF),1,f);
Page 68 of 87 | A.KH
printf("himself:%s %s\n",P.Fname,P.Lname);
fread(&P,sizeof(PF),1,f);
while(!feof(f))
{
if(P.G=='M')
printf("Brother:");
else
printf("Sister:");
printf("%s %s\n",P.Fname,P.Lname);
fread(&P,sizeof(PF),1,f);
}
fclose(f);
}
//part e
int main()
{
int id;
puts("input id");
scanf("%d",&id);
Create_Family(id);
Print_Info();
}
//part f
void Display_Ancestors(int id)
{
PF P;
P=Get_Info(id);
if(P.n>0)
{
Display_Ancestors(P.fn);
printf("%s %s\n",P.Fname,P.Lname);
}
}
Page 69 of 87 | A.KH
Final Exam 2013 Solutions
Exercise 1
//part 1
The role of this functions is to calculate the summation of integers from 0 to n ∑𝒏𝟎 𝒊
//part 2
int fn_iterative(int n)
{
int i,s=0;
for(i=1;i<=n;i++)
s+=i;
return s;
}
Exercise 2
//part 1
#include<stdio.h>
#include<math.h>
//part 2
int main()
{
float A;
int counter=1;
puts("input number");
scanf("%f",&A);
printf("square root of %f=%f\n",A,SQUARE_ROOT(A,1,&counter));
printf("number of calls=%d\n",counter);
}
Page 70 of 87 | A.KH
Exercise 3
void MERGE()
{
FILE *f,*g,*h;
int x,y;
f=fopen("A.txt","r");
g=fopen("B.txt","r");
h=fopen("C.txt","w");
fread(&x,sizeof(int),1,f);
fread(&y,sizeof(int),1,g);
while(!feof(f)&&!feof(g))
{
if(x<y)
{
fwrite(&x,sizeof(int),1,h);
fread(&x,sizeof(int),1,f);
}
else
{
fwrite(&y,sizeof(int),1,h);
fread(&y,sizeof(int),1,g);
}
}
if(feof(f))
{
while(!feof(g))
{
fwrite(&y,sizeof(int),1,h);
fread(&y,sizeof(int),1,g);
}
}
else
{
while(!feof(f))
{
fwrite(&x,sizeof(int),1,h);
fread(&x,sizeof(int),1,f);
}
}
fclose(f);
fclose(g);
fclose(h);
}
Page 71 of 87 | A.KH
Exercise 4
//part 1
typedef struct item
{
int id;
char name[20];
float price;
}item;
//part 2
int COUNT(int id)
{
FILE *f;
item I;
int counter=0;
f=fopen("DATA.txt","r");
fread(&I,sizeof(item),1,f);
while(!feof(f))
{
if(I.id==id)
counter++;
fread(&I,sizeof(item),1,f);
}
fclose(f);
return counter;
}
//part 3
item FIND(int id)
{
FILE *f;
item I,MAX;
f=fopen("DATA.txt","r");
fread(&I,sizeof(item),1,f);
while(!feof(f))
{
if(I.id==id)
break;
else
fread(&I,sizeof(item),1,f);
}
MAX=I;
while(!feof(f))
{
if(I.id==id&&MAX.price<I.price)
MAX=I;
Page 72 of 87 | A.KH
fread(&I,sizeof(item),1,f);
}
fclose(f);
return MAX;
}
//part 4
void DELETE(int id)
{
FILE *f,*g;
item I,MAX;
MAX=FIND(id);
f=fopen("DATA.txt","r");
g=fopen("temp","w");
fread(&I,sizeof(item),1,f);
while(!feof(f))
{
if(I.id!=id)
fwrite(&I,sizeof(item),1,g);
fread(&I,sizeof(item),1,f);
}
fwrite(&MAX,sizeof(item),1,g);
fclose(f);
fclose(g);
f=fopen("DATA.txt","w");
g=fopen("temp","r");
fread(&I,sizeof(I),1,g);
while(!feof(g))
{
fwrite(&I,sizeof(I),1,f);
fread(&I,sizeof(item),1,g);
}
fclose(f);
fclose(g);
}
//part 5
void CLEAN()
{
FILE *f;
item I;
while(1==1)
{
f=fopen("DATA.txt","r");
fread(&I,sizeof(item),1,f);
while(!feof(f)&&COUNT(I.id)==1)
fread(&I,sizeof(item),1,f);
if(feof(f))
Page 73 of 87 | A.KH
{
fclose(f);
break;
}
fclose(f);
DELETE(I.id);
}
}
Page 74 of 87 | A.KH
Final Exam 2012 Solutions
Exercise 1
#include<stdio.h>
//i must be entered three
int prime(int N,int i)
{
if(i>=N)return 1;
else
{
if(N%i==0) return 0;
return prime(N,i+1);
}
}
//part 2
int main()
{
int N;
do
{
puts("input an integer");
scanf("%d",&N);
}while(N<=0);
N=prime(N,2);
if(N==0)
puts("not prime");
else
puts("prime");
}
Exercise 2
#include<stdio.h>
//part 1
typedef struct stdn
{
int id;
char name[25];
}stdn;
//part 2
//i must be zero
stdn find(stdn s[50],int id,int i)
{
if(i<50)
{
Page 75 of 87 | A.KH
if(s[i].id==id)
return s[i];
return find(s,id,i+1);
}
else
{
stdn n;
n.id=-1;
return n;
}
}
//part 3
int main()
{
int i,id;
stdn s[50],s1;
for(i=0;i<50;i++)
{
puts("input id");
scanf("%d",&s[i].id);
puts("input name");
fflush(stdin);
gets(s[i].name);
}
puts("input an integer");
scanf("%d",&id);
s1=find(s,id,0);
if(s1.id<0)
puts("error student does not exsist");
else
{
printf("%d\n",s1.id);
puts(s1.name);
}
}
Page 76 of 87 | A.KH
Exercise 3
//part 1
typedef struct cit
{
int id,fid,mid,reg,status;
char name[30],sex;
}cit;
//part 2
void Add(cit C)
{
FILE *f;
f=fopen("CITIZENS.DTA","a");
fwrite(&C,sizeof(cit),1,f);
fclose(f);
}
//part 3
cit Search(int id)
{
FILE *f;
cit C;
f=fopen("CITIZENS.DTA","r");
fread(&C,sizeof(cit),1,f);
while(!feof(f))
{
if(C.id==id)
{
fclose(f);
return C;
}
fread(&C,sizeof(cit),1,f);
}
C.id=-1;
return C;
}
//part 4
void Print_Fathers(int id)
{
cit C;
C=Search(id);
if(C.id>0)
{
Print_Fathers(C.fid);
puts(C.name);
Page 77 of 87 | A.KH
}
}
//part 5
void Print_Citizen(int id)
{
cit C;
C=Search(id);
if(C.id<0)
puts("ERR citizen does not exist");
else
{
printf("id=%d\tname=%s\tregister=%d\tfather id=%d\tmother
id=%d\tsex=%c\t",C.id,C.name,C.reg,C.fid,C.mid,C.sex);
if(C.status==1)
printf("status=alive\n");
else
printf("status=dead");
}
}
//part 6
void Print_Family(int id)
{
cit C,T;
FILE *f;
C=Search(id);
if(C.id>0)
{
printf("Father:");
Print_Citizen(C.fid);
printf("Mother:");
Print_Citizen(C.mid);
printf("Person:");
Print_Citizen(C.id);
f=fopen("CITIZENS.DTA","r");
fread(&T,sizeof(cit),1,f);
while(!feof(f))
{
if((T.fid==C.fid||T.mid==C.mid)&&T.id!=id)
{
if(T.sex=='M')
printf("Brother:");
else
printf("Sister:");
Print_Citizen(T.id);
}
fread(&T,sizeof(cit),1,f);
Page 78 of 87 | A.KH
}
fclose(f);
}
else
puts("citizen does not exist");
}
//part 7
void Update(int id)
{
cit C;
FILE *f,*g;
C=Search(id);
if(C.id>0)
{
f=fopen("CITIZENS.DTA","r");
g=fopen("temp","w");
fread(&C,sizeof(cit),1,f);
while(!feof(f))
{
if(C.id==id)
C.status=0;
fwrite(&C,sizeof(cit),1,g);
fread(&C,sizeof(cit),1,f);
}
fclose(f);
fclose(g);
f=fopen("CITIZENS.DTA","w");
g=fopen("temp","r");
fread(&C,sizeof(cit),1,g);
while(!feof(g))
{
fwrite(&C,sizeof(cit),1,f);
fread(&C,sizeof(cit),1,g);
}
fclose(f);
fclose(g);
}
}
Page 79 of 87 | A.KH
Final Exam 2011 Solutions
Exercise 1
#include<stdio.h>
//part 1
//i must be enetered one
int SDivPerfect(int k,int i)
{
if(i>=k) return 0;
else
{
if(k%i==0)
return i+SDivPerfect(k,i+1);
return SDivPerfect(k,i+1);
}
}
//part 2
int main()
{
int k,sumofdivisors;
do
{
puts("input an integer k");
scanf("%d",&k);
}while(k<=0);
sumofdivisors=SDivPerfect(k,1);
if (k==sumofdivisors)
printf("%d is perfect\n",k);
else
printf("%d is not perfect\n",k);
}
Exercise 2
#include<stdio.h>
//part 1
//i must be included one
int MULTIPLY(int x,int y,int i)
{
if(i>=y)
return x;
return x+MULTIPLY(x,y,i+1);
}
Page 80 of 87 | A.KH
//part 2
int main()
{
int x,y;
do
{
puts("input a positive integer");
scanf("%d",&x);
}while(x<=0);
do
{
puts("input a positive integer");
scanf("%d",&y);
}while(y<=0);
printf("%d*%d=%d\n",x,y,MULTIPLY(x,y,1));
}
Exercise 3
// n=number of elements in the int array
//i must be entered 0
int EVEN (int A[],int n,int i)
{
if(i>=n)
return 0;
if(A[i]%2==0)
return 1+EVEN(A,n,i+1);
return EVEN(A,n,i+1);
}
Exercise 4
void Reverse(int A[],int x,int y)
{
if(x<=(x+y)/2)
{
int t;
t=A[x];
A[x]=A[y];
A[y]=t;
Reverse(A,x+1,y-1);
}
}
Page 81 of 87 | A.KH
Exercise 5
//part 1
typedef struct dep
{
int nb;
char name[25];
}dep;
//part 2
typedef struct emp
{
int nb,debnb,mannb;
char name[25],job[15],gender;
float salary;
}emp;
//part 3
dep DepSearch(int nb)
{
FILE *f;
dep D;
f=fopen("DEPT.DTA","r");
fread(&D,sizeof(dep),1,f);
while(!feof(f))
{
if(D.nb==nb)
{
fclose(f);
return D;
}
fread(&D,sizeof(dep),1,f);
}
D.nb=-1;
fclose(f);
return D;
}
//part 4
emp EmpSearch(int nb)
{
FILE *f;
emp E;
f=fopen("EMP.DTA","r");
fread(&E,sizeof(emp),1,f);
while(!feof(f))
{
Page 82 of 87 | A.KH
if(E.nb==nb)
{
fclose(f);
return E;
}
fread(&E,sizeof(emp),1,f);
}
E.nb=-1;
fclose(f);
return E;
}
//part 5
void MAXMIN(int nb,float *max, float *min)
{
FILE *f;
emp E;
f=fopen("EMP.DTA","r");
fread(&E,sizeof(emp),1,f);
while(!feof(f))
{
if(E.debnb==nb)
{
*min=E.salary;
*max=E.salary;
break;
}
fread(&E,sizeof(emp),1,f);
}
while(!feof(f))
{
if(E.debnb==nb)
{
if(E.salary<*min)
*min=E.salary;
if(E.salary>*max)
*max=E.salary;
}
fread(&E,sizeof(E),1,f);
}
fclose(f);
}
Page 83 of 87 | A.KH
//part 6
void Print(int nb)
{
FILE *f;
emp E,MANAGER;
dep D;
float max,min;
MAXMIN(nb,&max,&min);
D=DepSearch(nb);
printf("%s,%f,%f\n",D.name,max,min);
f=fopen("EMP.DTA","r");
fread(&E,sizeof(emp),1,f);
while(!feof(f))
{
if(E.debnb==nb)
{
MANAGER=EmpSearch(E.mannb);
printf("%d\t%s\t%s\t%s\t%f\n",E.nb,E.name,E.job,MANAGER.name,E.salary);
}
fread(&E,sizeof(emp),1,f);
}
}
Page 84 of 87 | A.KH
Final Exam 2010 Solutions
Exercise 1
#include<stdio.h>
//part 1
int FIB_R(int n)
{
if(n==1)return 0;if(n==2)return 2;if(n==0)return 3;
return(FIB_R(n-3)-FIB_R(n-2));
}
//part 2
int main()
{
int n,i,A[20],a=3,b=0,c=2,d;
for(i=0;i<20;i++)
{
A[i]=FIB_R(i);
}
for(i=0;i<20;i++)
printf("%d\n",A[i]);
//part 3
A[0]=3;
A[1]=0;
A[2]=2;
for(i=3;i<20;i++)
{
d=a-b;
A[i]=d;
a=b;
b=c;
c=d;
}
printf("using iterative way\n");
for(i=0;i<20;i++)
printf("%d\n",A[i]);
}
Exercise 2
//part 1
int Ret_I(int s)
{
if(s%2==0)
s=2;
else
s=1;
return s;
}
Page 85 of 87 | A.KH
//part 2
//i must be entered zero
void Prt_R(int k,int n,int i)
{
if(i<=n)
{printf("%d\n",k);
Prt_R(k+Ret_I(k),n,i+1);}
}
Exercise 3
//part a
typedef struct lamp
{
int code;
char name[20];
float price;
}lamp;
typedef struct arr
{
int code;
char name[20];
float cost;
}arr;
typedef struct lamp_arr
{
int lamp_code,arr_code,qtty;
}lamp_arr;
//part b
lamp SEARCH_LAMP(int nb)
{
lamp L;
FILE *f;
f=fopen("Lamps.dat","r");
fread(&L,sizeof(lamp),1,f);
while(!feof(f))
{
if(L.code==nb)
{
fclose(f);
return L;
}
fread(&L,sizeof(lamp),1,f);
}
L.code=-1;
return L;
}
//part c
float COST_SUM(int nb)
Page 86 of 87 | A.KH
{
FILE *f;
lamp_arr LA ;
lamp L;
f=fopen("Lamp-Arr.dat","r");
fread(&LA,sizeof(lamp_arr),1,f);
while(!feof(f))
{
if(LA.arr_code==nb)
{
L=SEARCH_LAMP(LA.lamp_code);
fclose(f);
return L.price*LA.qtty;
}
fread(&LA,sizeof(lamp_arr),1,f);
}
return 0;
}
//part d
void UPDATE_COST(int nb)
{
FILE *f,*g;
arr A;
f=fopen("Arrangements.dat","r");
g=fopen("temp","w");
fread(&A,sizeof(arr),1,f);
while(!feof(f))
{
if(A.code==nb)
A.cost=COST_SUM(nb);
fwrite(&A,sizeof(arr),1,g);
fread(&A,sizeof(arr),1,f);
}
fclose(f);
fclose(g);
f=fopen("Arrangements.dat","w");
g=fopen("temp","r");
fread(&A,sizeof(arr),1,g);
while(!feof(g))
{
fwrite(&A,sizeof(arr),1,f);
fread(&A,sizeof(arr),1,g);
}
fclose(f);
fclose(g);
}
Page 87 of 87 | A.KH