#(nimsy) c++ hw assignment

503 messages · Page 1 of 1 (latest)

solemn surge
#

// Program Description:
// Input one of the options listed to draw a pattern
// using whatever ASCII character the user desires
// in a 10x10 space.

exotic pecanBOT
#

(nimsy) c++ hw assignment

exotic pecanBOT
#

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>

solemn surge
#

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) {

}*/
solemn surge
# solemn surge

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)

stark frigate
#

what are you struggling with specifically with those? (enums/structs/arrays)

solemn surge
#

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?

stark frigate
#

you want to initialize a variable with a value from the enum?

solemn surge
#

no

#

initializing is the easy part

#

wouldn't it just be

#
Patterns pattern = Patterns::FILLED;
stark frigate
#

so you want to map the pattern string to that enum?

#

so turn like String Stripes into the matching enum?

solemn surge
#

ye, unless there's a better way to do it (without strings)

stark frigate
#

should be straightforward but I haven't touched C++ in a while

#

one sec

solemn surge
#

alright

stark frigate
#

i mean also why do you want to turn the string into an enum

#

are you reading it from the stream?

solemn surge
#

well idk how to use the enums

stark frigate
#

like do you want to take the user typing in "checkered" and then use that to get the enum

solemn surge
#

ye

stark frigate
#

I feel like you could have googled this lol

solemn surge
#

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

stark frigate
#

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

stark frigate
#

yeah it's mostly just a design/neatness thing

solemn surge
#

hmm okay

stark frigate
#

anyway so there's a way to map the input from std::cin to string but i have to look it up

solemn surge
#

hmmmm

#

im just afraid that we haven't learned about maps yet

#

and he'll get mad

stark frigate
#

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

solemn surge
#

it's like 5 slides

#

nothing has to do with maps

#

that's it

stark frigate
#

uh

#

you could use static_cast then?

solemn surge
#

how tho?

#

ik there's a slide for it but i still dont understand

stark frigate
#

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

solemn surge
#

yea i dont get it

stark frigate
#

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

solemn surge
#

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

stark frigate
#

also @solemn surge not sure if you care but your uni name is in the images

solemn surge
#

idc

stark frigate
#

just checkin'

solemn surge
#

bruh but still

#

a;ldskfja;ldkfja;lsdkjfal;sdkfj;lasdkfj

#

im so furstarted

stark frigate
#

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

solemn surge
#

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

fickle jacinth
#

The professor is right, you should learn how to use enums

#

In c++, strings are not that pleasant to work with

solemn surge
#

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

fickle jacinth
#

You can convert it from an int

#

In c++ enums are just ints

#

Mostly

solemn surge
#

wat

#

but the input is a string, and not a number

#

like im not inputting "0" or "1"

#

im inputting "triangle"

stark frigate
#

would that not still require a switch case then? where like 0 = Patterns::FILLED

solemn surge
#

or you mean

stark frigate
#

or uh, the string would need to be mapped to the int in question

solemn surge
#

so like

stark frigate
#

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

solemn surge
#

filled is 0 by default, because its the first entry

stark frigate
#

yes

#

the second entry would be 1

#

stripes

solemn surge
#

bruh

#

but this seems so redundant

#

almost like this isn't supposed to be what we're doing int he assignment

stark frigate
#

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

solemn surge
#

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

stark frigate
#

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

solemn surge
#

breuh

#

[aks fd'asdkjf;alsdjkf'asdf

#

then HOW TF AM I SUPOSED TO CONVERT STRING TO ENUM

stark frigate
#

so consider if else

solemn surge
#

oh right

#

forgot that existed

stark frigate
#

haha

solemn surge
#

fml

stark frigate
#

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

solemn surge
#

what's that

stark frigate
#

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

solemn surge
#

also- WHY DO THEY FUCKING PUNSIH YOU FOR GOING OUT OF YOUR WAY TO LEARN MORE ABOUT THE SUBJECT?!

stark frigate
#

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

solemn surge
#

fair

stark frigate
#

also lol

#

i have not the slightest what an upper triangle is supposed to look like though

solemn surge
#

...

#

did you make this?

stark frigate
#

yes

#

the fundamentals are simple it's just a lot of languagespecific stuff

solemn surge
#

using all the steps from the thing?

stark frigate
#

no lol

#

i did use the enum though

feral flint
stark frigate
#

i used the map way

#

because the if else way is psychotic

solemn surge
#
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)

stark frigate
#

well what you can do is not do a getPattern

solemn surge
#

what can i do then

stark frigate
#

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

solemn surge
#

wait what

stark frigate
#

isn't that in the notes you sent

solemn surge
#

so you're saying for 2 hours i have been doing absolutely nothing

stark frigate
#

create_shape and draw functions

#

not nothing

#

just move the logic out of that function and put it in the main

solemn surge
#

ye but then what's the point of enums?

stark frigate
#

you use it in create_shape

#

it takes the enum as an input right

#

so in there, use switch

solemn surge
#

ig so

stark frigate
#

so like in the create_shape do something like this
switch (pattern) {
case Patterns::FILLED:
blah blah fill the colours
}

solemn surge
#

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?

stark frigate
#

draw comes after create_shape

#

so create shape takes the pixel buffer and patterns

solemn surge
#

yea

stark frigate
#

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

solemn surge
#

yea

stark frigate
#

and draws it

solemn surge
#

so shouldn't create_shape be a variable?

#

and not void

stark frigate
#

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

solemn surge
#

ohh

stark frigate
#

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

solemn surge
#

@stark frigate did you just make array_size width*height?

stark frigate
#

Yes

fickle jacinth
#

could also use strtoi

#

(stoi)?

solemn surge
#
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

stark frigate
#

No? Just define it in main

solemn surge
#

define void in main too?

#

wait no that wouldn't work... unless i defined the function in the beginning

stark frigate
#

No as in before you call the draw and create shape functions, define pixel

#

Like Pixel pixels[width*height]

solemn surge
#

they were already

#

but i had to define it outside the main function for it to return as "defined" for create_shape

#

also this

stark frigate
#

Uh

#

Why are you passing the Pixel struct

#

You should be passing a variable of type Pixel

solemn surge
#

oh right

stark frigate
#

Not Pixel itself

solemn surge
#

so just like

#

Pixel p[]

stark frigate
#

Well it needs a variable name yes

#

Look at what I sent

solemn surge
#

ye 1 sec

stark frigate
#

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

solemn surge
#

bruh i have no idea

#
Pixel pixel_buffer = { Colors::FOREGROUND };
solemn surge
#

it's like the second assignment

#

it was due 4 days ago btw

stark frigate
#

Out of 100?

#

Also nimsy…

solemn surge
#

no it's just worth 60

stark frigate
#

But 60 what

#

How many points are in the entire unit

solemn surge
#

idk... points lol

#

uhh

#

no idea

#

it's 24% of the assignments cateogry tho

stark frigate
#

Why are you like this lol

#

Ah there we go

solemn surge
stark frigate
#

Oof

solemn surge
#

truly

#

anyways

solemn surge
#

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

stark frigate
#

It’s a 10*10 grid

solemn surge
#

ye

stark frigate
#

In this case it’s a 1D array

#

So it’s a 100 entry array

solemn surge
#

ye

stark frigate
#

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

solemn surge
#

Pixel pixel_buffer[width * height] = {};

#

so this would be 100 entries?

#

of the Color struct

stark frigate
#

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

solemn surge
#

but it gives a slight warning

stark frigate
#

Ah right

solemn surge
#

wouldn't it just be okay to add the curly brackets at the end?

stark frigate
#

Yeah you can

#

I didn’t use an actual IDE to test lmfao

#

I just ran it online

solemn surge
#

lol

stark frigate
#

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

solemn surge
#

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

stark frigate
#

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

solemn surge
#

ah

stark frigate
#

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

solemn surge
#
        case Patterns::FILLED:
            for (int i = 0; i < array_size; i++)
            {
                p[i].color = Colors::FOREGROUND;
            }
stark frigate
#

That should work yeah

#

Give it a run?

solemn surge
#

ye 1 sec gotta make the draw function rq

stark frigate
#

You probably want similar logic to what I just described

solemn surge
#

even for the filled?

stark frigate
#

Print out characters row by row column by column

#

You may as well right

#

Then they can all use the same loop

solemn surge
#

why not just print it out and mod[the length] and start a new line?

stark frigate
#

You could too

#

Whatever works

solemn surge
#

is there a thing that returns a list of arrays?

#

no i mean

stark frigate
#

What

solemn surge
#

is there a way to get for example entries one through 10 in an array out of 100?

stark frigate
#

Slicing?

solemn surge
#

yeyey slecing

#

eh whatever

#

it's too much work

#

ill just keep it simple

stark frigate
#

Oh that’s not a thing in C++ apparently?

solemn surge
#

L

stark frigate
#

Why do you want it anyway lol

solemn surge
#

c++ taking L after Ls

solemn surge
stark frigate
#

Oh yeah

#

Anyway just get it working first I think lol

solemn surge
#

ye

#

filled works

stark frigate
#

There we go

#

The rest is just patterns

#

I guarantee you can Google them lol

#

Checkered would just be like mod % 2

solemn surge
#

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;
        }
stark frigate
#

Why not use what you already have lol

solemn surge
#

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

stark frigate
#

… nest it under the first loop then?

solemn surge
#

?

#

im only using 1 loop

stark frigate
#

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?

solemn surge
#

ye

stark frigate
#

This doesn’t quit? Unless you add a

#

A

#

Q

#

lol

solemn surge
#

ye unless i add a Q for the first input

stark frigate
#

Isn’t that what you want

#

It loops until the user inputs q

solemn surge
#

yea but if i dont do the if (pattern == "q") { std::cout << "Exiting program."; return 0; }

stark frigate
#

Oh I see

solemn surge
#

ye

stark frigate
#

You could return 0 then yeah

#

You don’t need a while true though

solemn surge
#

what do i do then?

stark frigate
#

Actually on second though yes what you wrote will work

#

I’m phone posting now so sorry for mixed messages lmao

solemn surge
#

all g lol

stark frigate
#

You can use break instead of return 0

#

To end the whole loop

solemn surge
#

ah ok

#

ty

stark frigate
#

Ok good luck lol

solemn surge
#

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

merry portalBOT
#

+> YAY!!!

stark frigate
#

Neato

solemn surge
#

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

stark frigate
#

Isn’t it meant to be 3 spaces?

#

Look at the prompt

#

It says background should be “ “ doesn’t it

solemn surge
#

right right

stark frigate
#

Yeah it does I double checked

solemn surge
#

shits still off tho

stark frigate
#

Isn’t that an issue with your code then lol

#

Looms like it’s off by 1

solemn surge
#

yea i think it's because the loop starts at 0 and not 1

stark frigate
#

Could be yeah

solemn surge
#

that seemed to have fixed it but ours is still pretty fixed

stark frigate
#

Looks like it’s adding in an extra space somewhere? Dunno

solemn surge
#

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?

stark frigate
#

That was my original suggestion yeah

#

One for height and one for width

white ferry
solemn surge
#

idk

#

i just wasn't taught that way (yet) ig

solemn surge
#

what are you talking about?

white ferry
#

thats a basic syntax..?

#

using namespace std; completely removes the need to have std:: in front of things

solemn surge
#

Yeah but we haven’t been told to do that and I don’t wanna be deducted points so

stark frigate
solemn surge
stark frigate
#

Oh

#

Is there any real need to split them?

solemn surge
#

I forgot why

solemn surge
stark frigate
#

Lol

stark frigate
#

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

solemn surge
#

Oh so it’s just the foreground repeated 3 times in the same pixel?

stark frigate
#

It looks like it yeah

#

If you look a the square it’s the same

#

20x10

solemn surge
#

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

stark frigate
#

Uh

#

So it’s concatenating instead of adding?

solemn surge
#

yea

stark frigate
#

Are you definitely sure that’s what’s happening

solemn surge
#

yes

stark frigate
#

What type is row number, an int?

solemn surge
#
11223344556677889100
#

both are ints

stark frigate
#

Weird

solemn surge
#

p[i+=row_number].color = Colors::FOREGROUND;

#

ahhh so close

stark frigate
#

Oof

#

Wouldn’t you want it to cross in the center? So there should be a 2x2 in the enter

solemn surge
#

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

stark frigate
#

Jesus

solemn surge
#

?

#

why u jesusing

stark frigate
#

The formatting is wild

#

Partially mobile discord I imagine

solemn surge
#

o lol

stark frigate
#

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

solemn surge
#

ic

stark frigate
#

we want something like this?

solemn surge
#

ye

#

exactly

stark frigate
#
                    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?

solemn surge
#

@stark frigate

#

there's 4 thingies at the bottom and only 3 on top

stark frigate
#

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

solemn surge
#

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;
stark frigate
#

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

solemn surge
#

@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

stark frigate
#

nice

solemn surge
#

check it out

#

finally im done

safe gobletBOT
#
Resolved

Thread closed as resolved.

safe gobletBOT
#
Thread Reopened

Thread was manually reopened by @stark frigate.

stark frigate
#

Apparently adding an emote reopens the thread lmfao

solemn surge
#

Lol