LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-30-2012, 07:46 PM   #1
kevinamygrayson
LQ Newbie
 
Registered: Apr 2012
Location: Decatur, Ga
Distribution: Slackware 13.37, and others
Posts: 20

Rep: Reputation: Disabled
C++ passing 2d array to a function


Code:
#include<iostream>
using namespace std;

//Global constants
const int COLUMNS = 5;
const int ROWS = 5;

//function prototypes
int getTotal(const int [][COLUMNS]);

int main()
{
int total;

int fives[ROWS][COLUMNS]= {{1, 1, 1, 1, 1,},
                           {2, 4, 8, 16, 32},
                           {3, 9, 27, 81, 243},
                           {4, 16, 64, 256, 1024},
                           {5, 25, 125, 625, 3125}};

cout << "The total of the array is: ";
getTotal(fives, ROWS)
return 0;
}

int getTotal(const int array[][COLUMNS])
{
for(int row = 0; row < ROWS; row++)
{
    for (int column = 0; column < COLUMNS; column++)
    {
        int total += array[row][column];
        
    }
}
return total;
}
|error: too many arguments to function 'int getTotal(const int (*)[5])'|

I can't for the life of me figure how my function prototype has too many arguments. If I comment out the function call, the error goes away.

Last edited by kevinamygrayson; 10-30-2012 at 07:58 PM. Reason: readability
 
Old 10-30-2012, 08:32 PM   #2
shefla
Member
 
Registered: Oct 2012
Location: France
Distribution: Archlinux
Posts: 35

Rep: Reputation: 5
The problem is obviously in your call.
You only need to pass 1 argument which is the table pointer.
Here is how it should be:

Code:
#include<iostream>
using namespace std;

//Global constants
const int COLUMNS = 5;
const int ROWS = 5;

//function prototypes
int getTotal(const int [][COLUMNS]);

int main()
{
int total;

int fives[ROWS][COLUMNS]= {{1, 1, 1, 1, 1,},
                           {2, 4, 8, 16, 32},
                           {3, 9, 27, 81, 243},
                           {4, 16, 64, 256, 1024},
                           {5, 25, 125, 625, 3125}};

cout << "The total of the array is: " << getTotal(fives); // Change 1
return 0;
}

int getTotal(const int array[][COLUMNS])
{
int total = 0; // Change 2
for(int row = 0; row < ROWS; row++)
{

    for (int column = 0; column < COLUMNS; column++)
    {
        total += array[row][column];  // Change 3
    }
}
return total;
}
 
1 members found this post helpful.
Old 10-30-2012, 09:35 PM   #3
kevinamygrayson
LQ Newbie
 
Registered: Apr 2012
Location: Decatur, Ga
Distribution: Slackware 13.37, and others
Posts: 20

Original Poster
Rep: Reputation: Disabled
Now when I compile, I get a

undefined reference to `getTotal(int const (*) [5])'

error.
 
Old 10-30-2012, 09:42 PM   #4
shefla
Member
 
Registered: Oct 2012
Location: France
Distribution: Archlinux
Posts: 35

Rep: Reputation: 5
With g++ on my machine (Archlinux) I don't get the problem.

Code:
[shefla@vlm-desk ~]$ g++ main.cpp 
[shefla@vlm-desk ~]$ ./a.out 
The total of the array is: 5699[shefla@vlm-desk ~]$
What is your OS ?
Which compiler are you using (name + version) ?
What is the command line used ?
 
1 members found this post helpful.
Old 10-30-2012, 09:53 PM   #5
kevinamygrayson
LQ Newbie
 
Registered: Apr 2012
Location: Decatur, Ga
Distribution: Slackware 13.37, and others
Posts: 20

Original Poster
Rep: Reputation: Disabled
Code::Blocks 10.05 in Windows7 from the editor in the IDE rather than the command line(which is a non entity in Windows for all practical purposes).
 
Old 10-30-2012, 10:06 PM   #6
kevinamygrayson
LQ Newbie
 
Registered: Apr 2012
Location: Decatur, Ga
Distribution: Slackware 13.37, and others
Posts: 20

Original Poster
Rep: Reputation: Disabled
What gets me is that this code will compile and run:
Code:
#include<iostream>
#include<iomanip>
using namespace std;

const int COLS = 4;
const int TBL1_ROWS =3;
const int TBL2_ROWS = 4;

void showArray(const int [][COLS], int);

int main()
{
    int table1[TBL1_ROWS][COLS] = {{1, 2, 3, 4},
                                   {5, 6, 7, 8},
                                   {9, 10, 11, 12}};
    int table2[TBL2_ROWS][COLS] = {{10, 20, 30, 40},
                                   {50, 60, 70, 80},
                                   {90, 100, 110, 120},
                                   {130, 140, 150, 160}};
    cout << "The contents of table 1 are \n";
    showArray(table1, TBL1_ROWS);
    cout << "The content of table 2 are \n";
    showArray(table2, TBL2_ROWS);
    return 0;
}

void showArray(const int array[][COLS], int rows)
{
    for (int x = 0; x < rows; x++)
    {
        for (int y = 0; y < COLS; y++)
        {
            cout << setw(4) << array[x][y] << " ";
        }
        cout << endl;
    }
}
 
Old 10-30-2012, 10:34 PM   #7
kevinamygrayson
LQ Newbie
 
Registered: Apr 2012
Location: Decatur, Ga
Distribution: Slackware 13.37, and others
Posts: 20

Original Poster
Rep: Reputation: Disabled
It is now working after tweaking a bit. Thanks for your help!
 
Old 10-31-2012, 12:13 AM   #8
kevinamygrayson
LQ Newbie
 
Registered: Apr 2012
Location: Decatur, Ga
Distribution: Slackware 13.37, and others
Posts: 20

Original Poster
Rep: Reputation: Disabled
Just wanted to say thanks again. here is the finished result.

Code:
#include <iostream>
#include<ctime>
#include<cstdlib>
#include<iomanip>
using namespace std;

//Global constants
const int COLUMNS = 5;
const int ROWS = 5;

//function prototypes
int getTotal(const int [][COLUMNS]);
double getAverage(const int [][COLUMNS]);
int getRowTotal(const int [][COLUMNS], int);
int getColumnTotal(const int [][COLUMNS], int);
int getHighestInRow(const int [][COLUMNS], int);
int getLowestInRow(const int [][COLUMNS], int);
int random();

int main()
{
    double average;
    int total,
        rowTotal,
        columnTotal,
        rowHigh,
        rowLow,
        randResult;

    int fives[ROWS][COLUMNS]= {{1, 1, 1, 1, 1},
                              {2, 4, 8, 16, 32},
                              {3, 9, 27, 81, 243},
                              {4, 16, 64, 256, 1024},
                              {5, 25, 125, 625, 3125}};


    cout << "The total of the array is: "
         << getTotal(fives);
    cout << "\n\nThe average of all values is: "
         << fixed << showpoint << setprecision(2)
         << getAverage(fives);
    randResult = random();
    cout << "\n\nThe sum of row # " << (randResult +1) << " is: "
         << getRowTotal(fives, randResult);
    randResult = random();
    cout << "\n\nThe sum of column # " << (randResult +1) << " is: "
         << getColumnTotal(fives, randResult);
    randResult = random();
    cout << "\n\nThe high number in row # " << (randResult +1) << " is: "
         << getHighestInRow(fives, randResult);
    randResult = random();
    cout << "\n\nThe low number in row # " << (randResult +1) << " is: "
         << getLowestInRow(fives, randResult);

    return 0;
}

int getTotal(const int array[][COLUMNS])
{
    int total = 0;
    for(int row = 0; row < ROWS; row++)
    {
        for (int column = 0; column < COLUMNS; column++)
        {
            total += array[row][column];
        }
    }
return total;
}

double getAverage(const int array[][COLUMNS])
{
    int total = 0;
    double divisor = 0.00, avg = 0.00;
    for(int row = 0; row < ROWS; row++)
    {
        for (int column = 0; column < COLUMNS; column++)
            {
                total += array[row][column];
                divisor += 1;
            }
    }

avg = total / divisor;
return avg;
}

// function random
int random()
{
    unsigned seed = time(0);
    srand(seed);
    return 0 + rand() % 4;
}

int getRowTotal(const int array[][COLUMNS], int row)
{
    int total = 0;
    for (int column = 0; column < COLUMNS; column++)
    {
        total += array[row][column];
    }
    return total;
}

int getColumnTotal(const int array[][COLUMNS], int column)
{
    int total = 0;
    for (int row = 0; row < ROWS; row++)
    {
        total += array[row][column];
    }
    return total;
}



    //
int getHighestInRow(const int array[][COLUMNS], int row)

    {
        int highNum = array[row][0];
        for (int column = 0; column < COLUMNS; column++)
        {
           if (highNum < array[row][column])
            {
                highNum = array[row][column];
            }

        }
    return highNum;
    }


    //
int getLowestInRow(const int array[][COLUMNS], int row)

    {
        int lowNum = array[row][0];
        for (int column = 0; column < COLUMNS; column++)
        {
           if (lowNum > array[row][column])
            {
                lowNum = array[row][column];
            }

        }
    return lowNum;
    }
 
Old 10-31-2012, 06:17 AM   #9
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by kevinamygrayson View Post
Just wanted to say thanks again. here is the finished result.
For very large arrays, you should consider passing the array by reference. Otherwise, a copy of the array that is passed to the function is constructed, and this could lead to inefficiencies in your application.

Here's a simple example that you can examine to see how the different styles are accomplished:
Code:
#include <iostream>    // for cout, endl
#include <cstddef>     // for size_t

const size_t ROWS    = 5;
const size_t COLUMNS = 5;

void passByValue(const int array[][COLUMNS])
{
    std::cout << "address of array: " << &array << std::endl;
}

void passByReference(const int (&array)[ROWS][COLUMNS])
{
    std::cout << "address of array: " << &array << std::endl;
}

int main()
{
    int a[ROWS][COLUMNS];

    std::cout << "address of array (from main): " << &a << std::endl;

    passByValue(a);

    passByReference(a);
}
 
Old 10-31-2012, 06:54 AM   #10
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,142

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by dwhitney67 View Post
For very large arrays, you should consider passing the array by reference. Otherwise, a copy of the array that is passed to the function is constructed,
In C and C++, arrays are passed by pointer. A copy of the array is not made. Passing by reference just adds an inefficient extra layer of indirection.

Large structs should be passed explicitly by pointer in C or C++ or by reference in C++ for the reason you described. But it doesn't apply to arrays.

In C++, using std::vector is simpler than dealing with the array issues that were dealt with earlier in this thread. If you have a vector of vectors, the compiler and run time library deal with all 2d size issues and you can focus on your program's logic rather than on tricks for 2d arrays. Note that a std::vector is a struct, not an array so passing it by reference is important.
 
1 members found this post helpful.
Old 10-31-2012, 07:00 AM   #11
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by johnsfine View Post
In C and C++, arrays are passed by pointer.
Thanks for the correction.

I just realized that the program I posted prints out the address of the parameter in the function, and hence the reason why it is different from the address of the local variable in main(). Both variables point to the same region in memory.
 
1 members found this post helpful.
  


Reply

Tags
c++, error message, functions


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] passing a 2D array to a function in C++ aihaike Programming 2 03-28-2012 09:14 PM
C passing a Array of Structuers as an argument in a function dman777 Programming 5 08-22-2011 07:16 PM
[SOLVED] Passing array to thread function zak100 Programming 4 08-12-2010 12:07 PM
passing array and variable to function in bash script ajaypitroda Programming 2 07-08-2009 12:10 AM
ARGGGH! Passing array of structs to function in C. CoderMan Programming 5 02-05-2009 11:44 PM


All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration