Welcome! Log In Create A New Profile

Advanced

Assignment 2

Posted by tstevens 
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
Re: Assignment 2
May 03, 2006 09:42AM
Hello Icebabe,,,

You seem to be on a role with assignment 2. I only did question 1 last night. Tonight im doing question 2. Did you cruise through question 2 ?
avatar Re: Assignment 2
May 03, 2006 11:11AM
Hi ComputerGeek, Icebabe.

Notice your names appearing quite regularly on the forums. What coding background, if any, do you come from?

-Valkeye
Re: Assignment 2
May 03, 2006 03:38PM
Hi Valkeye ...

This is the first computer code that Im learning. how about you ?
avatar Re: Assignment 2
May 03, 2006 08:47PM
In 2003 I did a 6 month JAVA diploma at a place in Durbs called Van Zyl and Pritchard, and since then I've tried to teach myself VB6 and VB.NET.

But....C++ has always been the only language that I really care about, so now's the time to learn.

-Valkeye
Re: Assignment 2
May 03, 2006 11:37PM
Hello All,,,,,

Please would someone help me with Question 2 from Assignment 2. I have being trying to get it right all evening.

This is what I have so far for it

2a

//Value-returning functions with one value parameter
#include <iostream>
using namespace std;
int Position;
char charToChar(char c) {
Position = 'c' - 'A' + 1;
if ((Position >= 65) && (Position <= 116)) {
Position += 10;
return Position + 'A' - 1;
}
if (Position < 65) {
return '!';
}
else
return 'c';

}
int main() {
char c, New;
cout << "Type in any character: ";
cin >> c;
New = charToChar(c);
cout << "New character is " << New << endl;
return 0;
}


and for 2b I have this:

//Program to return an integer
#include <iostream>
using namespace std;

char charToInt(char & c) {
int Position = ('c' - 'A' + 1);
Position *= 2;
return Position;
}

int main() {
char c;
int j;
cout << "Type in any character: ";
cin >> c;
j = charToInt(c);
cout << "Changed to " << j << endl;

return 0;

please help....as it doesnt see mto work

Thanks
avatar Re: Assignment 2
May 04, 2006 08:56AM
Hi Computergeek,

I don't have a compiler at the office so there possibly could be a syntax error somewhere in my code.

Firstly in question 2a it seems that in your charToChar function there is a lot of unneccessary code, namely, the int Position. Also, it seems that your assignment of Position is wrong. Your line:
Position = 'c' - 'A' + 1;
will use the actual character c, not the character that you are holding in variable c.
If you want to use Position at all in this manner, rather use: Position = c - 'A' +1;
Although I didn't find it neccessary to use another variable at all. This resembles my code.

char charToChar(char c){

if(c >= 65 && c <= 116)
return char(c + 10);
if(c < 65)
return '!';
return c;
}

Also, you've made the same mistake in 2b. Another thing to watch out for is you've used a reference variable in your charToInt function but you don't assign to it, which means you may as well not have a reference variable at all.

Your body of the char to int function could simply be return c*2; If your return type is of type int, an implicit type conversion to int will automatically be made.

Hope this helps.

-Valkeye
Re: Assignment 2
May 04, 2006 09:20AM
Hey Valkeye

I also did the java course at VZAP in dbn. Are you working in a programming environment now?
avatar Re: Assignment 2
May 04, 2006 10:41AM
Hi,

yeah. Currently coding in VB.NET for a crowd in Kloof. But every so often I've got to jump to VB6 or VBA to do some apps.

When did you do your course at VZAP. I was 2003, July -> December course.

-Valkeye
Re: Assignment 2
May 04, 2006 04:13PM
i did it in 2004.
Re: Assignment 2
May 04, 2006 11:02PM
Hello Valkeye...

Thanks for the advice with regards to question 2. I managed to get it correct.

I attempted question 3 tonight.....im very close....the program doesnt seem to run and i cant see why. Please help. Here is my attepmted code,,please could you see what is wrong with it for me.

//Program to determine the temp differences and maximum difference
#include <iostream>
using namespace std;

float difference(float Temp1P, float Temp2P) {
return Temp1P - Temp2P;
}

float maximum(float Temp1P, float Temp2P) {
if (Temp2P >= Temp1P) {
return Temp2P;

}
else {
return Temp1P;
}
}

int main() {
float Temp1, Temp2, TempDiffs, maximum, difference, MaxTempDiffs;
float Maxdiffs = 0;
for (int i = 1; i <= 10; i++) {
cout << "Please enter the two temperatures of the patient: ";
cin >> Temp1 >> Temp2;
TempDiffs = difference(Temp1, Temp2);
MaxTempDiffs = maximum(Temp1, Temp2);
cout << "The difference in temperature of the two measurements is " << TempDiffs << endl;
cout << "The maximum up to this point is " << MaxTempDiffs << endl;

}
cout << "The largest difference is " << maximum(Temp1, Temp2) << endl;
return 0;
}
Mel
Re: Assignment 2
May 05, 2006 08:26AM
For the maximum function, I used the paramters of Diff & Max. Your code is comparing which of the 2 temperatures is greater, which is not what the question is.

In my main function I initialised Max to 0, and then called the difference function to calculatete the difference. I then called the maximum fuction, which compares if Diff is greater than Max. If Diff is greater than Max, Max = Diff. The return value is max.

A problem in your difference function is that you need to calculate the absolute difference. So, you seed to include <cmath> and then in the function call fabs(Temp1 - Temp2).

Hope this helps!
Re: Assignment 2
May 05, 2006 08:46AM
Your functions share the same name as two float type declarations - you can't do that and the compiler will return an error...
avatar Re: Assignment 2
May 05, 2006 09:52AM
Hi,

I never included cmath for this question. There's nothing wrong with including it, i just didn't. The way I see it for such a simple task, I personally would rather create my own code inside my own function.

If you decide not to include the cmath library, you could either use another variable inside your own function, eg.. float Dif; or you could just use the two variables you currently have, Temp1P, Temp2P.

eg. with another var.

float difference(float Temp1P, float Temp2P){

float Dif;
Dif = Temp1P - Temp2P;
if(Dif < 0)
Dif *= -1;
return Dif;
}

eg. without another var.

float difference(float Temp1P, float Temp2P){

if((Temp1P - Temp2P) < 0)
return Temp2P - Temp1P;
return Temp1P - Temp2P;
}

Personally, I would rather use the example without the other variable because each time you declare another variable, you use more RAM. Now, we obviously won't notice the difference in speed now, with these apps, but lets face it. We don't want to be developing "Hello World" apps for the rest of our lives. So, sooner or later the apps get bigger and bigger, and if we use a seperate variable for every little thing our apps are going to chow the system resources like nothing else.

So that's my view on that. I could be wrong though.

In your main() there's something not right. your maximum function works properly, but you're using it incorrectly.I agree with Mel, you need to call it with the difference in temperatures from the current for loop iteration and the largest difference in temperature up till that point. That way you're always comparing the largest diff with the current diff.

-Valkeye
Anonymous User
Re: Assignment 2
May 08, 2006 11:14AM
Question 3

Hi guys how can i get the maximum seemimgly my cod eis not working.If i use the normal if statement my program prints it correct.

Can you als0 help or show me how to get the maximum difference.

Thanx in advance for your help.
Anonymous User
Re: Assignment 2
May 08, 2006 11:22AM
I'm finding Assigment 2 Q4 & 5 a bit difficult. Any good sammarittan who can help?


sad smiley
avatar Re: Assignment 2
May 08, 2006 12:51PM
Hi,
question 3. The only way to get the maximum difference is to work out a difference of two numbers, multiple times.

with question 4 & 5 you're gonna have to tell us what your having a problem with. We can't just post the answers, that wouldn't help you.

-Valkeye
Anonymous User
Re: Assignment 2
May 08, 2006 01:12PM
Thanx alot for your help i'll take a look at my coding and post it.
Re: Assignment 2
May 08, 2006 01:43PM
Hi valkeye

Why waste code and time in Question 3 just use the C++ build in abs function.

float dif(float t1P, float t2P) {
float dif;
return dif = abs(t1P - t2P);
}

Simple hey.
Mel
Re: Assignment 2
May 08, 2006 01:54PM
Not so simple Brendon! the abs function only works for integers. That's why at the beginning you need to include the cmath library and call the fabs function - the absolute value of a floating number.
Re: Assignment 2
May 08, 2006 02:14PM
Nope not necessary !! Then again it might depend on the compiler u are using. MinGW developer Studio compiler does it for u. Without including library <cmath>. Try it, if it does not work. Let me know.
Sorry, only registered users may post in this forum.

Click here to login