How do I write a raptor flowchart that searches a string for a digit and once found returns a true statement - flowchart

I tried to the Is_Number command but either I am implementing it wrong or I am applying improperly.
Here is what I have so far.
Any ideas on how to proseed would be greatly appreciated.

I suggest you use to_ascii() function.
Example:
x="string"
//loop starts here
x=to_ascii(x[i]) //i=counter of your loop, if i=1 it converts "s" to ascii code
if: x>=48 && x<=57 //ascii numbers for 0-9 start at 48 and end at 57
return true;
//loop ends here
actual thing:
Output:

Related

loop until a certain condition is met

From this site https://www.geeksforgeeks.org/lca-for-general-or-n-ary-trees-sparse-matrix-dp-approach-onlogn-ologn/
I have a problem with this while loop part:
// runs till path 1 & path 2 mathches
int i = 0;
while (path[1][i] == path[2][i])
i++;
I want to increment i until two array elements are equal and I expected this loop to be like:
// runs till path 1 & path 2 mathches
int i = 0;
while (path[1][i] != path[2][i])
i++;
because I want to increment "i" when values are not equal but it does not seem so. Why equality is checked instead of inequality? This while loop confuses my mind. (Note: I run the whole code and it is working.)
By the line which follows (in your reference) where the last matching is returned, I see it that the error is in the comment. It should say something like "runs as long as the paths match" not "till".

Why is the length of a string off by one on the first read of a file?

I am perplexed with the way my program is performing. I am looping the following process:
1) take the name of a course from an input file
2) output the length of the name of the course
The problem is that the first value is always one less than the actual value of the string.
My first string contains 13 characters (including the colon), but nameOfClass.length() returns 12. The next string, the character count is 16 and indeed, nameOfClass.length() returns 16.
Every value after that also returns the expected value, it is only the first that returns the expected value minus 1.
Here's the (reduced) code:
std::ifstream inf("courseNames.txt");
int numberOfClasses = 10;
string nameOfClass;
for (int i = 0; i < numberOfClasses; i++) {
std::getline(inf, nameOfClass,':');
std::cout << nameOfClass.length() << "\n";
}
The file looks like this (courseNames.txt):
Pre-Calculus:
Public-Speaking:
English I:
Calculus I:
...etc. (6 more classes)
This is what I get:
12
16
10
11
Can anyone explain this behavior of the .length() function?
You have a problem, but you have the wrong conclusion. std::getline reads but doesn't output the delimiter, and so the first result is indeed 12.
It also doesn't output the delimiter for any subsequent lines, so why is there always one more? Well, look what is after that :. That's right, a new line!
Pre-Calculus:
^ a new line
So your nameOfClass variable, except for the first string, always stores an extra newline before the other characters.
The fix is easy enough, just ignore the newline after reading the string.
inf.ignore(); // ignore one character
So, not the first result was wrong, it was the only one right :)

How to print a random string with non-repetitive letters?

I am trying to write a c++ program to print a random 10 letter string from a-z (ascii code 97 to 122) with non-repetitive letters. I have written this code which sometimes runs perfectly but most of the times the while loop runs infinitely.
Where's the problem?
(EDIT: setting flag=0 at the beginning of while fixes the problem)
void randomstring()
{int i,j,flag=1;
char x, s[20]=" ";
srand(time(0));
for(i=0;i<10;i++)
{flag=1; //ensure entry into while
while(flag)
{x=rand()%26+97; //get random letter from a-z
for(j=0;j<10;j++)
{if(x==s[j]) //match with existing letters
flag=2; //if matched, try again
}
if(flag!=2)
{s[i]=x; //if doesn't match, add to string
flag=0; //exit while
}
}
}
cout<<s;
}
(Currently the loop will not terminate if a duplicate character is spotted.) But aside from this, the code is nasty for a couple of other reasons:
You're assuming ASCII encoding which is not guaranteed by the standard.
Sampling with replacement can cause problems with looping, and also can create statistical anomalies (although with a crude generator like rand() these will be no worse than the generator itself).
One solution is to write
char s[] = {'a', 'b', 'c', .../*ToDo - type out all the other letters*/, 'z'}
and shuffle this using
std::random_shuffle(std::begin(s), std::end(s));
and read out the first 10 elements of s.
Once the flag is set to 2, it is stuck. You should reset the flag to 1 inside the while loop.

How to remove a character from the string and change data if need it?

I have possible inputs 1M 2M .. 11M and 1Y (M and Y stand for months ) and I want to output "somestring1 somestring2.... and somestring12" note M and Y are removed and the last string is changed to 12
Example: input "11M" "hello" output: hello11
input "1Y" "hello" output: hello1
char * (const char * date, const char * somestr)
{
// just need to output final string no need to change the original string
cout<< finalStr<<endl;
}
The second string is getting output as a whole itself. So no change in its output.
The second string would be output as long as M or Y are encountered. As Stack Overflow discourages providing exact source codes, so I can give you some portion of it. There is a condition to be placed which is up to you to figure out.(The second answer gives that as well)
Code would be somewhat like this.
//Code for first string. Just for output.
for (auto i = 0 ; date[i] != '\0' ; ++i)
{
// A condition comes here.
cout << date[i] ;
}
And note that this is considering you just output the string. Otherwise you can create another string and add up the two or concatenate the existing ones.
is this homework? If not, here's what i'd suggest. (i ask about homework because you may have restrictions, not because we're not here to help)
1) do a find on 'M' in your string (using find), insert a '\0' at that position if one is found (btw i'm assuming you have well formatted input)
2) do a find on 'Y'. if one is found, insert a '\0' at that position. then do an atoi() or stringstream conversion on your string to convert to number. multiply by 12.
3) concatenate your string representation of part 1 or part 2 to your somestr
4) output.
This can probably be done in < 10 lines if i could be bothered.
the a.find('M') part and its checks can be conditional operator, then the conversion/concatenation in two or three lines at most.

C++: Program converting postfix to evaluation

How can I convert the char in the array into an integer?
Ignore lines 5-100 it is just my stack.
http://ideone.com/KQytD
Scroll down output #2 worked properly but output #3 did not. Some how when I pushed the value back into the stack and when I popped it it had the +'43' because of the ASCII and I cannot seem to get it into a regular integer value so I can do these operations easily.
line 116 puts input into char postfix. NOTE: input must be in postfix notation line 117 puts the single integer value into final after it has run through the function.
convertPostfixToEvaluation works as such: I scroll through each index of postfix until I read in '=' then I output the total/sum. The first if statement pushed the operands (0-9) into a stack. The second if statement if it reads in an operator then it attempts to do the operation as such in lines 134-158. After the if statements I increase the index value by 1 so it can scan the entire array.
The issue lies within the switch where I try adding,subtracting,multiply, or dividing more than 3 operands. so the 3rd one i believe is still has the value (+43 because of the ASCII).
My outputs(on the bottom of my program) show what the awkwardness is.
The cut to the chase issue. Issue converting char to int the second time around.
There are many things very likely wrong with this code.
Look up the function isdigit. This should eliminate the huge if statement.
You may want to use a string lookup instead of the other complex if statement:
const std::string my_operators = "+-/*";
if (my_operators.find(postfix[i]) != std::string::npos)
{
// Enter here if the character is a valid symbol.
}
If you "parse" character by character, you will have to build your number:
int number = 0;
// After detecting the character is a number:
number = number * 10 + (postfix[i] - '0');
The expression "postfix[i] - '0'" will return the distance between the number character and the character for zero. The C and C++ languages guarantee the following relationship:
'0' < '1' < '2' < '3' < '4' < '5' < '6' < '7' < '8' < '9'
The languages also state that those numbers are contiguous.
Suggestion: use std::string instead of an array of characters. The std::string contains some helpful functions for searching, skipping characters, and obtaining a substring.