1

All, I'm pretty new to C++. My teacher gave us the assignment of creating a function that would do the following:

//addbig( ) -- This function is sent an array of integers and the length 
//of the array.  
//It returns the sum of all integers in the array that are larger than 1000.

Maybe because, English is my second language, but I don't really understand what he is asking here.

Also, when taking input from the user, I was using cin >> when the arrays would be made of characters. But I tried to use it to receive input from an array of integers and is not letting me do that. Do I have to do a conversion first?

If any of you could shed some light, it would be great.

2
  • Doesn't sound like you are taking input from the user. You just need to make a function that takes in two parameters: an array of integers, and the size of the array who returns the sum of all integers in the array larger than 1000. Commented Nov 14, 2013 at 22:06
  • If you are responsible for reading the integers from a file (or the keyboard) before passing them to the assigned function then just file >> x; where x is an int and not a char. The stream is properly overloaded Commented Nov 14, 2013 at 22:08

2 Answers 2

1

I think you should start with the functions parameters. It says addBig has two parameters an array of integers and the length of the array.

int addBig(int arr[], int sze)
{
     int sum = 0;
     // do summation.
     return sum;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Note that the first parameter is not an array but actually a pointer. :)
Yeah I generally prefer to use pointers for this sort of thing. The only time I would actually use this (instead of say a vector) is if I wanted the function to work with different data structures. Who still uses arrays? I edited anyways for simplicity.
0

As for the input of integers

const size_t N = 10;
int a[N];

std::cout << "Enter " << N << " integer values: ";
for ( size_t i = 0; i < N; i++ ) std::cin >> a[i];

As for the function then you should declare it as

int addbig( int a[], size_t n );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.