Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So I wrote this code in C to print the ASCII table, but I was told to use SML for this specific task
Here is my current code in C:
// Program to print ASCII table.
#include <stdio.h>
int main()
{
unsigned char count;
for(count=32; count< 255; count+=1)
{
printf(" %3d - %c",count,count);
if(count % 6==0)
printf("\n");
}
return 0;
}
How would I go about doing this in SML? I scoured the internet but with no luck!
How would I go about doing this in SML?
First off, you would learn SML. ;-)
Instead of for(count=32; count< 255; count+=1) you might use List.tabulate.
In SML printf is considered a big gun, so perhaps stick to print.
Instead of %c you can use str and chr, e.g. str (chr 65) = "A"
Instead of %03d you do get Int.toString, but you have to make your own pad function, yay!
For applying a function f to every element of a list and discard the return values (which makes sense if you're interested in only the side-effects of f), you can use List.app rather than List.map.
A template for getting started is:
val table = List.tabulate (256 - 32, fn count => str (chr (count + 32)))
val _ = List.app (fn cstr => print "Ceci n'est pas une ASCII table\n") table
Try and make a reasonable attempt to solve this task and provide an answer to your own StackOverflow question.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
Given a string we have to output the string in a special way.
• If the string consists of one char, we output that char normally.
• Otherwise, we divide the string into two equal parts (if the number of letters in the substr is odd, the second part of the substr will be one letter longer than the first), and we output the first part twice and then the second part (according to the same rules).
For example, let's assume that we want to output the string YOGURT. We divide that string into two equal parts: YOG and URT.
How will we output the substr YOG? Again, it will be divided into two parts - Y and OG. The substr Y we output normally (but in the output of the substr YOG we will do it twice), and the substr OG we output as OOG. So the substr YOG we output as YYOOG.
Analogously, the substr URT is going to give the output UURRT. So the string YOGURT is going to be output as YYOOGYYOOGUURRT.
Length of the string can at max be 10000.
Now I tried using a non recursion way to solve this problem but it was way to slow so I have come to an conclusion I have to do this with recursion. And since I don't have that much experience with recursion I would really need some help.
This is very naturally implemented with recursion like so:
void print(std::string_view s) {
if (s.size() <= 1) std::cout << s;
else {
auto m = s.size() / 2;
print(s.substr(0, m));
print(s.substr(0, m));
print(s.substr(m));
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
suppose I've this code:
string str[] = {"devil", "chandra"};
// check if str[0] has properly devil, character by character or not without extra variable
Now I want to check str[ 0 ]'s all character which is 'd','e','v','i','l' one by one without extra variable.
with extra variable code will be :
string n1 = "devil";
for(int i=0; i<1; i++){
string s1 = str[i]
for(int j=0; j<s1.size(); j++){
if(s1[i] == n[i]){
cout << s1[i] << " ";
}
}
Basically, I want O(n) loop where I can access all indexes string and among them all characters.
Like s[ i ] is "devil" and s[[i]] = 'd' something like this, Know it's not valid, but is there any way to do that??
Even I don't know is it a valid question or not!
I'm not sure why you would need an extra variable. If you need a conditional that checks that the first value in the array of strings is "devil", it shouldn't be anymore complicated than:
if (str[0] == "devil")
{
* Do things *
}
C++ can check a standard string all at once. You don't need to check each individual character if that's what you're thinking.
Keep in mind, this isn't going to account for situations where the string is not exactly the same. For instance, if str[0] has "Devil" instead of "devil", then the conditional will evaluate to false.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to know about what is the use of that (index & 0x01) in the code?
if(((arr[index] >= 0) && (!(index & 0x01)))
|| ((arr[index] < 0) && (index & 0x01)))
{
outofplace = index;
}
A number is odd if and only if its last digit is odd, regardless of the base. So if we want to know the number's oddity, it's enough to check if the last bit is set.
index & 0x01
will be 1 if and only if index is odd.
If we have to deduce a general rule, we can say that for any non-negative number x,
x % y == (x & (y - 1))
provided that y is a positive power of 2.
This is a common hack in competitive coding. It is used because the competitive programmers think that bit-wise AND works faster than modulo.
In modern compilers, there is no performance difference at all. Read this thread.
There is no special reason in writing it as 0x01 instead of 1. Both compile to give the same assembly! Almost everyone (who uses this hack),= uses 1, because we have to type 3 characters extra in 0x01. :P
Here in this case, index & 0x1 is equivalent to index % 2 which is simply a condition to check if the number is odd. (Array indexes in C++ are always positive, unless you are going out of bound.)
As the other answers pointed out, while this is a well known pattern (see also this Q&A about that mask), it can be considered a premature optimization.
I'd like to suggest the following alternative to the posted code, which I find more readable. Your mileage may vary.
// Give a meaningful name.
constexpr auto is_odd = [] (auto x) -> bool {
return x % 2;
}
// Use it to simplify the condition.
if ( (arr[index] < 0) == is_odd(index) ) {
// Do something
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
int main()
{
int i;
float number[6];
for (i=0; i<=2; i++) {
printf("write number %i\n", i+1);
scanf("%i",&number[i]);
}
printf ("the first number you wrote: %i", number[1]);
return 0;
}
I am having trouble understanding how to print out the first number in an indexed variable. The answer is always 0, if i type number[1] or number[5] the output is always 0
I dont know what to write when im printing out the first number, the is answer always 0.
but from this statement it's clear that you are not printing the first number entered
printf ("the first number you wrote: %i", number[1]);
as number[1] refers to the second number you entered, try using number[0] instead of number[1]
printf ("the first number you wrote: %i", number[0]);
apart from the above one, there is one more problem.you are using the wrong format specifier for float in your scanf() and printf statements
use %f when scanning or printing float's, %i is for int's
generally using wrong format specifiers results in undefined behavior, have a look at this : What can happen if printf is called with a wrong format string?
additionally, try avoiding the use of printf() an scanf() and use std::cout and std::cin as others have pointed out.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hi I used to solve string char uniqueness using map in C++. I found this solution somewhere and working fine. But I can not understand how it is working. Please some one explain.
bool isUnique(string s){
int check = 0;
for(int i=0;i<s.length();++i){
if(s[i] != ' '){
int val = s[i]-'a';
if( (check & ( 1 << val)) > 0) return false;
check = check | (1 << val);
}
}
return true;
}
It returns true if string has no repeated character excluding spaces otherwise returns false.
It is using an int as if it were a bitmap. A bitmap is certainly a better data structure for a character uniqueness test than a map. An int is a crude and questionable (in this case) substitute for a bitmap.
Assume an int has 32 bits. Those bits are allocated in this code for the first 32 characters beginning with lower case 'a'. So the upper case letters and most special characters have no bit positions and are treated as unique by this code even if they are not unique.
If you only care about uniqueness for lower case letters, and you are sure the code is only used in architectures that have at least 32 bits in an int, then this is a decent approach. Otherwise, when you want an array of bits, use some actual array of bits.