Pointers program - c

int main()
{
static int a[2][2] = {1, 2, 3, 4};
int i, j;
static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i),
*(*(i+p)+j), *(*(p+j)+i));
}
}
return 0;
}
When I run this code the output is:
1, 1, 1, 1
2, 2, 2, 2
2, 2, 2, 2
3, 3, 3, 3
Can someone please explain how this code works?

The four pointer operations in the printf:
*(*(p+i)+j)
*(*(j+p)+i)
*(*(i+p)+j)
*(*(p+j)+i)
Evaluate to the following:
*(*(p+i)+j) -> *(p[i]+j)
*(*(j+p)+i) -> *(p[j]+i)
*(*(i+p)+j) -> *(i[p]+j)
*(*(p+j)+i) -> *(j[p]+i)
p[n] is the same as n[p] as the pointer logic (as seen above) is commutitive around +.
see this question for more details.
So there are really only two statements:
*(p[i]+j)
*(p[j]+i)
Of course, this is just p + offset [x] + offset. So really, it is only one statement:
*p[i+j]
Which is of course, the value stored in the p array at offset i+j.
Because of the nested loops, the values of i and j are as follows:
i j i+j
0,0 0
0,1 1
1,0 1
1,1 2
So it prints in turn, the values in each location of p (1,2, 2and3) four times.

Related

C - delete element from array and reorganise

Given this array:
int a[] = {5, 8, 5, 6, 9, 5};
Would it be possible to remove all ints which equals 5 and move the rest the front of the array?
So that after the removal the array would look like this:
int a[] = {8, 6, 9, 0, 0, 0}
I don't know if by removing a element it becomes a 0 or a NULL?
Thanks!
You can do it with two iterations over the array, first iteration two to turn the element you want to delete, second iteration to separate zeros from non-zeros.
int a[] = {5, 8, 5, 6, 9, 5};
int n = 6;
for(int i = 0 ; i < n ; i++ ) {
if(a[i] == 5 ) {
a[i] = 0;
}
}
int* zero = a;
int* nonZero = a;
int j = 0;
while(j < n) {
while(*zero != 0) {
zero++;
}
while(*nonZero == 0) {
nonZero++;
j++;
}
if(zero < nonZero) {
*zero = *nonZero;
*nonZero = 0;
}
j++;
}
Your array is statically allocated, so always has the same size and deleted elements have the 0 value (according how you define the deleted values).
This link can help you and explains about how to delete element from array.
It is been awhile that i have programmed in C but it is posibble.
This is just a pseudo code, but you just need to change it to way of C programming.
int a[] = {5, 8, 5, 6, 9, 5};
int b[] = {5, 8, 5, 6, 9, 5}; // copy of array a to hold temp
for(int i = 0; i < Size of array; i++ ){
for(int j = i; j < Size of array; j++ ){
if(b[j] != 5){
a[i] = b[j];
a[j] = b[i];
break;
}
}
}
It will be like (▼: Target to swap, F: Finished, X: Not a targe to swap, N: Not processed):
▼, ▼, N, N, N, N
5, 8, 5, 6, 9, 5
F, ▼, X, ▼, N, N
8, 5, 5, 6, 9, 5
F, F, ▼, X, ▼, N
8, 6, 5, 5, 9, 5
Result:
8, 6, 9, 5, 5, 5
And remove 5s, it is quite different depends what you mean. If you do not change size of array then they can be 0 or undefined(null). So I think it differs by how you program the function that returns array.
your array is not dynamic so you just can't reduce its size after its been allocated.setting the value zero might solve the problem in your case.

2D array in C, what t+= tab[i][j] means?

int tab[][3] = {
{1, 2, 3},
{6, 5, 4},
{7, 8, 9}
};
main(){
int i, j, t=0;
for(i=2, j=0; i; i--, j++){
t += tab[i][j++];
printf("%d", t);
}
I don't understand why the output is 11, firstly i = 2; (7, 8, 9) and j = 0 and then t += tab[i][j++] this means 7, 8, 9 + (j++ which is 1 now) (2, 5, 8) which means 7 + 8 + 9 + 2 + 5 + 8? I don't get this. t += tab[i][j++]
Two things:
First, it seems that you assume that t += tab[i][j] somehow adds a complete vector or a row to t, i.e. something like (7,8,9). Yet an access to tab[i][j] stands for one particular cell value (not a row), and with i==2, j==0, this means tab[2][0] and yields value 7.
Second, note that you have two statements that increment j, one in the for-part, and one in tab[i][j++] (note the j++). So when i decreases to 1, j has actually increased to 2, such that the second value will be tab[1][2], which is 4.
Then 7 + 4 = 11 should not be a surprise any longer :-).
Hope it helps.
First change printf() to printf("%d\n", t); to see each t value in one line.
running program produce this output:
as you see we have : 7 for first t,
before that you can use two simple for loop to print matrix all elements
and have better view on its elements order.
If you make for loop indentation better you can decode the output results.
grab a pen and paper and draw variables of loop in each iteration.
for(i=2, j=0 ; i != 0 ; i--, j++ ){
t += tab[i][j++];
printf("%d\n", t);
}
first iteration i:2 , j=0 as the condition i != 0(i.e i is not equals to 0 ) is true, then body of for is executed and we have t += tab[2][0];' 'tab [2][0] pointing to 7 (array index in C starts from Zero 0) and because of value of t is 0, now t holds 7 and simply printed on screen. after that j++ in the tab[i][j++] add one to j and now j:1 as this for iteration has been finished two commands in the third part of for is executed now (i--, j++) and make values to i:1 , j:2.
we start second for iteration.
as the condition i != 0 i still true we pass inside of for and
t += tab1[2]; tab[1][2] :4 and added to t:7 we have now t:7+4=11
and print t:11 on screen. now j added by one j:3 and this iteration is finished goto third part of for i--,j++ we have i:0, j:4.
in later iteration first we must check the for condition i != 0 as now i:0 this condition is False and obviously for is finished.
This is my version
int tab[][3] = {
{1, 2, 3},
{6, 5, 4},
{7, 8, 9}
};
//Main Program Function, Execution Part
int main()
{
int i, j, t=0;
for (int i=0;i<3;i++){
printf("\n");
for (int j=0;j<3;j++){
printf("%d\t", tab[i][j]);
}
}
printf("\n\n");
for(i=2, j=0 ; i != 0 ; i--, j++ ){
t += tab[i][j++];
printf("%d\n", t);
}
system ("pause");
}

Got an answer right but don't know how or why. Need an explanation

I recently did an exam and there was a bonus question and I was the only person who got it right. I am just as curious as much as my class mates are on how I got it right.
The question was:
Use this array: int a[5] = {5, 1, 15, 20, 25};
Use these variables: int i, j, m;
Use this base code:
i = /*Enter Code*/;
j = /*Enter Code*/;
m = /*Enter Code*/;
printf("%d, %d, %d", i, j, m);
Get this answer: 3, 2, 15
There were a bunch of conditions but the main was that we were only allowed to use the array and one '1' and one '++' per variable equals.
I got the answer right through trial and error but I have no idea how I got it right and how it is right. Here is my code:
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
_getch();
return 0;
}
I am purely curious about how I got the right answer. Any explanation would be great.
Thanks in advance.
So, just look at the three statements:
i = ++a[1];
This changes a[1] to 2, and assigns that value to i.
j = a[1]++;
This sets j to 2, and changes a[1] to 3.
m = a[i++];
This sets m to 15, and changes i to 3.
So the final values that are printed the required ones, 3, 2, 15.
If you ran this in a debugger, you could watch all the variables and see it happen statement by statement.
Like everyone said, just step through your code with a debugger, or manually :
at beginning
a[5] = {5, 1, 15, 20, 25}
after i = ++a[1];
a[5] = {5, 2, 15, 20, 25}
i = 2
because the pre-incrementation ++ will modify a[1] before affecting him to i.
after j = a[1]++;
a[5] = {5, 3, 15, 20, 25}
i = 2
j = 2
because the post-incrementation ++ will modify a[1] after affecting him to j.
after m = a[i++];
a[5] = {5, 3, 15, 20, 25}
i = 3
j = 2
m = 15
because the post incrementation will modify the value of i after the affectation to m, so at the time of affectation, i will be equal to 2, which a[2] = 15.
You need to understand the difference between ++a and a++ (and when the two incrementation will take place).

C best function to get split array on elements less, equals and greater than some value

I am programming in C. What is the best method (I mean in linear time) to spit array on elements less, equals and greater than some value x.
For example if I have array
{1, 4, 6, 7, 13, 1, 7, 3, 5, 11}
and x = 7 then it should be
{1, 4, 6, 1, 3, 5, 7, 7, 13, 11 }
I don't want to sort elements because I need more efficient way. Of course in this example in could be any permutation of {1, 4, 6, 1, 3, 5} and {13, 11}.
My thougt: less or grater than some element in array... In this example it is 7.
My function is:
int x = 7;
int u =0, z = 0;
for(int i=0; i<size-1; i++) // size - 1 because the last element will be choosen value
{
if(A[i] == x)
swap(A[i], A[u]);
else if(A[i] == x)
{
swap(A[i], A[n-(++z)]);
continue;
}
i++
}
for(int i = 0; i<z; i++)
swap(A[u+i],A[size-(++z)];
where u is number of current less elements, and z is the number of equals element
But if I have every elements in array equals there it doesn't work (size-(++z)) is going under 0
This is the so-called Dutch national flag problem, named after the three-striped Dutch flag. (It was named that by E.W. Dijkstra, who was Dutch.) It's similar to the partition function needed to implement quicksort, but in most explanations of quicksort a two-way partitioning algorithm is presented whereas here we are looking for a three-way partition. The classic quicksort partitioning algorithms divide the vector into two parts, one consisting of elements no greater than the pivot and the other consisting of elements strictly greater. [See note 1]
The wikipedia article gives pseudocode for Dijkstra's solution, which (unlike the classic partition algorithm usually presented in discussions of quicksort) moves left to right through the vector:
void dutchflag(int* v, size_t n, int x) {
for (size_t lo = 0, hi = n, j = 0; j < hi; ) {
if (v[j] < x) {
swap(v, lo, j); ++lo; ++j;
} else if (v[j] > x) {
--hi; swap(v, j, hi);
} else {
++j;
}
}
There is another algorithm, discovered in 1993 by Bentley and McIlroy and published in their paper "Engineering a Sort Function" which has some nice diagrams illustrating how various partitioning functions work, as well as some discussion about why partitioning algorithms matter. The Bentley & McIlroy algorithm is better in the case that the pivot element occurs infrequently in the list while Dijkstra's is better if it appears often, so you have to know something about your data in order to choose between them. I believe that most modern quicksort algorithms use Bentley & McIlroy, because the common case is that the array to be sorted has few duplicates.
Notes
The Hoare algorithm as presented in the Wikipedia Quicksort article, does not rearrange values equal to the pivot, so they can end up being present in both partitions. Consequently, it is not a true partitioning algorithm.
You can do this:
1) Loop through the array, if element is less than x then put in new array1.
2)If element is greater than x then put in new array2.
This is linear time O(n)
I tried something like this below which I think is O(n). Took me a little bit to work the kinks out but I think it's pretty similar to the dutchflag answer above.
My ouptput
a.exe
1 4 6 5 3 1 7 7 11 13
1 4 5 6 3 1 7 7 7 11 13
code:
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
void order(int * list, int size, int orderVal)
{
int firstIdx, lastIdx, currVal, tempVal;
firstIdx = 0;
lastIdx = size-1;
for ( ;lastIdx>firstIdx;firstIdx++)
{
currVal = list[firstIdx];
if (currVal >= orderVal)
{
tempVal = list[lastIdx];
list[lastIdx] = currVal;
lastIdx--;
list[firstIdx] = tempVal;
if (tempVal >= orderVal)
firstIdx--;
}
}
lastIdx = size-1;
for( ;lastIdx>firstIdx && middleNum>0;lastIdx--)
{
currVal = list[lastIdx];
if (currVal == orderVal)
{
tempVal = list[firstIdx];
list[firstIdx] = currVal;
firstIdx++;
list[lastIdx] = tempVal;
if (tempVal == orderVal)
lastIdx++;
}
}
}
int main(int argc, char * argv[])
{
int i;
int list[] = {1, 4, 6, 7, 13, 1, 7, 3, 5, 11};
int list2[] = {1, 4, 7, 6, 7, 13, 1, 7, 3, 5, 11};
order(list, ARRAY_SIZE(list), 7);
for (i=0; i<ARRAY_SIZE(list); i++)
printf("%d ", list[i]);
printf("\n");
order(list2, ARRAY_SIZE(list2), 7);
for (i=0; i<ARRAY_SIZE(list2); i++)
printf("%d ", list2[i]);
}
Here is an example using a bubble sort. Which type of sort algorithm is best, is up to you, this is just to demonstrate. Here, I treat values < x as -1, values == x as 0, values > x as 1.
Note that the elements < x and those > x are still in the same sequence.
#include <stdio.h>
int main(void)
{
int array[] = { 1, 4, 6, 7, 13, 1, 7, 3, 5, 11 };
int x = 7;
int len = sizeof array / sizeof array[0];
int i, j, m, n, tmp;
for (i=0; i<len-1; i++) {
m = array[i] < x ? -1 : array[i] == x ? 0 : 1;
for (j=i+1; j<len; j++) {
n = array[j] < x ? -1 : array[j] == x ? 0 : 1;
if (m > n) {
tmp = array[i]; // swap the array element
array[i] = array[j];
array[j] = tmp;
m = n; // and replace alias
}
}
}
for(i=0; i<len; i++)
printf("%d ", array[i]);
printf("\n");
return 0;
}
Program output:
1 4 6 1 3 5 7 7 13 11

How can I average a subset of an array and store the result in another array?

I have a C array fftArray[64] that contains values that I want averaged and placed into another array frequencyBar[8]. Getting the average of the entire array would be easy enough using a for statement.
int average, sum = 0;
for (i = 0; i < 64; i++)
{
sum += fftArray[i];
}
average = sum/64;
But I just can't seem to figure out how to get the average from fftArray[0] through fftArray[8] and store this in frequencyBar[0], the average of fftArray[9] through fftArray[16] and store this in frequencyBar[1], etc. Can anyone help me out with this? Thanks
This looks like a homework assignment, so, rather than give you the outright answer, I'd rather just point you in the right direction...
use a nested loop (one inside the other). One loop cycles 0-7, the other one 0 - 63. Use the smaller one to populate your sliced averages.
or better yet use the % operator to see when you've gone through 8 elements and do an average of your total, then reset the total for the next set. Then you'll have learned how to use the % operator too! :)
[EDIT]
ok, if not homework then something like this... I haven't written C in 5 years, so treat this as pseudo code:
//assuming you have a fftArray[64] with data, as per your question
int i,sum,avCounter,total;
int averages[8];
for(i=0 , avCounter=0, total=0 ; i<64; ){
total += fftArray[i];
if(++i % 8 == 0){ //%gives you the remainder which will be 0 every 8th cycle
averages[avCounter++] = total / 8
total = 0; //reset for next cycle
}
}
I think this will work better than a nested loop... but I'm not sure since % is division which is more processor heavy than addition... however... I doubt anyone would notice :)
int i, j;
for (i = 0; i < 8; i++) {
int sum = 0;
for (j = 0; j < 8; j++) {
sum += fftArray[ 8*i + j ];
}
frequencyBar[i] = sum / 8;
}
Bonus exercise: Optimize this code for speed on your chosen platform.
TF,
DISCLAIMER: This code is just off the top of my head... it hasn't even been compiled, let alone tested.
// returns the average of array[first..last] inclusive.
int average(int[] array, int first, int last) {
int sum = 0;
for (i = first; i <= last; i++)
sum += array[i];
return sum / (last - first + 1); // not sure about the +1
}
Then what you'd do is loop through the indexes of your frequencyBar array [0..7], setting frequencyBar[i] = average(array, first, last);... the tricky bit is calculating the first and last indexes... try i*8 and (i+1)*8 respectively... that may not be exactly right, but it'll be close ;-)
Cheers. Keith.
EDIT: Bored... waiting for my test results to come back. No news is good news, right? ;-)
It turns out that passing the length is a fair bit simpler than passing the last index.
#include <stdio.h>
int sum(int array[], int first, int length) {
int sum = 0;
for (int i = first; i < first+length; i++)
sum += array[i];
return sum;
}
double average(int array[], int first, int length) {
double total = sum(array, first, length);
#ifdef DEBUG
printf("DEBUG: [%2d..%2d] %d", first, first+length-1, array[first]);
for (int i = first+1; i < first+length; i++)
printf(" + %d", array[i]);
printf(" = %d / %d = %f\n", (int)total, length, total/length);
#endif
return total / length;
}
int main(int argc, char* argv[]) {
int array[] = { // average
1, 2, 3, 4, 5, 1, 2, 3, // 2.625
4, 5, 1, 2, 3, 4, 5, 1, // 3.125
2, 3, 4, 5, 1, 2, 3, 4, // 3
5, 1, 2, 3, 4, 5, 1, 2, // 2.875
3, 4, 5, 1, 2, 3, 4, 5, // 3.375
1, 2, 3, 4, 5, 1, 2, 3, // 2.625
4, 5, 1, 2, 3, 4, 5, 1, // 3.125
2, 3, 4, 5, 1, 2, 3, 4 // 3
};
double frequency[8];
for (int i = 0; i < 8; i++)
frequency[i] = average(array, i*8, 8);
for (int i = 0; i < 8; i++)
printf("%f ", frequency[i]);
printf("\n");
}
Watch your sum doesn't wrap around if fftArray has large value in!

Resources