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

Why the clrscr is used after declaration?

Use the help: this function clears the screen.

Note: non-standard Borland-specific function, don't use.

What is the difference between C programming and CPP programming?

C has no object oriented support. C++ is a superset of C that adds object-oriented support, but retains the concept of primitive variables. C# is a superset of C++, developed by Microsoft, that removes the concept of primitives, making it fully object-oriented but, unlike C and C++, is non-generic and is only useful for .NET programming. C# is not unlike Java, but Java is fully cross-platform and therefore has much wider support.

C program to find the given number is Adam number?

  1. /* To check whether a number is Adam number or not */
  2. #include
  3. #include
  4. void main()
  5. {
  6. int i,n,n1,n2,ns,r,rev=0,nr=0,nrs;
  7. clrscr();
  8. printf("Enter a no.n");
  9. scanf("%d",&n);
  10. ns=n;
  11. n1=n*n;
  12. n2=n1;
  13. while(n2>0)
  14. {
  15. r=n2%10;
  16. n2=n2/10;
  17. rev=rev*10+r;
  18. }
  19. while(ns>0)
  20. {
  21. r=ns%10;
  22. ns=ns/10;
  23. nr=nr*10+r;
  24. }
  25. nrs=nr*nr;
  26. if (rev==nrs)
  27. printf("%d is an Adam no.n",n);
  28. else
  29. printf("%d is not an Adam no.n",n);
  30. getch();
  31. }
  32. }

Is the instructions programmers have written in a higher level language?

"BASIC" is not a program, it is a programming language. A particular BASIC-interpreter or IDE may have been written in a high level language, maybe even in BASIC (C is more plausible though).

Examples of custom written program?

a custom written program that acts as our "intranet".This tool has a range of templates, letters and forms that we use in our everyday work both for our clients and for day-to-day tasks.

Write a program which will prompt the user to enter 10 int values each in the range 0 to 9 inclusive?

I can't give you the entire code since I don't know what programming language you are using. Here is a hint:

In the C language, you can simply use a loop statement that repeats the scanf() function

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.