Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Semester 1, 2010

EP 100 Examinations Review
Exam Structure
• Fundamentals (20 marks)
• Control Flow (20 marks)
• Array and Matrix (20 marks)
• Function and I/O (20 marks)
• Program Development (20 marks)

Note: Not necessarily the same structure for the current exam
Q1a ‐ Fundamentals

120 const float PI = 3.1415;

190 cannot change a constant value


200 power -> pow built-in function
Q1b

70 int factorial=1;

120 for(i=k;i>1;i--)
140 factorial=factorial*i;
Q1c

illegal
illegal
illegal
illegal
legal
Q2a – Control Flow

Loop variables int i = 0;

Initialisation for ( i=0; i<9; i++)
{
Termination test …
}
Variable updating
Q2b
#include <stdio.h>

int main()
{
int deposit,withdraw,balance; // 1 marks for variables

printf("Enter the amount on deposit "); // 1 marks for initial steps


for entering inputs
scanf("%d",&deposit);
printf("Enter the withdrawal ");
scanf("%d",&withdraw);
while(deposit>=0) // 2 marks for appropriate loop
{
balance=deposit-withdraw; // 2 mark for computing balance
if (balance>=0) // 3 marks for this if then else
statement
printf("Accepted. Balance is %d\n",balance);
else
printf("Refused. Balance is %d\n",deposit);
printf("\n");
printf("Enter the amount on deposit "); // 1 marks for re-entering
the inputs
scanf("%d",&deposit);
printf("Enter the withdrawal ");
scanf("%d",&withdraw);
}
printf("Exiting...\n");

return 0;
} Using a Do–While is also acceptable 
Q3a – Array and Matrix
#include <stdio.h>

int main()
{
float A[2][2],B[2][2],X[2][2]; // define A, B and X
// 2 marks for all correct definitions
float Anorm[2][2],Bnorm[2][2]; // define the A and B normalised matrix
float determA,determB; // define determinant
int i,j,k;
// 3 marks for correct inputs for A and B
printf("Enter the first row of matrix A :");
scanf("%f %f", &A[0][0],&A[0][1]);
printf("Enter the second row of matrix A :");
scanf("%f %f", &A[1][0],&A[1][1]);
printf("Enter the first row of matrix B :");
scanf("%f %f", &B[0][0],&B[0][1]);
printf("Enter the second row of matrix B :");
scanf("%f %f", &B[1][0],&B[1][1]);
// 2 marks for correct determinant operation
determA = A[0][0]*A[1][1] - A[1][0]*A[0][1];
determB = B[0][0]*B[1][1] - B[1][0]*B[0][1];

for(i=0;i<=1;i++) // 2 marks for normalisation


for(j=0;j<=1;j++){
Anorm[j][i]=A[j][i]/determB;
Bnorm[j][i]=B[j][i]/determA;
}
continue

for(i=0;i<=1;i++){ // 4 marks for correct matrix multiplication


using for loop
for(j=0;j<=1;j++)
{
X[i][j]=0;
for(k=0;k<=1;k++)
{
X[i][j]+=Anorm[i][k]*Bnorm[k][j];
}
}
}
printf("Matrix X is\n"); // 1 mark for output
printf("%.2f %.2f\n",X[0][0],X[0][1]);
printf("%.2f %.2f\n",X[1][0],X[1][1]);

return 0;
}

Problem with (iii) is the one of the determinant value could be zero which means 
the computation is not valid for this instance. Suggest to check value of determinants 
and if any of them is zero, exit the program.  // 2 marks
Q3b

for(i=0;i<19;i++)
reverselist[19-i]=list[i];
Q4a – Function and I/O
#include <stdio.h>
float biggest (float, float); // declaration of function

int main(void) // 2 marks


{
float x, y, z;
x = 5.0;
y = 10.0;
z = biggest (x,y);
printf("biggest is %f\n",z);
return 0;
}

float biggest (float x, float y) // 4 marks for function


definition
{
float temp;
if (x >= y)
temp = x;
else
temp = y;
return temp;
}
Q4b
#include <stdio.h>

float maximum(float storage[],int); // declaration of functions – 1 marks


float minimum(float storage[],int);
float average(float storage[],int);

int main()
{
FILE *fp; // variable declaration – 1 marks

int i=0,j,istatus;
float value;
float min,max,avg;
float storage[1000];

fp = fopen(“temperature.txt","r"); // open external file – 1 mark


if (fp == NULL) // check for file availability – 1 mark
printf("File did not exist\n");
else{ // looping through the data – 3 marks
do
{
istatus = fscanf(fp,"%f", &value);
storage[i]=value;
i++;
}while(istatus!=EOF);
}
fclose(fp); // close external file – 1 mark
continue

max=maximum(storage,i); // calling functions – 2 marks


printf("max = %f\n",max);
min=minimum(storage,i);
printf("min = %f\n",min);
avg=average(storage,i);
printf("avg = %f\n",avg);
return 0;
}

float maximum(float storage[],int i)


// max functions definition – 2 marks
{
int j;
float max=-1000000.0;
for(j=0;j<i;j++)
if (storage[j]>max)
max=storage[j];
return max;
}
continue

float minimum(float storage[],int i)


{
int j;
float min=1000000.0;
for(j=0;j<i;j++)
if (storage[j]<min)
min=storage[j];
return min;
}

float average(float storage[],int i)


// average function definition – 2 marks
{
int j;
float avg=0.0;
for(j=0;j<i;j++)
avg+=storage[j];
avg=avg/(float) i;
return avg;
}
Q5a – Program Development
#include <stdio.h>
#include <math.h>

#define PI 3.14159
#define g 9.807 // constant definition 1 mark

int main()
{
FILE *fp; // variables declarations 1 mark
float length, period;
float length_start,length_end;

fp=fopen("pendulum.csv","w"); // open file 1 mark

printf("Enter the starting length "); // prompts 1 mark


scanf("%f",&length_start);
printf("Enter the ending length ");
scanf("%f",&length_end);
// correct loop statement 2 marks
for(length=length_start;length<=length_end;length=length+1.0)
{
period = 2 * PI * sqrt(length/g); // correct computation 2 marks
fprintf(fp,"%f,%f\n",length,period); // note the comma delimiter 2 marks
}
fclose(fp); // close file 1 mark

return 0;
}
Q5b
#include <stdio.h>
#include <math.h>

#define I_ref 1.0E-12 // reference sound intensity (watt/m^3)

float intensity_to_loudness(float); // 2 marks to declare 2 function


float loudness_to_intensity(float);

int main()
{
float intensity; // in w/m^3
float L; // in decibel

// intensity to loudness conversion


intensity = 1.0E-11; // 1 mark here
L=intensity_to_loudness(intensity);
printf("The loudness of sound intensity of %g w/m^3 is %f dB\n",intensity,L);
intensity = 1.0E-10; // 1 mark here
L=intensity_to_loudness(intensity);
printf("The loudness of sound intensity of %g w/m^3 is %f dB\n",intensity,L);
intensity = 1.0E-9; // 1 mark here
L=intensity_to_loudness(intensity);
printf("The loudness of sound intensity of %g w/m^3 is %f dB\n",intensity,L);

// loudness to intensity coversion


L=100; // // 1 mark here
loudness_to_intensity(L);
printf("The intensity of sound with loudness value of %f dB is
%g\n",L,intensity);

return 0;
}
continue

float intensity_to_loudness(float intensity) // 2 marks


{
float L;

L = 10* log10(intensity/I_ref);

return L;
}

float loudness_to_intensity(float L) // 2 marks


{
float intensity;

intensity = I_ref * pow(10,L/10);

return intensity;
}
Loudness to Intensity Conversion
Code Tracing 
Code Tracing (Ans)

0
2 1
4 3 2
6 5 4 3
Semester 1, 2013 Exam Scope
• C programming fundamentals
• Control flow
• Looping structure
• Array, matrix and data structure
• Functions
• File IOs
Examination Structure 
Question 1 – Fundamentals
Question 2 – Control Flow
Question 3 – Repetition
Question 4 – Arrays and Matrices
Question 5 – Functions and File I/O
Finally
eVALUate is open!
Have your say now!

You might also like