0
#include <iostream>
using namespace std;

void matrice(int n){


cout<<"Input the number of the elements "<<endl;
cin>>n;
int D[n][n];
cout<<"Input the elements :";
for(int i=0;i<n;i++){
  for(int j=0;j<n;j++){
        cin>>D[i][j];

  }
}
int min;
for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
            if( min=0>D[i-1][j-1]){
        D[i-1][j-1]=min;
    }
    }
}
cout<<" The smallest element is : "<< min<<"and it is the"<< i <<" element ."  ;
}


int main(){
int i, min ,j,n;

int n1;
    cout<<"Decide: "<<endl;
    cout<<"Matrice[1]"<<"\t"<<"Vekcor[2]"<<endl;
cin>>n1;
if(n1==1){
    matrice(n);
}
else if(n1==2){
}
}



The problem is at line 22 at the cout and it gives this message: C:\Users\use\Documents\Dev C++\void_vektor.cpp|22|error: no match for 'operator<<' (operand types are 'std::basic_ostream' and '')|

  • 1
    `int D[n][n];` -- This is not valid C++. Arrays in C++ must have their size denoted by compile-time expression, not a runtime value such as `n`. – PaulMcKenzie Apr 01 '20 at 16:51
  • 1
    You've just discovered [why using namespace std is bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), why it results in confusing compiler errors, and why you should completely forget that it's a part of C++. Stop using it in your code, if you want to avoid confusing compiler error messages. – Sam Varshavchik Apr 01 '20 at 16:53
  • Thanks but that doesnt solve the problem! –  Apr 01 '20 at 16:56
  • 1
    The `min` in the `main` function, the `min` in `if (int min = 0 > D[i - 1][j - 1])` and the `min` in `cout << " Elementi me i vogel eshte : " << min;` are all different. You need to read up on [scope](https://en.wikipedia.org/wiki/Scope_(computer_science)) and decide which is the real `min`. – user4581301 Apr 01 '20 at 16:58
  • The title of this question mentions `cout>>`. You did mean `cout<<`, right? – Ted Lyngmo Apr 01 '20 at 17:12
  • There's already a standard function for doing this called [`std::min_element`](https://en.cppreference.com/w/cpp/algorithm/min_element). Use that instead. – Ted Lyngmo Apr 01 '20 at 17:14
  • Ok, whats the problem now? –  Apr 01 '20 at 17:20
  • 2
    @JPCommander please do not make changes to a question that invalidate correct answers. Ask a new question. – user4581301 Apr 01 '20 at 17:20

3 Answers3

1

min is only visible in for loop scope because you have declared it inside of loop.

declared it here:

int min=D[0][0];
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (min > D[i - 1][j - 1])
            {
                D[i - 1][j - 1] = min;
            }
        }
    }
    cout << " Elementi me i vogel eshte : " << min;

also note that you have used uninitialized n in main and even though you will take it as input in function sending an uninitialized variable to a function might be problematic.

and also move declaration of int D[n][n]; after taking n as input.

cout<<"Input the number of the elements "<<endl;
cin>>n;
int D[n][n];

instead of your loops I suggest this which is easier:

int min=D[0][0];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (min > D[i][j])
            {
                D[i][j] = min;
            }
        }
    }
    cout << " Elementi me i vogel eshte : " << min;

also note that if you initialize min=0 you can't find min in array in which all of elements>0. I suggest min=[0][0].

hanie
  • 1,863
  • 3
  • 9
  • 19
  • @user4581301 sorryI didn't notice that ,but I think min[0][0] is better since it's possible that all elements are positive – hanie Apr 01 '20 at 17:19
0

The main issue is that you declare min inside the for loop, it will go out of scope as the loop exits.

The bizarre error message is likely because of the std::min function. This is a good case study on why not to use using namespace std;.

At first glance there are other issues in your code:

In the future you should use compiler warnings.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
0

There's a fundamental problem here: the call matrice(n) in main uses the uninitialized value of n, so if your compiler supports that int D[n][n] syntax you have no idea how large the array actually is. Formally, the behavior is undefined. Informally, there's a 50/50 chance that you'll get a negative number, which doesn't make sense as an array size.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • I like your idea , but the problem is at the cout of line 22.Keep it up! –  Apr 01 '20 at 16:59
  • 1
    @JPCommander this solves a logic problem you will encounter later. you may be horrified to learn A) You can have more than one bug in the code at a time and B) where the error finally gets reported may be nowhere near where the error actually is. The error you are hunting is the result of at least two separate mistakes that happen in very different parts of the code. – user4581301 Apr 01 '20 at 17:06