#(nimsy) c++ hw assignment
503 messages · Page 1 of 1 (latest)
(nimsy) c++ hw assignment
Hi I'm AutoThreadBot! Don't mind me, I'll just be adding the helper team to this thread so they can see it. A human will get to you soon.
You can block this bot if you don't want to see these messages, I won't mind.
<@&525394568410038282>
end result would be something like this
here are the steps:
this is what i have rn
#include <iostream>
const int width = 10;
const int height = 10;
//main
int main() {
enum class Patterns { FILLED, STRIPES, CHECKERBOARD, SQUARE, X, UPPER_TRI };
enum class Colors { FOREGROUND, BACKGROUND };
//pixel would be either a FOREGROUND element (the character user inputs) or a BACKGROUND element (space)
struct Pixel { Colors color; };
//user inputs
std::string pattern;
char foreground = ' - ';
while (pattern != "q") {
std::cout << "Enter in a pattern to draw (or 'q' to exit)." <<
"\nOptions: FILLED, STRIPES, CHECKERBOARD, SQUARE, X, UPPER_TRI" <<
//"\nOptions: filled, striped, checkered, square, x, upper_right_triangle, filled" <<
"\nInput: ";
//Patterns pat = Patterns::pattern;
//if (Patterns)
std::cin >> pattern;
std::cout << "your input was " << pattern;
}
std::cout << "Exiting program.";
return 0;
}
/*/void create_shape(Pixel p[], int array_size, Patterns pattern) {
}
void draw(Pixel p[], int array_size, char foreground) {
}*/
i think i know how to do it without this shit (without following the steps) but im new to enums, structs, and arrays (i somewhat know what they are now)
what are you struggling with specifically with those? (enums/structs/arrays)
first off, id like to somehow make a pattern variable that uses the Patterns enum
is there any way i could get a pattern enum from the pattern string variable?
you want to initialize a variable with a value from the enum?
no
initializing is the easy part
wouldn't it just be
Patterns pattern = Patterns::FILLED;
so you want to map the pattern string to that enum?
so turn like String Stripes into the matching enum?
ye, unless there's a better way to do it (without strings)
ye
alright
i mean also why do you want to turn the string into an enum
are you reading it from the stream?
well idk how to use the enums
like do you want to take the user typing in "checkered" and then use that to get the enum
ye
I feel like you could have googled this lol
ive been trying to bruh
i feel like this is such a bad example of using enums tho....
like why not just create an array of strings and check if the inputted string is in that array
what you can do is map the string to the enum i believe
the main reason i mentioned was type safety/checking if the value exists
but u can just do this
yeah it's mostly just a design/neatness thing
hmm okay
anyway so there's a way to map the input from std::cin to string but i have to look it up
do you not have a class/lecture about enums that tells you how you could use them?
there's probably an exact way they want you to do it then
hm, actually from what I can see/remember then, the enums have values that are integers
so in the example in the last slide, yellow = 5 and green = 6
and i assume red = 0?
That wouldn'
't help with strings though
yea i dont get it
the other slides are comparing enums with other enums so that's not really helpful either
give me a second, I need to report a possible denizen bug
alr
whatever, screw this guy
ill use maps
brtuh
if im gonna define the enum's values as something else, i might as fucking well use arrays instead of enums?? bruhhh this is so annoying
also @solemn surge not sure if you care but your uni name is in the images
idc
just checkin'
also yeah i think without maps the only real way would be to use a switch case
and again yes, it would make sense to just use the string then
but I'm no C++ expert so I could be completely wrong
hmm maybe we're just overthinking this tho...
void create_shape(Pixel p[], int array_size, Patterns pattern) {
}
it says to have a function like this
The professor is right, you should learn how to use enums
In c++, strings are not that pleasant to work with
ok
so how would i use enums in my case?
all i wanna do is get the enum from the user input
(to start off with)
and im pretty sure the user input has to be a string, because otherwise we wouldn't be able to input q
wat
but the input is a string, and not a number
like im not inputting "0" or "1"
im inputting "triangle"
would that not still require a switch case then? where like 0 = Patterns::FILLED
or you mean
or uh, the string would need to be mapped to the int in question
so like
like "FILLED" would need to equal 0 to be used to get the corresponding enum
which would require a switch or if else or something
filled is 0 by default, because its the first entry
bruh
but this seems so redundant
almost like this isn't supposed to be what we're doing int he assignment
well it's a requirement isn't it
i imagine the point of it was to implement what you'd be taught
which includes enums
yea but it almost seems like we're not supposed to do it
it's just counterintuitive
void getPattern(std::string pat)
{
switch(pat)
{
case "FILLED":
return 0;
}
}
i dont get the syntax error bruh
what's the error say
ah, switch can't use string literals in C++
it's literally not allowed
i'm starting to understand why enums are preferable
breuh
[aks fd'asdkjf;alsdjkf'asdf
then HOW TF AM I SUPOSED TO CONVERT STRING TO ENUM
so consider if else
haha
fml
i always struggle with intro classes too because they punish you for using stuff you haven't learnt
it's like relearning the wheel
and I forgot the 'low-tech' method lol
what's that
as in stuff like that
I normally just use switch case too
so it didn't immediately occur to me to just use if else
also- WHY DO THEY FUCKING PUNSIH YOU FOR GOING OUT OF YOUR WAY TO LEARN MORE ABOUT THE SUBJECT?!
because you shouldn't know it yet
if you use a way they didn't teach you're not engaging with the content the way they expect you to
and you may be missing something fundamental
fair
also lol
i have not the slightest what an upper triangle is supposed to look like though
using all the steps from the thing?
nimsy was about to go off there 😂
int getPattern(std::string pat)
{
if (pat == "filled") {
return 0;
}
else if (pat == "striped") {
return 1;
}
else if (pat == "checkered") {
return 2;
}
else if (pat == "square") {
return 3;
}
else if (pat == "x") {
return 4;
}
else if (pat == "upper_right_triangle") {
return 5;
}
else if (pat == "q") {
return 999;
}
else {
return -1;
}
}
this is what the bullshit wants me to do?
the redundancy is insane
it makes me actually wanna commit crimes (not actually)
well what you can do is not do a getPattern
what can i do then
right after reading std::cin do the if else there
if (pattern == "FILLED") {
create_shape(pixels, Patterns::FILLED);
}
for example
since they asked for that function specifically
wait what
isn't that in the notes you sent
so you're saying for 2 hours i have been doing absolutely nothing
create_shape and draw functions
not nothing
just move the logic out of that function and put it in the main
ye but then what's the point of enums?
you use it in create_shape
it takes the enum as an input right
so in there, use switch
ig so
so like in the create_shape do something like this
switch (pattern) {
case Patterns::FILLED:
blah blah fill the colours
}
ye ik what u mean
but also
nvm
wait hang on
then what's the point of draw()?
like where would i put it
would i put it inside the createshape function?
yea
figures out how to fill the pixel buffer based on the pattern
then draw takes in that pixel buffer + the foreground character
eg the letter B
yea
and draws it
no because the pixel buffer variable is being changed, you're not returning it
you're just using it
you have a Pixels array right?
you declare that first
then you change it create_shape
then you reference that array in draw
ohh
there's no need to return it or anything because it can be accessed by the function
think of it as a server flag or something
but not global i guess
@stark frigate did you just make array_size width*height?
Yes
void create_shape(Pixel p[], int array_size, Patterns pattern) {
}
so it says Pixel (first arg) and Patterns (last arg) are undefined
does that mean i gotta define them globally?
ah ye it seemed that way
No? Just define it in main
define void in main too?
wait no that wouldn't work... unless i defined the function in the beginning
No as in before you call the draw and create shape functions, define pixel
Like Pixel pixels[width*height]
they were already
but i had to define it outside the main function for it to return as "defined" for create_shape
also this
Uh
Why are you passing the Pixel struct
You should be passing a variable of type Pixel
oh right
Not Pixel itself
ye 1 sec
Unrelated but what level is this unit
Or like how much is it (the assignment) worth
They’ve jammed a decent bit into this assignment
60 points
it's like the second assignment
it was due 4 days ago btw
no it's just worth 60
bro i was busy studying for my math exam that i got a 55 on
Oof
i have no clue
how to make this pixel buffer
my idea was that the pixel represents one of the dots that will either be a character or no character
It’s a 10*10 grid
ye
ye
Pixel pixels[width * height];
Where Pixel is a struct with a color
So each one of those entries in the array has a colour
Fore or background
Or just blank or the userinput character
Pixel pixel_buffer[width * height] = {};
so this would be 100 entries?
of the Color struct
What are tou
No, that WAS the declaration
Pixel pixels[width * height];
You fill it in create_shape
What I just posted creates a 100 entry array but the array has no values in it yet
Ah right
wouldn't it just be okay to add the curly brackets at the end?
lol
Did you need help with the actual drawing logic? I haven’t done the ones that are square but the fundamentals are done
Filled is hilariously easy lol
yea im still trying to get that lol
void create_shape(Pixel p[], int array_size, Patterns pattern) {
switch (pattern)
{
case Patterns::FILLED:
p[array_size] = Colors::FOREGROUND;
thought itd be something like that
Right kinda approach
What I did was for loop through p and set every entry to be Colors::BACKGROUND; first
You do that by setting the color variable for each entry
So like p[i].color = Colors::BACKGROUND;
You can do this to set it to foreground too
Array size would just set the last entry in the array
Not the entire array
ah
So for example filled you could do a for loop for the array size and then set every value to foreground
I would probably say don’t do this thought since it won’t be dynamic or handle the other kinds of drawing
Best case is to do two for loops, one for width and one for height
That way it can handle horizontal vs vertical
case Patterns::FILLED:
for (int i = 0; i < array_size; i++)
{
p[i].color = Colors::FOREGROUND;
}
ye 1 sec gotta make the draw function rq
You probably want similar logic to what I just described
even for the filled?
Print out characters row by row column by column
You may as well right
Then they can all use the same loop
why not just print it out and mod[the length] and start a new line?
What
is there a way to get for example entries one through 10 in an array out of 100?
Slicing?
Oh that’s not a thing in C++ apparently?
L
Why do you want it anyway lol
c++ taking L after Ls
idk i thought itd be cleaner/cooler if instead of printing each "pixel", we'd just print each row (there'd just be 10 loops as opposed to 100)
There we go
The rest is just patterns
I guarantee you can Google them lol
Checkered would just be like mod % 2
yo @stark frigate
should i just do while true for the while loop?
then i can add something like this in the block
if (pattern == "q") {
std::cout << "Exiting program.";
return 0;
}
Why not use what you already have lol
because im asking for two inputs
meaning even if i put in q for pattern, it'll still ask me for the foreground character
and then after drawing it it will quit
… nest it under the first loop then?
while (pattern != "q") {
std::cout << "Enter a pattern to draw (or 'q' to exit)." <<
"\nOptions: FILLED, STRIPES, CHECKERBOARD, SQUARE, X, UPPER_TRI" <<
"\nInput: ";
std::cin >> pattern;
std::cout << "Enter the character to use for the foreground: ";
std::cin >> foreground;
}
This?
ye
ye unless i add a Q for the first input
yea but if i dont do the if (pattern == "q") { std::cout << "Exiting program."; return 0; }
Oh I see
ye
what do i do then?
Actually on second though yes what you wrote will work
I’m phone posting now so sorry for mixed messages lmao
all g lol
Ok good luck lol
wait 1 sec
case Patterns::STRIPES:
int status = 0;
for (int i = 0; i < array_size; i++)
{
if (i % width == 0) {
if (status == 0)
{
status = 1;
}
else {
status = 0;
}
}
if (status == 1) {
p[i].color = Colors::FOREGROUND;
}
else {
p[i].color = Colors::BACKGROUND;
}
}
my attempt at adding striped
oh wait made a smal mistake
there fixed
yay it works
+> YAY!!!
Neato
wait but @stark frigate what's up with the spacing?
why is ours so fucked
hang on a sec... their shit is way more than 10 characters
Isn’t it meant to be 3 spaces?
Look at the prompt
It says background should be “ “ doesn’t it
right right
Yeah it does I double checked
yea i think it's because the loop starts at 0 and not 1
Could be yeah
Looks like it’s adding in an extra space somewhere? Dunno
also the status thing was dome
idk how to do checkered tho without it
i meant stripes**
yo @stark frigate should we just have two loops instead of 1 big while?
im curious, why arent you using namespace
wait no that's not what i was talking about
what are you talking about?
what.
thats a basic syntax..?
using namespace std; completely removes the need to have std:: in front of things
Yeah but we haven’t been told to do that and I don’t wanna be deducted points so
Which part are you talking about
When getting inputs for the pattern and character
I forgot why
But anyways, I gotta somehow figure out a way to fix the space thing
Lol
That example of striped is like 20*10 isn’t it
Oh I see, each ^^^ is one character it looks like
Yeah it looks like width is treated as 3 wide for whatever reason in the two provided examples
So basically what’s most likely causing this issue is just the line width vs character “width”
Since a line is 10 wide it can’t fit 3 ^^^s without remainder
Which is why your version overflows
Whereas in the example given there’s actually 5 ^^^s per line and 4 empty spaces
Oh so it’s just the foreground repeated 3 times in the same pixel?
yo @stark frigate
case Patterns::X:
for (int i = 0; i < array_size; i++)
{
if (i % width == 0) {
row_number++;
}
//left diagonal
if (i % width == 0) {
p[i+row_number].color = Colors::FOREGROUND;
}
else {
p[i].color = Colors::BACKGROUND;
}
}
break;
this is what im doing for making the x shape, but im confused as to why i + row_number is not return what i want
instead of doing like 1+1=2 it just does 1+1=11
yea
Are you definitely sure that’s what’s happening
yes
What type is row number, an int?
Weird
got it sorta to work
p[i+=row_number].color = Colors::FOREGROUND;
ahhh so close
Oof
Wouldn’t you want it to cross in the center? So there should be a 2x2 in the enter
yea idk how id do that
for (int i = 0; i < array_size; i++)
{
if (i % width == 0) {
row_number++;
}
//left diagonal
if (i % width == row_number) {
p[i].color = Colors::FOREGROUND;
}
else if (i % width == width-row_number) {
p[i].color = Colors::FOREGROUND;
}
else {
p[i].color = Colors::BACKGROUND;
}
std::cout << row_number;
}
this is what i hav ern
Jesus
o lol
oh wow that's much easier to read on desktop
yeah it was formatting
that's what it looks like on my phone lol
it doesn't format or color it either
ic
we want something like this?
if (x == y || x == width - 1 - y) {
p[index].color = Colors::FOREGROUND;
}
this is what i used but I used two for loops
so uh
replace x and y with row_number and stuff
I believe y would be row_number and x would be i % width?
so uh
int x = i % width;
if (x == row_number || x == width - 1 - row_number) {
p[i].color = Colors::FOREGROUND;
}
else {
p[i].color = Colors::BACKGROUND;
}
something like that?
i may have fucked up the conversion
mine is not like that
looking at it, the numbers are off by one
the height only has 9
is it because you start from 1
hmm
wdyn
wdym
this is what i have rn
case Patterns::X:
for (int i = 0; i < array_size; i++)
{
if (i % width == 0) {
row_number++;
}
if (i % width == row_number || i % width == width - 1 - row_number) {
p[i].color = Colors::FOREGROUND;
}
else {
p[i].color = Colors::BACKGROUND;
}
}
break;
like looking at the X
it's only got 9 rows
it should have 10
so the loop is probably off by one or something
@stark frigate
case Patterns::X:
for (int i = 0; i < array_size; i++)
{
if (i % width == 0) {
row_number++;
}
if (i % width == row_number-1 || i % width == width - 1 - row_number) {
p[i].color = Colors::FOREGROUND;
}
else {
p[i].color = Colors::BACKGROUND;
}
}
break;
added a -1 to row_number
oh wait a sec
it broke the center thing
got it to work finally
nice
Thread closed as resolved.
Thread was manually reopened by @stark frigate.
Apparently adding an emote reopens the thread lmfao
Lol