Welcome! Log In Create A New Profile

Advanced

Assignment 2 Question 1d

Posted by Jubei 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Assignment 2 Question 1d
March 24, 2011 05:17PM
Hi all

We were given this fragment of code and told to make changes to lines 7,9 and 11.

1 float cents = 0.01;
2 float total = 0;
3 total = cents;
4 cout << "Day Amount" << endl;
5 cout << "1 " << cents << endl;
6
7 for (int i = 2; i <=30; i++)
8 {
9 cents = 2 * cents;
10 cout << i << " " << cents << endl;
11 total = total + cents;
12 }
13
14 cout << "Sibusiso must pay his sister a total of: " << total + 0.01 << endl;

As you can see I needed to make a change at line 14 in order to avoid a logical error. Does anyone know of a way to include the change I made in line 14 into changes in line 7,9 or 11 in order to make this code function logically?
avatar Re: Assignment 2 Question 1d
March 24, 2011 07:35PM
I'm not registered so I don't have the questions but I think I understand what the point of the program might be.

If I understand correctly, your task is to produce a program that will calculate what the sum would be if you add incremental number of cents over a period of 30 days, ie On day 1 you have 1c. On day 2 you add 2c to your total, making it 3c. On day 3, you add 3c to your total, making it 5c. etc. If I'm correct, then lines 9 and 11 will definitely need to be changed. I'm not sure why 7 would need to be changed.

Firstly, before doing anything, you should calculate manually what the total should be in the end and then keep checking the total your program produces to see if it gets it correct. When in doubt, always make a trace table. You don't need to do one by hand, you can use a spreadsheet if you like.

Line 7 represents the time period. Since you've already got the first day's cent, you'll need to have the day's number of cents for each iteration. This appears to be what is being done already. I'll have to see the full question to see why they want line 7 changed.

Line 9 appears to be where you calculate how many cents need to be added to the total.

Line 11 is simple. This is a factorisation error. What this means is that the total represents rands yet you're adding whole cents. you need to convert your cents to rands before accumulating the total.

You didn't mention what logical error you got that necessitated the change in line 14.
Re: Assignment 2 Question 1d
March 24, 2011 09:21PM
Thank for the detailed reply robanaurochs.

You have given me my first lesson in making my programming questions clear smiling smiley

The whole point of the question was to fill in or add statements at lines 7,9 and 11 only. What I provided was what I had filled in already ie the fully working program. As presented there is a logic error, in that the total will be off by 0.01 rands.

What I failed to see was line 3 when I entered the program in the first time, this omitted I started to work on the program, it saves me correction at line 14. The only error is thus in my transcribing. By removing the "+ 0.01" to "total" in line 14 the program will function correctly.

Concerning the factorization error in line 11: Just a bad pick of variable name "cents" - perhaps should be "dailyPayoutAmount" or something like that. When the variable cents is initialized in line 1 it is 0.01 or 1/100 of a rand, so it is already in the correct unit (rands) - meaning the total when displayed will be in rands. Line 14 as well as the Amount column probably need to explicitly include a rand symbol to make this clear:

Language: C++
5 cout << "1 " << " R "<< cents << endl; 10 cout << i << " R " << cents << endl; 14 cout << "Sibusiso must pay his sister a total of: R " << total << endl;

Thanks again for the reply and I hope what I have written is clear and understandable smiling smiley
Sorry, only registered users may post in this forum.

Click here to login