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
avatar Re: Assignment 2
May 08, 2006 03:55PM
Hi Brendonw,

As I posted further up, there's nothing wrong using the one in cmath. But once again, it's such a small amount of code, why NOT do it yourself?

Remember, these are small projects we're doing, actually, they're miniscule. What... around 50 - 100 lines of code? If that.

When I did my final project at VZAP our project was over 10,000 lines of code. And even that is a small project. If I remember correctly Windows NT4 was 16,000,000 lines of code. If you import entire libraries to do a simple function that takes up, what, 3 lines of code, your program would have so many includes, and would be huge.

If I understand how includes work correctly, don't they take the entire file and almost paste the code in?

Now, I'm not saying don't use cmath, who am I to say something like that anyway, especially cause you probably will use a lot more functions in the cmath library, and it therefore becomes more effective to include it. I've just got an idea in my mind that says, if I can do it and if it wont take up too much time, then do it myself.

Hope this helps.

-Valkeye
Re: Assignment 2
May 08, 2006 04:11PM
Hi Valkeye,,,,

You seem to know alot about programming......shit man...but I want to be like you hey !!!

I have completed the first 4 question of assignment 2 and I have the correct answers. Tonight Im going to give Question 5 ago. Will you be on your computer tonight incase I need to ask you anything if Im stuck. Hopefully I will be able to work it out.
avatar Re: Assignment 2
May 08, 2006 04:56PM
I'll try to get on, but my evenings are quite hectic. I'll check for posts at around 9:30 or so.

But the questions are all straight forward enough, so i'm sure you'll do fine without any help. smiling smiley

-Valkeye
Re: Assignment 2
May 08, 2006 08:27PM
Valkeye,
sorry I reply only now to your message. I am a bit busy studing some other subjects, so I did not watch the forum. Believe me this is my first
coding experience.

Icebabe
Re: Assignment 2
May 08, 2006 11:57PM
Hi All....

Who of you could please tell me where I am going wrong in Question 5. I have given it a very good try and I dont seem to see wherei m going wrong. Here is the code I have written so far. Please help !!

//Program to calculate the average ages of people classified in different
//dreaming groups
#include <iostream>
using namespace std;

void inputData(char & CodeP, int & AgeP) {
cout << "Please enter the code and age of the person: " << endl;
cin >> CodeP >> AgeP;
if ((CodeP != 'A'winking smiley || (CodeP != 'B'winking smiley || (CodeP != 'C'winking smiley || (CodeP != 'D'winking smiley ||
(CodeP != 'X'winking smiley) {
cout << "Please enter the correct code of the person: " << endl;
cin >> CodeP >> AgeP;
}
}
int main() {
int NumPeople = 0;
int TotalAge = 0;
float AverageAgeA, AverageAgeB, AverageAgeC, AverageAgeD;
char Code;
int Age;
while (Code != 'X'winking smiley {
inputData(Code, Age);

switch (Code) {
case 'A':
NumPeople++;
TotalAge += Age;
AverageAgeA = (TotalAge / NumPeople);
break;
case 'B':
NumPeople++;
TotalAge += Age;
AverageAgeB = (TotalAge / NumPeople);
break;
case 'C':
NumPeople++;
TotalAge += Age;
AverageAgeC = (TotalAge / NumPeople);
break;
case 'D':
NumPeople++;
TotalAge += Age;
AverageAgeD = (TotalAge / NumPeople);
break;
}
}
cout << "The average age of group A is " << AverageAgeA << endl;
cout << "The average age of group B is " << AverageAgeB << endl;
cout << "The average age of group C is " << AverageAgeC << endl;
cout << "The average age of group D is " << AverageAgeD << endl;
return 0;
}
Re: Assignment 2
May 09, 2006 07:46AM
ComputerGeek,
I am looking at your code - do you have an email address?

Icebabe
Re: Assignment 2
May 09, 2006 07:48AM
Howzit.

Use seperate variables for each age group and the no. of people in each age group.
Calc the ave after you collect all the data.
Use an if statement so if there are no people in the group you don't get a floating point error.

Hope this helps.

Devesh
Re: Assignment 2
May 09, 2006 08:05AM
ComputerGeek,
mail me at babyblade at gmail.com
Icebabe
Mel
Re: Assignment 2
May 09, 2006 08:13AM
Thanks for that tip Brendon, I will try it. I saw that function in Apendix D (I think!) of the study guide, so I thought it was essentialto declare cmath!
Re: Assignment 2
May 09, 2006 08:21AM
ComputerGeek, mail me and I'll send you the solution to your code.

Icebabe
Re: Assignment 2
May 09, 2006 09:15AM
Hi Mel

Thanx for the tip on global constants. Try some of the functions in library <iomanip>, fantastic in manipulating output results.
avatar Re: Assignment 2
May 09, 2006 09:47AM
Hey Computergeek,

have you come right with assingnment 2 or do u still need a bit of assistance?

-Valkeye
Re: Assignment 2
May 09, 2006 10:47AM
Hi All....

You all very helpfull....I guess this forum thing really works.

Valkeye......I just have question 6 left. But I'd like to give it a good bash on my own first before I get any help with it. Icebabe is going to e-mail me the solution to question 5. I will study her solution and see where I went wrong

Thanks
Re: Assignment 2
May 21, 2006 10:00AM
Hiya!

Currently busy finishing up Assignment 2 (Have extension on it due to being away for work)

I've got problems with Question 2C and Question 5. The one doesn't compile and Question 5 there is a problem with the while and if statements.Anyone that can possible help??Urgent as I need to hand it in latest Wednesday.

Thanks!!
Anonymous User
Re: Assignment 2
May 23, 2006 10:24AM
Hey whizza send me your e-mail address perhaps i can help coz i also struggled with question 2 and 5.But mine compiled successfully and i also got some help from fellow students.

This phorum is very helpfull and i believe we can help each other to make a success of this module.
Re: Assignment 2
May 23, 2006 01:01PM
Hey whizza, don't know if this will be in time to help, but I only did Assignment 2 last night myself.

In Question 2, in my main function, I use 2 if statements inside the for loop to check if it was an even or odd entry. Since we have a variable counting the entries in a for loop, I used % to determine this.

for (int i = 1; i <= 12; i++) {
...
...
if ((i % 2) == 1) // Checks if odd entry
New = charToChar(c);
else if ((i % 2) == 0) // Checks if even entry
j = charToInt(c);
...
}

As for the function charToChar, remember that arithmetic functions can be performed on a char, so statements like this will work:
if ((c >= 65)&&(c <= 116))

No ifs are needed for charToInt. Just assign the value of c into an integer, multiply the integer by 2, and return it.
Re: Assignment 2
May 23, 2006 03:48PM
With regards Question 5, there are 10 variables to declare: two for the individual's dreamgroup and age that gets input (eg DreamCode and Age); four for the number of people in each group (eg NumA, NumB,...) - ensure these are all initialised to 0; and four for the total age of each group (eg TotalAgeA, TotalAgeB...) - ensure these are all initialised to 0.

Then run inputData on the input variables DreamCode and Age (make sure the variables are passed by reference). Then the schematic of the while loop looks something like this:

while (DreamCode != 'X'winking smiley {
switch statement for DreamCode {
in the case of 'A', add 1 to NumA & add Age to TotalAgeA
in the case of 'B', add 1 to NumB & add Age to TotalAgeB
in the case of 'C', add 1 to NumC & add Age to TotalAgeC
in the case of 'D', add 1 to NumD & add Age to TotalAgeD
} // end of switch
call the inputData function again
} // end of while

Then you can calculate and display the required output.

Incidentally, for the function inputData, I also used a switch statement after asking for and reading in the DreamCode, where 'A', 'B', 'C' & 'D' then asked for the age, 'X' did nothing, and the default case called inputData again.

It may not be the best way of doing things, but it is the way I did it, and I hope it helps smiling smiley

Re: Assignment 2
May 23, 2006 09:42PM
Thanks a stack for the help guys!!

Yeah , we're going to have to stick through it together smile

W
Sorry, only registered users may post in this forum.

Click here to login