Is it possible to create a subscripted array in C which uses another array as its indexes. I went through a link: IDL — Using Arrays as Subscripts
Arrays can be used as subscripts to other arrays. Each element in the subscript array selects an element in the subscripted array.
For another example, consider the statements:
A = [6, 5, 1, 8, 4, 3]
B = [0, 2, 4, 1]
C = A[B]
PRINT, C
This produces the following output:
6 1 4 5
Is the above possible in C programming.
Arrays can be used as subscripts to other arrays. Each element in the subscript array selects an element in the subscripted array.
Syntactically this is not directly possible in C. You've to use a loop to achieve this.
int a[] = {6, 5, 1, 8, 4, 3};
int b[] = {0, 2, 4, 1};
for (int i = 0; i < (sizeof(b)/sizeof(b[0])); ++i)
printf("%d\n", a[b[i]]);
If you really want it to look that neat, then wrap it in a function and you should be alright:
// returns the no. of elements printed
// returns -1 when index is out of bounds
int print_array(int *a, int *b, size_t na, size_t nb)
{
int i = 0;
for (i = 0; i < nb; ++i)
{
int const index = b[i];
if (index >= na)
return -1;
print("%d\n", a[index]);
}
return i;
}
The array index operator in C can only take integer arguments. This is because the compiler will expand the operation, like A[0], into a basic addition and dereference. As such, you can't pass an array as the operand to extract several indices from the original array.
Remember that A[0] is the same as *A, and A[1] is the same as *(A + 1), etc.
Yes ofcourse it is:
int a[6] = { 6, 5, 1, 8, 4, 3 };
int b[4] = { 0, 2, 4, 1 };
printf("\na[b[0]]=%d", a[b[0]]);
printf("\na[b[1]]=%d", a[b[1]]);
printf("\na[b[2]]=%d", a[b[2]]);
printf("\na[b[3]]=%d", a[b[3]]);
output:
a[b[0]]=6
a[b[1]]=1
a[b[2]]=4
a[b[3]]=5
No, that's not possible!
In C, the array operator [x] is just a shorthand for a pointer addition.
So a[x] is the same as (a+x)*
This means the only valid arguments for the array operator are Integers.
Related
im writting a program in which i want to use this piece of code to transfer a 2d array in a function, but i dont understand fully how it works exactly. Can someone explain it, specifically line 7?
#include <stdio.h>
void print(int *arr, int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", *((arr+i*n) + j));
}
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3, n = 3;
// We can also use "print(&arr[0][0], m, n);"
print((int *)arr, m, n);
return 0;
}
i also tried using
*( *(p + i) + j)
instead, but it didnt really work and i dont know why so if someone can explain why this didnt work as well i would really appreciate it.
In modern C, you should use Variable Length Array types introduced in C99.
void print(int m, int n, int arr[m][n])
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d ", arr[i][j]);
}
The function should be called with a simple:
int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int m = 3, n = 3;
print(m, n, arr);
return 0;
}
The VLA types are optional feature in C11 but they will be mandatory again in C23.
You pass a 2-dimesional Array to your print function, with the amount of items in the individual arrays and the amount of arrays in the 2D-Array.
Now let us come to the loop:
First of all if i and j are both zero you get the first Item of the first Arrays. In the next Iteration of the inner loop j is 1, thus (arr+i*n) + 1 points to 2 Element of the first Arrays, because i is still zero and j will be 1 ((arr + 0 * 3) + 1). In the next iteration it is the same but i is 2, thus pointing to the second element.
When the inner loop has finished i is increased to 1 and the expression is now (arr + 1 * 3) + 0. So now i * 3 will point to the first element of the second Array.
And in the third iteration of the outer loop i will point to the first element of the third array. So i * 3 is always the pointer to the first element of an array, in this 2D-Array and the + j always points to an individual element in the Array. By combining this the 2D-Array gets printed.
*( *(p + i) + j) Does not work because, assuming p is an pointer to the array arr, because you are dereferencing it, so it in the first iteration it would evaluate to *(1 + 0) which results in a segmentation fault because you are not allowed to read this Memory Adress. This is, because by dereferencing it you are *(p + 0) referring to the first Element of the first Array, which is 1.
In int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};, 1, 2, and 3 initialize an array of 3 int. An array is a contiguously allocated set of objects, so 1, 2, and 3 are contiguous in memory. 4, 5, and 6 initialize another array of 3 int, and so do 7, 8, and 9. These three arrays of 3 int are themselves another array, an array of 3 arrays of 3 int. Since an array is a contiguously allocated set of objects, the 3 arrays are contiguous in memory. So the 4, 5, and 6 follow the 1, 2, and 3, and the 7, 8, and 9 followed the 4, 5, and 6.
So the overall effect is that 1, 2, 3, 4, 5, 6, 7, 8, and 9 are contiguous and consecutive in memory.
*((arr+i*n) + j) uses this fact to calculate the location of the element in row i and column j. The starting address of the array is arr. i*n is the number of elements from the start to row i. That is, each row has n elements, so i rows have i*n elements. Then j is the number of elements from the start of the row to the element in column j of that row. So arr + i*n + j is where the element in row i, column j is, and *(arr + i*n + j) is that element. The extra parentheses in *((arr+i*n) + j) are unnecessary.
This code abuses the C type model. In main, arr is an array of 3 arrays of 3 int. When main calls print, it passes (int *)arr. This passes a pointer to an int instead of a pointer to an array of 3 int, and then print bypasses the normal application of array types to accessing the memory. Technically, the behavior of this code is not defined by the C standard, but it works in many C implementations.
C is an extremely simple language, it became popular mainly because the simple parts were designed to be combined in ways that replaced complex parts of previous languages (see for as an example). One side effect is that it leaves out parts you expect in other languages.
Specifically for arrays, an array has no information on its size or format, it's assumed that the programmer will keep track of that, or that the size of every dimension but the first is constant (and normally the first one as well). So however many dimensions it's declared as, an array is just a single block of memory large enough to hold all elements, and the location is calculated internally using the [] operator.
Fun fact, C allows you to specify a[1] as 1[a], because it all translates to addition and multiplication. But don't do that.
In the event that you have an array that has variable sizes for more than one dimension, C doesn't support that so you have to do the math yourself, which is what that print() function is doing, where m and n are the sizes of the dimensions. The first row starts at arr (or arr + 0), and goes to arr + (n - 1) (0 to n-1 is n elements), and would look like arr[0][0] to arr[0][n-1] in a language that supported it. The next row starts at arr + n (would be arr[1][0]) to arr + (2 * n) - 1, and so on (up to what would be arr[m-1][n-1]).
In the function here, i and j go from 0 to m-1 and n-1 respectively, so you don't see - 1 in the code.
One more thing, C is at least helpful enough to know when you use + on a pointer, you mean to increment by the size of the thing you're pointing to, so you don't have to figure out how many bytes in a int.
I tried this code..As you can see the problem is the empty elements are zero. So, I tried to check with it but the thing is I can have 0 as an element.
int main()
{
int array[10] = {1, 2, 0, 3, 4};
printf("%d\n", sizeof(array)/ sizeof(*array)); // This is the size of array
int i = 0;
while(array[i] != 0 && i < 10) {
i++;
};
printf("%d\n", i);
return 0;
}```
You can't. int array[10] will always create an array of 10 elements and you can't ask the compiler which of them have been assigned.
What you could do is int array[] = {1, 2, 0, 3, 4} then the compiler will infer the number of elements for you and you'll have sizeof(array)/ sizeof(*array) == 5
First set the array to a number outside the range of your inputs. Like a negative number.
for(i = 0;i < 10;i++)
array[i] = -1;
or set it to INT_MAX or INT_MIN
int array[10] = {1 , 2, 0, 3} . How can I find out that there are 4 elements here?
How can you say there are 4 elements there as you declared that int array[10] with the size of 10 elements. This implies, you already know the no. of elements. Also, in this scenario, you can't use an if statement to determine the no. of elements as you probably know that in C, if you initialize an array of 10 elements with less than 10 values, rest of them will automatically be assigned to 0.
You have several options:
You know how many elements are in the initializer, so you create another variable that stores that number:int array[10] = {1, 2, 0, 3, 4};
int num_items = 5;
You'll need to update num_items as you "add" or "remove" items to the array. If you treat the array as a stack (only add or remove at the highest index), then this is easy-ish:array[num_items++] = 7; // adds 7 after 4
...
x = array[--num_items]; // x gets 7, 7 is "removed" from the array, need special
// case logic for element 0
You pick a value that isn't valid (say -1) and initialize the remaining elements explicitly:int array[10] = {1, 2, 0, 3, 4, -1, -1, -1, -1, -1 };
You size the array for the initializer, meaning it can only ever store that many elements:int array[] = {1, 2, 0, 3, 4};
Otherwise, you'll need to use a different data structure (such as a linked list) if you need a container that can grow or shrink as items are added or removed.
I built a simple function that, given two arrays aa[5] = {5, 4, 9, -1, 3} and bb[2] = {16, -11}, orders them in a third array cc[7].
#include<stdio.h>
void merge(int *, int *, int *, int, int);
int main(){
int aa[5] = {5, 4, 9, -1, 3};
int bb[2] = {16, -11};
int cc[7];
merge(aa, bb, cc, 5, 2);
return 0;
}
void merge(int *aa, int *bb, int *cc, int m, int n){
int i = 0, j = 0, k = 0;
while(i < m && j < n){
if(aa[i] < bb[j])
cc[k++] = aa[i++]; /*Smallest value should be assigned to cc*/
else
cc[k++] = bb[j++];
}
while(i < m) /*Transfer the remaining part of longest array*/
cc[k++] = aa[i++];
while(j < n)
cc[k++] = bb[j++];
}
The cc array is correctly filled, but the values are not ordered. Instead of the expected cc = {-11, -1, 3, 4, 5, 9, 16} it returns cc = {5, 4, 9, -1, 3, 16, 11}.
Like the assignments cc[k++] = aa[i++] and cc[k++] = bb[j++] do not work, somehow, or the logical test if aa[i] < bb[j] goes ignored.
I hypothesized operators priority problems, hence I tested with two different standard, with no differences:
gcc main.c -o main.x -Wall
gcc main.c -o main.x -Wall -std=c89
I checked the code many times, unable to find any relevant error. Any suggestion at this point would be appreciated.
You need to think your algorithm through properly. There's no obvious bug in it. The problem is your expectations. One way to make this clear is to think about what would happen if one array was empty. Would the function merge change the order of anything? It will not. In fact, if two elements a and b are from the same array - be it aa or bb - and a comes before b in that array, then a will also come before b in cc.
The function does what you expect on sorted arrays, so make sure they are sorted before. You can use qsort for this.
Other than that, when you use pointers to arrays you do not want to change, use the const qualifier.
void merge(const int *aa, const int *bb, int *cc, int m, int n)
There's no bug in your implementation (at least I don't see any, imho) The problem is that the merging you have done is not for two sorted arrays (it's for several bunches of sorted numbers). Case you had feed two already sorted arrays you'd have the result sorted correctly.
The merge sorting algorithm begins with splitting the input into two parts of sorted arrays. This is done by switching arrays when you detect the element is not in order (it is not greater to last number) You get the first ordered set to fill an array (the first a elements of initial list which happen to be in order, to put into array A, and the second bunch of elements to put them into array B. This produces two arrays that can be merged (because they are already in order) and this merging makes the result a larger array (this fact is what warrants the algorithm will make larger and larger arrays at each pass and warrants it will finish at some pass. You don't need to operate array by array, as at each pass the list has less and less packs of larger bunch of sorted elements. in your case:
1st pass input (the switching points are where the input
is not in order, you don't see them, but you switch arrays
when the next input number is less than the last input one):
{5}, {4, 9}, {-1, 3}, {16}, {-11} (See note 2)
after first split:
{5}, {-1, 3}, {-11}
{4, 9}, {16}
after first merge result:
{4, 5, 9}, {-1, 3, 16}, {-11}
after second pass split:
{4, 5, 9}, {-11}
{-1, 3, 16}
result:
{-1, 3, 4, 5, 9, 16}, {-11}
third pass split:
{-1, 3, 4, 5, 9, 16}
{-11}
third pass result:
{-11, -1, 3, 4, 5, 9, 16}
The algorithm finishes when you don't get two bunches of ordered streams (you don't switch arrays), and you cannot divide further the stream of data.
Your implementation only executes one pass of merge sorting algorithm you need to implement it completely to get sorted output. The algorithm was designed to make it possible to do several passes when input is not feasible to put in arrays (as you do, so it doesn't fully illustrate the thing with arrays). Case you have it read from files, you'll see the idea better.
NOTE
Sort programs for huge amounts of data use merging algorithm for bunchs of data that are quicksort'ed first, so we start with buckets of data that don't fit in an array all together.
NOTE 2
The number 16 after number 3 should have been in the same bunch as previous bunch, making it {-1, 3, 16}, but as they where in different arrays at first, and I have not found any way to put them in a list that splits into this arrangement, I have forced the buckets as if 16 < 3, switching artificially the arrays on splitting the input. This could affect the final result in making an extra pass through the data, but doesn't affect the final result, which is a sorted list of numbers. I have made this on purpose, and it is not a mistake (it has no relevance to explain how the algorithm works) Anyway, the algorithm switches lists (I don't like to use arrays when describing this algoritm, as normally merging algorithms don't operate on arrays, as arrays are random access, while lists must be accessed by some iterator means from beginning to end, which is the requirement of the merging sort algorithm) The same happens to {4, 9}, {16} after the first split, just imagine the result of the comparisons was the one shown, as after the first merge everything is correct.
If your program works fine, you can sort in O(N) by comparison. As it is not possible and mentioned in comments by #Karzes, your program works fine just for the sorted sub arrays. Hence, if you want to implement merge function for the merge sort, you should try your program for these two inputs:
int aa[5] = {-1, 3, 4, 5, 9};
int bb[2] = {-11, 16};
Not the most efficient cause it's bobble sort...
#include<stdio.h>
void merge(int *, int *, int *, int, int);
void sortarray(int array[], int arraySize)
{
int c,d,temp;
for (c = 0 ; c < arraySize-1; c++)
{
for (d = 0 ; d < arraySize - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
temp = array[d];
array[d] = array[d+1];
array[d+1] = temp;
}
}
}
}
int main(){
int aa[5] = {5, 4, 9, -1, 3};
int bb[2] = {16, -11};
int cc[7];
int i;
sortarray(aa,sizeof(aa)/sizeof(aa[0]));
sortarray(bb,sizeof(bb)/sizeof(bb[0]));
merge(aa, bb, cc, 5, 2);
for(i=0;i<sizeof(cc)/sizeof(cc[0]);i++)
{
printf("%d,",cc[i]);
}
return 0;
}
void merge(int *aa, int *bb, int *cc, int m, int n){
int i = 0, j = 0, k = 0;
while(i < m && j < n)
{
if(aa[i] < bb[j])
cc[k++] = aa[i++]; /*Smallest value should be assigned to cc*/
else
cc[k++] = bb[j++];
}
while(i < m) /*Transfer the remaining part of longest array*/
cc[k++] = aa[i++];
while(j < n)
cc[k++] = bb[j++];
}
If I want to append a number to an array initialized to int, how can I do that?
int arr[10] = {0, 5, 3, 64};
arr[] += 5; //Is this it?, it's not working for me...
I want {0,5, 3, 64, 5} in the end.
I'm used to Python, and in Python there is a function called list.append that appends an element to the list automatically for you. Does such function exist in C?
int arr[10] = {0, 5, 3, 64};
arr[4] = 5;
EDIT:
So I was asked to explain what's happening when you do:
int arr[10] = {0, 5, 3, 64};
you create an array with 10 elements and you allocate values for the first 4 elements of the array.
Also keep in mind that arr starts at index arr[0] and ends at index arr[9] - 10 elements
arr[0] has value 0;
arr[1] has value 5;
arr[2] has value 3;
arr[3] has value 64;
after that the array contains garbage values / zeroes because you didn't allocated any other values
But you could still allocate 6 more values so when you do
arr[4] = 5;
you allocate the value 5 to the fifth element of the array.
You could do this until you allocate values for the last index of the arr that is arr[9];
Sorry if my explanation is choppy, but I have never been good at explaining things.
There are only two ways to put a value into an array, and one is just syntactic sugar for the other:
a[i] = v;
*(a+i) = v;
Thus, to put something as the element at index 4, you don't have any choice but arr[4] = 5.
For some people which might still see this question, there is another way on how to append another array element(s) in C. You can refer to this blog which shows a C code on how to append another element in your array.
But you can also use memcpy() function, to append element(s) of another array. You can use memcpy()like this:
#include <stdio.h>
#include <string.h>
int main(void)
{
int first_array[10] = {45, 2, 48, 3, 6};
int scnd_array[] = {8, 14, 69, 23, 5};
int i;
// 5 is the number of the elements which are going to be appended
memcpy(first_array + 5, scnd_array, 5 * sizeof(int));
// loop through and print all the array
for (i = 0; i < 10; i++) {
printf("%d\n", a[i]);
}
}
You can have a counter (freePosition), which will track the next free place in an array of size n.
If you have a code like
int arr[10] = {0, 5, 3, 64}; , and you want to append or add a value to next index, you can simply add it by typing a[5] = 5.
The main advantage of doing it like this is you can add or append a value to an any index not required to be continued one, like if I want to append the value 8 to index 9, I can do it by the above concept prior to filling up before indices.
But in python by using list.append() you can do it by continued indices.
Short answer is: You don't have any choice other than:
arr[4] = 5;
void Append(int arr[],int n,int ele){
int size = n+1; // increasing the size
int arrnew[size]; // Creating the new array:
for(int i = 0; i<size;i++){
arrnew[i] = arr[i]; // copy the element old array to new array:
}
arrnew[n] = ele; // Appending the element:
}
by above simple method you can append the value
#include<stdio.h>
int main() {
int buff[] = {1,2,3,4,5,6,9,10};
char c = (buff+1)[5];
printf("%d\n",c);//output is 9
return 0;
}
Can someone explain it clearly how this is happening and why
Recall:
In C the square braces [ ] are implicitly *( ... ).
What is going on in the snippet of code you provided is not obvious pointer arithmetic. This line:
char c = (buff+1)[5];
... is equivalent to the following (by the C standard):
char c = *( ( buff + 1 ) + 5 );
... which points to the 7th element in the array (6th position) and dereferences it. It should output 9, not 19.
Remark:
Following the note about square braces, it's important to see that the following is equivalent.
arr[ n ] <=> n[ arr ]
... where arr is an array and n is a numerical value. A more complicated example:
' '[ "]; i < 0; i++; while ( 1 ); do something awesome (y)." ];
... of entirely valid pointer arithmetic.
{1, 2, 3, 4, 5, 6, 9, 10};
| |
buff buff+1 = {2, 3, 4, 5, 6, 9, 10} (say buff_1)
|
buff_1[5] = 9