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

What is the height of a binary tree with n nodes in the worst case?

For the height `h' of a binary tree, for which no further attributes are given than the number `n' of nodes, holds:

ceil( ld n) <= h <= n

Where `ld' is the binary logarithm and `ceil' is rounding up to the next integer.

What are the duties of switchboard operator?

the purpose of a switch board is a connection for

What is the code for a C program to find the largest and smallest number?

Fist of all, let's assume that by "list" you mean "array." Otherwise we would need your list implementation in order to be able to iterate through the elements.

int[] nums; // assume these are the numbers you want to search through

int nums_length; // also assume that we know how many numbers are in nums

int min; // smallest number in nums

int max; // largest number in nums

// special case for an empty list of numbers: set min = max = 0

if(nums_length == 0) {

min = 0;

max = 0;

}else {

// start min and max off as equal to the first number

min = nums[0];

max = nums[0];

// iterate through nums

int i;

for(i = 1; i < nums_length; ++i) {

// update max, if necessary

if( nums[i] > max ) {

max = nums[i];

}

// update min, if necessary

if(nums[i] < min) {

min = nums[i];

}

}

}

// min and max are now properly set (or both equal to 0 if nums is empty)

What is the if-then-else structure?

This is one of the most simple flow control mechanisms in programming. It follows very closely to the spoken-language version.

IF something is true

THEN do something

ELSE do something else

if x == 1

print "x is one!"

else

print "x is not one!"

What is reference variable in c plus plus?

A reference is not a variable of any kind. A reference is simply an alias; an alternate name for an object that already exists. It is analogous to a person with the birth name Robert who might also be known as Robbie, Rob, Rab, Bob and so on. They are all aliases for the same person, so every operation we perform on Bob or Robbie is actually performed on Robert.

Once we assign an object to a reference, we cannot subsequently assign another object to that same reference. Thus a reference is always guaranteed to refer to the same object for as long as that reference remains in scope. Moreover, a reference can never be null; it must always refer to something. As such, references must be assigned to an object at the point of declaration, just as you would a constant.

Note that although a reference is not a variable, the object it refers to can be, unless the reference itself is explicitly declared constant. By the same token, a non-constant reference cannot refer to a constant object.

References are typically used whenever we need to pass an object to a function by reference rather than the usual by value semantics. Like so:

void by_value (int x) {}

void by_reference (int& y) {}

In the above examples, x is a copy of the object we pass to the function, so any changes made to x will not affect the object we passed. However, y is a reference thus any changes made to the object referred to by y will be reflected in the object we passed.

A pointer variable is also a type of reference, but it behaves quite differently. Firstly, a pointer variable really is a variable; it is used to store a memory address thus it requires memory of its own. Secondly, unless the pointer is declared constant, we can change the address a pointer refers to by changing its value. Finally, a pointer may be null.

References are generally much easier to work with and more intuitive than pointers. For instance, we do not need to dereference a reference, thus we can use the member-of operator (.) rather than the pointer-to-member operator (->) when working with object references. Moreover, given that a reference can never be null, there is no need to test for null as we would with a pointer. When passing an object by reference, the function can simply use the reference, safe in the knowledge the reference refers to a valid object. If we pass a pointer, we lose that guarantee and must test the pointer before attempting to dereference it. Nevertheless, there can often be cases where passing an object to a function is optional, in which case we use a pointer argument defaulting to null.

There is also one other type of reference known as an r-value reference. An r-value is an operand that appears on the right-hand-side of an operator. Typically, an r-value is a constant since we would normally expect to only modify the left-hand-operand (the l-value). However, since C++11, move semantics require that an r-value be modifiable. For that reason we use r-value references.

We typically use r-value references when declaring move constructors and move assignment operators, like so:

struct S {

S (const S&); // copy constructor

S& operator= (const S&) // copy assignment

S (S&&); // move constructor

S& operator= (S&&) // move assignment

}

Note that S& is a reference while S&& is an r-value reference. The reason we need an r-value reference is that r-values that are moved are always assumed to be objects that will shortly fall from scope (such as when return an object by value). Thus move semantics must leave the r-value in an unspecified but valid state in order to allow the object to be destroyed. To achieve that, the r-value must be modifiable, hence we use an r-value reference.

The classic scenario is when moving a vector. Copying vectors is highly inefficient, but moving them is simply a case of switching ownership of the resource being moved; the array itself. Once the l-value has taken ownership of the resource, the r-value can simply be switched to an empty state. Note that we don't actually clear the vector (the resource no longer belongs to the r-value) we simply change its internal to state to reflect that of an empty vector. The vector can then be allowed to safely fall from scope.

It should be noted that the C++ compiler may well implement references as constant pointers and r-value references as pointer variables, however that is a matter of concern only to compiler designers. When programming in C++, it is vital that we understand that a pointer is a type while a reference is a programming tool.

What is 400f in c?

400 degrees Fahrenheit = 204.444444 degrees Celsius

A very easy way to calculate it is type it into the Google search bar:

400f to c

What does default mean in switch statement?

Default clause in switch statement used to indicate that the desired option is not available with the switch case statement. it is similar to else statement of if statement which is used when the condition does not satisfy.

Can we declare register variable as global?

Globals and statics are both allocated in static memory. Locals are allocated on the stack.

Explain the features of doubly linked list?

Well, in a singly linked list you can only move forward, if the pointer you seek is behind your current position you'll have to cross the hole list to get there. In a doubly linked list you can simply move back as well as forward....

hope this helps...

What is object in programming?

Object in context of a class is the root of the class hierarchy in Java in the java.lang package.

Every class has Object as a superclass whether it's explicit or not. All objects, including arrays, implement the methods of this class.

How can you merge the contents of two sorted arrays into a new array such that the contents of this new array is sorted?

Create a new array, giving it a size that is enough to hold the combined array; copy all elements from the first array; copy all the elements from the second array.

Create a new array, giving it a size that is enough to hold the combined array; copy all elements from the first array; copy all the elements from the second array.

Create a new array, giving it a size that is enough to hold the combined array; copy all elements from the first array; copy all the elements from the second array.

Create a new array, giving it a size that is enough to hold the combined array; copy all elements from the first array; copy all the elements from the second array.

What is a queue Explain various operations perfomed using stack with examples?

Stack has the following operations:

1) push - insert an element into the stack

2) pop - remove an element out of the stack

3) top - display the topmost element in the stack


Queue has the following operations:


1) enqueue - insert an element into the queue

2) dequeue - remove an element from the queue

3) front - display the element at the front of the queue(next element to be dequeued)

4) rear - display the element at the rear of the queue(last element enqueued)


The following operations can be performed in both stack and queue:


1) size - returns the size of the container

2) isempty - returns whether the container is empty or not

What is register in English language?

Register in English language may be used as a noun which refers to official list or record of names or items. When used as a verb, it refers to the process of entering data into an official list or record.

What is the difference between actual and formal argument in c plus plus?

Formal parameters are the parameters as they are known in the function definition. Actual parameters (also known as arguments) are what are passed by the caller. For example, in the following code, a and b are the formal parameters, and x and y are the actual parameters:

int max(int a, int b) {
if (a > b) return a;
else return b;
}

int m = max(x, y);

What is a stack data explain with example in data structure?

One way to emulate a stack with a data structure is with a singly linked list wherein you can only add and remove nodes from the head or tail of the list (depending on implementation). An easy and straightforward application of this type of data structure would be a reverse polish notation calculator where values can be pushed onto the data structure and then popped when one comes across an arithmetic symbol.

What is advantages of recursion in a data structure?

Recursive procedures are huge memory hogs. Also, they're a nightmare to debug. Finally, it's pretty rare to find an application that actually needs recursion as opposed to a simpler, more friendly methodolgy.

What are examples of machine level language?

Machine language is nothing but numeric codes. Because humans have a difficult time remembering numeric codes, manufacturers of microprocessors create mnemonics; these sets of mnemonics are called assembly language. Each processor family has its own set of mnemonics, or assembly language, so assembly for the Intel processor used in many PCs is different from the assembly for a Motorola processor. In fact the assembly for different processors made by the same manufacturer will have assembly that differs, sometimes by a little, sometimes significantly. A quick example of one type of Motorola assembly, to clear 10 32-bit memory locations might look like this:

movea #$6000,ao

move.l #10,d7

10$

clr.l (a0+)

subq.l #1,d7

bne.s 10$

rts

What are the advantages and disadvantages of DLL over single linked list?

The main advantage of a doubly-linked list is that you can traverse and search the list both forwards and backwards. Although you can also add to the beginning and end of the list, and retrieve the same, in constant time O(1), this is also possible with a slightly modified singly-linked list simply by maintaining a pointer to the last node as well as the first.

Thus the only real difference is whether you need to traverse bi-directionally or not. If not, a modified singly-linked list would be more efficient. And if you only require fast access to the first node, a standard singly-linked list would be slightly more efficient.

Convert single linked list to double linked list?

You copy a singly linked list into a doubly linked list by iterating over the singly linked list and, for each element, calling the doubly linked list insert function.

What is file structure in c?

You mean source-file? The simplest format is one single main function:

int main (void)

{ puts ("Hello, World"); return 0; }