Draw a flow chart to find out the greatest number among the three given numbers ab c?
draw a flowchart to find the biggest number among the 3 numbers
Why should you never put a semicolon at the end of the while loop's parentheses?
There are rare occasions where you would do that, but in general you don't want to do that for most while loops because the semi-colon is a statement by itself (it becomes part of the loop).
For example,
while (a > b) ;
would terminate the loop body, whereas:
while (a > b) b++ ;
would have the increment of 'b' as part of the loop.
In the first example it might cause an infinite loop, depending on whether or not a is greater than b.
The semi-colon character means the semantic end of a statement. Putting it with the while logically doesn't make much sense most of the time.
Note: Never say never, for example the following is completely legal:
while ((*to++ = *from++)!='\0');
What is compiled time and run time?
Compile time is when the compiler translates your source code into computer language. Run time is when the actual program runs.
Write a Program to implement kruskal's algorithm in c?
/* Program for creating a minimum spanning tree from Kruskal's
algorithm */
#include
#define MAX 20
struct edge
{
int u;
int v;
int weight;
struct edge *link;
}*front = NULL;
int father[MAX]; /*Holds father of each node */
struct edge tree[MAX]; /* Will contain the edges of spanning tree */
int n; /*Denotes total number of nodes in the graph */
int wt_tree=0; /*Weight of the spanning tree */
int count=0; /* Denotes number of edges included in the tree */
/* Functions */
void make_tree();
void insert_tree(int i,int j,int wt);
void insert_pque(int i,int j,int wt);
struct edge *del_pque();
main()
{
int i;
create_graph();
make_tree();
printf("Edges to be included in spanning tree are :\n");
for(i=1;i<=count;i++)
{
printf("%d->",tree[i].u);
printf("%d\n",tree[i].v);
}
printf("Weight of this minimum spanning tree is : %d\n", wt_tree);
}/*End of main()*/
create_graph()
{
int i,wt,max_edges,origin,destin;
printf("Enter number of nodes : ");
scanf("%d",&n);
max_edges=n*(n-1)/2;
for(i=1;i<=max_edges;i++)
{
printf("Enter edge %d(0 0 to quit): ",i);
scanf("%d %d",&origin,&destin);
if( (origin==0) && (destin==0) )
break;
printf("Enter weight for this edge : ");
scanf("%d",&wt);
if( origin > n destin > n origin<=0 destin<=0)
{
printf("Invalid edge!\n");
i--;
}
else
insert_pque(origin,destin,wt);
}/*End of for*/
if(i
{
printf("Spanning tree is not possible\n");
exit(1);
}
}/*End of create_graph()*/
void make_tree()
{
struct edge *tmp;
int node1,node2,root_n1,root_n2;
while( count < n-1) /*Loop till n-1 edges included in the tree*/
{
tmp=del_pque();
node1=tmp->u;
node2=tmp->v;
printf("n1=%d ",node1);
printf("n2=%d ",node2);
while( node1 > 0)
{
root_n1=node1;
node1=father[node1];
}
while( node2 >0 )
{
root_n2=node2;
node2=father[node2];
}
printf("rootn1=%d ",root_n1);
printf("rootn2=%d\n",root_n2);
if(root_n1!=root_n2)
{
insert_tree(tmp->u,tmp->v,tmp->weight);
wt_tree=wt_tree+tmp->weight;
father[root_n2]=root_n1;
}
}/*End of while*/
}/*End of make_tree()*/
/*Inserting an edge in the tree */
void insert_tree(int i,int j,int wt)
{
printf("This edge inserted in the spanning tree\n");
count++;
tree[count].u=i;
tree[count].v=j;
tree[count].weight=wt;
}/*End of insert_tree()*/
/*Inserting edges in the priority queue */
void insert_pque(int i,int j,int wt)
{
struct edge *tmp,*q;
tmp = (struct edge *)malloc(sizeof(struct edge));
tmp->u=i;
tmp->v=j;
tmp->weight = wt;
/*Queue is empty or edge to be added has weight less than first edge*/
if( front NULL) /*Edge to be added at the end*/
tmp->link = NULL;
}/*End of else*/
}/*End of insert_pque()*/
/*Deleting an edge from the priority queue*/
struct edge *del_pque()
{
struct edge *tmp;
tmp = front;
printf("Edge processed is %d->%d %d\n",tmp->u,tmp->v,tmp->weight);
front = front->link;
return tmp;
}/*End of del_pque()*/
How do you merge two array without using function?
Take another array big enough to hold both array copy content of these two array into new one. You merged two array and haven't used a single function.!
Can a conditional operator replace an if statement always?
No. An if statement does not require an elseclause and the expression(s) do not return anything to the caller, whereas the conditional operator always executes one of two expressions and always returns the value of the executed expression back to the caller. The executed expression may be yet another conditional operator, thus allowing simulation of nested ifs and if...else if... else statements.
Consider the following example:
int x = rand();
if( x > 100 ) x = 100;
We can achieve the same result with a conditional operator:
int y = rand();
y = y>100 ? 100 : y;
However, if we were to expand this statement to an if statement we would not get the original if statement shown above:
int z = rand();
if( z > 100 ) z = 100;
else z = z;
The else clause is clearly unnecessary in this case, so the original if statement would be the statement of choice here.
As a general rule, if you can make use of the return value in a conditional operator, and must return one of at least two values, then use the conditional operator. Otherwise use an if statement.
When you are using C, structure padding is used to pad the data in such a way that it can be sent to external devices. Sometimes, the data is padded in such a way that it can be used on little endian vs big endian processors.
Padding is done to fast access of data from memory. Structure members are aligned to based on the memory pointer size, which is normally 32bit in Win32 systems. So a character variable defined may take 4 bytes. Hence the size of structure is not really the sum of size individual members.
Extra bits between or after the structure's defined fields, that do not belong to any of those fields. The purpose of the padding is to position the fields on byte boundaries or even word boundaries, which is convenient for the computer. And the purpose of that is faster reading / writing / searching.
Answer:
Most processors require specific memory alignment on variables certain types. Normally the minimum alignment is the size of the basic type in question, for instance this is common.
Char variables can be byte aligned and appear at any byte boundary
Short (2 byte) variables must be 2 byte aligned, they can appear at any even byte boundary. This means that 0x10004567 is not a valid location for a short variable but 0x10004566 is.
long (4 byte) variables must be 4 byte aligned, they can only appear at byte boundaries that are a multiple of 4 bytes. This means that 0x10004566 is not a valid location for a long variable but 0x10004568 is.
Structure padding occurs because the members of the structure must appear at the correct byte boundary, to achieve this the compiler puts in padding bytes (or bits if bit fields are in use) so that the structure members appear in the correct location. Additionally the size of the structure must be such that in an array of the structures all the structures are correctly aligned in memory so there may be padding bytes at the end of the structure too.
struct example {
char c1;
short s1;
char c2;
long l1;
char c3;
}
In this structure, assuming the alignment scheme I have previously stated then
c1 can appear at any byte boundary, however s1 must appear at a 2 byte boundary so there is a padding byte between c1 and s1.
c2 can then appear in the available memory location, however l1 must be at a 4 byte boundary so there are 3 padding bytes between c2 and l1
c3 then appears in the available memory location, however because the structure contains a long member the structure must be 4 byte aligned and must be a multiple of 4 bytes in size. Therefore there are 3 padding bytes at the end of the structure. it would appear in memory in this order
c1
padding byte
s1 byte 1
s1 byte 2
c2
padding byte
padding byte
padding byte
l1 byte 1
l1 byte 2
l1 byte 3
l1 byte 4
c3
padding byte
padding byte
padding byte
The structure would be 16 bytes long.
re-written like this
struct example {
long l1;
short s1;
char c1;
char c2;
char c3;
}
Then l1 appears at the correct byte alignment, s1 will be correctly aligned so no need for padding between l1 and s1. c1, c2, c3 can appear at any location. The structure must be a multiple of 4 bytes in size since it contains a long so 3 padding bytes appear after c3
It appears in memory in the order
l1 byte 1
l1 byte 2
l1 byte 3
l1 byte 4
s1 byte 1
s1 byte 2
c1
c2
c3
padding byte
padding byte
padding byte
and is only 12 bytes long.
I should point out that structure packing is platform and compiler (and in some cases compiler switch) dependent.
Memory Pools are just a section of memory reserved for allocating temporarily to other parts of the application
A memory leak occurs when you allocate some memory from the heap(or a pool) and then delete all references to that memory without returning it to the pool it was allocated from.
What is turnpike reconstruction problem in data structures?
•Given |D| distances, determine x coordinates for points lying on x-axis-|D|=N(N-1)/2
•Easy to go from points to distances in O(N2) - distances to points is worst-case exponential
What symbol separates variable names?
All names must be composed from some combination of letters, digits and underscores. A name cannot begin with a leading digit but it must contain at least one letter. The only symbols that can physically separate one name from another are the binary operator symbols and semi-colons. Note that whitespace characters are only of relevance when a name identifies a type:
int x; // int is a type, x is a name; whitespace is essential here
int y, z; // comma-separated list of names; all whitespace within the list is ignored
x = y + z; // binary operators '=' and '+' separate the names (all whitespace is ignored)
Why do you need files in c program?
Files are needed to store a program's source code. The compiler uses these files to create object files which can then be linked by the linker to produce an executable code file. However, not all systems support the concept of a file, thus files are not actually considered part of the C standard and therefore files are not needed. They are simply a convenience when programming upon systems that do support the concept of a file.
Which memory stores programs and data files?
All data is stored in the same memory locations being it permanent or temporary memory, programs and data are essentially the same thing .
The way that the data is differentiated is by using memory locations assigned to data string or information. In other words different data location address's for different data bits.
Hope i helped.
What are two reasons why a program might not do what it is programmed to do?
There is either a virus or your computer is having trouble with the program.
C program for a to the power b?
#include <stdio.h>
int main()
{
int a, b,i,ans;
printf("Enter a and b\t");
scanf("%d %d",&a,&b);
ans=1;
for(i=0;i<b;i++)
ans=ans*a;
printf("%d to the power %d = %d",a,b,ans);
return 0;
}
A = A xor B
B = A xor B
A = A xor B
in C...
A^=B;
B^=A;
A^=B;
How do you print two slashes - right after another - in c?
You will have to use "printf" when you want to print two slashes one after another in c.