Вы находитесь на странице: 1из 6

GNG1106 Winter 2016 - Assignment 4 Solution

Deduct 0 to 5 points for not submitting assignment according to instructions (in a zip file,
directory, with source code, etc.)
Question 1 (15 marks)
(a) (5 marks)

Marking Scheme:
Defining and initilialisation of the array in memory 1 marks
0.25 for properly creating the array in memory.
0.5 for first five elements (0.1/value), 0.25 for zeros in last element
-0.5 if 10 elements are not present (ex. only 5 elements)
Defining double pointer and its reference to the array 2 mark
1 for the pointer variable
1 for the reference (adr1 with dashed arrow)
Defining variable x in memory with values 1 mark
0.5 for variable, 0.25 for ? and 0.25 for 8.9 value
Results for each operation 1 marks
0.5 for each update to elements in the array
-0.25 if arrow showing that x is copied is missing.
Total 5 marks
(b) (5 marks)

Marking Scheme:
Variables in working memory for main 1 marks
0.5 for structure variable
0.5 for values (0.25 for correct values in the members and 0.25 for ? in aCube members.)
The char array can be represented as character array with a character in each element.
Variables in working memory for function getInput 2 marks
1 point for cubePtr pointer variable
1 point for adr1 value and dashed arrow showing reference
Console Output 2 marks
1 for input/output due to getInput function (first 5 lines)
0.5 for color output
0.5 for dimensions output (last 2 lines).
-0.5 if blank lines are missing
-0.5 if format in last line not correct (should display 2 numbers in fractional part).
Total 10 marks
Question 2 (10 marks)

/*-------------------------------------------------------
File: A4_Q2.c
Assignment 4, Question 2
Name/student no: Gilbert Arbez, 12345678

Present to the user the characteristics of 4 channels and


ask the user to select a channel number. Calculate and
present to the user the velocity of the channel.
---------------------------------------------------------*/

#include <stdio.h>
#include <math.h>
// Structure definition
typedef struct
{
char name[100];
double n;
double slope;
double width;
double depth;
} CHANNEL;
// Function prototypes
int makeSelection(CHANNEL *);
double computeVelocity(CHANNEL);
/*----------------------------------------------------------
Function: main
Description: Computes average velocity in a channel.
-----------------------------------------------------------*/
void main()
{
CHANNEL channels[4] =
{
{"Channel 1", 0.035, 0.0001, 10.0, 2.0},
{"Channel 2", 0.020, 0.0002, 8.0, 1.0},
{"Channel 3", 0.015, 0.0010, 20.0, 1.5},
{"Channel 4", 0.030, 0.0007, 24.0, 3.0}
};
int cNum;
double velocity;
// Get selection
cNum = makeSelection(channels);
// Compute the velocity
velocity = computeVelocity(channels[cNum]);
printf("The channel average velocity is: %.4f m/s\n", velocity);
}
/*----------------------------------------------------------
Function: makeSelection
Parameter:
chnls - reference to an array of CHANNEL structure values
Returns: The index to the channel selected by the user.
Description: Displays the contents of the array and ass the user
to select one of the channels.
-----------------------------------------------------------*/
int makeSelection(CHANNEL chnls[])
{
int cNum; // channel selected
printf("%s: n = %5.3f Slope = %6.4f Width = %4.1f Depth = %3.1f\n",
chnls[0].name, chnls[0].n, chnls[0].slope,
chnls[0].width, chnls[0].depth);
printf("%s: n = %5.3f Slope = %6.4f Width = %4.1f Depth = %3.1f\n",
chnls[1].name,chnls[1].n, chnls[1].slope,
chnls[1].width, chnls[1].depth);
printf("%s: n = %5.3f Slope = %6.4f Width = %4.1f Depth = %3.1f\n",
chnls[2].name,chnls[2].n, chnls[2].slope,
chnls[2].width, chnls[2].depth);
printf("%s: n = %5.3f Slope = %6.4f Width = %4.1f Depth = %3.1f\n",
chnls[3].name,chnls[3].n, chnls[3].slope,
chnls[3].width, chnls[3].depth);
printf("Please select a channel number: ");
scanf("%d", &cNum);
cNum = cNum - 1; // convert to index in array
return(cNum);
}
/*----------------------------------------------------------
Function: computeVelocity
Parameter:
ch - CHANNEL structure selected by the user
Returns: Average velocity of the channel.
Description: Computes the average velocity of the channel using
Manning's equation.
-----------------------------------------------------------*/
double computeVelocity(CHANNEL ch) // Can also use pointer to CHANNEL
{
double velocity;
double term;
term = (ch.width/ch.depth)/
(ch.width + 2.0*ch.depth);
velocity = (sqrt(ch.slope)/ch.n)*pow(term, 2.0/3.0);
return(velocity);
}
Output
Marking Scheme:

C Program
Structure definition 1 mark
0.1 for each member declaration for total of 0.5
0.5 for typedef struct { } CHANNEL;
Main function (3.5 total marks))
Comments (header) 0.5 mark
Call to makeSelection 1 mark
Calls to computeVelocity 1 mark
Function names can be different than those in the solution.
Output to display result message 1 mark
Function makeSelection (4.5 marks total)
Comments (header) 0.5 mark
Function header/prototype 1 marks
printf instructions to display the selections 1 mark
Prompt and get channel selection 1 mark
Compute index of selection 0.5 mark
Return statement 0.5 marks
-0.5 marks if output is not formatted properly, formatting does not have to match
exactly the formatting shown in the solution but numbers should be aligned.
Function computeVelocity (4 marks total)
Comments (header) 0.5 mark
Function header/prototype 1 marks
Variable declarations 1 mark
Computation of velocity 1 mark
Return statement 0.5 marks

Output (0.5 per output) 2 marks

Total 15 marks