logic or syntax error? C++ [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
int rVals[];
string rNum;
for (i=0; i < rNum.length(); ++i) {
if((rVals[i] < rVals[i+1]) && (rNum[i] =='C' || rNum[i]=='X' || rNum[i]=='I')){
continue; //checks to see if preceeding value is < the next value
} else {
valid = false;
cout << "you can't subtract by M, D, L, or V\n" << endl;
break;
}
}
rVals[] is a dynamic array and is set correctly. No matter what the input is the if statement seems to evaluate to false. what is wrong with the if statement?

Take a look at this: rVals[i] < rVals[i+1]. If rVals length is 10 for instance and i is 9 rVals[i+1] will "point" to the 11th element of the array (since the indexing of an array is starting from 0 and between 0 and 9 you heave 10 elements - the size of our array).

Related

Number of distinct elements in array using bits in int [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have this code
what is wrong in my code
it must display how many distinct elements are there
string name;
long long maps;
int i,j,test,l,counts=0,k;
cin>>test;
for(i=0;i<test;i++){
counts=maps=0;
cin>>name;
l = name.length();
for(j=0;j<l;j++){
k=(toupper(name[j])-'A');
cout<<endl<<(maps&(1<<k))<<" "<<k;
if(0 == maps&(1<<k)){
counts++;
maps|=(1<<k);
}else{
cout<<" "<<(int)(maps&(1<<k))==0;
}
}
cout<<counts;
}
what is wrong check witht the output i cant find why i is not working
The problem is here:
if(0 == maps&(1<<k)){
The equality operator == has higher precedence than the bitwise-AND operator &. So the above evaluates to this:
if ((0 == maps) & (1 << k )) {
You need to add parenthesis to get the desired behavior:
if (0 == (maps & (1 << k))) {
You'll need to do the same in your cout call:
cout<<" "<<(int)((maps&(1<<k))==0);

Infinite Loop when comparing a number in 2d array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Trying to write a function that sees if the number in the array is increasing or decreasing compared to the previous.
Getting an infinite loop.
for(int col=0; col < 5; col++) {
newArray[col][0] = printthis[col][0];
for(int row = 2; row < 5; row++) {
cout << col << "\t" << row << "\n";
if(stoi(printthis[col][row]) > stoi(printthis[col][--row])) {
newArray[col][row] = "Up";
}
else {
newArray[col][row] = "Down";
} //if else
}//inner loop
}
Here the loop index is decreased, so it will always stay at value 2, note the --row:
if(stoi(printthis[col][row]) > stoi(printthis[col][--row])){
You probably want:
if(stoi(printthis[col][row]) > stoi(printthis[col][row-1])){
Also the loop should probably start at row = 1 instead of 2, to compare to the first row instead of second.

How to make sense of for(int i = 5; i<=2*n; i++) in algorithm analysis? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
Characterize the following algorithm in terms of Big-O notation. (Assume that all variables are properly declared.)
for (int i = 5; i <= 2 * n; i++)
cout << 2 * n + i - 1 << endl;
This question comes from the exercises in Data Structures using C++ (D.S Malik). I can't seem to make sense of it. I think I'm thrown off mostly by the conditional part of the for loop, i<=2*n.
See https://www.interviewcake.com/article/java/big-o-notation-time-and-space-complexity (drop the constants section)
So big O of 2n-5 is the same as big O of n.

for loops instead of if else statement [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
program:
what else can I use in this program instead of using multiple if else statement
my desire number is until a thousand.
do {
System.out.print("Enter number: ");
a=Integer.parseInt(br.readLine());
if(a==1)
System.out.println(o[0]);
if(a==2)
System.out.println(o[1]);
if(a==3)
System.out.println(o[2]);
if(a==4)
System.out.println(o[3]);
if(a==5)
System.out.println(o[4]);
if(a==6)
System.out.println(o[5]);
}
while(a!=0);
if (a==0){
System.out.println("You entered number zero");
}
do {
System.out.print("Enter number: ");
a = Integer.parseInt(br.readLine());
if (a != 0 && a < o.length) {
System.out.println(o[a-1]);
}
} while(a != 0);
if (a == 0) {
System.out.println("You entered number zero");
}
Start by analyzing the pattern: given if(a==X) System.out.println(o[Y]);, what is the relation of X (and therefor a) to Y?
Now eliminate all the if-else statements to take this into account - the program should be left with a single System.out.println(o[..]); line (where the expression .. transforms a by the pattern identified above).
Then, what is the domain of X (and a by extension)? That is, for what values of X should this println operation occur?
Add an if statement around the above println; this will prevent output when the user enters 0 (or 7), for instance.

Convert String Contents to Integers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I'm trying to convert the individual contents of a string to integers. I need to take each character from the string and convert it to an integer to add to another. This is not using C++11. Is there a simple way to do it?
if the characters are numbers then the numeral value of each is
num_value(c) = c - '0'
This is only possible because the characters representing numbers are in order in the ASCII table.. All you have to do is loop across the string.
"I need to take each character from the string and convert it to an integer to add to another"
In case you want to calculate the sum of digits stored in std::string object, you could do:
std::string myNum("567632");
int sum = 0;
for (size_t i = 0; i < myNum.size(); ++i)
sum += (myNum[i] - '0');
std::cout << sum;
which outputs 29 (i.e. 5 + 6 + 7 + 6 + 3 + 2)
How about std::accumulate ?
#include<string>
#include<algorithm>
//...
std::string myNum("123456789");
std::cout<<accumulate( myNum.begin(), myNum.end(), 0,
[](int sum,const char& x){return sum+=x-'0'; });