First row in the array repeats itself when the dimensions is input from keyboard [duplicate]
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;
c++ arrays initialization rows repeat
marked as duplicate by Henri Menke, S.M., Baum mit Augen
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.
add a comment |
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;
c++ arrays initialization rows repeat
marked as duplicate by Henri Menke, S.M., Baum mit Augen
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 ifn
were initializedA[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 -- Usestd::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
add a comment |
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;
c++ arrays initialization rows repeat
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
c++ arrays initialization rows repeat
asked Nov 13 '18 at 0:58
Charles BoileveCharles Boileve
1
1
marked as duplicate by Henri Menke, S.M., Baum mit Augen
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
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 ifn
were initializedA[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 -- Usestd::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
add a comment |
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 ifn
were initializedA[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 -- Usestd::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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 13 '18 at 1:13
Russ Paul-JonesRuss Paul-Jones
11
11
add a comment |
add a comment |
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 ifn
were initializedA[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