please explain the output - c

#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|

Related

Accessing an array with Pointer this way? [closed]

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).

Multiply Matrix element in i-th row by the i-th element in the first row

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

Copy certain number from one matrix to another

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

Assigning through random generated indexes

I'm trying to write a function that assign N elements to M players. Here's what I wrote:
void assignElements(Player *p, Tab *t, int n) {
int i = 0, nRand, flagElements = 0;
do {
do {
nRand = MINRANDT + rand() % (MAXRANDT - MINRANDT + 1);
} while(t[nRand].type != Terrain && t[nRand].idProp != -1);
if (i == n) {
i = 0; //this makes "i" reset when it reaches the number of players
}
t[nRand].idProp = i;
p[i].numTerrains++;
i++;
flagElements++;
} while (flagElements != NELEMENTS);
}
So, if I try to run this function, it's not respecting the condition of the second while (maybe the problem is the condition.): in fact, it also assign to t elements that are not of type Terrain (this is an enum). The condition to do the actions under the nRand do / while, is that nRand have to be an index of a t element that is of type Terrain, and that its idProp is -1 (this mean it has not been assigned yet). Hope anyone can help. :)
t[nRand].type != Terrain | t[nRand].idProp != -1 | &&
-------------------------+-----------------------+---
0 | 0 | 0
0 | 1 | 0
1 | 0 | 0
1 | 1 | 1
Meaning that the loop will exit for 3 conditions and repeat only when both sub-conditions are met
Try ||
t[nRand].type != Terrain | t[nRand].idProp != -1 | ||
-------------------------+-----------------------+---
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1

c: draw layout for the table to console

Code works, however I do not know how could I format output correctly to match consistency of the layout (drawing dashes in this case) on any size?
#include <stdio.h>
int main(int argc, const char * argv[]) {
int i=0, k = 0, total_x = 3, total_y = 4;
char symbol = '+';
for (k = 1; k <= total_y; k++) {
// print symbol and row numbers
if (k == 1) {
printf("%3c | ",symbol);
int temp;
for (temp = 1; temp <= total_x; temp++) {
printf("%4d", temp);
}
printf("\n");
for (temp = 1; temp < total_x*5; temp++) {
if (temp == 5) {
printf("+");
}
printf("-");
}
printf("\n");
}
printf("%3d | ",k);
for (i = 1; i <= total_x; i++) {
printf("%4d", k + i);
}
printf("\n");
}
return 0;
}
Output when total_x = 3, total_y = 4;:
+ | 1 2 3
----+----------
1 | 2 3 4
2 | 3 4 5
3 | 4 5 6
4 | 5 6 7
Desired result:
+ | 1 2 3
----+--------------
1 | 2 3 4
2 | 3 4 5
3 | 4 5 6
4 | 5 6 7
Output when when total_x = 10, total_y = 4:
+ | 1 2 3 4 5 6 7 8 9 10
----+---------------------------------------------
1 | 2 3 4 5 6 7 8 9 10 11
2 | 3 4 5 6 7 8 9 10 11 12
3 | 4 5 6 7 8 9 10 11 12 13
4 | 5 6 7 8 9 10 11 12 13 14
Desired result:
+ | 1 2 3 4 5 6 7 8 9 10
----+------------------------------------------
1 | 2 3 4 5 6 7 8 9 10 11
2 | 3 4 5 6 7 8 9 10 11 12
3 | 4 5 6 7 8 9 10 11 12 13
4 | 5 6 7 8 9 10 11 12 13 14
Any printf function that could help me to print out correctly? Thank you so much!
You can make the dashes print prettier like this:
printf("----+--");
for (temp = 1; temp <= total_x; temp++) {
printf("----");
}
printf("\n");
OR correct the arithmetic in what you did:
for (temp = 1; temp <= total_x*4+6; temp++) {
if (temp==5)
printf("+");
printf("-");
}
printf("\n");

Resources