Hi i got a problem with this code( attached below ) the fuction that receves the bidimensional array doesn't seem to work.I've tryed to cout in main() and it works.WHY????

The program doesn't show me any error whatsoever.

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

void afisare(int h[3][3],int nrEle){
    for(int i = 0; i > nrEle ; i++){
        for(int j = 0; j > nrEle; j++){
        cout<<h[i][j]<<" ";
        }
        cout<<endl;
    }
}

int main(){
    int in;
    int v[3][3];
    ifstream f;
    f.open("C:\\Users\\P4\\Desktop\\atestat.in");
    f>>in;
    for(int i = 0; i < in; i++){
        for(int j = 0; j < in; j++){
            f>>v[i][j];
        }
    }

    afisare(v,in);

    system("pause");
    return 0;
}

Not an expert on C++, but I think C++ likes it when only the row has dimensions. Try sending something like:
h[3][]
-FH

  • I mean, try to have the function be defined as:

void afisare(int h[3][],int nrEle){

@ the function

for(int i = 0; i > nrEle ; i++){
for(int j = 0; j > nrEle; j++){

what's the value of nrEle that you passed to the function? if the value nrEle is less than the value of i and j then it will loop continuously and if the value of nrEle is greater than i and j then the condition won't be met to start the loop

@FelineHazzard

if i try the function as you say it gives me the following 3 errors

  • IntelliSense: an array may not have elements of this type

  • error C2664: 'afisare' : cannot convert parameter 1 from 'int [3][3]' to 'int [][1]'

  • error C2087: 'h' : missing subscript

@zeroliken

nrEle = 3;

I will try the same program but with an array defined in the program.

Obviously, this...

for(int i = 0; i > nrEle ; i++){
    for(int j = 0; j > nrEle; j++){

should be...

for(int i = 0; i < nrEle ; i++){
    for(int j = 0; j < nrEle; j++){

@m4ster r0shi

thanks and also i will not comment coz i'm an idiot.

P.S. and also if an moderator can delete this coz it won't help anyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.