Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am trying to figure out how to create a funtcion that passes values to an array.

Here is what I have so far.

void per_trip(int test[])
{
float miles;
float gallons;
float price;
miles = gallons = price = 0;
float sum;
float mpg;
int number;
number = 2;
miles = get_miles(miles);
gallons = get_gallons(gallons);
price = get_price(price);
sum = (price * gallons); // Cost paid for fuel
mpg = (miles / gallons); // Miles per gallon
cout << endl;
print_sum(sum, mpg);
test[ number ];
test[ 0 ] = ++miles;
test[ 1 ] = ++gallons ;
test[ 2 ] = ++price;
}

I need to pass the values of miles, gallons, and price to an array. The array needs to keep track of running totals.

I no this is wrong, and I am completely lost.

Someone please help

Thanks

 
ok, there are a few things here that catch my eye.

Declaring the array is the first problem I see.

1. The funtion per_trip is receiving the array and you are trying to redeclare the array in the function. At this point the array should already be defined. Now, while it is possible to declare the array in the function, I dont think dynamic allocation of the array is what you are trying to do.

2. I also notice that you are trying to declare an array with
int array[2]; this does not give you indices 0,1,2. With this declaration you only have access to index 0 and index 1

If I were trying to do this, I would use a struct

struct tripStruct{
float miles;
float gallons;
float price;
};

void per_trip(tripStruct t[], int index)
{

//from what i can assume get_miles,get_price etc
// receive a value by reference so this should work

get_miles(t.miles);
get_price(t.price);
get_gallons(t.gallons);

float sum = (t.price * t.gallons);
folat mpg = (t.miles / t.gallons);

cout<<&quot;sum: &quot;<<sum<<endl<<&quot;mpg: &quot;<<mpg<<endl;


}

and in the main i would have an array of type &quot;tripStruct&quot;

int main()
{
...
tripStruct myTripStruct[100];//however many you want

...
for(int i=0;i<100;i++)
{
set_miles(value);
set_price(value2);
set_gallons(value3);

per_trip(myTripStruct,i);
}



the above code is far from complete but it should give you a better idea of how to handle this. I would suggest reading up on arrays first and passing arrays to functions.

Please keep in mind that any changes you make to the passed array will effect it where you passed it from as it is treated as a pointer.

Matt

 
oops... in the function you want to call

get_miles(t[index].miles)
etc...

forgot the index...

good luck

Matt
 
it is an error on type assigning:
test[ 0 ] = ++miles;
test[ 1 ] = ++gallons ;
test[ 2 ] = ++price;
you should write:
test[ 0 ] = (int)++miles;
...
John Fill
1c.bmp


ivfmd@mail.md
 
This has been driving crazy all week. Would either one of you be willing to look at the .cpp file and help me fix it? It is a small program that just calculates fuel economy.

I am very new to the language and would appreciate any assistance.

Thanks
 
Ok,
email to me the attachment with project, I'll post maybe the answer in the forum. John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top