| | |
Interpretation of an instructors C++ program...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2003
Posts: 2
Reputation:
Solved Threads: 0
Below is posted a program as assigned halfway through my C++ class. I do not need any help doing the project itself, I just need help trying to get through the typographical errors and vagueness! Can someone give a shot at possibly trying to rewrite what he is asking for?
In my opinion one aspect of what he is asking for is conflicted 3 times!
Write a program with overloaded function for computing the average of integers, or appending characters (same name average for both functions). The integer unction should allow between 1 and 5 arguments, the character function between 0 and 5 - all these should be handled with default arguments. Test in an application, program looping until the user says no more (end of the file). Every time through the loop, the program will ask whether integers or characters are being given and how many (1 to 5 or 0 to 5), check the entry, and then will ask for the proper number of inputs separated by spaces. You may assume correct data type and a match beween the number of arguments stated and actually supplied. For characters, the function should produce a dynamically allocated NTS string by appending all the characters, returned by a pointer. Make sure not to leak memory. For integers, the average will be produced (as a float), returned by copy. If the call is made with no arguments, an exception message (string) should be thrown and handled by catching it, displaying "Error: " followed by the actual message thrown, and going on for another iteration of the loop.
Note: there will be different function calls for different number of arguments, thus for example if the user enters 2 characters a and b, the call should be average(a,b) and if the user entered no arguments the call should be average().
Make sure to put the functions in fun.cpp (with fun.h) and the application in app.cpp
For example, this is how the program should run (follow closely, user input in italic):
Welcome to my average program...
What data you have and how many: 1=integers 2=chars: 1 2
Enter 2 integers: 10 20
The result is 15
What data you have and how many: 1=integers 2=chars: 2 2
Enter 2 characters: a b
The result is ab
What data you have and how many: 1=integers 2=chars: ^D
Thank you.
In my opinion one aspect of what he is asking for is conflicted 3 times!
Write a program with overloaded function for computing the average of integers, or appending characters (same name average for both functions). The integer unction should allow between 1 and 5 arguments, the character function between 0 and 5 - all these should be handled with default arguments. Test in an application, program looping until the user says no more (end of the file). Every time through the loop, the program will ask whether integers or characters are being given and how many (1 to 5 or 0 to 5), check the entry, and then will ask for the proper number of inputs separated by spaces. You may assume correct data type and a match beween the number of arguments stated and actually supplied. For characters, the function should produce a dynamically allocated NTS string by appending all the characters, returned by a pointer. Make sure not to leak memory. For integers, the average will be produced (as a float), returned by copy. If the call is made with no arguments, an exception message (string) should be thrown and handled by catching it, displaying "Error: " followed by the actual message thrown, and going on for another iteration of the loop.
Note: there will be different function calls for different number of arguments, thus for example if the user enters 2 characters a and b, the call should be average(a,b) and if the user entered no arguments the call should be average().
Make sure to put the functions in fun.cpp (with fun.h) and the application in app.cpp
For example, this is how the program should run (follow closely, user input in italic):
Welcome to my average program...
What data you have and how many: 1=integers 2=chars: 1 2
Enter 2 integers: 10 20
The result is 15
What data you have and how many: 1=integers 2=chars: 2 2
Enter 2 characters: a b
The result is ab
What data you have and how many: 1=integers 2=chars: ^D
Thank you.
Seems straight forward to me. This is more academic than anything. You wouldn't do it in real life.
You will wind up with 11 functions like
string somefunc()
float somefinc(int a)
float somefunc(int a,b)
...
string somefunc(char a)
string soemfunc(char ab)
The only vague part is returning a float sorta makes no sense, because integer division is taking place, unless your result value is a float like x = a / b; where float x; If x were an int, you would lose the decimals.
Dont get thrown off by the way it should be done rather than the work that needs to be done. You definitely need an array of ints and chars to capture the input. Otherwise your code would be big, but maybe that is what the prof is looking for. Either way you'll get a passing grade. It is much easier to do
if data_type = integer
then *int ary
else *char ary
//ask for datainput
if data_type = integer
//message reads "Enter data_len integers
else
//message reads "Enter data_len characters
case datalen
0: somefunc; break;
1: somefunc(ary[0]);
2: somefunc(ary[0], ary[1]);
...
then repeating all that code over and over. The above code will definitely not compile, but then again you said you didn't need help with the coding :-)
I would throw two additional funcs in there that passes an integer pointer and a char pointer just to show that you know the right way to do things. Do your range checking in there.
float somefunc(*int a)
string somefunc(*char a)
You will wind up with 11 functions like
string somefunc()
float somefinc(int a)
float somefunc(int a,b)
...
string somefunc(char a)
string soemfunc(char ab)
The only vague part is returning a float sorta makes no sense, because integer division is taking place, unless your result value is a float like x = a / b; where float x; If x were an int, you would lose the decimals.
Dont get thrown off by the way it should be done rather than the work that needs to be done. You definitely need an array of ints and chars to capture the input. Otherwise your code would be big, but maybe that is what the prof is looking for. Either way you'll get a passing grade. It is much easier to do
if data_type = integer
then *int ary
else *char ary
//ask for datainput
if data_type = integer
//message reads "Enter data_len integers
else
//message reads "Enter data_len characters
case datalen
0: somefunc; break;
1: somefunc(ary[0]);
2: somefunc(ary[0], ary[1]);
...
then repeating all that code over and over. The above code will definitely not compile, but then again you said you didn't need help with the coding :-)
I would throw two additional funcs in there that passes an integer pointer and a char pointer just to show that you know the right way to do things. Do your range checking in there.
float somefunc(*int a)
string somefunc(*char a)
•
•
Join Date: Oct 2003
Posts: 2
Reputation:
Solved Threads: 0
Thanks for the responces,
First, my professor does not speak english as clear as he should, so asking questions tends to complicate things. I did that on my last project.
Second, here are the prototypes I assumed he wanted before I posted :
char* average(char a='\0', char b='\0', char c='\0', char d='\0', char e='\0');
float average(int i, int j=0, int k=0, int l=0, int m=0);
One of my problems is trying to understand what he wants me to do with average(); - he has 3 partially contradicting statments in my opinion.
Another problem is how to implement a try, catch, and where to put the throw.
First, my professor does not speak english as clear as he should, so asking questions tends to complicate things. I did that on my last project.
Second, here are the prototypes I assumed he wanted before I posted :
char* average(char a='\0', char b='\0', char c='\0', char d='\0', char e='\0');
float average(int i, int j=0, int k=0, int l=0, int m=0);
One of my problems is trying to understand what he wants me to do with average(); - he has 3 partially contradicting statments in my opinion.
Another problem is how to implement a try, catch, and where to put the throw.
![]() |
Similar Threads
- wage calculator HELP (VB.NET)
- Request help to fix my program (C)
- Please help me to convert a simple C++ program into an object-oriented one. (C++)
- run a java program (Java)
- My first "real" C program... tell me what you think... (C++)
Other Threads in the C++ Forum
- Previous Thread: Noob help!
- Next Thread: re: Sorting Algorithms
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





