首页 > > 详细

讲解留学生R语言、R程序讲解、讲解R、R编程辅导、解析Technical Programming

Foundations of Technical Programming
Assignment 2
(5 x 7= 35 Marks)
Instructions
1. This assignment contains 5 questions, each question carries 7 marks
2. Submit only one document (Single word document)
3. Copy and paste your codes into a word document
4. Copy and paste the screen shot of the output window in the same word document
5. Use only .doc, .docx extensions – no other format will be accepted for marking
6. Marks will be given for proper indentation and comments (No need to comment each
and every line, but give comments for important parts/sections.
7. For question 3 use SwinGame (SwinGame SDK is available in Canvas).

You may be asked to demonstrate/explain your work to the tutor, if you are
absent/unavailable or fail to demonstrate properly, zero marks will be awarded.

Please note, this is an individual task and it will be checked for plagiarism. All the
involved parties will be penalised if any plagiarism is found.

Please visit https://goo.gl/hQ87zg for more details.

Qn 1.
Some credit card companies use Luhn algorithm to detect invalid credit card numbers.
The key thing in this algorithm is to find a check digit, which should be the rightmost
digit in any credit card number.

In this task, you have to implement a version of Luhn algorithm using the instructions
given below.

Step1: Read in credit card number as a series of digits into an array

Ask the user to enter series of one digit positive numbers into a one dimensional array,
the number -1 is used to indicate the end of series, no need to read in -1 into the array.
The maximum size of the array is set to 20.

Step2: Find sum1 (main should call a function passing array and size)

Ignoring the last (right most digit) check digit of the credit card number, and moving left
double the value of every second digit and find the sum of these doubled numbers. If
the result of the doubling operation is a two digit number, you should add the digits of
the doubled number before finding the sum.

Step 3: find sum2 (main should call a function passing array and size)

Find the sum of all other numbers (last digit is not included in this sum as well).

Step 4: Calculate check sum

Compute the total of sum1 and sum2 and multiply the result by 9, checksum is found by
extracting the rightmost digit.

Image courtesy: http://echeck.org/citi-simplicity-credit-card-review/

5 4 2 4 1 8 0 1 2 3 4 5 6 7 8 9
5+5=10
1+0=1
2+2=4 1+1=2 0 2+2=4 4+4=8 6+6=12
1+2=3
8+8=16
1+6=7
sum1=1+4+2+0+4+8+3+7
= 29
4 4 8 1 3 5 7 sum2 = 4+4+8+1+3+5+7
= 32
Total=29+32
= 61
61 × 9 = 549
9 is the check sum digit.
Qn2. Write a complete C program to create a music player.
Your program should read in several album names, each album has up to 10 tracks as
well as a genre.
First declare genre for the album as an enumeration with at least three entries. Then
declare an album structure that has five elements to hold the album name, genre,
number of tracks, name of those tracks and track location.
You can use the template given below, but highly recommended to use your own
variable names and enumeration list.

Then implement the following options in your main function.
Option 1: call a function add_album – you should use a dynamic memory, it allows the
user to enter the album details.
Option 2: call a function named print_all_album to print out the album details.
Option 3: call a select_track_to_play function that allows the user to choose an album
and then a track to play. It should print out:
“The track you selected ” then the track name “ from the Album: ” then the album name
“ is now playing … from file location: ” then the file location.
When the user selects a track to play your program must call an external program
to play the track.
Option 4: Quit
Screen shot continue in the next page, these screen shots are taken from a single run.
Qn3. This question has two parts.
Part 1:
Write a complete program to draw rectangles on a SwinGame graphics window with width 1 and height
equal to the values of the array elements. Your program should have four functions, main(),
get_color(), fill_array() and draw_bar().
---------------------------------
Function: Main
---------------------------------
Local Variables:
- data (struct sample array with 800 (use SIZE) elements)

Steps:
1: Open a Graphics Window with title “Array rectangles” that is
800x700
2: Call srand() function
3: Call fill_array() passing data array
4: Do
5: Process Events
6: Clear the Screen to COLOR_WHITE
7: Call draw_bar() function passing data array
8: Refresh the Screen limiting it to 60 FPS
9: While Window Close is not Requested
use SIZE 800
---------------------------------
Function: fill_array
---------------------------------
Parameters received – data (struct sample array)
Return type - void
Local Variables:
- i (int for loop counter)

Fill the data array’s value element with 800 random integers ranging
between 0 to 699 (you can take modulus with screen height.

Fill the data array’s clr element with color returned by getcolor
function (getcolor function is called by passing each array element)

---------------------------------
Function: draw_bar
---------------------------------
Parameters – data (struct sample array)
Return type - void
Local Variables:
- i (int for loop counter)
- x,y, rectwidth (float values)
Steps
1: Set the rectwidth the value 1
2: Start a for loop that runs 800 times
3: Set x, the value of rectwidth times i
4: Set y, the value Screen Height – data[i].value
5: Fill Rectangle with data[i].clr, at position x,
y, with width rectwidth and height value part of data
array
---------------------------------
Function: get_color
---------------------------------
Parameters received – num (integer value)
Return type - color
Local Variables:
- clr (color)
- hue (float)
Steps:
1: set hue, the value of num devided with screen height (typecast for
float value)
2: set clr, the value returned by hsbcolor(hue,0.7,0.8)
3: return clr
Output:
Once you have completed part 1 extend your program to include a sort_data function. When the user
press the key S or R (SKEY and RKEY) the sort_data function should be called and it should sort the
rectangles in ascending order as shown below. Sort function is called with in the main inside do … while
loop.
Q4. Consider a sequence of floating point numbers, ix , i = 1, 2, …, m. The mean is defined as:
  mxxxx m /,,21 
the deviation about the mean is
xxd ii  i = 1, 2, …, m
and the standard deviation is
  )1/(,, 22221  mddds m
Write a complete C program to do the following tasks:
(i) Read a positive integer from user as the value of m (need to verify that m is positive
but less than 30).
(ii) Read in the m elements (up to 30) of a one-dimensional floating point array, compute
the sum of these elements, the mean, the deviation, and the standard deviation.
---------------------------------
Function: sort_data
---------------------------------
Parameters – data (int array)
Return type - void
Local Variables:
- I,j and temp (int)
Steps
1: for i, starts at 0 goes to SIZE -1
2: for j, starts at 0 goes to SIZE-1-i
3: if value part of data[j] > value part of data[j+1]
4: Set temp, the value part of data[j]
5: Set value part of data[j], the value part of data[j+1]
6: Set value part of data[j+1] equal to temp
Qn5. Write a complete C program to calculate area and circumference of a circle. Your program should
have the following functions in addition to a main() function.
1. menu – that displays
Do you want to use radius or diameter for the calculation?
Enter 1 for radius
Enter 2 for diameter
Enter -1 to exit
2. diameter_to_radius – receives diameter (double) as a parameter and returns radius (double).
3. random_value_generator – generates random values (double) between minimum and maximum.
4. area – receives radius (double) as a parameter and returns area (double).
5. circumference - receives radius (double) as a parameter and returns circumference (double).

You should start the program by prompting the user to select radius or diameter for the calculation
(validate the choice).
radius:
You should ask the user to choose a unit, either cm or m (validate the choice), for the calculation.
Generate a random double value (use random_value_generator) between 5. 0 (minimum) and 15.0
(maximum) for the radius. Prompt the user to choose either area or circumference calculation (prompt
by using character A for area and C for circumference – validate choice).
diameter:
You should ask the user to choose a unit, either cm or m (validate the choice), for the calculation.
Generate a random double value (use random_value_generator) between 15.0 and 30.0 for the
diameter. Before calculating area or circumference convert diameter to radius (use diameter_to_radius
function).
You should exit the program only when the user inputs -1.
All results are printed in main, show only two decimal numbers in the final result.
The formatting of the output should be similar (including the units) to the screen shot shown.
In this task double means double precision floating point value.
Area of a circle is 𝜋𝑟2 and circumference of a circle is 2πr, where r is the radius of the circle.
radius = diameter/2
Assume the value of π is 3.14159.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!