Don't really know what to do first year. 'Codeblocks'
Task 1 (30%): Create a project consisting of a single file named program.cpp
Create an empty “main” and check that program compiles: Let us get into the habit of very
frequent see if it still compiles checks. Don’t wait till the end of typing 10000 lines to see
if a code compiles! Fix the bugs one at a time as you go along, its easier.
Write in the main code lines to create an integer named i and then to ask the user via the
screen to enter a value for it in the range 0-10. We will calculate the square of their value.
Test if i is in the allowed range and if not, keep asking the user for a value until one in the
range 0-10 is entered3
. Print to the screen the square of i and ask if the user wishes to start the process again4
not terminate the program.
Task 2 (35%): I think of fuel economy in terms of miles per gallon, MPG, but
the bike's display gives me miles travelled and the petrol pumps
tell me litres used.
Now modify the above program to the do the following
MPG =4.55 * miles travelled / litres of fuel used
Create a function that accepts two floats representing miles and litres and then evaluates
and returns the MPG.
In the main code,
Ask the user to enter any values for miles and litres
Call the function and print to the screen the result for MPG
Once this works run your code and as the user enter miles=1 and litres=0
It is obvious5
that the code will crash – where and how?
Imagine we did not know why or where the crash occurred.
Start the program under the debugger6
.Step through the code one line at a time, until the
error is reached. You will get a message about floating point division by zero.
So first issue – WHERE IS THE ERROR OCCURRING – has been resolved.
The error message helps us identify WHAT CAUSED THE ERROR.
Examine the variables in the debugger to see that litres is indeed 0 – woops!
Programs don’t need to be that complicated before it becomes almost impossible to track
down errors on our own: LEARN HOW TO USE THE DEBUGGER NOW AT AN
Task 3 (35%):
We often need/wish to manipulate a lot of data together.
You will please write a program to evaluate the average value of a set of float data.
We will store the float data in an array7
and we will assume a maximum of 10 values.
Write a function which must accept two arguments, the data array8
and how many data
values there are in it to be averaged. The function must return a float which is the average
of the data provided. Note if the user entered just 3 values then we must average over only
the first 3 array elements not all 10.
In the main code first create a float array to hold 10 values.
Then repeatedly ask the user how many floats they wish to enter until they enter a number
in the range 1-10.
Then ask them to enter that many data values and fill (part of) the array.
Call the averaging function and print the result to the screen.
By the way, you can choose any names you like for the data array, number of values and
the averaging function. BUT, it would daft to call them a, b and func right? When we debug
large codes we need as much help as possible, so be clear. So why not call them
data_array, number_data_elements and average_value ? Good Practice9
There is three tasks of it
Cus i'm first year so not sure what to do.
I hope someone can help me.
You are Here: Home > Forums >< University and university courses >< Science, Technology, Engineering and Maths university courses >< Engineering
Electronics and Electrical Engineering Watch
- Thread Starter
- 02-02-2017 13:03
- Study Helper
- 02-02-2017 13:35