联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2021-03-26 10:04

Please make sure you have completed Practical 2 before continuing with Practical 3.

Create a new project or file for each question in order to complete the exercises (refer to practical 1 or ask your

supervisor if you are having problems doing so). As seen below, your file should include appropriate comments

consisting of the following – file name, your details, version number and date, brief program description and the

University’s academic misconduct statement. All practical and assignment work you produce in this course

should have the following comments.

/* File: practical03.c

Author: your name

Email Id: your email id

Description: Practical 3 - ...

This is my own work as defined by the University's

Academic Misconduct policy.

*/

Question 1

Properly indent the following statements using the examples in your lecture notes and information found in the

style guide (on course website: 'Additional Resources'->'C Programming Practice'). This

question develops graduate quality 6 by producing work that communicates appropriately with professional

colleagues through source code documentation and demonstrating the capacity to apply international coding

style standards, guidelines and good programming practices.

a)

int a = 1; int b = 1; int c = 1;

if (a == b) { printf("In if statement.\n"); printf("Equal.\n"); } else {

printf("In else statement.\n"); printf("Not equal.\n"); }

The above code should produce the following output:

In if statement.

Equal.

b)

int max = 5;

int count = 0; int k = 0;

while (k < max) {

count = count + 1; printf("In while loop.\n");

k = k + 1; } printf("Count is: %d\n", count);

The above code should produce the following output:

In while loop.

In while loop.

In while loop.

In while loop.

In while loop.

Count is: 5

c)

int k;

for (k = 0; k < 5; k++) {

printf("In for loop.\n");

printf("%d\n",k);

}

The above code should produce the following output:

In for loop.

0

In for loop.

1

In for loop.

2

In for loop.

3

In for loop.

4

Question 2

What is the output produced by the following code? You may check your results by entering the programs into

Microsoft Visual Studio.

a)

int k = 1;

while (k <= 5) {

printf("Stuck in a loop!\n");

k = k + 1;

}

printf("okay then...\n");

b)

int k = 6;

while (k >= 2) {

printf("Still looping.\n");

k = k - 2;

}

c)

int num1 = 2;

int num2 = 4;

if (num1 < num2)

printf("Yes.\n");

d)

int mark = 78;

if (mark >= 85)

printf("HD");

else if (mark >= 75)

printf("D");

else if (mark >= 65)

printf("C");

else if (mark >= 55)

printf("P1");

else if (mark >= 50)

printf("P2");

else if (mark >= 40)

printf("F1");

else

printf("F2");

e)

int k;

for (k =0; k < 4; k++)

printf("%d\n", k);

f)

int k;

for (k = 0; k < 10; k+=2)

printf("k is: %d\n", k);

Question 3

The area of a circle is calculated as follows: area = PI * (radius * radius). Write a program that prompts the user

to enter a radius, calculates the area of a circle and displays the area to the screen (with appropriate text). Refer

to practical 1 solution if you are needing help with this exercise. Create a file in order to complete this exercise.

Question 4

Write a program that asks the user for the number of males and the number of females enrolled in your practical

class. The program should display the percentage of males and females in the class. (Hint: suppose there are 8

males and 12 females in a class. There are 20 students in the class. The percentage of males can be calculated as

8 / 20 = 0.4 or 40%. The percentage of females can be calculated as 12 / 20 = 0.6 or 60%. (Modified: Gaddis,

Tony. Programming Exercises, Chapter 2).

Question 5

Write a program that prompts the user enter a price (dollar amount) and a percentage discount. Calculate and

display the new price with discount. Your output should be presented as follows (user input appears in bold to

differentiate it from text that is displayed to the screen by the program):

Example 1:

Please enter price in dollars: 100.00

Please enter percent discount: 0.25

Price with discount is: 75.0

Example 2:

Please enter price in dollars: 25.0

Please enter percent discount: 0.10

Price with discount is: 22.5

Question 6

Write a program that prompts the user to enter a temperature. If the temperature is greater than or equal to 40

degrees, display the message 'Way too hot – stay inside!', if the temperature is greater than or equal to 30

degrees, display the message 'Hot – beach time!', if the temperature is greater than or equal to 20 degrees,

display the message 'Lovely day – how about a picnic!?!', if the temperature is greater than or equal to 10

degrees, display the message 'On the cold side – better take a jacket!', if the temperature is less than 10 degrees,

display the message 'Way too cold – stoke up the fire!'. Hint: you will need to use an if statement to do this.

Checkpoint: Please make sure your supervisor sees your work so far.

Question 7

Write a program that prompts the user to enter a number within the range of 1 through 10. The program should

display the Roman numeral version of that number. If the number is outside the range of 1 through 10, the

program should display an error message. The following table shows the Roman numerals for the numbers 1

through 10. (Modified: Gaddis, Tony. Programming Exercises, Chapter 4).

Number Roman Numeral

1 I

2 II

3 III

4 IV

5 V

6 VI

7 VII

8 VII

9 IX

10 X

Your output should be presented as follows (user input appears in bold):

Enter an integer from 1-10: 7

VII

Question 8

The date 10 June 1960, is special because when it is written in the following format, the day times the month

equals the year:

10/6/60

Write a program that asks the user to enter a day, a month (in numeric form), and a two-digit year. The program

should then determine whether the day times the month equals the year. If so, it should display a message

saying the date is magic. Otherwise, it should display a message saying the date is not magic.

(Modified: Gaddis, Tony. Programming Exercises, Chapter 4).

Your output should be presented as follows (user input appears in bold):

Enter the day of month: 10

Enter the month in numeric form: 6

Enter the year in two digit format: 60

The date 10/6/60 is a magic date.

Checkpoint: Please make sure your supervisor sees your work so far.

Question 9

This question builds on the program written in an earlier practical and presented in your lecture slides.

 Write a program to generate a random number between 1 –10.

 Display the random number to the screen as follows:

Random number is: 7

 Modify your program so that it asks (prompts) the user to guess the random number.

 Display the user’s guess to the screen.

 Modify your program so that it displays ‘Well done –you guessed it!’ if the user guesses the number

correctly, displays ‘Too low’ if the guess is lower than the random number, and displays ‘Too high’ if the

guess is higher than the random number.

 Add a while loop which allows the user to keep guessing until they guess the correct number.

You may use the solution presented in the lecture slides in order to complete this exercise.

Modify the above program as follows:

a. Generate a random number between 1-100 (instead of 1-10).

b. Add another while loop to ask the user whether they wish to play again (hint: you will need to nest while

loops to get this to work). Refer to the following algorithm for help with this (an algorithm is a set of

steps describing how you would solve a problem – it is written in simple English and provided to help you

solve this problem):

set variable play = ‘y’

WHILE play == ‘y’

generate a random number

prompt for and read user’s guess

WHILE user’s guess is not equal to random number

IF guess < number

Display – ‘'Too low – please try again!'

ELSE

Display – ‘'Too high – please try again!'

prompt for and read user’s guess

prompt whether user would like to play again

Please note: the provided algorithm above is not C code. It has been provided in order to assist you with

solving this problem. Using the algorithm provided, helps you think less about the steps needed in order

to solve the problem, and more about how you would write it in the C programming language.

A note on reading characters:

The scanf function behaves differently when the %c format specifier is used from when the %d conversion

specifier is used. When the %d conversion specifier is used, white space characters (blanks, tabs or newlines, etc)

are skipped until an integer is found. However, when the %c conversion specifier is used, white space is not

skipped, the next character, white space or not, is read. You may find that this causes your loops to 'skip' the

prompt and not provide you with an opportunity to enter a character at the prompt.

Continued on the next page…

A note on reading characters (continued…):

To fix this, leave a space before the %c as seen in the following scanf statement.

scanf(" %c", &response);

For example:

#include <stdio.h>

int main() {

char playAgain = 'y';

while (playAgain == 'y') {

printf("\n\nDo stuff... : )\n\n");

/* space before %c skips white-space characters such as newline */

printf("Play again [y|n]? ");

scanf(" %c", &playAgain);

}

return 0;

}

Make sure your work adheres to the guidelines presented in the C Programming Practice and lecture slides. Make

sure your code is correctly indented and contains comments in order to correctly describe its use. Doing so will

develop graduate quality 6 (use of communication skills) by producing source code that has been properly

formatted, and by writing adequate, concise and clear comments.

Checkpoint: Please make sure your supervisor sees both your program output and code.

Question 10

Write a program that will read numbers until the user enters a negative number. Display the number of values

entered and the largest value entered. Do not use an array for this exercise. (Hint: use a while loop with a nested

if statement to do this).

Question 11

When a part is manufactured, the dimensions are usually specified with a tolerance. Assume that a certain part

needs to be 5.4 cm long, plus or minus 0.1 cm (5.4 ± 0.1 cm). Write a program that prompts for the dimension

and determines whether a part is within these specifications. The program should read dimensions until the user

enters a dimension of zero. (Hint: use a while loop with a nested if statement to do this).

Checkpoint: Please make sure your supervisor sees both your program output and code.

Please make sure you save and keep all of your practical and assignment work. Please ask your supervisor if

you are having difficulties doing so.

End of practical 3.


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp