sorting and pointers

sorting and pointers

Page 1 of 1

4 Replies - 753 Views - Last Post: 14 July 2009 - 06:27 AM Rate Topic: -----

#1 djustus1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 07-July 09

sorting and pointers

Posted 13 July 2009 - 08:01 PM

Hi I am having trouble understanding pointers. I cannot get my definition of yearTotal correct. Can someone help? This is the part of the program that I do not understand:

//*********************************************************
 //Definition of yearTotal. The year total, is a function that
 //gets the sales, salesSize and Sums the sales.
 //***********************************************************
 double yearTotal(sales, salesSize, sum)
 {

 for(int count =0;count < salesSize;count++)
 {
 sum += *sales;
 sales++;
 }
 return sum;
}




The assignment is, I can sort I just cannot get the linker

// This program will read in monthly sales into a floating dynamic array.
// A function finds the total yearly sales
// Another function finds the average sales


#include<iostream>
using namespace std;


void yearTotal(float * sales, int saleSize, float & sum);  //function that calculates yearly total
void yearAverage(float sum, int saleSize, float& avg);	 //function that calculates average

int main()
 // This program will read in monthly sales into a floating dynamic array.
// A function finds the total yearly sales
// Another function finds the average sales. ASSIGNMENT- you need to sort in
// desending order the monthly sales and display the output.
{
  float * sales; //points to a dynamic array
  int saleSize;  //holds the size of the array as given by the user
  float sum = 0;  //sum of sales
  float avg;	//the averge monthly sales

  cout << "Please input the number of monthly sales to be input" << endl;
  cin >> saleSize;

  sales = new float[saleSize];

  for (int pos = 0;  pos < saleSize;  pos++)
  {
	cout << "Please input the sales for  month " << pos + 1 << endl;
	cin >> sales[pos];
  }

 yearTotal(sales, saleSize, sum);

 cout << "The total sales for the year is $" << sum << endl;
 yearAverage(sum, saleSize, avg);
 cout << "The average monthly sale is $" << avg << endl;


	return 0;
}
 //*********************************************************
 //Definition of yearTotal. The year total, is a function that
 //gets the sales, salesSize and Sums the sales.
 //***********************************************************
 double yearTotal(sales, salesSize, sum)
 {

 for(int count =0;count < salesSize;count++)
 {
 sum += *sales;
 sales++;
 }
 return sum;

	 }


*Mod Edit: Added Code tags: :code:

Is This A Good Question/Topic? 0
  • +

Replies To: sorting and pointers

#2 GWatt  Icon User is offline

  • member icon

Reputation: 270
  • View blog
  • Posts: 3,068
  • Joined: 01-December 05

Re: sorting and pointers

Posted 13 July 2009 - 09:03 PM

when you declare a function you need to specify the types of its paramaters
double yearTotal(double *sales, int saleSize)  // I don't think you really need to pass sum as a paramater
{
	double sum = 0;
	for (int i = 0; i < saleSize; i++)
		sum += sales[i];  // pointers can be a special kind of array, and used as such
	return sum;
}


and btw,
:code:

This post has been edited by GWatt: 13 July 2009 - 09:07 PM

Was This Post Helpful? 1
  • +
  • -

#3 djustus1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 07-July 09

Re: sorting and pointers

Post icon  Posted 14 July 2009 - 05:55 AM

I am still getting a linker error, any ideas? Thanks

 //*********************************************************
 //Definition of yearTotal. The year total, is a function that
 //gets the sales, salesSize and Sums the sales for the year
 //***********************************************************
 double yearTotal(double *sales, int saleSize)
 {
   double sum=0;
 for(int i =0;i < saleSize;i++)

 sum += sales[i];


 return sum;
	 } 

**************************************************************************************************************
this is the whole program:
#include<iostream>
using namespace std;


void yearTotal(float * sales, int saleSize, float & sum);  //function that calculates yearly total
void yearAverage(float sum, int saleSize, float& avg);	 //function that calculates average

int main()
 // This program will read in monthly sales into a floating dynamic array.
// A function finds the total yearly sales
// Another function finds the average sales. ASSIGNMENT- you need to sort in
// desending order the monthly sales and display the output.
{
  float * sales; //points to a dynamic array
  int saleSize;  //holds the size of the array as given by the user
  float sum = 0;  //sum of sales
  float avg;	//the averge monthly sales

  cout << "Please input the number of monthly sales to be input" << endl;
  cin >> saleSize;

  sales = new float[saleSize];

  for (int pos = 0;  pos < saleSize;  pos++)
  {
	cout << "Please input the sales for  month " << pos + 1 << endl;
	cin >> sales[pos];
  }


*Mod Edit: Fixed code tags: :code:
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: sorting and pointers

Posted 14 July 2009 - 06:23 AM

Topics merged -- please do not start multiple topics for the same question.

What Linker error are you getting... we need to know to help you.

Look at you function definition VS your function declaration... you have floats in one, and doubles in the other -- the function declaration and definition need to match.
Was This Post Helpful? 0
  • +
  • -

#5 djustus1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 07-July 09

Re: sorting and pointers

Posted 14 July 2009 - 06:27 AM

View PostNickDMax, on 14 Jul, 2009 - 05:23 AM, said:

Topics merged -- please do not start multiple topics for the same question.

What Linker error are you getting... we need to know to help you.

Look at you function definition VS your function declaration... you have floats in one, and doubles in the other -- the function declaration and definition need to match.



Thank you, the error says :
[Linker Error] Error: Unresolved external 'yearTotal(float *, int, float&)' referenced from C:\USERS\OWNER\DOCUMENTS\BORLAND STUDIO PROJECTS\DEBUG_BUILD\UNIT2.OBJ
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1