I have this very large array, called grid. When I declare the array as below, every value in the array should be set to 0 according to the array constructor for integers
int testGrid[226][118];
However when I iterate through the entire array, I seem to get 0s for the majority of the array, however towards the lower part of the array I get arbitrary trash. The solution it is seems is to iterate over the array and manually set each value to 0. Is there a better way to do this?
You could do:
int testGrid[226][118] = {};
which will initialize your entries to 0.
Please see this C answer, which may come in handy for C++ too.
By the way, since this is C++, consider using an std::array, or an std::vector.
Related
So instead of using classes, or structs, I want to use a 5 element array. I want to use these just to keep track of simple numbers, and using several identical arrays, I can treat each one as an "object". However I'm trying to figure out if there's a way to store the entire array, into an element of another array, and be able to access the sub array elements when needed. It's been a while since I've done object oriented programming, so I'm struggling at the moment.
Thanks in advance
you can use a two-dimensional array in c++. a two dimensional array is kinda an array of arrays, and can be declared like this:
int ar[4][3];
(this is a 4 element array and each element is a 3 element array)
it can also be initiated too:
int ar[4][3]={{1,2,5},{10,15,6},{11,1,3},{7,5,3}};
and you can access the numbers like this:
a=ar[2][3];
(now 'a' is the 3rd element of the 2nd array of 'ar')
I hope it helps!
i have a class in which it's protected section i need to declare an array with unknown size (the size is given to the constructor as a parameter), so i looked around and found out that the best possible solution is to declare an array of pointers, each element points to an integer:
int* some_array_;
and simply in the constructor i'll use the "new" operator:
some_array_ = new int[size];
and it worked, my question is: can i declare an array in a class without defining the size? and if yes how do i do it, if not then why does it work for pointers and not for a normal array?
EDIT: i know vecotrs will solve the problem but i can't use them on my HW
You have to think about how this works from the compiler's perspective. A pointer uses a specific amount of space (usually 4 bytes) and you request more space with the new operator. But how much space does an empty array use? It can't be 0 bytes and the compiler has no way of knowing what space to allocate for an array without any elements and therefore it is not allowed.
You could always use a vector. To do this, add this line of code: #include <vector> at the top of your code, and then define the vector as follows:
vector<int> vectorName;
Keep in mind that vectors are not arrays and should not be treated as such. For example, in a loop, you would want to retrieve an element of a vector like this: vectorName.at(index) and not like this: vectorName[index]
Lets say that you have an integer array of size 2. So you have Array[0,1]
Arrays are continuous byte of memery, so if you declare one and then you want to add one or more elements to end of that array, the exact next position (in this case :at index 2(or the 3rd integer) ) has a high chance of being already allocated so in that case you just cant do it. A solution is to create a new array (in this case of 3 elements) , copy the initial array into the new and in the last position add the new integer. Obviously this has a high cost so we dont do it.
A solution to this problem in C++ is Vector and in Java are ArrayLists.
I want to know upto what index my array is filled. I know one method in which I will maintain a temporary variable inside the loop and will keep it updating which will at last determine the size.
I want to know that beside this method is their any other way to do this task? Better be O(1)(if possible) or anything better than O(n).
There is no generic way to do that as all elements of an array always contain a value.
Couple common ways to handle that:
keep track of "valid" elements yourself as you suggested in the post.
have sentinel element that marks "missing" value and check each element for it - first element with such value will mark "end of filled array". For reference types you can use null, for other types sometimes there is specific value that rarely used and can be treated as "missing" - i.e. max value of integer types.
The second approach is the way C-style strings are implemented - it is array of characters up to 0 character - so you can always compute length of the string even if it is stored in longer array of chars.
will this do?
size_t size_of_array = sizeof(array)/sizeof(array[0]);
something like that , and do correct the syntax :)
I have an assignment in which I must read a list of 4000 names from a text file and sort then into a C style array as they're being read in (rather than reading them all then sorting). Since this is going involve a lot elements changing indices would it be possible to use bitshifting to rearrange large quantities of elements simultaneously?For example,
declare a heap based array of 20 size
place variable x index 10
perform a bitshift on index 9 with the size of the array data type so that x is now in index 11
Also, if you have any tips on the task in general I'd appreciate it.
No, that doesn't sound at all like something you'd use bitshifting for.
You will have distinct elements (the names) stored in an array, and you need to change the order of entire elements. This is not what bitshifting is used for; it is used to move the bits in a single integer to the left or to the right.
You should just learn qsort().
Not sure about the "sort as they're being read in" requirement, but the easiest solution would be to just call qsort() as each name is added. If that's not allowed or deemed too expensive, think about how to do a "sorted insert" against an array.
By the way, a typical approach in C would be to work with an array of pointers to strings, rather than an array of actual strings. This is good, since sorting an array of pointers is much easier.
So you would have:
char *names[4000];
instead of
char names[4000][64 /* or whatever */];
This would require you to dynamically allocate space for each name as it's loaded though, which isn't to hard. Especially not if you have strdup(). :)
If using qsort() is not allowed(would be pretty stupid to do so after every insert), you could write your own insertion sort. It's not exactly a very efficient way of sorting large arrays but I suppose it's what your teacher is expecting for.
I've searched around the internet, and I can't seem to find the answer to my solution (or I'm blind/dumb and just can't figure out how to do it). Part of one of my assignments is as follows:
Constructor – creates an empty 2xn dynamic array. Your dynamic array should start as 2x5 in size but can grow to any length. The default value for empty `elements is “empty” and 0. The class should also have a nextElement variable that keeps track of the next empty spot in the array and is increment each time a data element is added.
Is there a way to create a 2*5 array that will accept string in one of the dimensions and integers in the other?