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

How does the assembly language can be executed by CPU?

Assembly language programs are the Low level programs. We write Assembly Language program in basically 8085 and 8086 microprocessors.

We can have several registers to do opreations with. Accumulator is one most important Register in a assembly program.

We use several instructions like..

Arithmetic:

INR - Increment Accumulator

ADD B - Add Content of Reg. B with Accumulator

SUB, etc.

Logical:

AND - Bitwise AND

Jump Instriction:

JZ label - Jump to label if ZERO flagged

JC Label - Jump on Carry

Etc..

Example:

MVI B, 06 //Load Register B with the Hex value 06

MOV A, B //Move the value in B to the Accumulator or register A

MVI C, 07 //Load the Register C with the second number 07

ADD C //Add the content of the Accumulator to the Register C

STA 8200 //Store the output at a memory location e.g. 8200

HLT //Stop the program execution

What is the c program to sort the n elements in the array in ascending order?

The following example repeatedly asks for a number. As each number is entered, the array (arr) is dynamically resized to accommodate the new number. To finish entering numbers, enter any non-number (such as the letter X). If any numbers were entered, the array is then sorted using a simple bubble sort and the result is then displayed.

Note that resizing an array like this is highly inefficient because the array must be copied each time it is resized. If you can determine how many numbers will be entered in advance (at runtime), then the array can be allocated just once, before reading the numbers into each array element.

#include <iostream>

int main()

{

float t, num = 0, *arr = NULL;

int max = 0, i, j;

while( 1 )

{

printf( "\nEnter a number:\n(Any letter to quit)\n>" );

int iResult = scanf( "%f", &num );

if( !iResult )

break;

arr = ( float * ) realloc( arr, ++max * sizeof( float ));

arr[ max-1 ] = num;

}

printf( "%d numbers entered.\n", max );

if( max )

{

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

{

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

{

if( arr[j] > arr[j+1] )

{

t = arr[j];

arr[j] = arr[j+1];

arr[j+1] = t;

}

}

}

printf( "Sorted:\n");

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

printf( "%f\n", arr[i] );

printf( "\n" );

free( arr );

arr = NULL;

}

return( 0 );

}

Write a c program to find positive and negative count?

  1. # include
  2. # include
  3. void main()
  4. {
  5. int a[20], i, n ;
  6. clrscr() ;
  7. printf("Enter the limit : ") ;
  8. scanf("%d", &n) ;
  9. printf("Enter the elements") ;
  10. for(i = 0 ; i < =n ; i++)
  11. scanf("%d", &a[i]) ;
  12. printf("The positive elements are") ;
  13. printf("the negative elements are);
  14. for(i = 0 ; i < n ; i++)
  15. {
  16. if(a[i] > 0)
  17. printf("%d", a[i]) ;
  18. if(a[i]<0
  19. printf("%d",a[i]);
  20. }
  21. getch() ;
  22. }

Advantage and disadvantage of for loop over while looop?

Any for loop is equivalent to some while loop, so the language doesn't get any additional power by having the for statement. For certain type of problem, a for loop can be easier to construct and easier to read than the corresponding while loop. The for statement makes a common type of while loop easier to write. It is a very good (perhaps the best) choice for counting loops.

What is the difference between Built in Function and User Defined Function?

Built-in functions are functions that are provided for you by the standard includes. User-defined functions are those that you write yourself. Third-party functions are those that are written for you, but that are not provided by the standard includes.

What are the drawbacks of assembly level language?

The assembly language does not support object oriented program

so they change to c and c++ the c++ will support object oriented program

this are the demerits of assembly language.

How can you increase the size of a statically allocated array?

/* Allocate space for an array with ten elements of type int. */

int *ptr = malloc(10 * sizeof (int));

if (ptr == NULL) { /* Memory could not be allocated, the program should handle the error here as appropriate. */ realloc It is often useful to be able to grow or shrink a block of memory. This can be done using realloc which returns a pointer to a memory region of the specified size, which contains the same data as the old region pointed to by ptr (truncated to the minimum of the old and new sizes). If realloc is unable to resize the memory region in-place, it allocates new storage, copies the required data, and frees the old pointer. If this allocation fails, realloc maintains the original pointer unaltered, and returns the null pointer value. The newly allocated region of memory is uninitialized (its contents are not predictable). The function prototype is void *realloc(void *pointer, size_t size);

Write a program to add two integers using c plus plus?

#include<iostream>

int main()

{

int a=40;

int b=2;

std::cout<<a<<'+'<<b<<'='<<a+b<<std::endl;

}

Output:

40+2=42

What is the most basic loop in c?

Answer

the answer to this question would vary from programmer to programmer... Basically for the embedded system programmer the while loop is more important and would be considered as basic. However for the application programmer the for loop is always a better option.

Answer

These are: LABEL-goto, while, for, do-while, and recursion, of course.

How do you write a C program to add 2 matrices using functions?

We'll assume the matrix elements are doubles, but we can easily adapt the code to cater for any numeric data type.

First we need a (primitive) function that emulates the += operator for two arrays of doubles:

double* add_assign_array (double* a, double* b, size_t sz) {

for (size_t i=0; i<sz; ++i) a[i] += b[i];

return a;

}

Note that we are wholly reliant upon the caller to ensure all arguments are valid. We could test for null pointer arguments, however there's no advantage in doing so when we cannot even guarantee that a and b actually refer to at least sz elements. For efficiency it's better if the caller handles any and all necessary runtime tests and thus keep those tests to a minimum.

With this function in place we can now add two matrices, row by row:

double* add_assign_matrix (double* a, double* b, size_t rows, size_t cols) {

size_t i;

for (size_t row=0; row<rows; ++row) {

i = row * cols;

add_assign_array (a[i], b[i], cols);

}

return a;

}

Example usage:

// Utility functions: void print_array (double*, size_t);

void print_matrix (double*, size_t, size_t);

int main (void) {

const size_t rows = 3;

const size_t cols = 4;

double a[rows][cols] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

double b[rows][cols] = {{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}};

printf ("Matrix a:\n");

print_matrix (a, rows, cols);

printf ("Matrix b:\n");

print_matrix (b, rows, cols);

printf ("Matrix a+=b:\n");

add_assign_matrix (a, b, rows, cols);

print_matrix (a, rows, cols);

return 0;

}

void print_array (double* a, size_t sz) {

for (size_t i=0; i<sz; ++i) printf ("%f\t") a[i];

printf ("\n");

}

void print_matrix (double* a, size_t rows, size_t cols) {

for (size_t row=0; row<rows; ++row) print_array (a[row * cols], cols);

}

Note that the add_assign function emulates a += b rather than c = a + b. However, we can easily emulate this by copying one of the matrices and then calling add_assign upon the copy:

// e.g., c = a + b;

double c[rows][cols]; // uninitialised matrix

memcpy (c, a, rows * cols * sizeof (double)); // c is a copy of a

add_assign_matrix (c, b, rows, cols); // c += b

It's far from intuitive but arrays and matrices are anything but intuitive in C programming.

Which loop does not have an entry condition in C plus plus?

The only loop that does not require an entry condition is the procedural goto loop:

again:

/*...*/

goto again;

Although a do-while loop has no entry condition per-se, it still requires a mandatory entry condition into the second and all subsequent iterations.

do { /*...*/} while (true); // mandatory entry condition into all but the 1st iteration

And although a for loop's condition is optional, it is implicit:

for (;;) {/*..*/} // implicit for ever loop

for (;true;) {/*...*/} // explicit for ever loop

Write a program to find the square root of the quadratic equation using flow chart?

You don't need a flow chart for that; just use the quadratic formula directly; most programming languages have a square root function or method. You would only need to do this in many small steps if you use Assembly programming. The formulae would be something like this:

x1 = (-b + sqrt(b^2 - 4*a*c)) / (2 * a)

and

x2 = (-b - sqrt(b^2 - 4*a*c)) / (2 * a)

where a, b, and c are the coefficients of the quadratic equation in standard form, and x1 and x2 are the solutions you want.

What are string literals?

A string literal is a sequence of characters, including backslash-escaped codes, between two double-quote characters. These are of type const char*, such that the character values are immutable (cannot be changed after creation). "Hello my little minions" is a string constant, for instance.

Note that under C you can pass const char* types as char* in function parameters, or assign values between constant and non-constant types, but C++ will issue warnings if you attempt to do as such (unless those warnings have been disabled, which may not be recommended).

C program for regular expression to nfa?

Conversion of a Regular Expression to NFA Algorithm Source code C programmingCS342 Compiler Lab Source code Algorithm C Programming

#include

#include

void main()

{

char reg[20];

int q[20][3],i,j,len,a,b;

clrscr();

for(a=0;a<20;a++)

{

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

{

q[a][b]=0;

}

}

printf("Regular expression: \n");

scanf("%s",reg);

len=strlen(reg);

i=0;

j=1;

while(i

{

if(reg[i]=='a'&®[i+1]!='/'&®[i+1]!='*')

{

q[j][0]=j+1;

j++;

}

if(reg[i]=='b'&®[i+1]!='/'&®[i+1]!='*')

{

q[j][1]=j+1;

j++;

}

if(reg[i]=='e'&®[i+1]!='/'&®[i+1]!='*')

{

q[j][2]=j+1;

j++;

}

if(reg[i]=='a'&®[i+1]=='/'&®[i+2]=='b')

{

q[j][2]=((j+1)*10)+(j+3);

j++;

q[j][0]=j+1;

j++;

q[j][2]=j+3;

j++;

q[j][1]=j+1;

j++;

q[j][2]=j+1;

j++;

i=i+2;

}

if(reg[i]=='b'&®[i+1]=='/'&®[i+2]=='a')

{

q[j][2]=((j+1)*10)+(j+3);

j++;

q[j][1]=j+1;

j++;

q[j][2]=j+3;

j++;

q[j][0]=j+1;

j++;

q[j][2]=j+1;

j++;

i=i+2;

}

if(reg[i]=='a'&®[i+1]=='*')

{

q[j][2]=((j+1)*10)+(j+3);

j++;

q[j][0]=j+1;

j++;

q[j][2]=((j+1)*10)+(j-1);

j++;

}

if(reg[i]=='b'&®[i+1]=='*')

{

q[j][2]=((j+1)*10)+(j+3);

j++;

q[j][1]=j+1;

j++;

q[j][2]=((j+1)*10)+(j-1);

j++;

}

if(reg[i]==')'&®[i+1]=='*')

{

q[0][2]=((j+1)*10)+1;

q[j][2]=((j+1)*10)+1;

j++;

}

i++;

}

printf("Transition function \n");

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

{

if(q[i][0]!=0)

printf("\n q[%d,a]-->%d",i,q[i][0]);

if(q[i][1]!=0)

printf("\n q[%d,b]-->%d",i,q[i][1]);

if(q[i][2]!=0)

{

if(q[i][2]<10)

printf("\n q[%d,e]-->%d",i,q[i][2]);

else

printf("\n q[%d,e]-->%d & %d",i,q[i][2]/10,q[i][2]%10);

}

}

getch();

}

How do you write a program for counting and displaying occurrence of a number in an array?

Your first step is accepting input, which is done using the scanf() function:

scanf("%d", &number);

This means that you want scanf() to accept input, convert the input to a number, and store it in the memory at the address of number.

Use a for() loop, counting from 0 to 9, and an array of integers to hold the numbers. Then simply scanf("%d", &intarray[counter]);

The next step is a little tricky, but not very if you plan it out in advance.

Each integer can contain 256, 65,536 or 4,294,967,296 different numbers. Creating an array to hold the count of each of those numbers is a waste of RAM.

Instead, you'll want an "associative" array as follows:

int numcount[MAXNUM][2];

MAXNUM is 10, or the number of integers in the array you're checking. The second dimension, 2, consists of the number and its count.

Obviously, you'll want a way to keep track of how many integers you've stored in numcount. An int called numcountnuminitialized to 0 would be the fastest way.

Use a for() loop to iterate through the integers. If the integer does not exist in numcount, then set numcount[numcountnum][0] to the integer, set numcount[numcountnum][1] to 1, and increment numcountnum. Otherwise, if the integer exists, increase numcount[the integer index][1].

Once the for() loop is finished, display the results. The only thing you have left to figure out is the function that searches the numcount array for an integer, and returns its index (or -1 if it's not found).

What is the purpose of the Milestone C decision?

Approval at Milestone C is dependent on an approved CPD, compliance with the DoD strategic plan and demonstration that the system is affordable throughout the life cycle. The MDA also approves the updated acquisiton strategy and the acquisition decision memorandum at Milestone C.

How do you use qsort function to sort an array of structures?

It depends what language you are using and how the function is implemented. However, generally, you need to pass 4 arguments to the function:

1. A reference to the array.

2. The lower bound of the sub-array to be sorted (usually 0).

3. The upper bound of the sub-array to be sorted (usually n-1 for an array of n elements).

4. A binary predicate to perform comparisons between the elements (usually a less-than predicate).

How do you you delete a node in singly linked list only pointer to that node is given in C?

Firstly, thanks for trying solve this algorithm.

I will rewrite the wrong way and debug it in the next line, and the causes in th last line , O right?


wrong:void delete(int n).
debug:void delete_node(node* list, int n).
causes:
-You have to include the name of the list from which you want to delete a node. -you used delete as a name of the function, and that is impossible because delete is a predefined function, use delete_node for example.




wrong:you didn't affect the address of the list to the temporary pointer named here P.
debug:p=list.
causes:
-one of the disadvantages of the chained list -unlike contiguous list-is: to arrive to the the required node, we have to traverse all previous nodes to get the required one, therefor we use that temporary pointer, and this latter mast have to start from the first node names here list.


the rest is right, try this code, I think it works::


void delete_node(node *list, int n)
{
node *p;
p=list;
int i=1;
while(i
{
p=p->next;
++i;
}
node *q;
q=p->next;
p->next=q->next;
free(q);
}



Best regards of InfoMan.






C program to swap three variables?

#include<stdio.h>

#include<conio.h>

void main()

{

int a=2,b=3;

swap(a,b);

printf("%d%d",a,b);

getch()

}

swap(int *x,int *y)

{

int t;

t=*x;

*x=*y;

*y=t;

printf("%d%d",x,y);

}

How do you print 1 121 1331 14641 in c?

#include


#include


void main()


{


clrscr();


for (int i=1;i<=5;i++) // this for loop controls the line


{


for(int

j=1;j<=i;j++) // this for loop controls the values(1,12,123..)


{


printf("%d",j);


}


printf("\n");


}



getch();


}


What are the 2 high level programming language?

BASIC , FORTRAN ,COBOL , PASCAL are high level lanhuages which are simply understood by us used in the computers.

What is the function of variable frequency drive?

The function of a frequency drive is to control the speed of an electric motor.

In general, a frequency drive converts the ac supply voltage into a dc voltage and then converts this dc voltage into a ac voltage of which the amplitude (voltage) and the frequency can be varied. Giving you the possibility to fully control the speed of the motor.

Applications: (big) ventilators, pumps, cranes, elevators and virtually all other applications where electric motors are used.

What is a simplex data communication mode?

One way data communication.

http://en.wikipedia.org/wiki/Simplex_communication

Parts of the program C plus plus?

parts of a program

Structure of C++ program

Documentation Section

Preprocessor Section

Definition Section

Global Declaration Section

main()

{

Declaration part;

Executable part;

}

sub program section

{

Sub program execution part

}