-2

In this class exercise we are to use a textfile that includes 20 numbers. So, the max of both arrays are to be 20. Using a function we need to fill the array. Each successive position in the array will be a calculated value based on the original start value and the index into the array as follows:
1) Add 45 to the start value
2) if the array position is evenly divisible by 3:
A) The calculated value is the start value times the array position
3) otherwise:
A) The calculated value is the start value divided by array position
4) Store the calculated value in the next array position
5) Increment the number of values in this array

For some reason I get an error telling me I can't convert argument 2 from 'int[20] to 'int'?

void FillArray(ifstream& Ex5Numbers, int calculatedArray[], 
                int& numOfCalculatedValues, const int MAX)
{
 int index;
 int startValue;

//Read the starting value
Ex5Numbers >> startValue;

//Filling the array through the input file
for (index = 0; index <= MAX; index++)
{
    //Store the start value into the array
    calculatedArray[index] = startValue;

    //Add 45 to the start value
    startValue += 45;

    //Calculate the next number in the array
    if (numOfCalculatedValues / 3 == 0)
    {
        startValue = index * startValue;
    }
    else
    {
        startValue = startValue / index;
    }

    //Increment the tag field for the array
    numOfCalculatedValues++;

}
4
  • Maybe you could search StackOverflow for "C++ passing array function", and see what other people have posted? Nov 19, 2014 at 17:47
  • As you tagged this as C++, why don't you use the containers of the C++ standard library ? (std::vector to name it) instead of error-prone C-style arrays ? This will be much easier.
    – kebs
    Nov 19, 2014 at 17:49
  • 1
    Please edit your question and append the code that calls your function. Nov 19, 2014 at 17:51
  • nb using /= and *= or ternary would be nice here... it's good to learn using the language's features ASAP. Also, why not just return the numOfCalculatedValues from this function instead of modifying some int through a reference?
    – user719662
    Nov 19, 2014 at 17:53

1 Answer 1

0

Works fine here (I replaced ifstream with std::istream because I'm too lazy to open a file):

const int MAX = 20;
int calculatedArray[MAX + 1]; // fix off-by-one error in FillArray
int numOfCalculatedValues = 1; // must be one of the computation is off

FillArray(std::cin, calculatedArray, numOfCalculatedValues, MAX);

Note: (numOfCalculatedValues / 3 == 0) does not test for divisibility by 3.

Note2: passing numOfCalculatedValues is harmfull. Use the index instead.

Note3: you have an off-by-one error, you are computing MAX + 1 elements

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.