473,386 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Real Fast While Loop Question

[PHP]#include <iostream>

using std::cout;
using std::cin;
using std::endl;


int main(void)
{

int m1;
int g1;
int x=1;
int MPG;
m1=287,200,120;
g1=13,10,5;


while ( x <= 3 )
{
MPG= m1/g1;
cout<<" MPG this tankful:" << MPG << endl;
x++;

}


char holdscr;
cout << "\n\n\t*** Enter a character and press return to exit the program! ***\n\n\t"<< std::endl;
cin >> holdscr;

return 0;
}[/PHP]

How do i make it so it computers different numbers per MPG, as it it would read of the row of m1 and divide it by g1. Do i use vectors here?
Oct 17 '08 #1
5 1464
boxfish
469 Expert 256MB
m1=287,200,120;
is not valid code. If the amount of numbers isn't going to change while the program is running, use a static array:
int m1[] = {287, 200, 120};
But if you're going to be adding more numbers while the program is running (you currently aren't), you should use a vector. May I suggest using doubles instead of ints for accuracy? Just a thought.
Hope this helps.
Oct 17 '08 #2
whodgson
542 512MB
Doubles are more accurate than ints? how?
As boxfish says:
set up one array for distance units e.g. dougle a[] ={123,145,167};
and one for fluid volume units e.g. double b[]={12,14,16};
now mpg equals a[0]divided by b[0] like:
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<3;i++)
  2. cout<<"mpg "<<i<<"="<<a[i]/b[i]<<endl;
/*prints mpg 0=10.25
mpg 1= 10.357142
mpg 2= 10.4375*/
Oct 17 '08 #3
thanks both of you got it :)
Oct 17 '08 #4
JosAH
11,448 Expert 8TB
m1=287,200,120;
is not valid code.
That code is (syntactically) valid; it just doesn't do what the OP had in mind: that
sequence expression results in m1=120, no more no less. Note that a sequence
operator in an initialization expression is not allowed:
Expand|Select|Wrap|Line Numbers
  1. int m1=287, 200, 120;
  2.  
while a nested sequence expression is allowed there:
Expand|Select|Wrap|Line Numbers
  1. int m1=(287, 200, 120);
  2.  
The C syntax and its accompanying semantics have many many idiosyncrasies ;-)

kind regards,

Jos
Oct 17 '08 #5
donbock
2,426 Expert 2GB
Doubles are more accurate than ints? how?
The ratio of two integers is more accurately expressed using floating-point, so MPG should be a double. If m1 and g1 are integers then the inaccurate integer ratio is what gets converted to floating-point.
Oct 17 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
9
by: AnandaSim | last post by:
Hi All, I've had Access 97, 2000 connections to the corporate Oracle database for a few years now - but seldom use it. When I did use it years ago, performance was not fast but the features were...
10
by: Arsen V. | last post by:
Hello, Does anyone know if an object or how to create one, that will allow me to fetch up to 10 URLs (containing XML-feed data) in an extremelly fast server side fashion? If the request is...
7
by: billr | last post by:
but ireally does need clearing up ... Traditionally (i.e. in C++) I might do the following: SomeObj ptrSomeObj; for(int i = 0; i < iCount; ++i) { ptrSomeObj = new SomeObj();...
4
by: Alexis Gallagher | last post by:
(I tried to post this yesterday but I think my ISP ate it. Apologies if this is a double-post.) Is it possible to do very fast string processing in python? My bioinformatics application needs to...
4
by: Jean-François Blais | last post by:
Many must have asked that question. I intend to write a few programs which will be CPU intensive. I know a little of Java. Would a language that yields native code, instead of bytecode perfrom much...
13
by: Arno R | last post by:
Hi all, I am deploying an A2k app to users with different versions of Access. Using Access 2000 the relinking on startup (on deploying a new frontend or when backend has changed) is very fast....
0
by: adubra | last post by:
Hi there, I am using a device context (DC) and a buffer to successfully draw to screen. However, when I update the DC at very high frame rate and drag the frame containing the image very quickly...
2
by: IdlePhaedrus | last post by:
Hi, I have a FFT routine that I converted from C++ to VB in a module as follows: Const M_PI = 3.1415926535897931 ' Fast Fourier Transform Public Sub FFT(ByRef rex() As Single, ByRef imx() As...
9
by: Salad | last post by:
I have access, for testing at my client's site, a Win2000 computer running A2003 retail. He recently upgraded all of his other machines to DualCore Pentiums with 2 gig ram and run A2003 runtime. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.