write a program that declares three one dimensional arrays named price,quantity, and amount. each array should be declared in main() and be capable of holding 10 double-precision numbers. the numbers to be stored in price are 10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, and 3.98. the numbers to be stored in quantity are 4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, and 4.8. have your program pass these three arrays to a function called extend(), which calculates the elements in the amount array as the product of the equivalent elements in the price and quantity arrays: for example, amount[1] = price[1] * price[1].

#include <iostream>

using namespace std;

void extend(double *price, double *quantity, double *amount);

int main()
{
	double price[10] = {10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98}; 
	double quantity[10] = {4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8}; 
	double amount[10];

	extend(price, quantity, amount);

	cout << "Amount [0] = " << amount[0] << endl << "Amount [1] = " << amount[1] << endl;
	cout << "Amount [2] = " << amount[2] << endl << "Amount [3] = " << amount[3] << endl;
	cout << "Amount [4] = " << amount[4] << endl << "Amount [5] = " << amount[5] << endl;
	cout << "Amount [6] = " << amount[6] << endl << "Amount [7] = " << amount[7] << endl;
	cout << "Amount [8] = " << amount[8] << endl << "Amount [9] = " << amount[9] << endl;
	



	system ("PAUSE");
	return 0;

}

void extend(double *price, double *quantity, double *amount)
{

	amount[0] = price[0] * quantity[0];
	amount[1] = price[1] * quantity[1];
	amount[2] = price[2] * quantity[2];
	amount[3] = price[3] * quantity[3];
	amount[4] = price[4] * quantity[4];
	amount[5] = price[5] * quantity[5];
	amount[6] = price[6] * quantity[6];
	amount[7] = price[7] * quantity[7];
	amount[8] = price[8] * quantity[8];
	amount[9] = price[9] * quantity[9];
}

i came up with this program but my teacher said that i need to use pointers and i need to use loopings to compute for the amount. and i will show the answers in main(). how can i do that? can someone help me?

Recommended Answers

All 5 Replies

I don't see why you'd need pointers here, but you definitely should use a loop:

for(unsigned int i = 0; i < amount.size(); i++) // note you can only use .size() if you use an std::vector, which you should definitely consider!
{
  amount[i] = price[i] * quantity[i];
}

David

I don't see why you'd need pointers here, but you definitely should use a loop:

for(unsigned int i = 0; i < amount.size(); i++) // note you can only use .size() if you use an std::vector, which you should definitely consider!
{
  amount[i] = price[i] * quantity[i];
}

David

I'm not sure why you would need to use pointers here?

You should definitely use a loop though:

for(unsigned int i = 0; i < 10; i++)
{
amount[i] = price[i] * quantity[i];
}

If you use an std::vector, you can loop until amount.size() instead of hard coding "10" :)

David

how will i use std::vector? will i declare something? i really having a hard time :( can u please edit my work and place it there. :( if you can. please. thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.