Welcome! Log In Create A New Profile

Advanced

Study Guide Activities

Posted by ruig 
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
Study Guide Activities
January 10, 2009 05:30PM
Hello People spinning smiley sticking its tongue out

Was wondering if anyone else had this same question as me?

When one completes the very first activity on page 2 of Lesson 1 (Hello World), the program runs but only for a few seconds(one does not actually see "Hello World".

If one opends up a command prompt and navigates to the folder containing the exe, runing it produces the required outcome.

Is this because it's a comand prompt program, or is it operating system specific?( I have run other programs from the desktop directly where the compand prompt does not exit without tying in the exit command).

Operating system is Windows XP SP 3.

Thanks
Rui
Re: Study Guide Activities
January 10, 2009 11:26PM
Hi Ruig

Assuming that you sourced your compiler and IDE from the Unisa disk, the basic structure of the program would be like the ff exactly:


#include <iostream>
using namespace std;

int main()
{
cout<<"Hello world"<<endl;

return 0;
}
Then I would suggest appending the code "system("PAUSE"winking smiley;" just above the "return 0" line.
I see mine prompts for the "press any key to continue...." twice, which means it should force your command prompt to wait for your intervention. I hope that makes sense. Try it and see.

NB: Mine works fine without this extra line of code, I just put it to test out this theory of mine. I am running WINXP SP2 by the way.
Re: Study Guide Activities
January 10, 2009 11:28PM
The line is meant to be system('PAUSE"winking smiley ; I c a smiley has been added for some strange reason.
Tx
Re: Study Guide Activities
January 11, 2009 02:01PM
Hi Zakhele,

Thanks for the pointer. Your solution does work for me smile

The question still remains, why does yours work without the "System(Pause);" entry and mine does not?
Surely it can not be the fact that I am running SP3?

hmm an intresting one smile

Later
Rui
Study Guide Activities - Lesson 4.b
January 11, 2009 02:44PM
Ignore this, page 45 has the answer smile

Hi,

Can anyone advise why the value of 15 is given to the variable answer (from the code below (page 44 in the study guide))
Is this a random number assigned because no value or 0 is not specified, so the compiler assigns a number?

//Lesson 4 - Activity 4.b
//Thinking of a number
#include <iostream>
using namespace std;

int main()
{
int number = 40;
int answer;

....

}

Thanks
Rui
Re: Study Guide Activities
January 11, 2009 09:01PM
Glad I could help Rui, I suppose finding out why command prompt does not work ok is something to be researched at some other time. I certainly don't see it hindering your progress. I see you also discovered that unassigned variables tend to carry "bogus/ghost values". I have made it a practice of mine to assign values at declaration time where possible, it just makes it a lot easier to trace and debug my code later.
avatar Re: Study Guide Activities
January 12, 2009 06:31PM
ruig Wrote:
-------------------------------------------------------
> Hi Zakhele,
>
> Thanks for the pointer. Your solution does work
> for me smile
>
> The question still remains, why does yours work
> without the "System(Pause);" entry and mine does
> not?
> Surely it can not be the fact that I am running
> SP3?
>
> hmm an intresting one smile
>
> Later
> Ruit

The solution to your problem is to create a new folder in C drive and name it COS111, making sure there is NO spaces in the folder name.
Now use this folder to do all your programming from.

It has something to do with the spaces in the folder name.
Re: Study Guide Activities
January 12, 2009 09:07PM
Oh! I see, strange though how that could influence the command prompt. Noted.
Thanks.
Re: Study Guide Activities
January 13, 2009 09:11AM
Nice CountZero.

Lesson Learnt smile
Re: Study Guide Activities - Lesson 4.b
January 13, 2009 10:48PM
Hi Ruig ,

When you do not explicitly assigned a value to a variable in C++ , that variable will be assigned a kind of random value (not really random) from the heap.
That is why you see a strange value on that variable (in your case the integer answer.).



ruig Wrote:
-------------------------------------------------------
> Ignore this, page 45 has the answer smile
>
> Hi,
>
> Can anyone advise why the value of 15 is given to
> the variable answer (from the code below (page 44
> in the study guide))
> Is this a random number assigned because no value
> or 0 is not specified, so the compiler assigns a
> number?
>
> //Lesson 4 - Activity 4.b
> //Thinking of a number
> #include
> using namespace std;
>
> int main()
> {
> int number = 40;
> int answer;
>
> ....
>
> }
>
> Thanks
> Rui
Stu
Re: Study Guide Activities
January 15, 2009 09:09AM
This may have nothing to do with anything...But my console windows also kept on closing when trying to run the "Hello World" program. I found that the saved file name was incorrect.i.e it doesn't like "spaces"..... save as "myfirst.cpp" and not as "My Frirst.cpp".
avatar Re: Study Guide Activities
January 21, 2009 03:00PM
ruig Wrote:
-------------------------------------------------------
> Hello People spinning smiley sticking its tongue out
>
> Was wondering if anyone else had this same
> question as me?
>
> When one completes the very first activity on page
> 2 of Lesson 1 (Hello World), the program runs but
> only for a few seconds(one does not actually see
> "Hello World".
>
> If one opends up a command prompt and navigates to
> the folder containing the exe, runing it produces
> the required outcome.
>
> Is this because it's a comand prompt program, or
> is it operating system specific?( I have run other
> programs from the desktop directly where the
> compand prompt does not exit without tying in the
> exit command).
>
> Operating system is Windows XP SP 3.
>
> Thanks
> Rui

Hi there.
Most if not all of the compiler problems that you will experience was experienced by fellow students in the past.
Take time to search some of the COS111-U threads in the past years.thumbs up smiley
Re: Study Guide Activities
January 22, 2009 11:37AM
Hi has anyone succeded in installing the UNISA C++ on Windows Vista Home Basic. I have tried on a Dual Core
laptop. I do get the icon on Desktop but when I try to run it, the GUI itself looks very funny and when I try to write a
program the thing complains of an error on memory 0x00678. I dont think this is true. Maybe I need to setup my machine, but how do I do that.

Please help...
Re: Study Guide Activities
January 22, 2009 09:32PM
Hey Henrys,

I have installed it successfully on my dual core running vista. You do have to change some settings however.

If you have tutorial letter 101, you will find on page 102, Appendix 2. It explains the process of changing your default program settings. After you installed the C++, change those settings and when you open C++ it should work. If you don't have a hard copy of the tut letter, download it from osprey or myunisa.
Re: Study Guide Activities
January 27, 2009 11:17AM
Hi G9rd tks for that just that I havent registered yet. Im waiting for sup. results. Just wanted to get the thing running
bfore I get my study material.

Could u please post/email me the extract that talks about the setup please??

Tks again.
avatar Re: Study Guide Activities
February 04, 2009 01:54PM
Quote
mikek
When you do not explicitly assigned a value to a variable in C++ , that variable will be assigned a kind of random value (not really random) from the heap.
That is why you see a strange value on that variable (in your case the integer answer.).

Half right. There are two kinds of memory available to you: stack and heap. For the purposes of COS111, you'll only be using stack memory and you won't even have to think about it.

Now that's over with, when you define a variable, only space in RAM is reserved, nothing is assigned to that memory. In other programming languages, you usually get a default value (zero for numerical types or empty strings). C++ doesn't waste that time, it doesn't assume to know what you want as a starting value so it leaves that up to you to decide.

If you do not provide an initial value, whatever was in the RAM when the space was reserved will be your starting value.
Re: Study Guide Activities
February 09, 2009 09:23AM
Have you received a solution for this problem?"> When one completes the very first activity on page
> 2 of Lesson 1 (Hello World), the program runs but
> only for a few seconds(one does not actually see
> "Hello World"."
For I am experiencing the same problem using WinXP sp2
Re: Study Guide Activities
February 16, 2009 11:34PM
Dude, the posts are there for you to read smile

There are several options for you to consider in this thread, personally I went with the one where spaces are removed from the path to the file (*.cpp) for the first program.

Hope this sets you on the right path...
Study Guide Activities - Lesson 12
March 02, 2009 07:20PM
Activity 12.a

Althought the lesson is about nested if statements, and there are multiple correct solutions to a given problem,
The lesson does not state why one would use nested if statements, unless there are three or more conditions that need
to be evaluated to either true or false. (Is this correct?)

The only reason that I am asking is that I came up with an answer that does not use nested if statements yet:
1) easier to read
2) still uses two if statements
3) meets all the criteria and is not so hard on the head.


I have included it and would greatly appreciate an answerconfused smiley

Language: C++
//Lesson Notes //Lesson 12 - Activity 12a //Write a program that reads in the starting and finishing times //of a waitron and calculates the wage for the work done. //Constants are: // R32.50 per hour between 1 - 6 // R44.00 per hour between 6 -12     #include <iostream> using namespace std;   int main() { //declare variables const float DAY_RATE = 32.50; //holds the day rate const float NIGHT_RATE = 44.00; //holds the night rate int dayRange, nightRange; //hold the amount of hours float amount = 0.00; //holds the amount to be paid int startTime, endTime; //holds the times   //ask and store for user start time cout << "Wage Calculation" << endl; cout << "================" << endl; cout << "Starting time: "; cin >> startTime;   //ask and store for user end time cout << "Finishing time: "; cin >> endTime;   //calculate the amount that needs to be paid dayRange = 6 - startTime; nightRange = endTime - 6;   if (dayRange !=0) { amount += dayRange * DAY_RATE; }   if (nightRange !=0) { amount += nightRange * NIGHT_RATE; }   //display the result cout.setf(ios::fixed); cout.precision(2);   cout << endl; cout << "The payment is R " << amount << endl;   //end the program return 0;

Regards,
Rui
avatar Re: Study Guide Activities
March 03, 2009 09:06AM
Consider the following input:
Starting time = 7
Finishing time = 12
Output from program in Activity 12a:
Wage calculation
================
Starting time: 7
Finishing time: 12
The payment is R 220.00
Press any key to continue . . .
Output from your program:
Wage Calculation
================
Starting time: 7
Finishing time: 12

The payment is R 231.50
Press any key to continue . . .
The reason for the difference is that both your if statements will be evaluated whilst only one of the (nested) if statements in the original program in the Activity will be evaluated; ie.
Either this
if (start < 6)
OR
else
wage = (finish - start) * EVENING_RATE;
Debugging your program showed that when the compiler evaluates the first if statement the variable amount = -32.50
Execution moves to the next if statement and evaluates
Language: C++
amount += nightRange * NIGHT_RATE;
But since the variable 'amount' has a value = -32.50 ( calculated previously ) the above statement evaluates to
amount = -32.50 + 6 * 44.00 = 231.50

So getting back to what you said earlier:
The lesson does not state why one would use nested if statements, unless there are three or more conditions that need
to be evaluated to either true or false. (Is this correct?)
It depends on the problem at hand and how you want to implement it.
When using nested if statements you usually have the problem where a decision must be made based on what happens first and if that doesn't happen what happens next , etc.
Later on you'll learn another decision structure called "case statements" which can replace nested if statements.

Hope this makes sense thumbs up smiley
Re: Study Guide Activities
March 03, 2009 09:40AM
Thank you.

So I could change the code to be !<=0 and it would work.
Also orking through the rest of 12 it makes sense to use nested if statements when input is required in order to proceed based on a logicall outcome.

Do you agree?
avatar Re: Study Guide Activities
March 03, 2009 09:53AM
Quote:
So I could change the code to be !<=0 and it would work
I'm curious to know how you'll implement it .. post (or pm if you wish) your updated version of the 'if' statement which should result in the same answer as in the Activity
Quote:
Do you agree?
I agree !!
Re: Study Guide Activities
March 05, 2009 09:05PM
okay so I changed the statements to read >=0 smileys with beer that should work, (it does I tested it)

Secondly how did you debug, since the software that comes on the disk ensures that the debugger is disabled. (Well for me it is, and when I asked on the forums for help on this, well let's just say that I am waiting for a response .

Chat Later
15 Days to deadlines!
avatar Re: Study Guide Activities
March 06, 2009 08:39AM
F8 to debug (provided that you did compile before- you obviously have to put in breaks) and F7 to step.
CTRL_ALT+F2 to stop debuggingthumbs up smiley
Re: Study Guide Activities
March 11, 2009 11:02AM
Hi everyone.
I need help with activity 14.h, the solution given in the book is incorrect
avatar Re: Study Guide Activities
March 11, 2009 11:18AM
Are you referring to this one ?
Language: C++
//Calculates the number of digits in a number #include <iostream> using namespace std; int main( ) { int number, num, count; //Prompt the user for a number cout << "Enter an integer: "; cin >> number; num = number; //Determine the number of digits in the number count = 0; do { count++; num /= 10; } while (num != 0); cout << number << " contains " << count << " digit(s)" << endl; return 0; }

If so what is the size of the integer that you are using as input ?eye rolling smiley
Sorry, only registered users may post in this forum.

Click here to login