1

I have a quick question. Trying to create a method to print out an array, however, the compiler is telling me that it isn't declared in the scope.

Here's my code:

int main() {
    int theArray[10] = {41, 23, 43, 12, 43, 23, 12, 41, 29, 102};
    printArray(theArray, 10);
}

Here's my printArray method:

void printArray(int arr[], int size) {
    int i;

   for (i = 0; i < size; i ++) {
       std::cout <<" "<< arr[i];
       std::cout << std::endl;
   }
}

Any clue what I'm doing wrong?

2
  • 2
    You forgot to declare the function before main. Commented Feb 23, 2016 at 6:38
  • @molbdnilo, Thank you, sir! Commented Feb 23, 2016 at 6:39

1 Answer 1

3

Your code is perfectly fine. Just make sure that necessary includes are exist. And the order of the methods:

#include <iostream>
#include <string>

void printArray(int arr[], int size) {
    int i;
    for (i = 0; i < size; i++) {
        std::cout << " " << arr[i];
        std::cout << std::endl;
    }
}

int main() {
    int theArray[10] = { 41, 23, 43, 12, 43, 23, 12, 41, 29, 102 };
    printArray(theArray, 10);
    return 0;
}

Or use forward declaration:

#include <iostream>
#include <string>

void printArray(int arr[], int size);

int main() {
    int theArray[10] = { 41, 23, 43, 12, 43, 23, 12, 41, 29, 102 };
    printArray(theArray, 10);
    return 0;
}

void printArray(int arr[], int size) {
    int i;
    for (i = 0; i < size; i++) {
        std::cout << " " << arr[i];
        std::cout << std::endl;
    }
}

However, you may write it in more C++ modern Style:

1) Using templated function :

#include <iostream>
#include <string>
#include <array>
template<std::size_t N> void printArray(std::array<int, N> x) {
    for (auto const& item:x){
        std::cout << " " << item << std::endl;
    }
}

int main() {
    std::array <int, 10> theArray{ { 41, 23, 43, 12, 43, 23, 12, 41, 29, 102 } };
    printArray<10>(theArray);
    return 0;
}

2)The better by using iterators:

#include <iostream>
#include <string>
#include <array>
template<typename Iterator> void printArray(Iterator beg, Iterator end) {
    for (auto it = beg; it != end; it = std::next(it)){
        std::cout << " " << *it << std::endl;
    }
}

int main() {
    std::array <int, 10> theArray{ { 41, 23, 43, 12, 43, 23, 12, 41, 29, 102 } };
    printArray(std::begin(theArray), std::end(theArray));
    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Go with option 1. The forward declare doesn't get you anything here except another line of code to be maintained and weird linker errors if you change the function's prototype and not the forward declaration.
Or use a std::vector i know the memory storage is different, but will likely be more useful thing to learn in this scenario.
use std::vector just when the number of elements is not constant.

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.