Welcome! Log In Create A New Profile

Advanced

Assignment 2 Question 6c - Advise Please

Posted by myospreyuser 
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 6c - Advise Please
March 28, 2010 01:52PM
The question requests for a FOR loop to be used using a constant. Namely (const int NR_JOBS = 6).

obviously that variable cannot change because its a constant. How should the For loop be:

eg: for (int cnt=1; NR_JOBS > 6; cnt++);

HOW does this loop terminate because we cannot increment NR_JOBS as its a contant!
the loop has to execute 6 times using the constant NR_JOBS.
I cannot even assign cnt to NR_JOBS because NR_jobs is a constant!


help.
Re: Assignment 2 Question 6c - Advise Please
March 28, 2010 01:53PM
Sorry i meant Assignment 2 Question 5c
Please advise regarding the for loop using a constant integer.
avatar Re: Assignment 2 Question 6c - Advise Please
March 28, 2010 03:54PM
You're quite right that NR_JOBS cannot change which makes me wonder why your comparison is "NB_JOBS > 6". If you rewrite what you have in more pseudocode you'll get

1. make NB_JOBS constant with a value of 6 

2. make cnt an integer with an initial value of 1

3. if NB_JOBS > 6 continue with 4. If not, jump forward to 7.

4. /// whatever the contents of your FOR loop would be

5. increment cnt

6. jump back to 3.

7. // the rest of your program

The comparison "6 > 6" will always be false so your FOR loop will never execute. If you want your loop to run for up to NB_JOBS times, you need to compare something to NB_JOBS, not the other way round. Hint: You already have a variable that you're incrementing. winking smiley
Re: Assignment 2 Question 6 - The compiler doesnt like calling functions with mixed variables
March 28, 2010 05:18PM
How do i call a function with mixed variables?

eg:

float DailyWorkout(float hours, float days)

the above will work, but if I mix variables the compiler will complain.

eg:

float DailyWorkout(float hours, int days)

how would i call a function with mixed variables from the main().
and how do I initialize them. Should I initialise the variable as a float then convert it to an integer after returning from the function?

Please advise and show an example.
Re: Assignment 2 Question 6c - Advise Please
March 29, 2010 10:20AM
for your first question about the for loop: it should be (int cnt=1; cnt > NR_JOBS; cnt++); because you are comparing your counter variable with the constant.

calling functions with more than one variable should be done in the order in which your function's signature has been set up, ie.
Language: C++
int myFunction (int param1, float param2) { //whatever you want to do return anyInt; //a function returns something, an int in this case, do not leave this out }

to call myFunction, you would say
Language: C++
myFunction(anIntVar, aFloatVar);
it has to be in the order, you can't mix them up... it won't work

if you want to call your float first then your function declaration should change
Language: C++
int myFunction (float param2, int param1)
Sorry, only registered users may post in this forum.

Click here to login