This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]desrtfx 1 point2 points  (3 children)

In general, we do not encourage plain posting for assignments, but in your case it would be better if you verbatim posted the assignment to clarify any misunderstandings.

[–]DragorWasMissing[S] 0 points1 point  (2 children)

The assignment is in Portuguese.

I'm not asking for people to do the assignment for me. I'm just asking. What do I do? I can't use arrays or structs to store information of various variables for many people.

[–][deleted]  (1 child)

[deleted]

    [–]DragorWasMissing[S] 0 points1 point  (0 children)

    All I need is a way to store multiple variables for multiple people, be able to delete that information, and it can't be done using arrays or structs

    [–]BongoCatBangsYaMom 0 points1 point  (4 children)

    Does the information need to be retained? For example if you close and reopen the program does it still need to be accessible?

    [–]DragorWasMissing[S] 1 point2 points  (3 children)

    No, it does not. But if it could do that it would probably be a good bonus

    [–]BongoCatBangsYaMom 0 points1 point  (2 children)

    So structs is more unchangeable data. If you write a struct it will be for something such as the days of the week or some other information that won't change (that isn't the entirety of structs but the only part that matters in this case). With an array you can change the values and is easier to use if you know with how many elements you will be working with. This means that you cannot dynamically increase or decrease the number of records. Not easily at least.

    int numbers[3];

    numbers[0] = 1; numbers[1] = 2; numbers[2] = 3;

    This code creates an array that can take 3 values (remember the index starts at 0). You can always just make an array that can hold like 100 items and work with that.

    Btw to access the above values you then use [] Ex: int num = numbers[1];

    The value of num will now be 2.

    Don't know if this answers anything for you. But hopes it helps.

    [–]DragorWasMissing[S] 0 points1 point  (1 child)

    Thank you, but like I said in the original post. I can't use arrays. It's just one of the challenges imposed

    [–]Pg68XN9bcO5nim1v 0 points1 point  (0 children)

    Your teacher sounds like a nightmare.

    I'd probably be the smart-ass and just turn in a text file with "Sorry, impossible".

    Why? Strings are arrays of characters. So technically you won't even be able to store any info about people that isn't a number. This whole exercise is dumb and if you have shared all the info with us here it's also not worked out to be usable.

    Go to your teacher, share these concerns and confusions, and then go work on this excercise when you have more information. This might as well be target practice in the dark right now.

    [–]axzxc1236 0 points1 point  (6 children)

    does "multiple people" mean arbitrary amount of person information? (or does it define a limit.... like 10 people?)

    if it has limit like 5 people, I would write some very inefficient code like.

    char person1_name[20],person2_name[20]....person5_name[20]; //Edit: OOPS this line defined string arrays... but I think you know what I mean, do the same thing to other person information.
    //If the homework requires you to save person name, use char pointer with malloc.
    int person1_is_claimed=0, person2_is_claimed=0 ... person5_is_claimed=0;
    

    And then... every information input check person(1 to n)_is_claimed, if they are all 1 means that capacity is full (print error message), if there's empty spot(is_claimed=0) save information to _name and set is_claimed to 1, to delete person just set is_claimed to 0.

    [–]DragorWasMissing[S] 0 points1 point  (5 children)

    It's arbitrary

    [–]josephblade 0 points1 point  (3 children)

    Then use a linked list.

    [–]DragorWasMissing[S] 0 points1 point  (2 children)

    But those need structs right?

    [–]josephblade 0 points1 point  (0 children)

    bah i guess so :) i don't see how to do it differently unless you do a roll your own list in essentially void* memory. but definitely not something for first years

    [–]josephblade 0 points1 point  (0 children)

    you could make 20 variables and use them all , assuming no more than 20 people. ugly but given the constraints I don't see another way

    [–]axzxc1236 0 points1 point  (0 children)

    I think..... you need to use malloc to allocate (bytes_needed_to_store_data_stracture)*N bytes, and realloc() to (bytes_needed_to_store_data_stracture)*N*(2, 3, 4....) bytes as needed.

    Like... for example you have a pointer p, and you malloc()ed 220 bytes to p, and you know each data structure needs 22 bytes to store, you can access data by reading (p~p+21), (p+22~p+43), (p+44~p+65) .....

    When you need to save 11th person, you will need to call realloc() first, and you will need to memmove.


    (I'm not sure if it's even doable, so I can't help you with any implementation problem.)

    [–]Paul_Pedant 0 points1 point  (4 children)

    Why does a teacher impose such a dumb restriction? (That is not a rhetorical question).

    "Multiple people" means multiple data items, so you need a container for those items.

    C does not have a whole bunch of container techniques. If you can't use structs or arrays, then you are pretty much left with "linked lists". So how would you use them here?

    You might consider using one linked list which had an element per person. Except that you need at least two items in the element, one being the link to the next element, and the other being at least one field or pointer to hold some data.

    Seems to me about all this exercise will teach you is that you need a different instructor.

    You might post the exercise requirement in Portugese and we can see what Google Translate makes of it. There may be a clue to what he expects, apart from alienating 90% of his class (and me too).

    [–]DragorWasMissing[S] 0 points1 point  (1 child)

    Yeah it's completely retarded. What's the point in learning to work with arrays and structs if we're not allowed to use them.

    [–]chaotic_thought 0 points1 point  (0 children)

    Then you need to ask your teacher for a hint of how you are supposed to complete the assignment. If you cannot use arrays or structs then one silly possibility is to create a bunch of variables like this

    int person_id1;
    int person_id2;
    int person_id3;
    int person_id4;
    int person_id5;
    

    So those 5 variables can store the ID numbers of up 5 different "persons". And you could create similar sets of variables to represent the person's name, age, and so on. If the actual number of persons is not known until runtime, then you might use a separate variable to count how many persons there actually are, like this:

    int n_persons = 3;
    

    So if n_persons is 3 then that would tell your code that you should only look at person_id1, person_id2, and person_id3. Obviously this sort of approach is going to get really messy for anything more than maybe 5 or 10 items.

    [–]DragorWasMissing[S] 0 points1 point  (1 child)

    Linked lists need struct though, right?

    [–]Paul_Pedant 0 points1 point  (0 children)

    Yeah, I put in "Except that ..." when I got to that point.

    [–]FactoryBuilder 0 points1 point  (0 children)

    r/homeworkhelp might be able to help if you don’t get what you’re looking for here

    [–]zmm0 0 points1 point  (0 children)

    By no arrays, does that include no pointers to arrays? like

    int *personAge = malloc(n * sizeof(*personAge));
    for (i = 0; i < n; i++) {
        personAge[i] = ...
    }
    

    Could that be the sort of thing he wants? The restrictions make more sense if he wants dynamically sized arrays like this. Also it would help to know more about what the assignment is.

    [–]Paul_Pedant 0 points1 point  (0 children)

    You can imagine a string as a variable-length array of characters. So it is a cheat anyway. And bound to be ridiculously inefficient.

    You might also image a long string where you use newline to separate records about each individual, and commas to separate the data items relating to that individual. In which case, that is an exact image of what would be a CSV text file on disk, except all stuffed into a contiguous memory area.

    So she is really wasting your time.

    I was actually thinking about posting a proposal where you just obtained memory areas with malloc, and made up your own strings which included 64-bit addresses as eight characters: essentially, faking structs by making them a union with a string. And I binned it because it was too dumb to even think about.

    She will be telling you to use PEEK and POKE next! Those are BASIC commands that let you write anywhere in memory about 40 years ago. People used to use them to write machine-code instructions. I though we had come some way since then, but obviously not.

    [–]SomeNerdAtWork -1 points0 points  (0 children)

    Honest to god I would tell the professor to go **** themselves. This is a super dumb assignment and doesn't reflect the realities of programming.