First row in the array repeats itself when the dimensions is input from keyboard [duplicate]










0
















This question already has an answer here:



  • Why aren't variable-length arrays part of the C++ standard?

    13 answers



I made a program which allows the user to enter the dimension of an array and the array values itself. The code runs fine however whenever the first row of the array seems to repeat itself.



E.g For the A[n][n] Array when n = 2, and I enter values for array such as 1,6,4 and 3, the code outputs an array of [1,6][1,6].



Might be easier to understand if you ran the code yourself:



#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

void v_in();
// void v_out;

void v_in()

int i,j,n;
int A[n][n];


cout << "Enter the dimension of your matrix. Enter a value between 1-20.n";
cin >> n;
if(n < 1)

cout << "nValue enter is out of range. The value of n is now 1.n";
n = 1;

else if(n > 20)

cout << "nValue enter is out of range. The value of n is now 20.n";
n = 20;


cout << "Enter values the array A[" << n << "][" << n << "].n";
for(i=0;i<n;i++)

for(j=0;j<n;j++)

cin >> A[i][j];



cout << "nnA = n";
for(i=0;i<n;i++)

for(j=0;j<n;j++)

cout << A[i][j] << "t";

cout << "n";




int main()
answer == "YES")

v_in();


cout << "nEnd of program.n";

else

cout << "nEND.n";

return 0;










share|improve this question













marked as duplicate by Henri Menke, S.M., Baum mit Augen c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 18 '18 at 19:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • int i,j,n; int A[n][n]; -- Sigh. Another new programmer gets taken in by this piece of non-C++ code. First, n isn't initialized to a value. Second, even if n were initialized A[n][n] is not legal C++. Blame g++ or whatever your compiler you're using that allows this syntax. Arrays in C++ must have their sizes denoted by a constant expression, not a variable.

    – PaulMcKenzie
    Nov 13 '18 at 1:05












  • I made a program which allows the user to enter the dimension of an array and the array values itself -- Use std::vector<std::vector<int>> A(n, std::vector<int>(n)); That is how you declare dynamic arrays in C++.

    – PaulMcKenzie
    Nov 13 '18 at 1:08















0
















This question already has an answer here:



  • Why aren't variable-length arrays part of the C++ standard?

    13 answers



I made a program which allows the user to enter the dimension of an array and the array values itself. The code runs fine however whenever the first row of the array seems to repeat itself.



E.g For the A[n][n] Array when n = 2, and I enter values for array such as 1,6,4 and 3, the code outputs an array of [1,6][1,6].



Might be easier to understand if you ran the code yourself:



#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

void v_in();
// void v_out;

void v_in()

int i,j,n;
int A[n][n];


cout << "Enter the dimension of your matrix. Enter a value between 1-20.n";
cin >> n;
if(n < 1)

cout << "nValue enter is out of range. The value of n is now 1.n";
n = 1;

else if(n > 20)

cout << "nValue enter is out of range. The value of n is now 20.n";
n = 20;


cout << "Enter values the array A[" << n << "][" << n << "].n";
for(i=0;i<n;i++)

for(j=0;j<n;j++)

cin >> A[i][j];



cout << "nnA = n";
for(i=0;i<n;i++)

for(j=0;j<n;j++)

cout << A[i][j] << "t";

cout << "n";




int main()
answer == "YES")

v_in();


cout << "nEnd of program.n";

else

cout << "nEND.n";

return 0;










share|improve this question













marked as duplicate by Henri Menke, S.M., Baum mit Augen c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 18 '18 at 19:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • int i,j,n; int A[n][n]; -- Sigh. Another new programmer gets taken in by this piece of non-C++ code. First, n isn't initialized to a value. Second, even if n were initialized A[n][n] is not legal C++. Blame g++ or whatever your compiler you're using that allows this syntax. Arrays in C++ must have their sizes denoted by a constant expression, not a variable.

    – PaulMcKenzie
    Nov 13 '18 at 1:05












  • I made a program which allows the user to enter the dimension of an array and the array values itself -- Use std::vector<std::vector<int>> A(n, std::vector<int>(n)); That is how you declare dynamic arrays in C++.

    – PaulMcKenzie
    Nov 13 '18 at 1:08













0












0








0









This question already has an answer here:



  • Why aren't variable-length arrays part of the C++ standard?

    13 answers



I made a program which allows the user to enter the dimension of an array and the array values itself. The code runs fine however whenever the first row of the array seems to repeat itself.



E.g For the A[n][n] Array when n = 2, and I enter values for array such as 1,6,4 and 3, the code outputs an array of [1,6][1,6].



Might be easier to understand if you ran the code yourself:



#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

void v_in();
// void v_out;

void v_in()

int i,j,n;
int A[n][n];


cout << "Enter the dimension of your matrix. Enter a value between 1-20.n";
cin >> n;
if(n < 1)

cout << "nValue enter is out of range. The value of n is now 1.n";
n = 1;

else if(n > 20)

cout << "nValue enter is out of range. The value of n is now 20.n";
n = 20;


cout << "Enter values the array A[" << n << "][" << n << "].n";
for(i=0;i<n;i++)

for(j=0;j<n;j++)

cin >> A[i][j];



cout << "nnA = n";
for(i=0;i<n;i++)

for(j=0;j<n;j++)

cout << A[i][j] << "t";

cout << "n";




int main()
answer == "YES")

v_in();


cout << "nEnd of program.n";

else

cout << "nEND.n";

return 0;










share|improve this question















This question already has an answer here:



  • Why aren't variable-length arrays part of the C++ standard?

    13 answers



I made a program which allows the user to enter the dimension of an array and the array values itself. The code runs fine however whenever the first row of the array seems to repeat itself.



E.g For the A[n][n] Array when n = 2, and I enter values for array such as 1,6,4 and 3, the code outputs an array of [1,6][1,6].



Might be easier to understand if you ran the code yourself:



#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

void v_in();
// void v_out;

void v_in()

int i,j,n;
int A[n][n];


cout << "Enter the dimension of your matrix. Enter a value between 1-20.n";
cin >> n;
if(n < 1)

cout << "nValue enter is out of range. The value of n is now 1.n";
n = 1;

else if(n > 20)

cout << "nValue enter is out of range. The value of n is now 20.n";
n = 20;


cout << "Enter values the array A[" << n << "][" << n << "].n";
for(i=0;i<n;i++)

for(j=0;j<n;j++)

cin >> A[i][j];



cout << "nnA = n";
for(i=0;i<n;i++)

for(j=0;j<n;j++)

cout << A[i][j] << "t";

cout << "n";




int main()
answer == "YES")

v_in();


cout << "nEnd of program.n";

else

cout << "nEND.n";

return 0;





This question already has an answer here:



  • Why aren't variable-length arrays part of the C++ standard?

    13 answers







c++ arrays initialization rows repeat






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 0:58









Charles BoileveCharles Boileve

1




1




marked as duplicate by Henri Menke, S.M., Baum mit Augen c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 18 '18 at 19:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Henri Menke, S.M., Baum mit Augen c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 18 '18 at 19:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • int i,j,n; int A[n][n]; -- Sigh. Another new programmer gets taken in by this piece of non-C++ code. First, n isn't initialized to a value. Second, even if n were initialized A[n][n] is not legal C++. Blame g++ or whatever your compiler you're using that allows this syntax. Arrays in C++ must have their sizes denoted by a constant expression, not a variable.

    – PaulMcKenzie
    Nov 13 '18 at 1:05












  • I made a program which allows the user to enter the dimension of an array and the array values itself -- Use std::vector<std::vector<int>> A(n, std::vector<int>(n)); That is how you declare dynamic arrays in C++.

    – PaulMcKenzie
    Nov 13 '18 at 1:08

















  • int i,j,n; int A[n][n]; -- Sigh. Another new programmer gets taken in by this piece of non-C++ code. First, n isn't initialized to a value. Second, even if n were initialized A[n][n] is not legal C++. Blame g++ or whatever your compiler you're using that allows this syntax. Arrays in C++ must have their sizes denoted by a constant expression, not a variable.

    – PaulMcKenzie
    Nov 13 '18 at 1:05












  • I made a program which allows the user to enter the dimension of an array and the array values itself -- Use std::vector<std::vector<int>> A(n, std::vector<int>(n)); That is how you declare dynamic arrays in C++.

    – PaulMcKenzie
    Nov 13 '18 at 1:08
















int i,j,n; int A[n][n]; -- Sigh. Another new programmer gets taken in by this piece of non-C++ code. First, n isn't initialized to a value. Second, even if n were initialized A[n][n] is not legal C++. Blame g++ or whatever your compiler you're using that allows this syntax. Arrays in C++ must have their sizes denoted by a constant expression, not a variable.

– PaulMcKenzie
Nov 13 '18 at 1:05






int i,j,n; int A[n][n]; -- Sigh. Another new programmer gets taken in by this piece of non-C++ code. First, n isn't initialized to a value. Second, even if n were initialized A[n][n] is not legal C++. Blame g++ or whatever your compiler you're using that allows this syntax. Arrays in C++ must have their sizes denoted by a constant expression, not a variable.

– PaulMcKenzie
Nov 13 '18 at 1:05














I made a program which allows the user to enter the dimension of an array and the array values itself -- Use std::vector<std::vector<int>> A(n, std::vector<int>(n)); That is how you declare dynamic arrays in C++.

– PaulMcKenzie
Nov 13 '18 at 1:08





I made a program which allows the user to enter the dimension of an array and the array values itself -- Use std::vector<std::vector<int>> A(n, std::vector<int>(n)); That is how you declare dynamic arrays in C++.

– PaulMcKenzie
Nov 13 '18 at 1:08












1 Answer
1






active

oldest

votes


















0














Consider replacing



int A[n][n];


with



int A[20][20];


I think you'd be better off using std::vector or std::array, but you certainly can't initialize an c-style array with an uninitialized local variable.






share|improve this answer





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Consider replacing



    int A[n][n];


    with



    int A[20][20];


    I think you'd be better off using std::vector or std::array, but you certainly can't initialize an c-style array with an uninitialized local variable.






    share|improve this answer



























      0














      Consider replacing



      int A[n][n];


      with



      int A[20][20];


      I think you'd be better off using std::vector or std::array, but you certainly can't initialize an c-style array with an uninitialized local variable.






      share|improve this answer

























        0












        0








        0







        Consider replacing



        int A[n][n];


        with



        int A[20][20];


        I think you'd be better off using std::vector or std::array, but you certainly can't initialize an c-style array with an uninitialized local variable.






        share|improve this answer













        Consider replacing



        int A[n][n];


        with



        int A[20][20];


        I think you'd be better off using std::vector or std::array, but you certainly can't initialize an c-style array with an uninitialized local variable.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 1:13









        Russ Paul-JonesRuss Paul-Jones

        11




        11













            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo