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

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,468
  • Joined: 08-August 08

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

Posted 03 November 2017 - 07:47 PM

Well, I was taking advantage of my OS's handling the memory: It's all freed when the app exits, and the last thing the code does is use the pointer, so it's not really necessary to free it manually.

As for the parameters in main(), Xcode provides them by default when you start a new project. I rarely bother to remove them. Sometimes they come in handy!

Updating my code to "fix" these two:
#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() {
	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;

	delete smm;
	smm = nullptr;

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#17 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1376
  • View blog
  • Posts: 4,311
  • Joined: 20-March 10

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

Posted 03 November 2017 - 11:48 PM

Again,

the OP is using C not C++,

see the title....CTphpnwb

This post has been edited by snoopy11: 03 November 2017 - 11:48 PM

Was This Post Helpful? 0
  • +
  • -

#18 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,468
  • Joined: 08-August 08

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

Posted 05 November 2017 - 05:55 AM

Oops.
#include <stdio.h>
#include <stdlib.h>

#define ARRAYSIZE 5

struct prodSum {
	int sum, product;
} typedef SumProduct;

SumProduct * sumProd(int arr[], int arrSize) {
	SumProduct *sumP = (SumProduct*)malloc(sizeof(SumProduct));
	sumP->sum = 0;
	sumP->product = 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[ARRAYSIZE] = {2,3,4,5,6};
	SumProduct *smm = sumProd(anArray, ARRAYSIZE);
	printf("Sum: %d  Product %d\n",smm->sum, smm->product);
	free(smm);

	return 0;
}


This post has been edited by CTphpnwb: 05 November 2017 - 02:54 PM

Was This Post Helpful? 0
  • +
  • -

#19 jimblumberg  Icon User is online

  • member icon

Reputation: 5333
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

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

Posted 05 November 2017 - 07:21 AM

Normally when I see someone saying "C function to calculate sum & product of array using pointers?" I tend to think that they want to traverse the array using pointer notation instead of array notation.

    for(size_t i = 0; i < arrSize; ++i){
		sumP.sum += *(arr + i);
		sumP.product *= *(arr + i);
	}


Or

       for(size_t i = 0; i < arrSize; ++i){
          sumP.sum += *arr;
          sumP.product *= *arr;
          *arr++;
	}


Jim

This post has been edited by jimblumberg: 05 November 2017 - 07:22 AM

Was This Post Helpful? 0
  • +
  • -

#20 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,387
  • Joined: 27-December 05

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

Posted 05 November 2017 - 07:42 AM

The topic title was a bit ambiguous, but the op said in the first post:

Quote

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

Was This Post Helpful? 1
  • +
  • -

#21 jimblumberg  Icon User is online

  • member icon

Reputation: 5333
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

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

Posted 05 November 2017 - 08:13 AM

Okay, I agree, and after re-reading the first post both the first post and the title are a little vague.

Quote

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


Here one could construe that the use of a structure is necessary since you can only "return" one item. But if you used a structure pointers wouldn't be necessary, since you can return a structure from a function without using pointers. Now if you leave out structures then you are left with using pointer parameters to "return" the required values.

So IMO the best solution, for a beginner, would go to post #8, the OP's solution.


Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2