-2

In this code I'm finding the values inside an Array using n (size of matrix) that starts with indices 1;1 (is that okay to be used?) and after the code calculates the sin (using the formula in the code) it counts the amount of positive elements in the Array.

And my question is, how do i use cout<< in a way that shows the conclusive number, instead of it counting after each value of sin?

#include < iostream >
#include < conio.h >
#include < math.h >
using namespace std;
int main ()
{
    int n ;
    float f ;
    cout << "Kvadrat matriciin irembiig oruulnuu:" << endl ;  //Энэ матриц квадрат байна. Яагаад гэвэл i,j = 1,....n.
    cin >> n;
    float A [n] [n] ;
    for ( int i = 1 ; i < n + 1 ; i++)
    {
        for ( int j = 1 ; j < n + 1 ; j++)
        {
            f = j ;
            f  / = 2 ;
            A [i] [j] = float ( sin ( i + f ) ) ;
            cout << "[ " << i << "]" << "[" << j << "]" << A [i] [j] <<endl ;

        if ( A [i] [j] > 0 )
        {
            int count = 0 ;
            count + = count + i ;
        }
        }
    }



    return 0;
}

EDIT: because when i write the cout<< withing the loop it counts it after calculating each sin value, but outside the loop it shows an error

EDIT2:

#include < iostream >
#include < conio.h >
#include < math.h >
using namespace std;
int main ()
{
    int n ;
    float f ;
    cout << "Kvadrat matriciin irembiig oruulnuu:" << endl ;  //Энэ матриц квадрат байна. Яагаад гэвэл i,j = 1,....n.
    cin >> n;
    float A [n] [n] ;
    for ( int i = 1 ; i < n + 1 ; i++)
    {
        for ( int j = 1 ; j < n + 1 ; j++)
        {
            f = j ;
            f  / = 2 ;
            A [i] [j] = float ( sin ( i + f ) ) ;
            cout << "[ " << i << "]" << "[" << j << "]" << A [i] [j] <<endl ;

        if ( A [i] [j] > 0 )
        {
            int count = 0 ;
            count + = count + i ;
            cout << count << endl ;  //this counts it after each value of sin, meaning it doesn't show one answer but multiple of them 

        }
         cout<<count<<endl;  //when i do this, it says that count wasn't declared
        }
    }



    return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
B U
  • 1
  • 1
  • "but outside the loop it shows an error" what error? putting the printing out of the loop is the obvious solution, if that didnt work you better show the code that does not work together with the error you get – 463035818_is_not_an_ai Nov 27 '18 at 14:48
  • 3
    ` cin >> n; float A [n] [n] ; ` This is not valid C++ code. – Matthieu Brucher Nov 27 '18 at 14:49
  • 3
    1) VLAs (variable length arrays) are not standard C++. 2) You are indexing the arrays out of bounds, since valid indexes of arrays, in c++, are: `[0; n-1]`. 3) If you are learning C++, consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), instead of coding randomly. – Algirdas Preidžius Nov 27 '18 at 14:49
  • edited as per requested. i'm not sure why it has to always start with 0? i mean besides the whole thing with cout<< it is running pretty okay, though? – B U Nov 27 '18 at 14:55
  • 1
    @BU 1) "_i'm not sure why it has to always start with 0_" Because that's how language is defined. 2) "_ i mean besides the whole thing with cout<< it is running pretty okay, though_" Undefined behavior is undefined. Your code might seem to run fine, only to break, when it's full moon. You can't rely on one specific behavior of undefined behavior. 3) "_when i do this, it says that count wasn't declared_" Because it isn't. It is declared inside the scope of `if ( A [i] [j] > 0 )`, and is destroyed at the end of the scope. – Algirdas Preidžius Nov 27 '18 at 15:10
  • err, then where should i place if i wish to obtain what i need? thanks for the answers tho. EDIT: oooh, i see what you mean now, yes. thank you very much – B U Nov 27 '18 at 15:16

2 Answers2

1

As already stated in one of the comments, if you're starting to code now, read a good beginner's book first instead of coding randomly.

I'll point out some of the errors in your code; it might be difficult to digest in the beginning but come back later on and you may find valuable information.

The first mistake is in the declaration of your A array. In C++, the size of an array must be what we call a constant expression (or constexpr as for the language type). A constant expression is something that your compiller is able to evaluate and its value will not change in runtime. Thus, since your variable n is not a constant expression -- it might change during runtime, i.e. during execution of your code--, this declaration is an error. As another user said, this would only be possible in a language where variable-size arrays are admitted; C++ is not such language. Somehow looks like your compiller is bypassing it.

Second, in C++ array indexing starts always at 0, not at 1, like Fortran for example. Thus, the range of your array is [0,n-1], not [1,n]. This means that your loop is wrong, because you are starting at 1 and ending at n. There is no problem when you start at 1 (besides that the code might not do what you intend to), but there is a problem when you try to subscript your array to index n, since it doesn't exist. Even if it is working, this is an undefined behavior, and you shouldn't rely on it. When your input changes, or when your compiller changes, or when you run it on another machine, you could get an error or a completely different output.

Third, if you want your counter to be displayed only at the end of your computation, there is no doubt you should put your cout expression outside your for loops. Since it is inside, it will indeed print every count of your loop, because it will be executed in every iteration of your for. Also, count variable is defined inside the if block statement, so it is only visible inside that block. This means that if you try to access the variable count outside the if, you won't be able to and will receive a compiller error -- we say that count is out of scope. So declare your counter outside your for loops, where your last cout will be able to "see" it, since they will be both inside main() scope.

Good luck on your studies.

Eron
  • 116
  • 4
0

you can not use this line in c++:

cin >> n;
float A[n][n];

if you want to do so, you have allocate memory this way:

cin >> n;
float** A = new float*[n];
for(int i = 0; i < n; ++i)
a[i] = new float[n];

or just use constant value this way:

A[5][5]; // or any other number that youre interested of

and in the beggining of your code add this line:

int count = 0; // or any other value youre interested of

the final code looks like this:

int main()
{
int n;
int f;
int count = 0;
cout << "Kvadrat matriciin irembiig oruulnuu:" << endl;  //Энэ матриц квадрат байна. Яагаад гэвэл i,j = 1,....n.
cin >> n;

float** A = new float*[n];
for (int i = 0; i < n; ++i)
    A[i] = new float[n];

    for (int i = 0; i < n; i++)
{
    for (int j = 0; j < n; j++)
    {
        f = j;
        f /= 2;
        A[i][j] = float(sin(i + f));
        cout << "[ " << i << "]" << "[" << j << "]" << A[i][j] << endl;

        if (A[i][j] > 0)
        {
            int count = 0;
            count += count + i;
            cout << count << endl;  //this counts it after each value of sin, meaning it doesn't show one answer but multiple of them

        }
        cout << count << endl;  //when i do this, it says that count wasn't declared
    }
}
for (int i = 0; i < n; ++i)
    delete A[i];
delete A;
return 0;
}

EDIT: I fixed your for loop because of writing access violation. good luck!

Benny
  • 488
  • 5
  • 18