how to call 2d arrays with arguments

I have not posted the entire code, just the part where I get the error.

I keep getting this error when i run the code: error: cannot convert ‘int*’ to ‘int (*)[10]’ for argument ‘1’ to ‘int ex3(int (*)[10])’

Also, do i have to make a prototype for the function ex3? My instructor wrote like x = dajkld(); inside int main() without making a prototype.

Please help me, i don't know how to call the function ex3 with the correct parameters



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  int ex3(int a[10][10])        // this is a function header
{                           }                     // some code was written inside brackets



bool tested(int a[], int len)       // another function header, how do i call it inside main?
{                                }

 int main()
{

 ex3(arr);            // this code keeps giving me errors, whenever i run it.
                      // How do i call it correctly with the header's parameters?




}

Last edited on
closed account (1vRz3TCk)
Does this help?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

int ex3(int a[3][2])
{ 
    return a[1][0];
}   

bool tested(int a[], int len)
{
    return false;
}

int main()
{
 int arr[3][2] = {{1,2},
                  {3,4},
                  {5,6}};
                  
 int arr2[4] = {1,2,3,4};
 
 std::cout << ex3(arr) << "\n"; 
 
 std::cout << tested(arr2, 4) << "\n";
                      
}
Last edited on
thanks for the reply. it is not want i wanted. i want to know how to just call the function - - -

ex3(arr); with parameters inside the int main() correctly when its header is int ex3(int a[10][10]). Also do

i have make a prototype out of it?
Last edited on
closed account (1vRz3TCk)
I've added code to call bool tested(int a[], int len).

Apart from the fact that I used a 3x2 array rather than a 10x10 it shows calling the ex3(arr) function on line 21.
Hey thanks for the reply. I tried to apply what you typed but it does not make sense to me. Here is the code i am stuck on. I am stuck on line 87 and line 91, i do not know how to call them using the functions provided below. Line 83 is a variable used for a function that i did not include here. On a side note, why do you use std:: cout instead of using namespace std; is it just a personal preference?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<iostream>
#include<string>
using namespace std;

int countNegatives(int a[100][100])
{

  int count = 0;

  for (int i = 0; i < 100; i++)

  {

    for (int j = 0; j < 100; j++)

    {

      if(a[i][j] < 0)

      {
          
          count++;

      }


    }

  }


  return count;



}



bool isStrictlyIncreasing(int a[], int len)


{

  bool positive;



for (int i=0; i < len-1; i++)



{



  if (a[i] < a[i+1])

  bool positive = true;

  else

  {

    return false;

  }
  

}


return positive;



}


int main()
{
    
      int arr[9] = {1,2,3,4,5,6,7,8,9};                        // Used for different functions not 
                                                                             // displayed in this code.
      
      
      
      cout << countNegatives(( int*)arr,100,200);         //Help with function parameters, gives
                                                                               //errors



cout << isStrictlyIncreasing();        //Help  with function parameters.

}
Last edited on
closed account (1vRz3TCk)
Lets start with int a[100][100]. This 100 lines of 100 int elements. now I'm lazy so this is what int a[10][10],10 lines of 10 int elements, would look like:
1
2
3
4
5
6
7
8
9
10
    int arr[10][10] = { {-4, -2, 3, 4, 7, 6, 7, 8, 9, 5},
                        {-4, -2, 3, 4, 7, 6, 7, 8, 9, 5},
                        {-4, -2, 3, 4, 7, 6, 7, 8, 9, 5},
                        {-4, -2, 3, 4, 7, 6, 7, 8, 9, 5},
                        {-4, -2, 3, 4, 7, 6, 7, 8, 9, 5},
                        {-4, -2, 3, 4, 7, 6, 7, 8, 9, 5},
                        {-4, -2, 3, 4, 7, 6, 7, 8, 9, 5},
                        {-4, -2, 3, 4, 7, 6, 7, 8, 9, 5},
                        {-4, -2, 3, 4, 7, 6, 7, 8, 9, 5},
                        {-4, -2, 3, 4, 7, 6, 7, 8, 9, 5}, };


when you have a variable that is an int [100][100] you can just pass it to the function, countNegatives( arr). In the code you have arr is a 1d array of 9 int you will never be able to pass that to countNegatives().

You can pass your arr array to bool isStrictlyIncreasing(int a[], int len) like this bool isStrictlyIncreasing(arr, 9)

On a side note, why do you use std:: cout instead of using namespace std; is it just a personal preference?
Not entirely personal preference but my reasons might be a bit over your head at this point. I'll find a link to an old post about it...
The gist is Prefer Using Declarations to Using Directive but prefer full scope resolution to Using Declarations.
http://www.cplusplus.com/forum/beginner/9181/#msg42419 is a post from an old account of mine back in 2009
Last edited on
Thank you! Here's the modifications i made:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{



int a[100][100];
  

cout << "Negatives:" << countNegatives(a) << endl;        // this function keeps giving 
                                                         // specific numbers like 62,
                                                        // 117, and 17.  different numbers are given
                                                       // each run. so it gives me 62 then i run it
                                                      // again and it gives 117


isStrictlyIncreasing(arr, 9);
cout << "strictly increasing:" << isStrictlyIncreasing(arr, 9);     // this function keeps giving
                                                                   // 0 when i run the code
                              
                                 

}





One last question. Is this the right output when running the code or there is a weird issue with the program?
I was expecting a specific number like 1 rather than getting random numbers every time i run the code or get a zero.
Last edited on
closed account (1vRz3TCk)
I would expect a random number of negatives as you have not initialised the array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
using namespace std;

int countNegatives(int a[100][100]) 
{
    int count = 0;

    for (int i = 0; i < 100; i++)
    {
        for (int j = 0; j < 100; j++)
        {
            if (a[i][j] < 0)
            {
                count++;
            }
        }
    }
    return count;
}

bool isStrictlyIncreasing(int a[], int len)
{
    bool positive;

    for (int i = 0; i < len - 1; i++)
    {
        if (a[i] < a[i + 1])
        {
            positive = true;
        }
        else
        {
            return false;
        }
    }
    return positive;
}

int main()
{
    int arr[100][100] = { 0 }; //initialise all members to zero

    cout << "Negative element count: " << countNegatives(arr) << "\n"; 

    arr[1][2] = -2;

    cout << "Negative element count: " << countNegatives(arr) << "\n";
           
    int arr2[5] = { 1,2,3,4,5 };
    cout << boolalpha << "Strictly Increasing: " << isStrictlyIncreasing(arr2, 5) << "\n";

    arr2[3] = 8;
    cout << boolalpha << "Strictly Increasing: " << isStrictlyIncreasing(arr2, 5) << "\n";

    // boolalpha is a stream manipulator, read about it here :
    // http://www.cplusplus.com/reference/ios/boolalpha/

    return 0;
}
Thanks. I hope i made the correct adjustments.
Topic archived. No new replies allowed.