Docsity

Final Term Exam for Advanced C Programming | ECE 26400, Exams of Electrical and Electronics Engineering

Toggle sidebar
bg1
May 5, 2007 Name _________________________________
ECE264 Advanced C Programming
Final Exam
Solve the following problems. The number of points for each problem is shown next to
the problem and in the table below. The outcomes corresponding to each question are
also shown. Use only the space provided to solve each problem.
Problem Points Outcome
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
1 (a) / 10 2,3
(b.1) / 10
(b.2) / 10
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
2 (a) / 8 1
(b) / 8
(c) / 8
(d) / 8
(e) / 8
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
3 / 10
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
4 / 10
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
5 / 10
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Total / 100
ccccccccccccccccccccccc
ccccccccccccccccccccccc
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Final Term Exam for Advanced C Programming | ECE 26400 and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

May 5, 2007 Name _________________________________

ECE264 Advanced C Programming Final Exam

Solve the following problems. The number of points for each problem is shown next to the problem and in the table below. The outcomes corresponding to each question are also shown. Use only the space provided to solve each problem.

iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiProblem^ Points^ Outcome 1 (a) / 10 2, (b.1) / 10

iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii(b.2)^ / 10 2 (a) / 8 1 (b) / 8 (c) / 8 (d) / 8

iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii(e)^ / 8

iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii^3 / 10

iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii^4 / 10

iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii^5 / 10 Total cc / 100

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

cc

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

c

Problem 1 (30 points)

In the following program, a map is represented using two formats. The first format is a two-dimensional array map2d. map2d[i][j]=1 if nodes i and j are connected by a road; otherwise, map2d[i][j]=0. Note that each road connects the two nodes at its endpoints in both directions. The second format is represented by the array map.

#include <stdio.h> #include <stdlib.h> #include <assert.h>

#define MAXN 5 char map2d[MAXN][MAXN]; struct element { int node; struct element *next; } *map[MAXN];

int main(void) { void fillmap2d(),printmap2d(),translate(),printmap();

fillmap2d(); printmap2d(); translate(); printmap(); printf("\n"); }

void fillmap2d() { int i,j;

for(i=0;i<MAXN;i++) for(j=i+1;j<MAXN;j++) { map2d[i][j]=rand()%2; map2d[j][i]=map2d[i][j]; }; }

void printmap() { int i,j; struct element *p;

printf("\n\nmap (format 2):"); for(i=0;i<MAXN;i++) { printf("\n%d:",i); for(p=map[i];p!=NULL;p=p->next) printf(" %d",p->node); }; }

(a) Suppose that the function fillmap2d() fills map2d so that it contains the following values. Show what the array map would contain after calling the function translate(). Include all the information that is relevant to the representation of the map exactly as it is stored.

iiiiiiiiiiiiiiiiiiiii^0 1 2 3 0 0 0 0 1 1 1 0 0 1 1 0 2 0 1 0 1 0 3 1 1 1 0 0 4 c 1 0 0 0 0

c

c

c

c

c

c

c

(b) Suppose that the map is changed so that roads now have a direction. map2d[i][j]= now implies that there is a road leading from node i to node j (it does not mean that there is a road from j to i).

(b.1) On the copy of the program given above, mark how the function translate() needs to be changed to accommodate the new definition of direction. Make as few changes as possible. There is no need to consider the other functions.

(b.2) Suppose that a modified version of the function fillmap2d() fills map2d so that it contains the following values. Show what the array map should contain after calling the modified function translate(). Include all the information that is relevant to the representation of the map exactly as it is stored.

iiiiiiiiiiiiiiiiiiiii^0 1 2 3 0 0 1 0 1 1 1 0 0 1 1 0 2 0 0 0 1 0 3 1 1 1 0 0 4 c 1 0 1 0 0

c

c

c

c

c

c

c

(b)

#include <stdio.h>

int main(void) { char ch1,ch2,ch3,ch4; FILE pf1,pf3;

pf1=fopen("fileA","r"); pf3=fopen("fileC","w");

while(fscanf(pf1,"%c%c%c%c",&ch1,&ch2,&ch3,&ch4)==4) fprintf(pf3,"%c%c%c%c",ch1,ch2,ch3,ch4);

fclose(pf1); fclose(pf3); }

Name(s) of the file(s) that change:

Contents of these files:

(c)

#include <stdio.h>

int main(void) { char ch1,ch2; FILE pf1,pf2,*pf3;

pf1=fopen("fileA","r"); pf2=fopen("fileB","r"); pf3=fopen("fileC","w");

while(fscanf(pf1,"%c",&ch1)+fscanf(pf2,"%c",&ch2)==2) fprintf(pf3,"%c%c",ch1,ch2);

fclose(pf1); fclose(pf2); fclose(pf3); }

Name(s) of the file(s) that change:

Contents of these files:

(e)

#include <stdio.h>

int main(void) { char ch; FILE pf1,pf3;

pf1=fopen("fileA","rb"); fseek(pf1,-1,2); pf3=fopen("fileC","w");

while(ftell(pf1)>0) { fscanf(pf1,"%c",&ch); fseek(pf1,-2,1); fprintf(pf3,"%c",ch); }; fscanf(pf1,"%c",&ch); fprintf(pf3,"%c",ch);

fclose(pf1); fclose(pf3); }

Name(s) of the file(s) that change:

Contents of these files:

Problem 3 (10 points)

The following program is compiled and run using the command

a.out file1 file

What will the program print, and what else will it do?

#include <stdio.h> #include <stdlib.h> #define MAXSTR 100

int main(int argc, char *argv[]) { char str[MAXSTR]; FILE *fp;

sprintf(str,"cp %s %s",argv[1],argv[2]); printf("\n%s\n",str); system(str); }

The program will print:

In addition the program will:

Problem 5 (10 points)

Complete the following program so that it would fill the array a with N random numbers in the range [R1,R2]. The program should also fill the array b so that b[i] will be the number of appearances of the number i in a, for R1 <= i <= R2. Note: The program should work correctly if the values of the constants R1, R2 and N are changed. You can assume that R1, R2 and N will remain positive and R2 >= R1.

#include <stdio.h> #include <stdlib.h> #define R1 1 #define R2 50 #define N 20

int main(void) { int i,r,a[____________],b[____________];

for(i=____________;i<=____________;i++) b[i]=0;

for(i=0;i<N;i++) {

r=rand()____________________________________;

a[__________]=__________;

b[__________]___________; };

printf("\n\na:"); for(i=0;i<N;i++) printf(" %d",a[i]);

printf("\n\nb:"); for(i=R1;i<=R2;i++) printf(" %d",b[i]); printf("\n"); }