Welcome! Log In Create A New Profile

Advanced

Assignment 2 Queries

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 Queries
March 26, 2009 11:26AM
Hi,

Can someone assist with assignment 2, question 3b?

The function seems to be easy but i just cannot get the formula for this function.
avatar Re: Assignment 2 Queries
March 26, 2009 11:37AM
Something like this ....

Language: C++
int nrContainers(int numPack,int numFit) { /* your code ......... your code ......... your code ......... your code ......... */ return numNeeded; // where numNeeded is a variable of type int that you used in your code above }
smile
Re: Assignment 2 Queries
March 26, 2009 11:39AM
Okay, but would you need to declare "numNeed" as a local or global variable then?

Kronic
avatar Re: Assignment 2 Queries
March 26, 2009 11:52AM
OK... the 2nd value parameter ( int numFit) you get from the answer in Q3a and the first value parameter you get from the input of the user in Q3b
Calculation within function int nrContainers would be something like this ...

Language: C++
int x = nrToBepacked / nrInEveryContainer; // int division discards the remainder //test to see if there is a remainder //can';t have 1/2 of a container !! if ( nrToBepacked % nrInEveryContainer) > 0 x++;   return x

Hope this helps ?grinning smiley
Re: Assignment 2 Queries
March 26, 2009 11:55AM
Ahhh, the light just went on brighter now, thanks Wiseguy!

Kronic
avatar Re: Assignment 2 Queries
March 26, 2009 11:59AM
nishkarb@nedbank.co.za Wrote:
-------------------------------------------------------
> Okay, but would you need to declare "numNeed" as a
> local or global variable then?


It's local within function int nrContainers BUT it gets assigned to the variable nrContainersNeeded when execution returns back to main
nrContainersNeeded = nrContainers(nrToBePacked, nrInEveryContainer);
smile
Re: Assignment 2 Queries
March 26, 2009 05:00PM
Hi guys/gals,

I have a question on functions.

I have a function that has 2 variable parameters say (int x, y).
The function adds these 2 values up & gives you an output, say Z.

1. Where in the program do you decalre variable Z, in the function or main program?

2. If x & y takes on the values of variables in the main program (actual parameters to formal parameters), how does the program know which value should go to x and which to y?
Unless we use the same name variables as in the main program?

Kronic
avatar Re: Assignment 2 Queries
March 26, 2009 05:21PM
Language: C++
//Simple function call with parameters #include <iostream> using namespace std;   //function prototype int func(int x, int y);   //main program int main() { int n1, n2;     cout << "Enter 2 integers" << endl; cin >> n1 >> n2;   //calling function with parameters (n1 and n2) // NB! The position of the parameters when the call is made !!   cout << n1 << " + " << n2 << " = " << func (n1,n2) << endl;   return 0; }   //function implementation int func(int x, int y) {   return ( x + y);   }     /* OR like this   int func(int x, int y) { int z; return (z = x + y);   } */

Hope this helps !!
smile
Re: Assignment 2 Queries
April 06, 2009 09:42AM
Hi,

Does anyone know the difference between these 2 operators? :

" = "

" =="

Don't know when you use what.

Kronic
avatar Re: Assignment 2 Queries
April 06, 2009 10:22AM
nishkarb@nedbank.co.za Wrote:
-------------------------------------------------------
> Hi,
>
> Does anyone know the difference between these 2
> operators? :
>
> " = "
>
> " =="
>
> Don't know when you use what.

"="
This operator is used to assign(allocate) data to a variable(can be of type int, char, etc) ; eg.
The above stores the number 5 in the variable of type int
Language: C++
int x = 5;

"=="
This operator compares; eg.
Language: C++
int x = 5;   if (x==6) then cout << " True" << endl; else cout << "False" << endl;

The output will be "False" since x is not 6 but 5 !
ie. for the operator to do what it's suppose to the variable x on the left of the ("==" ) must be intialised before.

Hope this makes sense
smile
Re: Assignment 2 Queries
April 06, 2009 10:28AM
hmmm,

Ok thanks, i will use the "==" operator more when i perform condtions or something similiar.

Kronic
avatar Re: Assignment 2 Queries
May 06, 2009 11:40PM
I lost a couple of marks using "=" in places where I should've used "==", but I still managed to get good marks. smile
Sorry, only registered users may post in this forum.

Click here to login