Need assistance with c++ question in my book?

The question that is in my book says:
define a function with a single int type parameter n. the function should utilize a loop to read a loop to read in n integer values , then return the sum of these n numbers.

write the function definition only.

I thought of something like this but i don't know if im doing it right
1
2
3
4
5
6
7
8
int readInt(int n)
{
	for( int i = 0; i<n; i++)
{
	cout << i <<","<<endl;
}
return n;
}
the code i put did not make sense maybe this does I really dont know what to do when it worded like that.

1
2
3
4
5
6
7
8
int readInt(int n)
{ int sum = 0;
	for( int i = 0; i<n; i++)
{     sum +=i;
	cout << sum <<","<<endl;
}
return n;
}
You're not reading in any values, nor adding these values to a running sum.

What's the point of returning n? The caller already knows what n is. Don't you mean to return sum?
Last edited on
yes i do mean return sum sorry .If I am not reading in any values what can i change in code so it does read any value.
1
2
3
4
5
6
7
8
 int readInt(int n)
{ int sum = 0;
	for( int i = 0; i<n; i++)
{     sum +=i;
	cout << sum <<","<<endl;
}
return sum;
}
1
2
3
4
5
6
7
8
9
10
 int readInt (int n)
{ int sum = 0;
   int val;
   
   for (int i = 0; i<n; i++)
   {     cin >> val;    // Read in a value
         sum += val;  // Add the value to the running total
   }
   return sum;
}

Topic archived. No new replies allowed.