Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Its basically passing an array to a function via pointer. Then it prints out each element in the array. Im trying to understand a very specific line of pointers since my class has never done a example like this before. After finishing intro I never encountered this in the pointers topic, hence why Im confused. thank you.
main( )
{
inta[3][4] = {
1, 2, 3, 4,
5, 6, 7, 8,
9, 0, 1, 6
} ;
display ( a, 3, 4 ) ;
show ( a, 3, 4 ) ;
print ( a, 3, 4 ) ;
}
display ( int*q, introw, intcol )
{
inti, j ;
for ( i= 0 ; i< row ; i++ )
{
for ( j = 0 ; j < col ; j++)
printf("%d ",*(q+i*col+j)); // THIS is the line Im trying to understand
printf( "\n" ) ;
}
printf("\n" ) ;
}
Let's view the 2D array as a single array:
-------------------------------------------------
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | 1 | 6 |
-------------------------------------------------
^
index 0
And if we take a closer look at the expression (q+i*col+j):
q + (0 * 4) + 0 = 0th index
q + (0 * 4) + 1 = 1st index
q + (0 * 4) + 2 = 2nd index
q + (0 * 4) + 3 = 3rd index
^ ^
i j
After the first row has been printed, i becomes 1:
-------------------------------------------------
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | 1 | 6 |
-------------------------------------------------
^
4th index
q + (1 * 4) + 0 = 4
q + (1 * 4) + 1 = 5
q + (1 * 4) + 2 = 6
q + (1 * 4) + 3 = 7
^ indices
As you can see, this points to the next row.
i becomes 2:
-------------------------------------------------
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | 1 | 6 |
-------------------------------------------------
^
q + (2 * 4) + 0 = 8
q + (2 * 4) + 1 = 9
q + (2 * 4) + 2 = 10
q + (2 * 4) + 3 = 11
^ indices
And so on. Hope this helps
*(q+i*col+j) is equivalent to q[i*col+j].
When using pointer arithmetic, adding a number x to a pointer evaluates to an address x bytes times the size of the pointer type ahead of the initial address (offset).
Related
I have the following matrix:
s=[1,2,3; 4,5,6;7,8,9];
1| 2| 3
4| 5| 6
7| 8| 9
Now I want each integer in the first row and i-th column to be multiplied by the corresponding i row number.
Desired output:
1 | 2 | 3
8 | 10 | 12
21| 24 | 27
Note that if A is diagonal matrix, then A*x scales the rows of x by the weights specified by the diagonal in A. So, for your problem you could simply use:
s = [1,2,3; 4,5,6;7,8,9];
% 1 2 3
% 4 5 6
% 7 8 9
s = diag([1:size(s,1)]) * s;
% 1 2 3
% 8 10 12
% 21 24 27
Using bsxfun you can write:
bsxfun(#times,s,(1:size(s,1)).')
that in MATLAB R2016b or Octave ,thanks to implicit expansion, can be written as:
s .* (1:size(s,1)).'
s = [1,2,3; 4,5,6;7,8,9];
1 2 3
4 5 6
7 8 9
[~, y] = size(s);
a = s(ones(y,1),:).';
b = a.*s;
b =
1 2 3
8 10 12
21 24 27
Im using matlab and I have a matrix
1 1
2 1
3 1
4 2
5 2
6 2
7 1
8 1
9 2
10 2
11 2
How can i copy matrix from second column but only certain number? the other number will be random either 1 or 2. Example
1 1 1 | | 1 1 1
2 1 1 | | 2 1 1
3 1 1 | | 3 1 1
4 2 2 | | 4 2 2
5 2 1 | OR | 5 2 2
6 2 1 | | 6 2 1
7 1 1 | | 7 1 1
8 1 1 | | 8 1 1
9 2 2 | | 9 2 2
10 2 2 | |10 2 1
11 2 1 | |11 2 1
If the third row of 2 become 1, the rest of the column will become 1. process repeat until it reach another set of 2
You can use the logical indexing and the function randi:
a = [1 1;
2 1;
3 1;
4 2;
5 2;
6 2;
7 1;
8 1;
9 2;
10 2;
11 2];
b = randi(2,length(a),1); %generation of random value ∈ [1,2]
b(a(:,2)==1) = 1; %if a(:,2) = 1 b = 1;
a = [a,b]
A= [1 1
2 1
1 1
4 2
5 2
6 2
7 1
8 1
9 2
10 2
11 2] ;
colLength = length (A(:,1)) ;
thridcol = randi (2,colLength,1)
A(:,3) = thridcol ;
flag = 1 ;
i = 1 ; ;
if ( sum (A(3,:) == 1) == length (A(2,:)))
while (flag && i < colLength)
A(3+i,3 ) = 1 ;
if (sum (A(3+i,:) == 2) == length (A(3+i,:)))
flag = 0 ;
end
i = i +1 ;
end
end
I want to make a function like this
>> matdup([1 2],3,4) %or any other input that user wish to enter
ans=
1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2
I am stuck in my code. My logic:
m = matdup(input,row,col)
for i = 1:row
for j = 1:col
m(i, j)= input;
This is producing this:
>> matdup(1,2,2)
ans=
1 1
1 1
But failed at this:
>> matdup([1 2],3,4)
error at console:
Subscripted assignment dimension mismatch.
Error in ==> matdup at 6
m(i, j)= input
Any idea?
Method 1: Are you allowed to use ones? Try this -
A = [1 2]
rowIdx = [1 : size(A,1)]';
colIdx = [1 : size(A,2)]';
out = A(rowIdx(:, ones(3,1)), colIdx(:, ones(4,1)))
Output
out =
1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2
1 2 1 2 1 2 1 2
Method 2: Are you allowed to use bsxfun and permute? Try this for the same result -
A = [1 2]
row_mapped = bsxfun(#plus,A,zeros(3,1))
out = reshape(bsxfun(#plus,row_mapped,permute(zeros(4,1),[3 2 1])),[3 8])
Matlab has a funcion called repmat that does the same.
If you want to create a similar function, you could do something like this:
function B = matdup(A, M, N)
[nr, nc] = size(A);
B = zeros([nr nc] .* [M N]);
for r = 1:M
for c = 1:N
rr = (r - 1) * nr + 1;
cc = (c - 1) * nc + 1;
B(rr:rr + nr - 1, cc:cc + nc - 1) = A;
end
end
end
Note this function is restricted to 2D matrices.
Try kron:
matdup = #(x,m,n) kron(ones(m,n),x)
Demonstration:
>> A = [5 6 7];
>> out = matdup(A,3,2)
out =
5 6 7 5 6 7
5 6 7 5 6 7
5 6 7 5 6 7
Note that you can switch the inputs to kron to effectively replicate elements rather than the whole matrix:
repel = #(x,m,n) kron(x,ones(m,n));
Demonstration:
>> A = [5 6 7];
>> out = repel(A,3,2)
out =
5 5 6 6 7 7
5 5 6 6 7 7
5 5 6 6 7 7
The replication can be done easily using mod:
function R = matdup(A, M, N)
[m n]= size(A);
R = A(mod(0:m*M-1,m)+1, mod(0:n*N-1,n)+1)
I really don't know how to find out the result of nested loops.
For example in the following pseudo-code, I can't sort out what will be given at the end of execution. I'll be so glad if anyone gives me a simple solution.
r <- 0
for i <- 1 to n do
for j <- 1 to i do
for k <- j to i+j do
r <- r + 1
return r
Question is:
What is the result of the code and give the result r in terms of n?
I write it but every time I get confused.
In your pseudo-code, Inner most loop, k <- j to i+j can be written as k <- 0 to i (this is by removing j). Hence your code can be simplified as follows:
r <- 0
for i <- 1 to n do
for j <- 1 to i do
for k <- 0 to i do // notice here `j` removed
r <- r + 1
return r
Based on this pseudo-code, I have written a C program(as below) to generate sequence for N = 1 to 10. (you originally tagged question as java but I am writing c code because what you wants is independent of language constraints)
#include<stdio.h>
int main(){
int i =0, k =0, j =0, n =0;
int N =0;
int r =0;
N =10;
for (n=1; n <= N; n++){
// unindented code here
r =0;
for (i=1; i<=n; i++)
for (j=1; j<=i; j++)
for (k=0; k<=i; k++)
r++;
printf("\n N=%d result = %d",n, r);
}
printf("\n");
}
Output of this program is something like:
$ ./a.out
N=1 result = 2
N=2 result = 8
N=3 result = 20
N=4 result = 40
N=5 result = 70
N=6 result = 112
N=7 result = 168
N=8 result = 240
N=9 result = 330
N=10 result = 440
Then, Tried to explore, How does it work? with some diagrams:
Execution Tree For N=1:
1<=i<=1, (i=1)
|
1<=j<=i, (j=1)
/ \
0<=k<=i, (K=0) (K=1)
| |
r=0 r++ r++ => r = 2
( 1 + 1 )
That is (1*2) = 2
Tree For N=2:
1<=i<=2, (i=1)-----------------------(i=2)
| |---------|------|
1<=j<=i, (j=1) (j=1) (j=2)
/ \ / | \ / | \
0<=k<=i, (K=0) (K=1) (K=0)(K=1)(k=2) (K=0)(K=1)(k=2)
| | | | | | | |
r=0 r++ r++ r++ r++ r++ r++ r++ r++ => 8
-------------- ---------------------------------
( 1 + 1) ( 3 + 3 )
That is (1 + 1) + (3 + 3) = 8
Similarly I drawn a tree for N=3:
1<=i<=3, (i=1)-----------------------(i=2)--------------------------------------------(i=3)
| |---------|------| |----------------------|----------------------|
1<=j<=3, (j=1) (j=1) (j=2) ( j=1 ) ( j=2 ) ( j=3 )
/ \ / | \ / | \ / | | \ / | | \ / | | \
0<=k<=i, (K=0) (K=1) (K=0)(K=1)(k=2) (K=0)(K=1)(k=2) / | | \ / | | \ / | | \
| | | | | | | | (K=0)(K=1)(k=2)(k=3) (K=0)(K=1)(k=2)(k=3) (K=0)(K=1)(k=2)(k=3)
r=0 r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++ r++
That is (1 + 1) + (3 + 3) + (4 + 4+ 4)= 20
N = 1, (1 + 1) = 2
N = 2, (1 + 1) + (3 + 3) = 8
N = 3, (1 + 1) + (3 + 3) + (4 + 4 + 4)= 20
N = 4, (1 + 1) + (3 + 3) + (4 + 4 + 4) + (5 + 5 + 5 + 5) = 40
N = 5, (1 + 1) + (3 + 3) + (4 + 4 + 4) + (5 + 5 + 5 + 5) + (6 + 6 + 6 + 6 + 6) = 70
N = 6, (1 + 1) + (3 + 3) + (4 + 4 + 4) + (5 + 5 + 5 + 5) + (6 + 6 + 6 + 6 + 6) + (7 + 7 + 7 + 7 + 7 + 7)= 112
For N=6 we can also be write above sequence as:
(1*2) + (2*3) + (3*4) + (4*5) + (5*6) + (6*7)
Finally, I could understand that sum of N in three loop is:
(1*2) + (2*3) + (3*4) + (4*5) + (5*6) + ... + (N * (N+1))
With help from math.stackexchange.com, I could simplify this equation:
I asked here: How to simplify summation equation in terms of N?
So as I commented to your question, Result in term of N is ( ((N) * (N+1) * (N+2)) / 3 ).
And, I think its correct. I cross checked it as follows:
N = 1, (1 * 2 * 3)/3 = 2
N = 2, (2 * 3 * 4)/3 = 8
N = 3, (3 * 4 * 5)/3 = 20
N = 4, (4 * 5 * 6)/3 = 40
N = 5, (5 * 6 * 7)/3 = 70
Try using some code like this to work it out... i.e. code up what it is and what you think it should be and test it.
EDIT: updated based on comment above.
public class CountLoop{
public static void main(String[] args){
for(int i=1;i<=10;i++)
System.out.println("It's "+run(i)+" and I think "+guess(i));;
}
public static int run(int n){
int r = 0;
for(int i=1;i<=n;i++)
for(int j=1; j <= i;j++)
for(int k=j; k <= i+j; k++)
r += 1;
return r;
}
public static int guess(int n){
// taken from the comments
int r = ((n * (n+1) * (n+2)) /3);
return r;
}
}
Running this gets
It's 2 and I think 2
It's 8 and I think 8
It's 20 and I think 20
It's 40 and I think 40
It's 70 and I think 70
It's 112 and I think 112
It's 168 and I think 168
It's 240 and I think 240
It's 330 and I think 330
It's 440 and I think 440
so we're happy.
I am getting it something like this :
n = 1: r = 2
n = 2: r = 8
n = 3: r = 20
n = 4: r = 40
n = 5: r = 70
n = 6: r = 112
n = 7: r = 168
n = 8: r = 240
n = 9: r = 330
n = 10: r = 440
lets say for n = 10,
r = 2 + 6 + 12 + 20 + 30 + 42 + 56 + 72 + 90 + 110 = 440
=> r = 2(1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 + 55)
Intuitively, I think
n = sum(n-1) + n * (n + 1).
where
sum(n-1) = value of r for n-1
#include<stdio.h>
#include<conio.h>
int t=8;
int dok(int);
int doky(int);
int main()
{
int clrscr();
int x,y;
int s=2;
s*=3;
x=dok(s);
y=doky(s);
printf("%d%d%d",s,y,x);
getch();
return 0;
}
int dok(int a)
{
a+=-5;
t-=4;
return(a+t);
}
int doky(int a)
{
a=1;
t+=a;
return(a+t);
}
Answer to above code: 665
I understand why s=6, x=1+4=5 (a=6-5=1,t=8-4=4)... Please tell me how y comes as 6, I thought y would be 1+4=5 (a=1, t=4)
Thanks, please help me.
tell me how y comes as 6 ...
Call to dok function modifies t to 4.
int doky(int a)
{
a=1;
t+=a; // Previously t is 4 because of t-=4 in earlier function call
// t = 4+1 = 5
return(a+t); // 1+5 = 6 retured
}
first t increases by a and then sum of a and t is returned
so, t was 4. then operator t += a is executed and t becomes 5.
and a+t == 1+5 == 6 is returned
The value of t is changed to 4 with the dok function, and the doky function increments that value by 1 (the value in a). Sum that (5 so far) to the value of a again (set to 1), and that's 4+1+1 = 6.
//t is 4, the value of a is irrelevant since it changes on the next instruction.
a=1;
t+=a; // t is now 5
return(a+t); // 1+5 = 6
y = a + t = a + t + a = 1 + 4 + 1 = 6 :)
Just do it with pencil and paper ...
| t | x | y | s | a |
-----------------+---+---+---+---+---+
before main | 8 |#NA|#NA|#NA|#NA|
before x=dok(s) | 8 | ? | ? | 6 |#NA|
inside dok | 4 |#NA|#NA|#NA| 1 |
after dok | 4 | 5 | ? | 6 |#NA|
before y=doky(s) | 4 | 5 | ? | 6 |#NA|
inside doky | 5 |#NA|#NA|#NA| 1 |
after doky | 5 | 5 | 6 | 6 |#NA|