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 pointer to function in c?

Accessing data by their address. A good example is parameter argv of function main.

1. Easy access

2.To return more than one value from a function.

3. To pass as arguments to functions. For eg. consider the following structure

struct student

{

char name[10];

int rollno;

};

If you pass this structure object as argument to function then, 14 bytes(10+4) of memory will be passed to the function. Instead, if you pass the pointer to the structure as argument then only 4 bytes (or 8 bytes)of memory will be passed to the function.

Individual digits sum with flowchart and algorithm?

Well, it's very hard to write a flowchart in text, so I'll give you some pseudo code instead.

int number = the given number

int sum = 0

loop while number is not 0

sum = sum + (number mod 10)

number = number / 10

What translates a program written in Assembly language into machine code?

Well, let's say you have the following Assembler statement:

MOV AX, 0005

Each processor (depending on the processor architecture, being some of them: SPARC, Intel 80x86, Motorola...) translates each Assembler mnemonic and register into Machine Code according to an Opcode Table.

Think of an Opcode Table this way:

Instruction OpCode

-----------------------------------

MOV A1

ADD A2

SUB A3

MUL A4

DIV A5

...

AX B0

BX B1

CX B2

...

Each mnemonic/register has its corresponding hex code for the processor to understand the operation, so:

MOV AX, 0005

Could be translated as:

A1 B0 0005

Hopefully this gives you an idea of how a processor assembles code and generates machine code.

How can structures be declared and used in c program?

You declare a structure as follows:

struct name {

typename_1 member_name_1; typename_2 member_name_2;

// additional members...

};

C program for upper triangular matrix for a given matrix?

This sounds very much like a homework problem. If you work on it and get started, you found a great place to ask a specific question. However, this is not a place to have your homework done for you.

What is a loop type?

A Loop is a programming language construct that instructs the processor to repeat a sequence of operations a number of times until a specific condition is reached. There are different types of loops. They are: * for loop * while loop * do while loop

Which header file is used to develop a function that can accept variable number of arguments?

.If you want to accept variable no of arguments then you have to include which of the following header files

a) Vararg.h b) stdarg.h c) stdlib.h d) stdioh

What is perfect number in C language?

Perfect numbers have nothing to do with programming languages. Some of them are: 6, 28, 496, 8128, 33550336.

What is the worst case and average case complexity of bubblesort?

Best case: O(n)

Worst case: O(n2)

Let's assume we're sorting data in an array of length n. Let's also assume that we're sorting in ascending order (low-high).

The worst case is that you will have the smallest value in the last space in the array. This means that it will move exactly once each pass towards the first space in the array. It will take n-1 passes to do this, doing n comparisons on each pass: O(n2)

The best case is that the data comes to us already sorted. Assuming that you have a smart implementation (which you should, because it's easy) which stops itself once a pass makes no changes, then we only need to do n comparisons over a single pass: O(n)

What is an array to show 4X4?

An array of 4 times 8 is a 2D array with 4 Rows and 8 Columns.

// Declaration of a 2D array [ROWS] [COLUMNS]

int [][] arr = new int[4][8];

// What a 2D Array looks like populated with numbers

//

// _____________________

// Row 0 | 1 0 0 0 0 0 0 7

// Row 1 | 0 0 0 0 0 0 0 0

// Row 2 | 0 0 0 0 0 0 4 0

// Row 3 | 0 0 0 0 0 0 0 0

The example array above has the number 1 place in the first row of the first column.

In the first row of the eight column the number 7 is placed.

In the third row of the seventh column the number 4 is placed.

// Accessing these values

int one = arr[0][0];

int seven = arr[0][7];

int four = arr[2][6];

Types of header file?

stdio.h math.h conio.h cstring.h OR string.h - i can't remember ... ctype.h some more i can't remember answer by riya: ALLOC.H ASSERT.H BCD.H BIOS.H COMPLEX.H CONIO.H CTYPE.H DIR.H DIRENT.H DOS.H ERRNO.H FCNTL.H FLOAT.H FSTREAM.H GENERIC.H GRAPHICS.H IO.H IOMANIP.H IOSTREAM.H LIMITS.H LOCALE.H MALLOC.H MATH.H MEM.H PROCESS.H SETJMP.H SHARE.H SIGNAL.H STDARG.H STDDEF.H STDIO.H STDIOSTR.H STDLIB.H STREAM.H STRING.H STRSTREA.H SYS\STAT.H SYS\TIMEB.H SYS\TYPES.H TIME.H VALUES.H

How t print a table in tabular form in two dimensional array in c plus plus?

#include<iostream>

#include<iomanip>

#include<vector>

#include<random>

#include<ctime>

using data_t = std::vector<std::vector<int>>;

size_t get_width (int num)

{

size_t width=1;

while (num/=10)

++width;

return width;

}

std::vector<size_t> get_column_widths (const data_t& data)

{

std::vector<size_t> width;

for (auto row : data)

{

if (width.size() < row.size())

width.resize (row.size());

for (size_t i=0; i<row.size(); ++i)

{

size_t w = get_width (row[i]);

if (width[i]<w)

width[i]=w;

}

}

return width;

}

void print_table (const data_t& data)

{

using std::cout;

using std::endl;

using std::right;

using std::setw;

std::vector<size_t> width = get_column_widths (data);

cout << right;

for (auto row : data)

{

for (size_t i=0; i<row.size(); ++i)

{

cout << setw(width[i]) << row[i] << ' ';

}

cout << endl;

}

}

int main()

{

// Random number generator (range: [1:1000])

std::default_random_engine generator ((unsigned) time (0));

std::uniform_int_distribution<unsigned> distribution (1, 10000);

// Create a 10x5 matrix:

data_t data;

for (size_t row=0; row<10; ++row)

{

data.push_back (std::vector<int>{});

for (size_t col=0; col<5; ++col)

data[row].push_back (distribution (generator));

}

print_table (data);

}

How carefully does the compiler pay attention to indentation?

The compiler does not pay any attention whatsoever to indentation in C and C++.

Calculate no of palindromes in given string palindrome?

A string of length n always has at least n palindromes given that any string of length 1 is a palindrome. Thus we initialise the count to n and test all substrings of length 2 or more:

int count_palindromes (char* str) {

if (!str) return 0;

int n = strlen (str);

char* sub = malloc (n+1); // allocate memory for substring (+1 to include null-terminator)

memset (sub, 0, n+1); // zero the memory int count = n;

for (int len=2; len<=n; ++len) { // length of string (2 to n)

for (int i=0; i<=n-len; ++i) { // index of start character

memcpy (sub, str+i, len); // copy len characters from str+i

if (is_palindrome (sub)) ++count; // test the substring

}

free (sub);

sub = NULL;

return count;

}

Usage:

assert (count_palindromes ("racecar") == 10);

assert (count_palindromes ("abcde") == 5);

Note that counting palindromes in this manner is not generally useful. For every palindrome of length n>2 there has to be at least n+n/2 palindromes within it, and we can easily compute this figure without testing every substring. E.g., the palindrome "racecar" includes the palindromes "racecar", "aceca", "cec", "r", "a", "c", "e", "c", "a" and "r", but the only one we're actually interested in is "racecar" itself.

To achieve this we simply examine those substrings with either 2 or 3 characters. That is, when we find "cec" in the middle of "racecar", there's no need to test for "aceca" or "racecar" because "cec" is common to all three.

int count_palindromes (char* str) {

if (!str) return 0;

int n = strlen (str);

if (n<2) return 0;

char sub[4];

memset (&sub, 0, 4);

int count = n;

for (int len=2; len<=3; ++len) {

for (int i=0; i<n-len; ++i) {

memcpy (sub, str+i, len);

if (is_palindrome (sub)) ++count;

} return count;

}

Usage:

assert (count_palindromes ("racecar") == 1); // "cec"

assert (count_palindromes ("abbabcded") == 3); // "bb", "bab" and "ded"

The is_palindrome() function has the following implementation:

bool is_palindrome (char* str) { int x, y;

if (!str) return false;

int n = strlen (str);

if (n<2) return true; // empty strings and single character strings are always palindromes

x = 0; // point to first character

y = n-1; // point to last character

// work towards middle of string while characters are equal

while (x<y && str[x]==str[y]) ++x, --y;

return x>=y; // if the pointers met or passed one another, the string is a palindrome

}

What is the difference between gets and puts?

Direction:

gets: from standard input to memory

puts: from memory to standard input

note: 'gets' is unsafe, use 'fgets' instead

What does a count-controlled loop mean in programming?

A condition-controlled loop is one that has an indefinite number of iterations; its opposite is the count-controlled loop. Condition-controlled loops execute until some event occurs, which is usually user-initiated. For example, modern programs run an condition-controlled loop similar to the following:

while(GetMessage(message,hwnd,0,0)) { ... }

This loop continues to execute until there are no messages left (the WM_QUIT message is returned, which has a value of zero).

It is impossible to identify before execution the number of times such a loop will run, except during controlled tests, although you can easily identify what conditions will cause it to terminate.

What happens when a program is compiled?

when you compile your program , it is sequentially checking your coding and check whether there is an error. thats the simple thing happening. if there is no error, then it will create an executable file for your coding which is run when you ask to run the program after compiling. if your coding have an error, then you have to correct those errors. REMEMBER that it is compulsory to compile the program after you correct the errors. otherwise, it will not make an executable file with your corrections.

How do you write c plus plus program to insert an element into a binary search tree?

Binary Search is an algorithm that finds an element by successively halving the search space. Typically, pointers are used, one for the beginning of an array, one for the end. Then a midpoint pointer is chosen, and a test is performed. You either find the element, or you discover that the target element is before or after the midpoint. You then adjust either the start pointer or the end pointer and you iterate. When you reach the point where the pointers are out of order, you conclude that the target is not found, and you also know where to insert it. Binary Search is best implemented with an ordered array, because you want to make "random" access to each element. The problem with arrays is that they are typically fixed size, and must be dynamically adjusted when they need to grow, a potentially "expensive" operation. You can also implement a Binary Tree, but there is cost in development and processing. Even trees have issues because, when inserting and deleting elements, you must use a rebalancing algorithm, otherwise the tree might degrade to a linked list, which is not efficient when used as a search space. In C++, it is possible to declare and define a class of elements that you can add to, subtract from, and search. If you do this correctly, you could start with a static or dynamic array, and then upgrade if need be to a binary tree, and then upgrade if need be to a balanced binary tree, all the while without requiring change to the public interface. That is perhaps the most important value of an Object Oriented Language such as C++.

How do you write a program in c to compute the power of a number without using the mathh library?

There is a function which can do it for you. You have to include math.h in headers. And then use the function pow(x, y) which returns a value of type double (x and y are double too).
pow(x, y) = x to the power of y.

Assembly language program to find the largest of series of numbers in 8051?

include irvine32.inc

.data

istno db "enter ist no",0

2nd db "enter second no",0

lagest db "largest no",0

.code

main proc

mov edx,offset istno

call writestring

call readint

call crlf

mov bx,ax

mov edx,offset 2nd

call writestring

call readint

call crlf

jg loop

loop;

mov edx,offset lagest

call writestring

call writeint

main endp

end main

assembly program to find the greatest of between two numbers is as follows:

Program

MVI B, 30H

MVI C, 40H

MOV A, B

CMP C

JZ EQU

JC GRT

OUT PORT1

HLT

EQU: MVI A, 01H

OUT PORT1

HLT

GRT: MOV A, C

OUT PORT1

HLT

Why should one learn C programming language?

C++ is a general purpose, cross platform, object oriented language that evolved from C. We learn it because although there are simpler high-level languages such as Java available, the amount of abstraction involved in these languages is both an advantage and a disadvantage. The advantage is that by separating the source code from the machine, you can write one program that can execute on many different machines, or platforms, without the need to compile separately upon each machine. The disadvantage is that the executable must be interpreted by a virtual machine, which translates the abstract instructions into machine-executable instructions. This results in greatly reduced performance and greater memory requires.

C++ code is just as portable as Java, but it requires a good deal more effort on the part of the programmer. However, the result is code that is comparable to that of assembly language in terms of efficiency, but with sufficient abstraction to produce machine code far more easily than with assembler alone. Although the learning curve is steeper than with Java, it is not as steep as that of assembler, and the skills learned from programming in C++ are highly transferable, largely due to the lower level of abstraction.

C++ is also the most popular language in use today thanks to its high performance and efficiency, and is particularly useful in developing operating system code, real time applications, games and such-like, as well as more general purpose uses. Newer languages such as Java have much in common with C++ (and much that is different), and although Java's popularity has grown, thanks to the mobile market and particularly in non-industrial applications (such as iPhone applets), C++ remains as popular as ever because, without C++, there would far fewer, newer platforms upon which to run Java applications.

Difference between const in C and final in Java?

Static:

we can use the keyword static either to method or to a variable.

when we declare to a method,(eg: public static void main(String args[]),we can use this method without any object.

when we use to a variable,there will be only one instance of that variable irrespective of how many objects that get created of that class.

Final:

Usage of final to method or to a variable makes them as constant. It's value cannot be changed...

Which is the syntax used for passing a structure member as an argument to a function?

You can pass the address by using '&' with the pointer variable, while passing actual arguments. In formal arguments '*' is used in the place of '&'. To pass the address of a pointer variable a double pointer variable should be used .

How to write a program to generate the following pattern abcdefgfedcba abcdef fedcba abcde edcba abcd dcba abc cba ab ba a a?

# include main(){ int n=65,s=1,i,j,k,l; for(i=1;i<=6;i++){ printf("%c ",n); n++;} printf("%c ",n); n--; for(j=1;j<=6;j++){ printf("%c ",n); n--;} printf("\n\n"); for(i=6;i>=1;i--){ n=65; for(j=1;j<=i;j++){ printf("%c ",n); n++;} n--; for(k=1;k<=s;k++){ printf(" ");} for(l=1;l<=i;l++){ printf("%c ",n); n--;} s=s+2; printf("\n\n"); }}

Or:

# include

int main (void)

{ puts ("ABCDEFGFEDCBA ABCDEF FEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA"); return 0; }