[SOLVED] Would I look retarded turning this in?(The answer is: Not this time)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Would I look retarded turning this in?(The answer is: Not this time)
My semester has resumed, and lo and behold 4 weeks into it my C++ class is beginning! Hooray for incompetency, but that's neither here nor there.
The project was pretty simple sounding, create a program that will convert miles, yards, feet, and inches to meters, and print it out in a table.
Basically I'm unhappy with my C++ professor, because for whatever reason, he doesn't like teaching C++, but he does like complaining, and making fun of students every time they ask a question.
Anywho, would this be a bad program to turn in? It does what the requirements ask for...
What would be better? (aside from splitting the program into separate files, I'll do that later just for fun)
Also - What types of classes would I need to be in to be participating in and/or observing the development of real software? I'm tired of these newbie classes, and not because I've been over the material before, but because they're full of newbies who don't care about anything, much less programming.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int NumIn = 4; //Max number of units
const float CONV_FACT_INCH_MILES = .000015782;//1/63360
const float CONV_FACT_FOOT_MILES = .0001893;//1/5280
const float CONV_FACT_YARD_MILES = .0005681;//1/1760
const float CONV_FACT_MILES_MILES = 1;// one mile, for symmetry
const float CONV_FACT_INCHES_METRIC = .0254;//1/39.37
const float CONV_FACT_FEET_METRIC = .3048;//1/.3048
const float CONV_FACT_YARDS_METRIC = .9144;//1/.9144
const float CONV_FACT_MILES_METRIC = 1609.3000;//1/1609.3
int Miles, Yards, Feet, Inches, i;//units, counter
float TotalAmerican = 0.0, TotalMetric = 0.0; // We'll put the totals here
cout <<"\n\
||#################################||\n \
|| Lab2 - A option. ITSE 1307 ||\n \
|| ||\n \
|| Inputs - Miles, Yards, ||\n \
|| Feet, Inches. ||\n \
|| Outputs - Conversions to meters ||\n \
|| ||\n \
|| ||\n \
||#################################||\n\n";
cout <<" First, enter the length in miles\n";
cin >> Miles;
cout <<" Next, enter the length in yards\n";
cin >> Yards;
cout <<" Next, enter the length in feet\n";
cin >> Feet;
cout <<" Finally, enter the length in inches\n";
cin >> Inches;
cout <<"\n";
const char *Units[] ={ "Miles", "Yards", "Feet", "Inches" }; //Unit array
float Values[] ={ Miles, Yards, Feet, Inches }; //array of entered values
//Array of Metric Conversion Factors
float MCF[] ={ CONV_FACT_MILES_METRIC, CONV_FACT_YARDS_METRIC, \
CONV_FACT_FEET_METRIC, CONV_FACT_INCHES_METRIC };
//Array of American Conversion Factors
float ACF[] ={ CONV_FACT_MILES_MILES,CONV_FACT_YARD_MILES, \
CONV_FACT_FOOT_MILES, CONV_FACT_INCH_MILES };
//Array of Calculations of other arrays, Metric
float ResultsMetric[] ={ Values[0]*MCF[0], Values[1]*MCF[1], \
Values[2]*MCF[2], Values[3]*MCF[3] };
//Array of Calculations of other arrays, American
float ResultsAmerican[] ={ Values[0]*ACF[0], Values[1]*ACF[1], \
Values[2]*ACF[2], Values[3]*ACF[3] };
//build the columns
cout <<"[Units]\t\t\t[Value]\t\t\t[Meters]\n\n";
//print table
for(i = 0; i < (NumIn); i++)
{
cout<<Units[i]<<"\t\t\t"<<Values[i]<<"\t\t\t"<<ResultsMetric[i]<<"\n";
TotalAmerican += ResultsAmerican[i];
TotalMetric += ResultsMetric[i];
}
//print the total
cout << "\n";
cout.precision(4);
cout<<"Total:\t\t\t"<<TotalAmerican<<"mi";
cout.precision(4);
cout<<"\t\t\t"<<TotalMetric<<"m"<<"\n";
return 0;
}
My semester has resumed, and lo and behold 4 weeks into it my C++ class is beginning! Hooray for incompetency, but that's neither here nor there.
The project was pretty simple sounding, create a program that will convert miles, yards, feet, and inches to meters, and print it out in a table.
Basically I'm unhappy with my C++ professor, because for whatever reason, he doesn't like teaching C++, but he does like complaining, and making fun of students every time they ask a question.
Anywho, would this be a bad program to turn in? It does what the requirements ask for...
What would be better? (aside from splitting the program into separate files, I'll do that later just for fun)
Also - What types of classes would I need to be in to be participating in and/or observing the development of real software? I'm tired of these newbie classes, and not because I've been over the material before, but because they're full of newbies who don't care about anything, much less programming.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int NumIn = 4; //Max number of units
const float CONV_FACT_INCH_MILES = .000015782;//1/63360
const float CONV_FACT_FOOT_MILES = .0001893;//1/5280
const float CONV_FACT_YARD_MILES = .0005681;//1/1760
const float CONV_FACT_MILES_MILES = 1;// one mile, for symmetry
const float CONV_FACT_INCHES_METRIC = .0254;//1/39.37
const float CONV_FACT_FEET_METRIC = .3048;//1/.3048
const float CONV_FACT_YARDS_METRIC = .9144;//1/.9144
const float CONV_FACT_MILES_METRIC = 1609.3000;//1/1609.3
int Miles, Yards, Feet, Inches, i;//units, counter
float TotalAmerican = 0.0, TotalMetric = 0.0; // We'll put the totals here
cout <<"\n\
||#################################||\n \
|| Lab2 - A option. ITSE 1307 ||\n \
|| ||\n \
|| Inputs - Miles, Yards, ||\n \
|| Feet, Inches. ||\n \
|| Outputs - Conversions to meters ||\n \
|| ||\n \
|| ||\n \
||#################################||\n\n";
cout <<" First, enter the length in miles\n";
cin >> Miles;
cout <<" Next, enter the length in yards\n";
cin >> Yards;
cout <<" Next, enter the length in feet\n";
cin >> Feet;
cout <<" Finally, enter the length in inches\n";
cin >> Inches;
cout <<"\n";
const char *Units[] ={ "Miles", "Yards", "Feet", "Inches" }; //Unit array
float Values[] ={ Miles, Yards, Feet, Inches }; //array of entered values
//Array of Metric Conversion Factors
float MCF[] ={ CONV_FACT_MILES_METRIC, CONV_FACT_YARDS_METRIC, \
CONV_FACT_FEET_METRIC, CONV_FACT_INCHES_METRIC };
//Array of American Conversion Factors
float ACF[] ={ CONV_FACT_MILES_MILES,CONV_FACT_YARD_MILES, \
CONV_FACT_FOOT_MILES, CONV_FACT_INCH_MILES };
//Array of Calculations of other arrays, Metric
float ResultsMetric[] ={ Values[0]*MCF[0], Values[1]*MCF[1], \
Values[2]*MCF[2], Values[3]*MCF[3] };
//Array of Calculations of other arrays, American
float ResultsAmerican[] ={ Values[0]*ACF[0], Values[1]*ACF[1], \
Values[2]*ACF[2], Values[3]*ACF[3] };
//build the columns
cout <<"[Units]\t\t\t[Value]\t\t\t[Meters]\n\n";
//print table
for(i = 0; i < (NumIn); i++)
{
cout<<Units[i]<<"\t\t\t"<<Values[i]<<"\t\t\t"<<ResultsMetric[i]<<"\n";
TotalAmerican += ResultsAmerican[i];
TotalMetric += ResultsMetric[i];
}
//print the total
cout << "\n";
cout.precision(4);
cout<<"Total:\t\t\t"<<TotalAmerican<<"mi";
cout.precision(4);
cout<<"\t\t\t"<<TotalMetric<<"m"<<"\n";
return 0;
}
Well, I wouldn't call it C++, even though nominally it is, and probably compiles and works.
My point is that C++ was created as an OO extension of "C". Using C++ first of all makes sense (compared to plain old "C") if you are using C++ OO features.
I don't know what your tutor expects from you; he might expect some kind of easily extensible program. I.e. for example, you create an abstract class (or, rather, template class) with generic conversion function and then instantiate the class per needed conversions, so adding a conversion would be adding an instance with the appropriate conversion coefficient.
Disclaimer: I an not a C++ guy; OTOH I hope my ideas are generic enough.
Last edited by Sergei Steshenko; 02-12-2010 at 11:25 PM.
And it is possible to have an elegant enough (IMO) solution with just plain C++ class - constructors may accept arguments; the argument can be conversion coefficient.
If you haven't covered objects/classes yet, it is a reasonable effort.
A few minor comments:
Blank lines (particularly before comments) can make the code a bit more readable.
You can initialize float constants using the actual expression instead of a calculated value (as long as at least one of them is a float), eg:
Code:
const float CONV_FACT_INCH_MILES = 1./63360;
The code doesn't cope well with non-numeric input, but that might be too much effort if it is meant to be a short assignment.
I personally think it is not good to have 'using namespace std', since it pollutes the global namespace too much, but many people do it.
It is conventional to use lowercase initial letters for variables and objects (so that uppercase can be used for classes, and perhaps for constants).
It isn't clear to me if the requirement was to be able to convert miles or yards or feet or inches to metric, or whether it was to convert a total which consisted of a combination of miles/yards/feet/inches. You look like you have hedged your bets a little, which complicates the code.
The cout that is split over multiple lines would be more neatly done by either multiple couts, or concatenated string constants (eg "abc" "def" is the same as "abcdef". This would mean you could maintain the indentation and get rid of the line continuations.
It would be worth having a comment at the top of the file to explain what it does (and with the author's name).
If you are already familiar with coding, then what you want in a C++ course is to learn about object oriented programming, and to a lesser extent about generic programming (templates). The actual choice of language is not the most important thing; you can learn the language as you go, but concepts can take time to absorb.
Learning about "real software" is best done by practice, often by working on significant projects with other people who can mentor you. At the very least, find someone else in the class who is genuinely interested in learning, and bounce ideas off each other.
Oh, and take a look at Stroustrup's web page, there is some great information there.
Last edited by neonsignal; 02-12-2010 at 11:57 PM.
By the way, 'float' is kind of awkward in this context too. 'float' is justified when the developer knows very well what he/she is doing, e.g. intentional sacrifice of precision in favor of speed. And by itself 'float' is often not faster than 'double', but in SIMD instructions it is.
Also, with large amount of data 'float' of course, means smaller memory bandwidth.
1) Use double not float. Float is useless, don't use it.
2) You may want to separate all this into functions if you want to re-use it or build upon it. Or you can waste a lot of time and make it use OOP. Your choice.
3) Personally I would have made those 'const' into '#define' and these would have been moved into the header (but maybe that would be more what is done in C, I don't remember if this is what is right in C++)
I don't think you would look retarded turning it in, I turned in code like this all the time. My teacher was a b****, with the moto "But it works != A". Yeah, well my moto is: "if (it_compiles) { grade="pass"; } ".
Last edited by H_TeXMeX_H; 02-13-2010 at 04:13 AM.
For what it's worth, from my experience, you'd look more "retarded" if you didn't hand it in, whether or not it's the best solution to a particular problem. I also found that it was best to stick to using only the elements of the language that the lecturer has covered so far, even though you may have done your own study and know elements of a language that would make a particular task easier. Again only an opinion, the worst thing you can do is use some part of the language that hasn't been covered and use it wrong. It's a surefire way to get less marks than someone who knows less but has completed the task using only the elements taught so far.
As for the Professor/Lecturer's attitude, they are a funny breed and I'd take it with a pinch of salt ( unless it is really, really appalling ) and the fact that some of your classmates appear not to want to learn, get good grades and leave them to dropout/repeat the class.
All opinions are my own and do not reflect the view of any organisation
3) Personally I would have made those 'const' into '#define' and these would have been moved into the header (but maybe that would be more what is done in C, I don't remember if this is what is right in C++)
In C++ it's preferred to either use static const <type> inside a function or class, or const <type> in an anonymous namespace outside a function or class. The main issue with #define for constants is that it doesn't respect scope.
For such a simple task I wouldn't bother with OOP or any particularly complicated stuff. I've done significantly more complicated stuff during the ACM programming competition and OOP was never really appropriate for those problems. Actually, nowadays OOP is a very small part of modern C++.
Since it looks like you are probably using a lot of repeated code, it's beneficial to clean it up a bit by using a function. Also, the usage of arrays distract the reader (me) from what you're actually doing.
Most of what I'm talking about is personal improvement, everyone else here has covered the biggest issues. As it is now if it passes all tests (including corner conditions), it's probably better code than than 90% of your classmates.
Oh man, I got great responses in this thread. Thank you, everyone.
I don't understand entirely what this guy is asking for, and as I said before, he hasn't even begun speaking about C++ yet, 4 weeks into the semester, so another way to say it is, "None of the subject material on this particular homework assignment has even been considered in class, yet. OOP? I don't even think we'll get past loops."
Actual text from assignment
C option
The requirements on this poorly worded assignment are, "Write a program that will promkpt the user for Miles, Yards, Feet, and Inches. Input one value per cin. Convert the measurements into meters and display the results on the screen. Make the meters value a float.
B option
Create constants values for any/all conversion factors used in the program. Use the constants in all calculations. Define the constants just inside your main() function.
A option
Output each of the sub totals individually..(i.e. Miles, Yards, Feet, Inches)
Code:
Units --- Value --- Meters
Miles xxx xxx.xx
Yards xxx xxx.xx
Feet xxx xxx.xx
Inches xxx xxx.xx
Total: xxx xxx.xx
then output the grand total.
to limit your decimal places use the following: Example:
Code:
include <iostream>
#include <iomanip>
using namespace std;
int main() {
double f = -3.14159;
cout << fixed;
cout << setprecision(2) << f <<endl;
No, it doesn't work to call the professor with questions
and he doesn't check his e-mail, or just doesn't answer them.
My primary questions - Which total is the grand total? I don't know, so I figured I would print something for both options.
Do I have to use setprecision(2)? (for the grade, I mean.)
I thought it was kind of funny that the first result for the googling of "how to set precision in c++" yields a first result of that exact code snippet.
Typical rubbish assignment, like I might expect. Guess you gotta use float, even tho in real life, you should remember never to use it.
I'm confused about the totals as well. I think sub-total would mean add together the values in the same category (Miles, Yards, Feet, Inches), and grand total would mean add them all together. I guess you would have to convert them to a unit and display the answer in this unit ? Or just do 12 miles 400 yards 20 feet and 2 inches ... ? or 12.343534564356 miles ? in which case you would probably want to use setprecision.
Ok, I think this is a pretty non-offensive way of playing with my assignment.. Should this get an A?
Code:
#include <iostream>
#include <iomanip>
using namespace std;
/*
Lab 2 - A option. ITSE 1307
Inputs - Miles, Yards, Feet, Inches.
Outputs -
Table containing columns Units of measurement,
The values entered for Miles, Yards, Feet, and Inches, and
their value when converted to the metric representation
*/
int main()
{
//Max number of units
const int NumIn = 4;
//Ratios for the American measurement system, and the Metric measurement system
const double CONV_FACT_INCH_MILES = 1/63360;
const double CONV_FACT_FOOT_MILES = 1/5280;
const double CONV_FACT_YARD_MILES = 1/1760;
const double CONV_FACT_MILES_MILES = 1; // one mile, for symmetry
const double CONV_FACT_INCHES_METRIC = 1/39.37;
const double CONV_FACT_FEET_METRIC = 1/3.2808399;
const double CONV_FACT_YARDS_METRIC = 1/1.0936133;
const double CONV_FACT_MILES_METRIC = 1609.344;
//units
double Miles, Yards, Feet, Inches;
//The counter
int i;
// We'll put the totals here
double TotalAmerican = 0.0, TotalMetric = 0.0;
//print info
cout << "\n\n";
cout << "############################################################################\n";
cout << "This program takes values for Miles, Yards, Feet, and Inches\n";
cout << "It then converts and prints the values into a table, and totals the results.\n";
cout << "############################################################################cd ";
cout << "\n\n";
//Collect values for Miles, Yards, Feet, and Inches
cout <<" First, enter the length in miles\n";
cin >> Miles;
cout <<" Next, enter the length in yards\n";
cin >> Yards;
cout <<" Next, enter the length in feet\n";
cin >> Feet;
cout <<" Finally, enter the length in inches\n";
cin >> Inches;
cout <<"\n";
//Create array of Units
const char *Units[] ={ "Miles", "Yards", "Feet", "Inches" };
//Create array of the values entered for Miles, Yards, Feet, and Inches.
double Values[] ={ Miles, Yards, Feet, Inches };
//Array of Metric Conversion Factors
double MCF[] ={ CONV_FACT_MILES_METRIC, CONV_FACT_YARDS_METRIC, CONV_FACT_FEET_METRIC, CONV_FACT_INCHES_METRIC };
//Array of American Conversion Factors
double ACF[] ={ CONV_FACT_MILES_MILES,CONV_FACT_YARD_MILES, CONV_FACT_FOOT_MILES, CONV_FACT_INCH_MILES };
//Array of Calculations of other arrays, Metric
double ResultsMetric[] ={ Values[0]*MCF[0], Values[1]*MCF[1], Values[2]*MCF[2], Values[3]*MCF[3] };
//Array of Calculations of other arrays, American
double ResultsAmerican[] ={ Values[0]*ACF[0], Values[1]*ACF[1], Values[2]*ACF[2], Values[3]*ACF[3] };
//Print the columns to stdout
cout <<"[Units]\t\t\t[Values]\t\t[Meters]\n\n";
//Print the rows of the table to stdout
for(i = 0; i < NumIn; i++)
{
cout << setprecision(4) << Units[i]<<"\t\t\t"<<Values[i]<<"\t\t\t"<<ResultsMetric[i]<<"\n";
TotalAmerican += ResultsAmerican[i];
TotalMetric += ResultsMetric[i];
}
//print the total
cout << "\n";
cout << setprecision(4) << "Total:\t\t\t"<<TotalAmerican<<"mi";
cout << setprecision(4) << "\t\t\t"<<TotalMetric<<"m"<<"\n";
//Return 0 on completion
return 0;
}
Ok, I think this is a pretty non-offensive way of playing with my assignment.. Should this get an A?
Code:
#include <iostream>
#include <iomanip>
using namespace std;
/*
Lab 2 - A option. ITSE 1307
Inputs - Miles, Yards, Feet, Inches.
Outputs -
Table containing columns Units of measurement,
The values entered for Miles, Yards, Feet, and Inches, and
their value when converted to the metric representation
*/
int main()
{
//Max number of units
const int NumIn = 4;
//Ratios for the American measurement system, and the Metric measurement system
const double CONV_FACT_INCH_MILES = 1/63360;
const double CONV_FACT_FOOT_MILES = 1/5280;
const double CONV_FACT_YARD_MILES = 1/1760;
const double CONV_FACT_MILES_MILES = 1; // one mile, for symmetry
const double CONV_FACT_INCHES_METRIC = 1/39.37;
const double CONV_FACT_FEET_METRIC = 1/3.2808399;
const double CONV_FACT_YARDS_METRIC = 1/1.0936133;
const double CONV_FACT_MILES_METRIC = 1609.344;
//units
double Miles, Yards, Feet, Inches;
//The counter
int i;
// We'll put the totals here
double TotalAmerican = 0.0, TotalMetric = 0.0;
//print info
cout << "\n\n";
cout << "############################################################################\n";
cout << "This program takes values for Miles, Yards, Feet, and Inches\n";
cout << "It then converts and prints the values into a table, and totals the results.\n";
cout << "############################################################################cd ";
cout << "\n\n";
//Collect values for Miles, Yards, Feet, and Inches
cout <<" First, enter the length in miles\n";
cin >> Miles;
cout <<" Next, enter the length in yards\n";
cin >> Yards;
cout <<" Next, enter the length in feet\n";
cin >> Feet;
cout <<" Finally, enter the length in inches\n";
cin >> Inches;
cout <<"\n";
//Create array of Units
const char *Units[] ={ "Miles", "Yards", "Feet", "Inches" };
//Create array of the values entered for Miles, Yards, Feet, and Inches.
double Values[] ={ Miles, Yards, Feet, Inches };
//Array of Metric Conversion Factors
double MCF[] ={ CONV_FACT_MILES_METRIC, CONV_FACT_YARDS_METRIC, CONV_FACT_FEET_METRIC, CONV_FACT_INCHES_METRIC };
//Array of American Conversion Factors
double ACF[] ={ CONV_FACT_MILES_MILES,CONV_FACT_YARD_MILES, CONV_FACT_FOOT_MILES, CONV_FACT_INCH_MILES };
//Array of Calculations of other arrays, Metric
double ResultsMetric[] ={ Values[0]*MCF[0], Values[1]*MCF[1], Values[2]*MCF[2], Values[3]*MCF[3] };
//Array of Calculations of other arrays, American
double ResultsAmerican[] ={ Values[0]*ACF[0], Values[1]*ACF[1], Values[2]*ACF[2], Values[3]*ACF[3] };
//Print the columns to stdout
cout <<"[Units]\t\t\t[Values]\t\t[Meters]\n\n";
//Print the rows of the table to stdout
for(i = 0; i < NumIn; i++)
{
cout << setprecision(4) << Units[i]<<"\t\t\t"<<Values[i]<<"\t\t\t"<<ResultsMetric[i]<<"\n";
TotalAmerican += ResultsAmerican[i];
TotalMetric += ResultsMetric[i];
}
//print the total
cout << "\n";
cout << setprecision(4) << "Total:\t\t\t"<<TotalAmerican<<"mi";
cout << setprecision(4) << "\t\t\t"<<TotalMetric<<"m"<<"\n";
//Return 0 on completion
return 0;
}
Regarding, say
Code:
const double CONV_FACT_INCH_MILES = 1/63360;
- I wouldn't write it this way just to be on the safe side.
The issue is when conversion to double takes place - after or before expression calculation. If after, then the result is 0 - because in terms of integers expressions a / b are always 0 for abs(a) < abs(b).
The first case (where the whole expression became "0.0") is an example of "integer truncation".
In C, floating point literals can be single-precision "float", double-precision "double", or "long float". Even if you qualify the literal with a "." (default "double"), you still might get "floating point truncation" unless you also qualify it with the "L".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.