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 an algorithm to print the Fibonacci series 0123 up to n terms?

void print_fib (unsigned terms) {

unsigned f1 = 0; // the first term

unsigned f2 = 1; // the second term

while (terms--) {

std::cout<<f1<<std::endl;

unsigned f3 = f1 + f2; // the next term

f1 = f2; // shift the terms for the next iteration

f2 = f3;

}

}

Write program to convert a string in lower case without using library function?

It is not certain if the question asked to convert lower case to upper case, or upper case to lower case. This answer assumes the latter. You could easily change this around for the former.

ConvertToLower (char*psz) {

while (*psz != '\0') {

switch (*psz) {

case 'A': *psz = 'a'; break;

case 'B': *psz = 'b'; break;

case 'C': *psz = 'c'; break;

case 'D': *psz = 'd'; break;

case 'E': *psz = 'e'; break;

case 'F': *psz = 'f'; break;

case 'G': *psz = 'g'; break;

case 'H': *psz = 'h'; break;

case 'I': *psz = 'i'; break;

case 'J': *psz = 'j'; break;

case 'K': *psz = 'k'; break;

case 'L': *psz = 'l'; break;

case 'M': *psz = 'm'; break;

case 'N': *psz = 'n'; break;

case 'O': *psz = 'o'; break;

case 'P': *psz = 'p'; break;

case 'Q': *psz = 'q'; break;

case 'R': *psz = 'r'; break;

case 'S': *psz = 's'; break;

case 'T': *psz = 't'; break;

case 'U': *psz = 'u'; break;

case 'V': *psz = 'v'; break;

case 'W': *psz = 'w'; break;

case 'X': *psz = 'x'; break;

case 'Y': *psz = 'y'; break;

case 'Z': *psz = 'z'; break;

}

psz++;

}

Warning. Do not be tempted to replace the switch statement with ...

if (*psz >= 'A' && *psz <= 'Z') *psz += 32;

... because that will only work on ASCII implementations, and it is most definitely not portable, such as in EBCDIC implementations.

What is the difference between left recursion and right recursion in a grammar?

Recursion is what it's called when a function calls itself. When a function calls itself immediately before returning, it's called tail recursion. Tail recursion can be more efficiently written as iteration. In fact a good compiler will recognize tail recursion and compile it as iteration.

There is no such thing as left or right recursion in C programming.

What does c n c mean?

comment n' critisize constructive criticism command and conquer Computer Numerical Control

What is the role of data structure in compiler design?

The role of the data structure in compiler designer is to take an input of a program written in another language and produce an output in another language. It also performs error detection.

The int data type requires more memory than the double data type?

No, the int variable uses less memory, and therefore it is preferable to use an int rather than a double where you can.

A boolean variable uses even less memory, but obviously is useful only in limited circumstances.

Fortran is a low level language?

FORTRAN is a third generation language.

Note: I don't know what these generations are, but I'm quite sure that Fortran is one of the oldest high-level programming languages, as are Cobol and Algol.

Explain the Difference between bitwise operator ' and ' and address operator ' and ' of pointer?

The bitwise logical operator and (&) calculates the bitwise logical and of two integral values. It is a binary operator.

The address of (&) operator returns the address of the value to its right. It is a unary operator.

The distinction between the two is one of context. The logical and operator will follow (and be preceeded by) a value, while the address of operator will follow an operator.

Write various ways to implement stack data structure?


2. Write a program using switch statement that reads a character representing a geometrical figure, then asks the user to enter the required data (ex. Radius for a circle, length and height for a rectangle, etc. ...) . The program should then print the area and circumference.
Figures are: circle(c), square(s), rectangle(r), triangle (t).

Write and explain recursive backtracking algorithm for n-queens?

This is not a question, this is your homework.

For a start, read this: https://en.wikipedia.org/wiki/Eight_queens_puzzle

How do you convert a binary number to its hex?

Counting from the right, group the binary into groups of 4. If you are left with less than 4 binary digits, then add preceding zeros.

Then recode each quartet as follows:

0000 = 0 0001 = 1 0010 = 2 0011 = 3

0100 = 4 0101 = 5 0110 = 6 0111 = 7

1000 = 8 1001 = 9 1010 = A 1011 = B

1100 = C 1101 = D 1110 = E 1111 = F

Write a c program on blood bank automation system?

Not possible to write one here (too complex). Look for a commercial program to do this.

If a decimal number has 25 digits then how many bits are required for binary representation?

If this is a homework assignment, please consider trying to answer it yourself first, otherwise the value of the reinforcement of the lesson offered by the assignment will be lost on you.

The largest decimal number with 25 digits is 9,999,999,999,999,999,999,999,999.

The smallest decimal number in the form 2n-1 which is greater than or equal to that is 19,342,813,113,834,066,795,298,815. That corresponds to 284-1.

So, the minimum number of binary bits required to represent the decimal number 25 nines in a row is 84. This is 84 ones in a row. If you want to support negative as well as positive numbers, you will need 85.

Since the largest integer in most compilers is 64 bits, this will require a special library supporting 128 bits, or an arbitrary length decimal library, if you want to manipulate such large numbers in a computer and still retain the precision of an integer.

How convert octal to binary?

If necessary, pad the value with zeroes so the number of bits is an exact multiple of 3. Then divide the binary value into groups of 3 bits. Convert each group to its corresponding octal digit as follows:

Bin = Oct

000 = 0

001 = 1

010 = 2

011 = 3

100 = 4

101 = 5

110 = 6

111 = 7

Example 1:

16-bit value: 1011101101100011

3-bit groupings: (00)1 011 101 101 100 011

Octal digits: 1 3 5 4 3

Octal value: 13543

Example 2:

24-bit value: 010111011010010101011010

3-bit groupings: 010 111 011 010 010 101 011 010

Octal digits: 2 7 3 2 2 5 3 2

Octal value: 27322532

Use the Multiple Main method C?

There are no methods in C, and you should have only one main function.
Methods are associated with a specific class, and there are no classes in c.

Write a program that takes 5 numbers 1 to 10 from user and then prints the number of distinct values?

The simplest solution is to use a std::set<size_t> sequence container to store the values as they are input. Duplicate entries are ignored automatically, thus when all 5 numbers have been input, the set will have at least 1 number but no more than 5. Thus the size of the set represents the count of distinct values that were input.

Why do you have type data gram socket?

A datagram socket represents a connectionless, non-guaranteed communication protocol. It represents the UDP protocol in the TCP/IP suite.

Contrast this with a stream socket, which represents a connection oriented, guaranteed, communication protocol. It represents the TCP protocol in the TCP/IP suite.

UDP is the underlying protocol for TCP. If you are talking OSI model, UDP is layer 3, and TCP is layer 4.