answersLogoWhite

0

📱

C Programming

Questions related to the C Computer Programming Language. This ranges all the way from K&R to the most recent ANSI incarnations. C has become one of the most popular languages today, and has been used to write all sorts of things for nearly all of the modern operating systems and applications. It it a good compromise between speed, power, and complexity.

9,649 Questions

Which algorithm has some average worst case and best case time?

All algorithms have a best, worst and average case. Algorithms that always perform in constant time have a best, worst and average of O(1).

Write the algorithm of quick sorting?

1- set pass =1

2- repeat step 3 varying j from 0 to n-1-pass

3- if the element at index j is >than the element at index j+1 swap the two element

4- increment pass by 1

5-if pass is <=n-1 go to step 2

What is the solution of towers of hanoi problem?

This algorithm reads the value of number of discs and prints the move that are to be done for playing towers of hanoi.

Pre: n is a positive integer which carries the value of number of discs.

Post: prints the moves that are to be done for transferring discs in ascending order in another peg

1.initialize value of n(positive integer greater than 1)

2.moves = pow(2,disk)-1

3.if(n=1)

i) moves disk from "from" to "to"

4. end if

5. else

i)hanoi(n-1,from,aux,to)

ii) moves disc from "from" to "to''

iii) hanoi(n-i,from,aux,to)

6.end else

7. prints the moves

end towers_of_hanoi

What is meant by entry controled loop?

An exit-controlled loop is a loop where the controlling conditional expression is evaluated at the end of each iteration, as opposed to an entry-controlled loop where the expression is evaluated at the beginning of each iteration. The upshot is that an exit-controlled loop always executes at least one complete iteration whereas an entry-controlled loop might not iterate at all:

int x = 100;

// entry-controlled loop

while (x<50) { printf ("%d\n", x++); // won't execute at all because x<50 is false }

// exit-controlled loop

do {

printf ("%d\n", x++); // will execute one time only

} while (x<50);

Why do we need type casting in programming?

Becuase, sometimes data is in a form that is unrecognizable to the program. Example:

int a = 2;

int b = 3;

double c = a / b;

We cast c as a double. if it was not, then the data would not have a decimal value.

What is role of stack in calling a subroutine and returning from the routine?

The stack has many purposes, but is chiefly used to enable procedure calls. When you invoke a function, the return address (the next instruction after the function call) is pushed onto the stack. Control is then passed to the function. When the called function returns, it pops the return address off the stack and control is returned to that address. In this way functions can call functions without losing track of where the calls came from because return addresses are popped in the reverse order they were pushed. In other words, it provides an automatic "breadcrumb trail" without which it would be impossible to call and return from functions.

The stack is also used to pass actual arguments to functions by pushing values onto the stack immediately after the return address. The called function can then pop those values off the stack and assign them to its formal arguments.

Finally, the stack is used to store the function's local variables prior to calling a function. These are pushed onto the stack before the return address is pushed. In this way, a function can call itself recursively without losing track of its own local variables.

Since invoking and returning from functions is a time-consuming operation, compilers will try to expand functions inline and thus eliminate the overhead of the function call. However, this needs to be weighed against the increased code size which can affect overall performance. By keeping functions small (one or two very simple statements), programs not only become much easier to read and maintain, there's also a much greater opportunity for inline expansion. Complex functions are never good examples for inline expansion.

Program to copy one string to another?

As usual, you should check official documentation before you ask a question like this.

string.h

// Copies num characters from source into destination.

char* strncpy (char* destination, const char* source, size_t num);

// Copies characters from source into destination.

char* strcpy (char* destination, const char* source);

Write an algorithm and draw corresponding flow chart to check whether a given string is a palindrome or not?

Program

# include<stdio.h> void main() { long n,rev,t; int r; clrscr(); printf("Enter number : "); scanf("%ld",&n); t=n; rev=0; while(t>0) { r=t%10; rev=(rev*10)+r; t=t/10; } if(n==rev) printf("Number is palindrom"); else printf("Number is not palindrom"); getch(); }

algorithm

step 1 : input n

step 2 : s = 0, a=n

step 3 : while(n>0)

begin

rem=n%10

s=s*10+rem

n=n/10

end

step 4 : if(s==a)

print 'it is a palindrome'

else

print 'it is not a palindrome'

step 5 : stop

Program in C language to find factorial value Fibonacci valueGCD value using recursion?

#include<stdio.h>

#include<conio.h>

void main()

{

int i,fact=1,n;

clrscr();

printf("\nEnter the no of terms =");

scanf("%d",&n);

for(i=1;i<=n;i++)

fact*=i;

printf("\nFactorial value for %d terms = %d",n,fact);

getch();

}

What is a header node in c plus plus?

A header node, or head node, is a node that marks the start of a series of nodes, usually as part of a list or queue structure. The head node is often a sentinal that holds no data of its own. Sentinels are used to simplify algorithms by ensuring that a list can never be empty, even when it has no data.

What is time complexity and space complexity?

"Running Time" is essentially a synonym of "Time Complexity", although the latter is the more technical term. "Running Time" is confusing, since it sounds like it could mean "the time something takes to run", whereas Time Complexity unambiguously refers to the relationship between the time and the size of the input.

Write a program in c language to reverse elements of a queue?

There are many ways to reverse the order of the elements in a queue. Provided that you have access to the implementation of the queue, it is of course easy to read the elements from the tail end rather than the front end, thus reversing the elements.

However, considering the queue as a black box, and assuming the queue only allows for its characteristic operations (removal of head element, addition to tail), the best method to reverse the elements in a queue to engage a stack.

You'd remove the elements from the queue (always reading the head of the queue), and push each element onto the stack. When the queue is empty, you reverse that process: pop each element from the stack until it is empty, and add each element in this order to the end of the queue.

Your queue will have the exact same elements as in the beginning, but in reverse order.

The exact implementation of this in C, or in any other programming language, is trivial, but the exact source code depends on the implementation of queue and stack containers.

Following is pseudocode:

Queue<Item> reverse (Queue<Item> queue) {

Stack<Item> stack;

Item item;

while (queue.remove(&item)) {

stack.push(item);

}

while(stack.pop(&item)) {

queue.add(item);

}

return queue;

}

How do you display alphanumerics in C program?

#include<iostream.h>

int main()

{

char i,j;

for(i='a';i,<='z';i++)

{

cout<<i<<(char)(i-32);

}

return 0

}

What is the object-oriented version of C programming language that is used to develop software for PCs such as Fractal Design Painter Lotus 123 and games?

There isn't one; C is strictly non object oriented. Although C++ is often considered to be an object-oriented extension for C (it was originally called C with Classes), it would be more accurate to describe them as siblings. The two have evolved separately and while they still retain a high-level of compatibility through their common ancestry, they are not the same language.

Why is Java better than C programming?

Java is very popular among high schools and universities for an introductory language. Java syntax is very similar to the "classic" C and C++ languages, but Java is much more developer-friendly when it comes to error feedback and memory management.

When entire generations of programmers are introduced to the field with the same language, that language tends to become the "popular" one.

How can you pass a function name as argument of another function?

== == Let me correct the Q. Strictly speaking, You never pass a function name to another function, you actually pass function address as argument to another function. However, since the function name automatically resolves into function address, it could be deemed correct to say that you pass function name.

Now, the answer:

If you're talking about function pointers:

void Foo(double (*fptr)(int), int x, int y);

double Bar(int i);

int main()

{

Foo(&Bar, 1, 2);

/*Previous Line passes Bar's address to Foo. The & behind Bar is optional because it's implicit, but I put it there to emphasize that it's the address of Bar being passed.*/

return 0;

}

void Foo(double (*fptr)(int), int x, int y)

{

(*fptr)(10);

/*Previous line will call Bar(10) because main passed it Bar's address. Explicitly declaring the dereference is not required, however I explicitly wrote it in to emphasize that it is a pointer that is getting dereferenced.*/

//do something

}

double Bar(int i)

{

//do something

return 0;

}

Note, the function pointer must have the same parameter list as the function you are trying to set the function pointer equal to.

List the steps how to implement a RPC?

RPC at a Glance 1. Client: invokes stub through a local procedure call

2. Client stub: convert presentation, package parameters, send to server

3. Server stub: unpack parameters, convert presentation

4. Server: executes procedure returns value

5. Server stub: package return values, send to client

6. Client stub: unpack return values and pass to client

Submitted by : Mr. Abdul Malik Y.

sakr232000@yahoo.co.in

Advantages of switch - case over if else?

1.Switch statement looks a lot more tidy and easy to read and understand.

2.Here multiple statements need not to be enclosed within a pair of braces.

3.Switch statement executes faster than if_else because the switch statement checks the condition first and then jumps to the suitable case statement.

Explain the basic datatypes in C?

C datatype can be categories into two part

1) Scalar data type 2) derived data type..

The scalar data type is also called basic data type

they are

int

char

float

double

long

signed or unsigned are key word which effectively change the storing power of these data type.

by default they are signed in nature..

How many types of operators?

The different types of operators are:

Assignment operator- This is used to assign values to variables. Ex: =

Arithmetic Operators - These are used to perform arithmetic operations. Ex: +, -, *, /, %

Logical Operators - These are used to perform logical checks like: I < 10 or x == Y etc.

What is A part of a program in witch a variable may be accessed?

The part of a program in which a particular variable may be accessed is called the 'scope' of the variable.

In most cases, the scope of a variable is limited to the function within which it was created, or any function it is passed to as an argument.

You can also use global variables, which can be accessed from any part of the program and have 'global scope'. However, this is generally considered as poor programming practice, and should be used cautiously and sparingly as it tends to make code difficult to read and maintain.

Is algorithm a pictorial representation of flowchart?

Flow Chart DefinedA flow chart is a graphical or symbolic representation of a process. Each step in the process is represented by a different symbol and contains a short description of the process step. The flow chart symbols are linked together with arrows showing the process flow direction. Common Flowchart SymbolsDifferent flow chart symbols have different meanings. The most common flow chart symbols are:
  • Terminator: An oval flow chart shape indicating the start or end of the process.
  • Process: A rectangular flow chart shape indicating a normal process flow step.
  • Decision: A diamond flow chart shape indication a branch in the process flow.
  • Connector: A small, labeled, circular flow chart shape used to indicate a jump in the process flow. (Shown as the circle with the letter "A", below.)
  • Data: A parallelogram that indicates data input or output (I/O) for a process.
  • Document: Used to indicate a document or report (see image in sample flow chart below).

What is pointer and pointer types and uses of pointer and the meaning of pointer?

The main advantages of using pointers are

1.) Function cannot return more than one value. But when the same function can modify many pointer variables and function as if it is returning more than one variable.

2.) In the case of arrays, we can decide the size of the array at runtime by allocating the necessary space.

C has a minimum number of fundamental data types - a single character, a single integer or float, and a few derived data types such as a structure, an enumerated list, and an array. If you want to do much more than that, you make use of pointers.

Pointers allow you to dynamically request memory to store off information for use later. They allow you to create linked lists and other algorithmically oriented data structures.

What mark is used to separate arguments in a function?

That depends on the syntax rules of the language in which you are programming. However, the "," is the most usual separator).