Pascal - how sets work? [duplicate] - c

This question already has answers here:
What is the implementation of sets used in pascal?
(2 answers)
Closed 6 years ago.
I'm going to a high school programming competition tomorrow, and they use Pascal, about which I can't find much information on the internet, or if I do, I can't really understand it (English isn't my native language).
It would be much appreciated if - someone who still remembers, would explain me: what is a set? Or, how would it look like in C programming language? I guess it's something related to arrays, but I'm not sure though.
Thanks for help in advance!

A set is an unordered collection of elements in which each element can occurr only once.
Depending on what the unique identification of an element is, there can be many ways to implement a set, in any language.
For example, the unique identification is a name and it is mapped onto a number from zero to the size of the set in some way, and this number is used as an index into an array where each array element is [a pointer to] the element. Or there is an array of 32 bit ints and each bit tells whether the element exists in the set and the elements themselves are stored by number in an ordered linked list.
So you see, whithout having more information of what is to be stored in the set, there are numerous implementations possible.

Related

Iterate trought struct in C [duplicate]

This question already has answers here:
Is there any way to loop through a struct with elements of different types in C?
(5 answers)
Closed 6 years ago.
I am writing program in C. I would like to print out values of each element in struct, so that I can print out according value for each bitmap header member. Hence, is it possible to iterate trough each element of struct?
Also is it possible to get number of elements in struct if each element is different size?
Regards
No, you cannot do this in C. There is no iterator over data types. You have to print each field in your struct separately in your function.
Addition:
One way to do this on your own would be to use X-Macros as suggested by coderredoc. But it might get a bit nast for different data types to print.
You can do this in C. Use some thing called X-macro . Not exactly iterator over data types but a smart way out.
link

generating new variables in c [duplicate]

This question already has an answer here:
Creating variable names from parameters
(1 answer)
Closed 8 years ago.
Is is possible for your code to generate new variables in c? For example, if I made "example_variable = 15", is there any way to automatically generate 15 new variables such as: "generated_variable_1", "generated_variable_2", "generated_variable_3", all the way to "generated_variable_15"?
I'm very new to c, and I haven't had a proper introduction to it, so I only know the basics, especially when it comes to variables. I am pretty sure this is really high-level stuff, so I'm sorry if the question doesn't make sense. I am open to any suggestions for alternate ways of generating the variables.
I know there are probably answers already out there, but I've had trouble finding them and would like answers specific to what I'm looking for, as opposed to piecing together what I need from what I can find.
What you are talking about - generating variables at runtime - is not possible in C. The reason is that C is a low-level language and does not expose an API for runtime manipulation. In fact, once compiled, C programs don't use variables - are values are stored directly in memory using memory addresses.
The closet equivalent to what you're looking for that's available in C is an "array". To declare an array, you can do:
int var[15];
int var2[n]; // in C99+, n is a variable saying how many elements you want in the array
You can also do this with malloc, but this is a bit more complicated and then you must free the values.
A running C program doesn't use your variable names at all. Those names were useful for the compiler to build the program, but are discarded before you run it. This means that in C (but not in interpreted languages like python):
If you rename your variables, you get the exact same program
If you do strings <your program> you won't see any variable names (unless you retained debugging symbols)
Hence, runtime is too late to create new variables. In C, variables are compile-time only. Of course, you can use arrays, or dictionaries, to simulate run-time variable creation, like the other answer, and a few commenters, suggest.

How give array name from string variable in C [duplicate]

This question already has answers here:
Variable with char[] name
(3 answers)
Closed 8 years ago.
I am stuck with a situation where I need to give array name from string variable.
Basically I want to create an array with same name as value in another string variable "name":
char *name="arr_name";
In my case the string being hold by variable name may change. Hence advice accroding.
Thanks!
I think you're looking for some mechanism, similar to the ones found in the higher level languages as in python (introspection), or C# (reflections). C doesn't provide this kind of insight from the runtime, not even the variable names are existing in the bytecode - so basically it's not possible and doesn't make any sense in the terms of the way how C works.
I don't know if that helps, but one thing you could do is to statically (so in the compilation time, not while it's running!) populate char* and create variable with the same name, given that the value of the string is a proper name for the variable (Naming convention for C/C++). You can achieve that by defining a proper macro (#define your_macro(...) code_to_populate_char_and_declare_variable), but I cannot see any point in doing so.

algo to find number of common characters in 2 strings [duplicate]

This question already has answers here:
Finding common characters in two strings
(13 answers)
Closed 8 years ago.
I am writing a c program to find the number of common characters in two strings.
Eg: aabbccc aabc Ans:4
aabcA aa Ans:2
(Strings will have upper case ,lower case and numbers)
I have two algorithms in my mind
Assuming length of strings is n,m
1.Sort the arrays and then count O(nlogn+mlogm) complexity
2.scan through two strings and use a count arrays - O(n+m) complexity
Can anyone please suggest further optimization or any other methods to do this?
basically you are asking about a Bag(Multiset) Intersection.
and I guess there won't be any more efficient algo than O(n+m) because you will have to go through each and every element of two bags at least once.
Since, optimization is needed for big input, I think your second method is pretty fine(counting array method). Whatever algorithm you try to find out, you can't find the answer to your problem without looking at the two strings completely. Hence, there shouldn't be any further optimization to this problem as it is already O(m+n). I think for smaller input your first algorithm will work faster as there is a constant of O(26+26+10) associated with your second algorithm. But if you are really interested in a faster code then try to optimize the method of reading and writing the output. You may google for "faster I/O in C++" and read about it.

Do Perl's sigils have anything to do with memory allocation? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why do Perl variables need to start with $, %,#?
Other scripting languages seems to get along just fine without this or something similiar.
I guess it has something to do with memory allocation and helping the interpreter in order to speed things up, but I couldn't find anything specific on it. $scalar would probably be put into stack, #array into heap and %hash? Into heap as well? And what about ?subroutine?
Could someone help me figure this out or point me to some documentation? I am still trying to grasp some fundamentals and understand how everything works under the hood...
Because it makes it easier to read.
You know which identifiers are nouns, and whether they're singular or plural, because of the sigaldry. It's the same reason in English we have singular and plural determiners and agreement, as in this species is vs these species are. It's nice to know which is which.
Perl stores all data associated with a name in a single symbol table entry. The structure stored there is called a typeglob. The values for $foo, #foo, %foo, and &foo (subroutine reference) are all stored in the typeglob for "foo". The entire typeglob is denoted *foo (where * indicates "all sigils"). All this is explained in the perldata section of the Perl documentation.

Resources