C function to calculate sum & product of array using pointers?

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 857 Views - Last Post: 05 November 2017 - 08:13 AM Rate Topic: -----

#1 rampager112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 17

C function to calculate sum & product of array using pointers?

Posted 02 November 2017 - 09:51 PM

Hi

I have to create a function that can calculate the sum and product of an array and return both values using pointers

I've searched for simpler programs such as finding min/max of array using pointers however with sum and product I think I need to first initialise sum to 0 and product to 1. For my function prototype I was thinking of Void FindSumAndMul(int a[], int size, int* Sump, int* Mulp) where sump and mulp are sum and multiplication pointers. I know how to accept an array but then do you first initialize sump and mulp to variables that are initialised to 0 and 1 respectively? and then do

for(i=0;i<size;i++)
{
   *Sump += &a[i];
   *Mulp *= &a[i];
}


?

I found a different way but it uses a single pointer to the array and leaves sum and product as values, which I don't want.

Is This A Good Question/Topic? 0
  • +

Replies To: C function to calculate sum & product of array using pointers?

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,470
  • Joined: 08-August 08

Re: C function to calculate sum & product of array using pointers?

Posted 02 November 2017 - 10:01 PM

Since you need to return two values, why not use a pointer to an array containing those two values?

Or a pointer to a struct/class containing them?
Was This Post Helpful? 0
  • +
  • -

#3 rampager112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 17

Re: C function to calculate sum & product of array using pointers?

Posted 02 November 2017 - 10:08 PM

View PostCTphpnwb, on 02 November 2017 - 10:01 PM, said:

Since you need to return two values, why not use a pointer to an array containing those two values?

Or a pointer to a struct/class containing them?


So you're saying to calculate the sump and mulp then create another array and store them in that second array and return that array? Also in my class our lecturer taught us using pointers, i mean like ive read some examples that pack it into a structure then returns that structure but the question specifically stated pointers.

But is there a way without returning them both in an array? Like just doing the sum and product then returning them?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,470
  • Joined: 08-August 08

Re: C function to calculate sum & product of array using pointers?

Posted 02 November 2017 - 10:12 PM

You don't return the array. You return a pointer to it. Note that the array should be dynamically allocated. Also note that you can use another type of container such as a struct or class instead of the array.
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2011
  • View blog
  • Posts: 5,391
  • Joined: 27-December 05

Re: C function to calculate sum & product of array using pointers?

Posted 03 November 2017 - 05:25 AM

View Postrampager112, on 03 November 2017 - 12:51 AM, said:

For my function prototype I was thinking of Void FindSumAndMul(int a[], int size, int* Sump, int* Mulp) where sump and mulp are sum and multiplication pointers.


That's a perfectly good idea. If you know how to do that, what exactly is the problem?

Quote

... I know how to accept an array but then do you first initialize sump and mulp to variables that are initialised to 0 and 1 respectively? and then do ...


You don't create those variables in the function. You create them in main and send their addresses in the funcction call (the same as you are doing with the array). Then you can assign the appropriate initial values to *sump and *mulp in the function, e.g.:
*sump = 0;

Was This Post Helpful? 1
  • +
  • -

#6 rampager112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 17

Re: C function to calculate sum & product of array using pointers?

Posted 03 November 2017 - 05:32 AM

Thanks for the assistance guys i've managed to get it working :)
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,470
  • Joined: 08-August 08

Re: C function to calculate sum & product of array using pointers?

Posted 03 November 2017 - 06:45 AM

Working is nice, but only about 50% of the solution. Is it readable? Do you have a memory leak? Maybe you should post your solution.
Was This Post Helpful? 0
  • +
  • -

#8 rampager112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 17

Re: C function to calculate sum & product of array using pointers?

Posted 03 November 2017 - 05:19 PM

#include <stdio.h>

void sum_multi(int a[], int size, int* sump, int* mulp);

int main(void)
{
	int i, sum, mul, size = 5, a[size];
	printf("Enter array elements\n");
	for(i=0;i<size;i++)
	{
		scanf("%d", &a[i]);
	}

	sum_multi(a, size, &sum, &mul);

	printf("sum = %d product = %d", sum, mul);

	return 0;
}


void sum_multi(int a[], int size, int* sump, int* mulp)
{
	int j;
	*sump = 0;
	*mulp = 1;
	for(j=0;j<size;j++)
	{
		*sump += a[j];
		*mulp *= a[j];
	}
}




this is my solution. I found this way easier rather than how you told me to do it.
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,470
  • Joined: 08-August 08

Re: C function to calculate sum & product of array using pointers?

Posted 03 November 2017 - 06:18 PM

Way easier? Than this?
#include <iostream>
struct prodSum {
	int sum, product;
	prodSum(int s, int p) : sum(s), product(p) {}
};

prodSum * sumProd(int arr[], int arrSize) {
	prodSum *sumP = new prodSum(0,1);
	for(int i = 0; i < arrSize; i++) {
		sumP->sum += arr[i];
		sumP->product *= arr[i];
	}
	return sumP;
}

int main(int argc, const char * argv[]) {
	const int arraySize = 5;
	int anArray[arraySize] = {2,3,4,5,6};
	prodSum *smm = sumProd(anArray, arraySize);

	std::cout << "Sum: " << smm->sum << "  product: " << smm->product << std::endl;
	return 0;
}


What happens if you need to add in mean, variance, and standard deviation? Your function will have 7 parameters! Mine will still have 2.
Was This Post Helpful? 0
  • +
  • -

#10 rampager112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 17

Re: C function to calculate sum & product of array using pointers?

Posted 03 November 2017 - 06:33 PM

ah I see. I'm a beginner at C so I didn't think of that/Am not very used to programming. Like all the programs we have made in my class(it's a fundamentals class) used int main(void) so I didn't think of changing the inputs to the main lol
Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2011
  • View blog
  • Posts: 5,391
  • Joined: 27-December 05

Re: C function to calculate sum & product of array using pointers?

Posted 03 November 2017 - 06:36 PM

Speaking of memory leaks, when do those prodSums get deleted?

FYI, rampager112: whenever you use new to dynamically allocate memory, you have to use delete to de-allocate it before the program ends.
Was This Post Helpful? 0
  • +
  • -

#12 rampager112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 17

Re: C function to calculate sum & product of array using pointers?

Posted 03 November 2017 - 06:39 PM

View Postr.stiltskin, on 03 November 2017 - 06:36 PM, said:

Speaking of memory leaks, when do those prodSums get deleted?

FYI, rampager112: whenever you use new to dynamically allocate memory, you have to use delete to de-allocate it before the program ends.


ah is this C++? In my class I learned that if we want to dynamically allocate new memory we free the initial pointer then allocate the memory
Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2011
  • View blog
  • Posts: 5,391
  • Joined: 27-December 05

Re: C function to calculate sum & product of array using pointers?

Posted 03 November 2017 - 06:55 PM

Not sure exactly what you mean by "free the initial pointer". There's nothing to free before you've allocated anything.

First you declare a pointer
myClass *ip;

then allocate memory to it
ip = new myClass;

(those first two steps can be combined into one)
and when you're finished using it delete it
delete ip;


Note that usually the object or function that allocates the memory should be responsible for deleting it, so I don't think it's best practice for that function (sumProd) to return a pointer to a struct that was allocated by the function.









(And don't confuse "free" with the C function free.)
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1377
  • View blog
  • Posts: 4,316
  • Joined: 20-March 10

Re: C function to calculate sum & product of array using pointers?

Posted 03 November 2017 - 07:11 PM

rumple,

I think the OP is actually using C.... not C++.....

see post #8 , #10 and the title...

This post has been edited by snoopy11: 03 November 2017 - 07:14 PM

Was This Post Helpful? 1
  • +
  • -

#15 r.stiltskin  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2011
  • View blog
  • Posts: 5,391
  • Joined: 27-December 05

Re: C function to calculate sum & product of array using pointers?

Posted 03 November 2017 - 07:36 PM

Oops, yes, I was looking at code in Post #9 and didn't look back any further.

So then yeah, in C it would be malloc and free rather than new and delete. But still, there's nothing to free before anything has been malloc'd.

Also, still not a good idea to return a pointer to memory that was allocated in the function. Better to return the struct by value.

So to take CTphpnwb's approach in C I'd do something like
#include <stdio.h>

#define arraySize 5

struct prodSum {
	int sum, product;
};

struct prodSum sumProd(int arr[], int arrSize) {
	struct prodSum sumP = {0,1};
	for(int i = 0; i < arrSize; i++) {
		sumP.sum += arr[i];
		sumP.product *= arr[i];
	}
	return sumP;
}

int main(int argc, const char * argv[]) {
	int anArray[] = {2,3,4,5,6};
	struct prodSum smm = sumProd(anArray, arraySize);

	printf("Sum: %d; product: %d\n", smm.sum, smm.product);
	return 0;
}





Note -- no dynamic memory allocation now (and no memory leak). Ah, but it's also not using pointers (except for the array itself) so not complying with the original topic.

And therefore rampager112's code in post #8 is "better" from the point of view of the actual assignment.

Edit: sorry, I kinda conflated C and C++ at first. I think it's fixed now.

This post has been edited by r.stiltskin: 03 November 2017 - 07:42 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2