0
In this program:

1) an integer typed 2D array named  student_scores[Rows][Cols] is initialized by providing initialization list

2) then this array is passed to a function named total_scores which recevies two aurguments
    .- the first aurgument is for array with explicit use of column subscript
    .- the second one is row size
    .- e.g int total_scores(const int [][Cols], int);

3) in the funciton definition, the function sums all the contents/ scores of arrays into an integer type variable named int total

4) then the total is shown by calling this funciton in the main() function with cout statement as:

   cout<<"Total of all the values in student_scores[][]: "
       <<total_scores(student_scores, Rows);

5) here it is ok

** 6) But when i compile this program, the compiler generates an error something like that

in function main:
[Linker error] undefined reference to 'total_socres(int const[][3], int)'
id returned 1 exit status


thus the problem is 
** 1) why this error occours while i have provided the function definition

** 2) how to resolve this error in order to run this program**





    #include<iostream>
    #include<conio.h>
    using namespace std;

    const int Rows=3;   
    const int Cols=3;

    int total_scores(const int [][Cols], int);

    main()
    {
       int student_scores[Rows][Cols]={ {1,3,5},
                                        {2,4},
                                        {4,5,6}};


       cout<<"Total of all the values in student_scores[][]: "
           <<total_scores(student_scores, Rows);   


    getch();      
    }

    int total_socres(const int array[][Cols], int Rows)
    {
     int total =0;
     for(int i=0; i < Rows; i++ )
     {
       for(int j=0; j<Cols; j++)
       {
        total = total+array [Rows][Cols];   

       }      

     }    

        return total;

    }
3
Contributors
3
Replies
23
Views
3 Years
Discussion Span
Last Post by usama sadaqat
1

line 44 should be int main() and obviously return an int value.

line 42 int total_scores(const int [][Cols], int)
line 58 int total_socres(const int array[][Cols], int Rows) notice the spelling mistake?

line 65 total = total + array [Rows][Cols]; - so for each iteration you are adding array[3][3] which is out of bounds for the array and would just contain memory junk.

It should be total = total + array[i][j]; which is equivalent to total += array[i][j];

As for getch(), using cin.get() would be preferable.
Better code formatting would be helpful both for you and others reading your code.

0

Thank YOU very much nullptr. your desribing method is very good. i got it eaisly. and solved my problem.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.