You are on page 1of 3

Q1) Write a program in c to transpose the given matrix?

#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
printf("%d ", a[i][j]);
printf("\n");
}
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
{
transpose[j][i] = a[i][j];
}
printf("\nTranspose of the matrix :\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
}
Q2) How a negative integer is stored in C?

Generally, integers are unsigned i.e., they can store both positive and negative values. For
this, the first bit of all the bits used to store an integer plays a significant role .It is called
the Sign bit . If the integer is positive , then 0 is stored in it while for negative integers 1
is stored in this sign bit . This system of storage was fine but it has an exception which
was for storage of integer zero (0). For integer 0 there were two representations in this
system (one was positive zero having sign bit as 0 while other representation was for
negative zero having sign bit as 1).
In order to resolve this issue , the concept of 2’s complement was introduced. In this
method whenever a negative integer is encountered, the number (ignoring minus sign) is
converted to its binary equivalent. Then its 2’s complement is calculated and that is
stored at the place allocated for this integer in the memory . As we are taking 2’s
complement so the first bit i.e., the sign bit becomes 1. (For calculating the 2’s
complement the , firstly we take 1’s component by transforming the bit 1 to 0 and 0 to 1
vice versa of the binary equivalent of the given number and then adding 1 to it ).
Whenever it comes on accessing that value, firstly, its sign bit will be checked. If the sign
bit is 1, then the binary will be two’s complement and converted to an equivalent decimal
number and will be represented with a minus sign.
Q3) i++ or i=i+1, which can be recommended in c to increment
the value by 1 and why?

The expression i++ denotes “post increment operator “ which means it would firstly
return the previously stored value of i and after that (i.e. post returning the value of i)
would increment the value of i in the C program i.e., increments i by one but returns the
previously stored value of i.
While on the other hand the expression i=i+1 firstly increments the value of i by 1 ,
writes it to i and then returns the new value of i after assignment.
Both the expression i++ and i=i+1 lead to increment in the value by 1 and both take the
same amount of compilation time . But since there is a slight difference in there way of
working, depending on the requirement , either of the two can be used .
Since i++ returns the previously stored value , it uses the additional memory to store it
and which is discarded as the new incremented value is actually used .This additional
cached value of i is never used in post-increment, the compiler will just optimize that line
away, making the two operators equivalent.
However, for more complex types, such as user-defined types or iterators with the +
operation overloaded, the compiler may not be able to safely optimize the caching
operation.
So, it seems that in most cases, the pre-increment operator is better than, or equal to, the
post-increment operator, as long as one does not need the previous value of whatever you
are incrementing.

You might also like