# sorting and pointers

Page 1 of 1

## 4 Replies - 1320 Views - Last Post: 14 July 2009 - 06:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=114620&amp;s=69f80608bb0bdff2ab5e3a0e2469ca65&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 djustus1

Reputation: 0
• 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;

}
```

Is This A Good Question/Topic? 0

## Replies To: sorting and pointers

### #2 GWatt

Reputation: 307
• Posts: 3,105
• 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,

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

### #3 djustus1

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

## Re: sorting and pointers

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:

### #4 NickDMax

Reputation: 2255
• 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.

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.

### #5 djustus1

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

## Re: sorting and pointers

Posted 14 July 2009 - 06:27 AM

NickDMax, on 14 Jul, 2009 - 05:23 AM, said:

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