LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2010, 11:03 PM   #1
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Rep: Reputation: 25
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;
}

Last edited by Dogs; 02-17-2010 at 07:17 PM.
 
Old 02-12-2010, 11:24 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Dogs View Post
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.
 
Old 02-12-2010, 11:36 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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.
 
Old 02-12-2010, 11:51 PM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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.
 
1 members found this post helpful.
Old 02-13-2010, 12:31 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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.

...

neonsignal made really good comments in http://www.linuxquestions.org/questi...07#post3862207 .
 
Old 02-13-2010, 03:22 AM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Tips:

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.
 
Old 02-13-2010, 04:07 AM   #7
aspire1
Member
 
Registered: Dec 2008
Distribution: Ubuntu
Posts: 62

Rep: Reputation: 23
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
 
Old 02-13-2010, 04:31 AM   #8
bmxcess
Member
 
Registered: Jan 2009
Location: Brisbane, Australia
Distribution: @work:RHEL 5.4/Fedora 13, @home:slack64-current,ubuntu lynx studio
Posts: 65

Rep: Reputation: 19
Firstly, kudos for the interesting subject line (I bit).

What was your assignment question? Without it its hard to know if your solution meets what was asked.
 
Old 02-13-2010, 10:14 AM   #9
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
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.
 
Old 02-13-2010, 12:20 PM   #10
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Original Poster
Rep: Reputation: 25
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.

Last edited by Dogs; 02-13-2010 at 12:45 PM.
 
Old 02-13-2010, 01:10 PM   #11
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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.
 
Old 02-15-2010, 12:26 AM   #12
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Original Poster
Rep: Reputation: 25
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;
}

Last edited by Dogs; 02-15-2010 at 01:01 AM.
 
Old 02-15-2010, 01:52 AM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Dogs View Post
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).

So, a safe way is to write

Code:
const double CONV_FACT_INCH_MILES = 1.0/63360.0;
 
Old 02-15-2010, 02:40 AM   #14
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Sergei Steshenko is absolutely correct. To elaborate:
Code:
#include <stdio.h>

const double CONV_FACT_INCH_MILES_BAD = 1/63360;
const double CONV_FACT_INCH_MILES_GOOD = 1.0/63360.0;
const double CONV_FACT_INCH_MILES_OFTEN_BETTER = 1.0L/63360.0L;

int
main (int argc, char *argv[])
{
  printf ("CONV_FACT_INCH_MILES_BAD= %lf\n", CONV_FACT_INCH_MILES_BAD);
  printf ("CONV_FACT_INCH_MILES_GOOD= %lf\n", CONV_FACT_INCH_MILES_GOOD);
  printf ("CONV_FACT_INCH_MILES_OFTEN_BETTER= %lf\n", CONV_FACT_INCH_MILES_OFTEN_BETTER);
  return 0;
}
Quote:
CONV_FACT_INCH_MILES_BAD= 0.000000
CONV_FACT_INCH_MILES_GOOD= 0.000016
CONV_FACT_INCH_MILES_OFTEN_BETTER= 0.000016
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".

'Hope that helps .. PSM

Last edited by paulsm4; 02-15-2010 at 11:14 AM.
 
Old 02-15-2010, 03:18 AM   #15
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Original Poster
Rep: Reputation: 25
That's cool. I haven't been in a situation where I thought the L would be necessary.

so what you mean is -

1.3 is a float, whereas 1.3L is a double? (long float?)

Last edited by Dogs; 02-15-2010 at 03:19 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Gparted bootcd - I'm retarded Plastech Linux - Newbie 2 07-29-2007 02:40 PM
Who's socially retarded Garda General 21 08-25-2006 06:25 PM
Azureus retarded in unfathomable way lasalsademuerte Linux - Software 4 04-02-2006 10:53 AM
Help a retarded user computer_67 Mandriva 6 11-02-2003 05:04 PM
Retarded nonstandard card FallingFrog Linux - General 1 04-04-2001 06:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration