If during the following steps you cannot follow something, or it doesn’t work, please check with your neighbour that you are doing it right. If it still doesn’t work, raise your hand and the tutor will try to help.
NOTE! There will be a dropbox on NOW called Session8. You are EXPECTED to code for 8-10 hours per week outside class.
So put your programs in the dropbox (just the cpp files, not the whole folder) so that there is evidence of your activity BEYOND the lab.
Exercise 1 – file data handling
Part A – Reading a file of an arbitrary length
Note also that this program is mainly carried out by calling functions, with very little else going on in main. Our aim is always to make the main program as clean as possible.
Part B – Proving that this method can cope with different file lengths…?
This shows the problem that can occur if an accidental newline is added at the end of the file.
Part C – Making the file reading safer
If the last line read (ie stored in tempLine) has a length of 0, decrement the counter value by one, since it should not have been incremented for that empty line.
Part D – Doing it yourself
Part E – Doing it yourself again…
Part F – changing the way it works by changing the file format
In this stage we shall use a file with a counter at the start that specifies the number of entries, so that we don’t have to read the whole file twice.
Discuss these problems with your tutor and see if you can add some error checking code.
Part G – changing your code to use the same structure
Exercise 2 – Problem Solving Examples
Last week you were given a range of problems to solve – on paper first, and then writing the program. Here a few are visited again.
Part A – Finding largest integer
Write a program that prompts the user to enter a set of positive integers in any order (stopping when they enter 0). It then reports the highest number they entered. Do NOT use an array for the numbers.
Solution
The important idea in this problem is that we need a way of remembering the ‘current highest’ value. This is similar to the way we iterate through an array – except we are just comparing the latest value input against our ‘current highest’.
Note that since the problem specifies ‘positive integers’ only, we can initialize the ‘current highest’ to be zero. If the problem had just specified ‘integer’, then we would have had to use a FLAG to ensure that when the first number was entered, it was used to initialize the ‘current highest’. We would also have to find a different way of ending the loop since we might want to include -1 as a possible integer.
So we have a version that can handle negative numbers, and relies on the user entering “Y” to finish. This is where we use stringstream to take input as a string, and then convert it to a number (if possible).
A stringstream is declared and initialised with the text as input. (stringstream valStr(valString);)
An attempt is made to send the stringstream to an integer, thus converting a string into an integer. (valStr >>currentValue)
If the conversion fails ie returns false, then we end the loop. (if (!(valStr>>currentValue))
IMPORTANT – the line where we check whether the highest has yet been set, or whether the currentValue is higher, HAS to check first for !highest.
The c++ ‘if’ does not test multiple conditions if the first one is already true. So this ordering is important, otherwise the ‘if’ will test currentHighest which has not yet been given a value – and will fail.
else if ((currentValue>currentHighest) ||!highest)
Part B – Dinner menus
A school has a dinner menu that repeats itself every 4 weeks. If the menus are denoted by week A,B,C,D, and the school year runs from week 1 to week 36, display a table that shows which menu is in each week of the year. To make this table more compact for printing, get it to print weeks 1 to 18 and their menus in 1 column, and weeks 19 to 36 in the next column.
Solution
We need to start by visualizing what is required. So first write out on paper what the dinners look like eg the one column version:
We know that we have to cycle around every 4 weeks – so if the week number is a multiple of 4 it is a D week, if the week number when divided by 4 has a remainder of 1 it is an A week. This obviously suggests the modulus operation (%)!
Now the program needs to be modified for 2 columns. Again, sketch out what the display would look like:
So the % operation still applies, but we now need to change the loop through the weeks so that it covers week 1+ week 19, week 2 + week 20, week N + week N+18…
This means the loop just goes from 1 to 18, displaying week N and week N+18 on each iteration, with the same modulus tests used on N and N+18 to decide what dinner letter to print.
We provide professional writing services to help you score straight A’s by submitting custom written assignments that mirror your guidelines.
Get result-oriented writing and never worry about grades anymore. We follow the highest quality standards to make sure that you get perfect assignments.
Our writers have experience in dealing with papers of every educational level. You can surely rely on the expertise of our qualified professionals.
Your deadline is our threshold for success and we take it very seriously. We make sure you receive your papers before your predefined time.
Someone from our customer support team is always here to respond to your questions. So, hit us up if you have got any ambiguity or concern.
Sit back and relax while we help you out with writing your papers. We have an ultimate policy for keeping your personal and order-related details a secret.
We assure you that your document will be thoroughly checked for plagiarism and grammatical errors as we use highly authentic and licit sources.
Still reluctant about placing an order? Our 100% Moneyback Guarantee backs you up on rare occasions where you aren’t satisfied with the writing.
You don’t have to wait for an update for hours; you can track the progress of your order any time you want. We share the status after each step.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
Although you can leverage our expertise for any writing task, we have a knack for creating flawless papers for the following document types.
From brainstorming your paper's outline to perfecting its grammar, we perform every step carefully to make your paper worthy of A grade.
Hire your preferred writer anytime. Simply specify if you want your preferred expert to write your paper and we’ll make that happen.
Get an elaborate and authentic grammar check report with your work to have the grammar goodness sealed in your document.
You can purchase this feature if you want our writers to sum up your paper in the form of a concise and well-articulated summary.
You don’t have to worry about plagiarism anymore. Get a plagiarism report to certify the uniqueness of your work.
Join us for the best experience while seeking writing assistance in your college life. A good grade is all you need to boost up your academic excellence and we are all about it.
We create perfect papers according to the guidelines.
We seamlessly edit out errors from your papers.
We thoroughly read your final draft to identify errors.
Work with ultimate peace of mind because we ensure that your academic work is our responsibility and your grades are a top concern for us!
Dedication. Quality. Commitment. Punctuality
Here is what we have achieved so far. These numbers are evidence that we go the extra mile to make your college journey successful.
We have the most intuitive and minimalistic process so that you can easily place an order. Just follow a few steps to unlock success.
We understand your guidelines first before delivering any writing service. You can discuss your writing needs and we will have them evaluated by our dedicated team.
We write your papers in a standardized way. We complete your work in such a way that it turns out to be a perfect description of your guidelines.
We promise you excellent grades and academic excellence that you always longed for. Our writers stay in touch with you via email.