Writing functions that require a single parameter

Hello, I am not sure if I am even starting this correctly? If anyone could point me in the right direction or even refer a resource that could help me with this. Another issue I cant figure out is that the assignment is asking for the sum and product of a number, meaning if the number is 4, I must calculate 1+2+3+4 and 1*2*3*4. but I am not sure of a way to do so, most methods show from searching have more advanced and unfamiliar code in them. I am only in intro to C++.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  #include <iostream>
#include <string>
void sums(int);
void products(int);
double getSum();
double getProduct();
using namespace std; 

int main() 
{
   double number;              	
   int sum;
   int product;						
   cout << "Enter a positive integer or 0 to quit: ";
   cin >> number; 

   while(number != 0)
   {		
      // Call sums function here 
      sum = getSum();
      // Call products function here
      product = getProduct();
      cout << "Enter a positive integer or 0 to quit: ";
      cin >> number; 
   }
   return 0;
} // End of main function
	
// Write sums function here
double getSum()
{
  for (int i = 0, i < number, i++)
  {
    
  }

}

// Write products function here
double getProduct()
{
  for (int i = 0, i < number, i++)
  {
    
  }

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
	for (int number {}; (cout << "Enter a positive integer or 0 to quit: ") && (cin >> number) && (number > 0); ) {
		int sum {}, product {1};

		for (int i = 1; i <= number; ++i) {
			sum += i;
			product *= i;
		}

		cout << "Sum is: " << sum << "\nProduct is: " << product << '\n';
	}
}

I am not able to change up a lot of the original code. only what after the comments,
but I do understand now how to do the calculations, thank you.

Is there any reason why my call functions aren't working. My dev C++ tells me the lines that have errors and a small explanation.

for
sum = sums(int);
I get
[Error] expected primary-expression before 'int'
what does this mean?
learning to read the error messages is useful. I recommend to beginners that once you fix the error, you re-read the message and try to see if you can grasp what it was telling you. A starting place is that the line number will be given: what line has that error message, and what was before it? You may have missed a ;.

I don't get the same errors you did, is it the same code?
anyway, lets start slowly.
- your functions do not DO anything.
- getSum refers to 'number' which is a LOCAL variable to main; there IS NO SUCH variable at this "SCOPE" -- the compiler can't find it!

- you asked about a parameter, but have none.
- the product one is factorial and will blow out even a double after a short while (too big to fit). Don't worry about this, just use smaller numbers for now.

I would comment out product for now and get sum working.

try these ideas:
1
2
3
4
5
6
7
8
9
double getSum(int number); //fix the header part

double getSum(int number); 
{
     double total = 0.0;
     for(int i = 1; i <= number; i++)
            total+=i;
    return total;
}


the sum from 1 to N can just be directly computed with a formula, but that is math, not programming, and you can check out doing it that way later. https://en.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_%E2%8B%AF

if you cannot change the while loop in main (that seems wrong?) then what is getSum supposed to DO? The loop is adding up something. All getsum would do is increment a static variable, as written:
double getsum()
{
static double derp = 0;
derp +=1;
return derp;
} //returns 1, 2, 3, 4, 5,...

or are you doing the 1 to N for 1, then again for 2, then again for 3, ..? Its really unclear.
Last edited on
My apologies, I was changing things in an attempt to understanding how things worked,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// SumAndProduct.cpp - This program computes sums and products 
// Input:  Interactive
// Output:  Computed sum and product 

#include <iostream>
#include <string>
void sums(int);
void products(int);
using namespace std; 

int main() 
{
   double number;              	
   						
   cout << "Enter a positive integer or 0 to quit: ";
   cin >> number; 

   while(number != 0)
   {		
      // Call sums function here 
      
      // Call products function here
      
      cout << "Enter a positive integer or 0 to quit: ";
      cin >> number; 
   }
   return 0;
} // End of main function
	
// Write sums function here


// Write products function here 


this is the starting code and I can only add what's after the comments.
if you really must use that code, then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>

void sums(int);
void products(int);

using namespace std;

int main()
{
	double number;

	cout << "Enter a positive integer or 0 to quit: ";
	cin >> number;

	while (number != 0)
	{
		// Call sums function here
		sums(number);

		// Call products function here
		products(number);

		cout << "Enter a positive integer or 0 to quit: ";
		cin >> number;
	}
	return 0;
} // End of main function

// Write sums function here
void sums(int n)
{
	cout << "Sum is: " << (n * (n + 1)) / 2 << '\n';
}

// Write products function here
void products(int n)
{
	int product {1};

	for (int i = 1; i <= n; product *= i++);

	cout << "Product is: " << product << '\n';
}

Is there a reason as to why the original code was written as
1
2
void sums(int);
void products(int);

and not ?
1
2
void sums();
void products();


Also I noticed you used a different method for solving the sum that am unfamiliar with, I understand the math but would the method used for products not work for sums if '*' is changed to '+' ?
void sum(int);

is a function declaration. It declares that sums is a function and takes one parameter that is an int and doesn't return a value. So when the code later uses sums it knows about it. void sums() is a function that takes no parameters which is not what is used here.

The formula for the sum of n numbers is n * (n + 1) / 2. Yes, the method used for products would work with * replaced by + and the initial value initialised to 0 rather than 1. See my earlier post.
Thank you all so much, Emailed my teacher 2 days ago...
Topic archived. No new replies allowed.