[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Anyone here know C?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 50
Thread images: 3

File: cprog.png (20KB, 300x300px) Image search: [Google]
cprog.png
20KB, 300x300px
I need help creating a function that randomly produces a letter from the set (a,b,c)

I was thinking the rand function but I don't know how to apply it to characters, or constrain it to a smaller set of options.
>>
Have letters in an array, then generate a random number which you use to determine which element you'll retrieve from that array?
>>
>>61269322

we haven't learned arrays yet, so I don't think we should have to use them.
>>
>>61269262
learn about mod(%) operator, and you should be fine.
>>
>>61269262
OHH I forgot, search for the ASCII table.
>>
>>61269262
random()*N and then remove the decimal part to get an integer in the range [0,N-1]
>>
>>61269367
isn't that for remainders?
>>
>>61269383
in the ACS table, a b and c are 97-99, so would i just set the function to select from those 3 numbers?
>>
Are you guys retarded? Why has nobody posted valid code yet? I think a is 97 in ascii, so assuming that, you can do
char thing() { return (char)(((int)rand()*3)+97);}
>>
>>61269449
make an array containing the three characters you want
generate a random number from 0 to 2
return character in the array index of that number
>>
>>61269262
Quit 4ch and study, nobody will do you homework kid
>>
testing
>>
>>61269465
>
rand()*3

Did you mean
rand()%3
?
>>
>>61269483
what if I just generated a random number from 0 to 2 and associated each number 0,1,2 with a,b,c? unless that's basically the same as what you're saying
>>
>>61269493
not asking anyone to do it, just for some guidance
>>
C is for brainlets.
>>
>>61269537
yes that is exactly what
though if your characters are in the same order as the alphabet then you can do the ascii value-thing too and just shift the random number to start at the relevant ascii value
the +97 in >>61269465 "shifts" the starting point of the random range
*3 scales the value of random(), which is between 0.0 and 1.0, to [0, 3)
taking the integer value makes it either 0, 1, or 2
>>
I'm assuming it's really just a, b and c, since you mentioned that you haven't learned arrays yet. If it's supposed to handle any general case, then i don't know what the fuck your teacher is doing.

>>61269367
this is probably the easiest way for beginners.

>>61269406
You get the remainder of a random number to get a random number whose range of possible values is smaller. So for example, 10 % 3 will give you 0, 1 or 2 and you can easily convert that to get an output of a,b and c.

>>61269449
>>61269465
These are probably right and maybe even the best way to do it, but I'm not sure if OP has learned about casting given that he said he doesn't know about arrays yet. I'm pretty sure if I were the teacher I'd teach arrays first.
>>
Just generate 0,1,2 random and use a case/if statement
>>
>>61269566
for some reason the rand function keeps producing the same number every time i run it. is this expected?
>>
>>61269742
I think C needs you to set the seed the random-function uses yourself (for instance using the current system clock), otherwise it uses some static system-dependent one or something.

idk how to do that in C
>>
>>61269800
thanks
>>
>>61269262
is this actually what intro CS people struggle with?
>>
>>61269865
yes
>>
>>61269800
srand(time(NULL));
Include time.h lib
>>
>>61269353
then how are you representing the set in code
>>
niggas really doing another niggas homework in this thread
>>
>>61270018
I assume if num == 0 then "a" elif num == 1 then "b" ...
>>
>>61270027
aha I nagged people at every tiny problem I had reading tutorials/documentation when I started out like 7 years ago to get where I am today so I mean sure maybe when someone is asking for help they are just trying to get done quickly but then again maybe not
>>
>>61270027
no they aint nigguh
>>
>>61270056
Yes they are, nigga:
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
char my_function(int randominput){
if(randominput == 0)
return 'a';
if(randominput == 1)
return 'b';
if(randominput == 2)
return 'c';
return 'X';
}

int main(void)
{
time_t t;
srand((unsigned) time(&t));
int random_number = rand() % 3;
char displayed_letter = my_function(random_number);
printf("Testing %c\n", displayed_letter);
return 0;
}
>>
>>61270228
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
srand(time(NULL));
printf ("%c\n", "abc"[rand() % 3]);
return 0;
}
>>
>>61270254
See:
>>61269262 (FUNCTION)
And:
>>61269353 (NO ARRAYS)

But good job passing a function a NULL value to time. Not portable!
>>
>>61270030
That's the most retarded fucking thing I've ever heard. Tell your teacher to fuck off and use an array like a rational individual.
>>
>>61270254
What the fuck? No parameter in your main? So I guess we'll just pass it infinite parameters, eh? int main(void) or take proper arguments from the system
>>
>>61270323
I am not OP but that is the absolutely easiest way to do it if you have barely touched any language features
>>
>>61270392
You are allowed to omit parameter list in C. That code is perfectly fine and you are retarded.
>>
what about
return rand() %3 + 'a'

?
>>
>>61270499
Not sure this compiles, don't you need to cast the literal?
>>
>>61270392
t. Dunning-Kruger
>>
>>61270519
no, a char is just a pretty way to display an integer
>>
>>61270519
welcome to C

it can be your best friend or your worst enemy depending on how new you are
>>
>>61270392
GCC won't even warn about this with -Wmain. The definition of main is implementation-defined, and this is valid in GCC.
>>
>>61271348
>The definition of main is implementation-defined
The definition of main is either
int main(void)
or
int main(int argc, char *argv[])
or anything which is equivalent to those (different argument names, compatible types.
An empty argument list for a function is compatible with (void), so
int main()
is a valid definition of main.
>>
>>61269262
>not using C++ instead
>>
>>61271427
>Using C++
Could you be any more of a shit-eater?
>>
>>61271431
>not using haskell for web development
Could you be any more of a shit-eater?
>>
>>61271431
>Could you be any more of a shit-eater?
By using C.
>>
>>61271415
Empty parameter lists in a function declaration are obsolescent as of C99. Stop it.
>>
>>61269998
This is not Library. This is Header.
Library would be included during compilation, which is not necessary, since time.h is in standard.
Thread posts: 50
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.