std::cin issues? What is going on here?? std in vs hard coded value
up vote
0
down vote
favorite
Afternoon all, So i am working on the rod cutting problem. Anyone know why this code works when n is hard coded to 7 or some other number in main, but when I grab n via std:cin it does not work and gives all the wrong output... What is going on here?
Here is output from Setting in main int n = 7
18
1 6 -1
While using std in to get n = 7 i get this:
35
1 1 1 1 1 1 1 -1
Price list is the same in both instances....
1
5
8
9
10
17
17
#include <iostream>
#include <cstring>
using namespace std;
const int A = 1000;
int p[A];
int r[A], s[A];
void init() {
p[0] = 0;
p[1] = 1;
p[2] = 5;
p[3] = 8;
p[4] = 9;
p[5] = 10;
p[6] = 17;
p[7] = 17;
p[8] = 20;
p[9] = 24;
p[10] = 30;
}
int extendedButtomUpCutRod(int n) {
for (int j = 1; j <= n; ++j) {
int q = -2145631872;
for (int i = 1; i <= j; ++i)
if (q < p[i] + r[j - i]) {
q = p[i] + r[j - i];
s[j] = i;
}
r[j] = q;
}
return r[n];
}
// prins the extended method's output
void printCutRodSoln(int n) {
do
cout << s[n] << " ";
while ((n -= s[n]) > 0);
}
int main(){
init();
r[0] = 0;
//int n = 7; //works when this is used.
int n;
std::cin >> n; //doesnt work when this is used?
for(int i = 0; i <= n; i++){
std::cin >> p[i];
}
cout << extendedButtomUpCutRod(n) << endl;
printCutRodSoln(n);
std::cout << "-1"<< std::endl;
}
c++ stdin
|
show 1 more comment
up vote
0
down vote
favorite
Afternoon all, So i am working on the rod cutting problem. Anyone know why this code works when n is hard coded to 7 or some other number in main, but when I grab n via std:cin it does not work and gives all the wrong output... What is going on here?
Here is output from Setting in main int n = 7
18
1 6 -1
While using std in to get n = 7 i get this:
35
1 1 1 1 1 1 1 -1
Price list is the same in both instances....
1
5
8
9
10
17
17
#include <iostream>
#include <cstring>
using namespace std;
const int A = 1000;
int p[A];
int r[A], s[A];
void init() {
p[0] = 0;
p[1] = 1;
p[2] = 5;
p[3] = 8;
p[4] = 9;
p[5] = 10;
p[6] = 17;
p[7] = 17;
p[8] = 20;
p[9] = 24;
p[10] = 30;
}
int extendedButtomUpCutRod(int n) {
for (int j = 1; j <= n; ++j) {
int q = -2145631872;
for (int i = 1; i <= j; ++i)
if (q < p[i] + r[j - i]) {
q = p[i] + r[j - i];
s[j] = i;
}
r[j] = q;
}
return r[n];
}
// prins the extended method's output
void printCutRodSoln(int n) {
do
cout << s[n] << " ";
while ((n -= s[n]) > 0);
}
int main(){
init();
r[0] = 0;
//int n = 7; //works when this is used.
int n;
std::cin >> n; //doesnt work when this is used?
for(int i = 0; i <= n; i++){
std::cin >> p[i];
}
cout << extendedButtomUpCutRod(n) << endl;
printCutRodSoln(n);
std::cout << "-1"<< std::endl;
}
c++ stdin
Seems to work fine testing it myself, what about the output is unexpected?
– Mitchel Paulin
Nov 10 at 22:16
What is the rod cutting problem? Have you tried using a debugger?
– Kevin
Nov 10 at 22:16
When I run your program as written and use the inputs you supplied, it hangs waiting for an additional input. This is because of the<=
which should just be<
in thefor
loop inmain()
.
– user1118321
Nov 10 at 22:18
1
Your program expects 8 numbers but you are giving it only 7.
– n.m.
Nov 10 at 22:20
Changed a few things... that were wrong. it only runs fine when n is hard coded. when i remove n= 7 us the comented line below it fails.,
– Johnjames123
Nov 10 at 22:20
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Afternoon all, So i am working on the rod cutting problem. Anyone know why this code works when n is hard coded to 7 or some other number in main, but when I grab n via std:cin it does not work and gives all the wrong output... What is going on here?
Here is output from Setting in main int n = 7
18
1 6 -1
While using std in to get n = 7 i get this:
35
1 1 1 1 1 1 1 -1
Price list is the same in both instances....
1
5
8
9
10
17
17
#include <iostream>
#include <cstring>
using namespace std;
const int A = 1000;
int p[A];
int r[A], s[A];
void init() {
p[0] = 0;
p[1] = 1;
p[2] = 5;
p[3] = 8;
p[4] = 9;
p[5] = 10;
p[6] = 17;
p[7] = 17;
p[8] = 20;
p[9] = 24;
p[10] = 30;
}
int extendedButtomUpCutRod(int n) {
for (int j = 1; j <= n; ++j) {
int q = -2145631872;
for (int i = 1; i <= j; ++i)
if (q < p[i] + r[j - i]) {
q = p[i] + r[j - i];
s[j] = i;
}
r[j] = q;
}
return r[n];
}
// prins the extended method's output
void printCutRodSoln(int n) {
do
cout << s[n] << " ";
while ((n -= s[n]) > 0);
}
int main(){
init();
r[0] = 0;
//int n = 7; //works when this is used.
int n;
std::cin >> n; //doesnt work when this is used?
for(int i = 0; i <= n; i++){
std::cin >> p[i];
}
cout << extendedButtomUpCutRod(n) << endl;
printCutRodSoln(n);
std::cout << "-1"<< std::endl;
}
c++ stdin
Afternoon all, So i am working on the rod cutting problem. Anyone know why this code works when n is hard coded to 7 or some other number in main, but when I grab n via std:cin it does not work and gives all the wrong output... What is going on here?
Here is output from Setting in main int n = 7
18
1 6 -1
While using std in to get n = 7 i get this:
35
1 1 1 1 1 1 1 -1
Price list is the same in both instances....
1
5
8
9
10
17
17
#include <iostream>
#include <cstring>
using namespace std;
const int A = 1000;
int p[A];
int r[A], s[A];
void init() {
p[0] = 0;
p[1] = 1;
p[2] = 5;
p[3] = 8;
p[4] = 9;
p[5] = 10;
p[6] = 17;
p[7] = 17;
p[8] = 20;
p[9] = 24;
p[10] = 30;
}
int extendedButtomUpCutRod(int n) {
for (int j = 1; j <= n; ++j) {
int q = -2145631872;
for (int i = 1; i <= j; ++i)
if (q < p[i] + r[j - i]) {
q = p[i] + r[j - i];
s[j] = i;
}
r[j] = q;
}
return r[n];
}
// prins the extended method's output
void printCutRodSoln(int n) {
do
cout << s[n] << " ";
while ((n -= s[n]) > 0);
}
int main(){
init();
r[0] = 0;
//int n = 7; //works when this is used.
int n;
std::cin >> n; //doesnt work when this is used?
for(int i = 0; i <= n; i++){
std::cin >> p[i];
}
cout << extendedButtomUpCutRod(n) << endl;
printCutRodSoln(n);
std::cout << "-1"<< std::endl;
}
c++ stdin
c++ stdin
edited Nov 10 at 22:23
asked Nov 10 at 22:06
Johnjames123
163
163
Seems to work fine testing it myself, what about the output is unexpected?
– Mitchel Paulin
Nov 10 at 22:16
What is the rod cutting problem? Have you tried using a debugger?
– Kevin
Nov 10 at 22:16
When I run your program as written and use the inputs you supplied, it hangs waiting for an additional input. This is because of the<=
which should just be<
in thefor
loop inmain()
.
– user1118321
Nov 10 at 22:18
1
Your program expects 8 numbers but you are giving it only 7.
– n.m.
Nov 10 at 22:20
Changed a few things... that were wrong. it only runs fine when n is hard coded. when i remove n= 7 us the comented line below it fails.,
– Johnjames123
Nov 10 at 22:20
|
show 1 more comment
Seems to work fine testing it myself, what about the output is unexpected?
– Mitchel Paulin
Nov 10 at 22:16
What is the rod cutting problem? Have you tried using a debugger?
– Kevin
Nov 10 at 22:16
When I run your program as written and use the inputs you supplied, it hangs waiting for an additional input. This is because of the<=
which should just be<
in thefor
loop inmain()
.
– user1118321
Nov 10 at 22:18
1
Your program expects 8 numbers but you are giving it only 7.
– n.m.
Nov 10 at 22:20
Changed a few things... that were wrong. it only runs fine when n is hard coded. when i remove n= 7 us the comented line below it fails.,
– Johnjames123
Nov 10 at 22:20
Seems to work fine testing it myself, what about the output is unexpected?
– Mitchel Paulin
Nov 10 at 22:16
Seems to work fine testing it myself, what about the output is unexpected?
– Mitchel Paulin
Nov 10 at 22:16
What is the rod cutting problem? Have you tried using a debugger?
– Kevin
Nov 10 at 22:16
What is the rod cutting problem? Have you tried using a debugger?
– Kevin
Nov 10 at 22:16
When I run your program as written and use the inputs you supplied, it hangs waiting for an additional input. This is because of the
<=
which should just be <
in the for
loop in main()
.– user1118321
Nov 10 at 22:18
When I run your program as written and use the inputs you supplied, it hangs waiting for an additional input. This is because of the
<=
which should just be <
in the for
loop in main()
.– user1118321
Nov 10 at 22:18
1
1
Your program expects 8 numbers but you are giving it only 7.
– n.m.
Nov 10 at 22:20
Your program expects 8 numbers but you are giving it only 7.
– n.m.
Nov 10 at 22:20
Changed a few things... that were wrong. it only runs fine when n is hard coded. when i remove n= 7 us the comented line below it fails.,
– Johnjames123
Nov 10 at 22:20
Changed a few things... that were wrong. it only runs fine when n is hard coded. when i remove n= 7 us the comented line below it fails.,
– Johnjames123
Nov 10 at 22:20
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
Comments helped but here was the exact line. that needed changing...
for(int i = 1; i <= n; i++),
i = 1 fixed it in all test cases.
Price list was off .
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Comments helped but here was the exact line. that needed changing...
for(int i = 1; i <= n; i++),
i = 1 fixed it in all test cases.
Price list was off .
add a comment |
up vote
0
down vote
Comments helped but here was the exact line. that needed changing...
for(int i = 1; i <= n; i++),
i = 1 fixed it in all test cases.
Price list was off .
add a comment |
up vote
0
down vote
up vote
0
down vote
Comments helped but here was the exact line. that needed changing...
for(int i = 1; i <= n; i++),
i = 1 fixed it in all test cases.
Price list was off .
Comments helped but here was the exact line. that needed changing...
for(int i = 1; i <= n; i++),
i = 1 fixed it in all test cases.
Price list was off .
answered Nov 10 at 22:32
Johnjames123
163
163
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243891%2fstdcin-issues-what-is-going-on-here-std-in-vs-hard-coded-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Seems to work fine testing it myself, what about the output is unexpected?
– Mitchel Paulin
Nov 10 at 22:16
What is the rod cutting problem? Have you tried using a debugger?
– Kevin
Nov 10 at 22:16
When I run your program as written and use the inputs you supplied, it hangs waiting for an additional input. This is because of the
<=
which should just be<
in thefor
loop inmain()
.– user1118321
Nov 10 at 22:18
1
Your program expects 8 numbers but you are giving it only 7.
– n.m.
Nov 10 at 22:20
Changed a few things... that were wrong. it only runs fine when n is hard coded. when i remove n= 7 us the comented line below it fails.,
– Johnjames123
Nov 10 at 22:20