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 to fill the enter screen with a smiling face the smiling face has an ASCII value 1?

#include<stdio.h>

#include<conio.h>

void main()

{

int a=1;

clrscr();

for(a=1;a<=47*(80);a++)

{

printf("%c",1);

}

getch();

}

What is a double ended queue?

A double ended queue, or deque, is a queue in which you can access or modify both the head and the tail. The front pointer can be used for insertion (apart from its usual operation i.e. deletion) and rear pointer can be used for deletion (apart from its usual operation i.e. insertion)

What is the difference between auto storage class and register storage class?

register behaves like auto class but auto variables are actually local variables.They are created when the functions are created but they are destroyed when the fns is exited.But register-variables are used to speed up the operations

Differences between scope and visibility of a variable?

Scope of a variable is the lines of code from which it can be seen and/or manipulated. Scope can be thought of as visibility, because a variable's scope is where it is visible from.

A variable declared within a function (or any block, for that matter) has scope only within that block. It is visible within that block, and from within any contained block, but not from within any containing (outer) blocks. However, it should be noted that if a variable's name is reused in a nested declaration, then the outer variable loses scope within that block. As a result, you can reuse, for instance, the variable "i" within a new block without compromising any use outside that block.

What are advantages and disadvantages of linear search?

1)in linear search it needs more space and time complexity.

2) in linear search if the key element is the last element and the search is from first element that is a worst case, or if the key element is the first element and the search is from last element then also is the worst case.

What are the applications of circular queue?

Priority queues can be found in operating systems for load-balancing and interrupt handling, network servers for bandwidth management, compression algorithms (such as Huffman encoding), Dijkstra's algorithm, Prim's algorithm and artificial intelligence systems.

C program for tribonacci series?

#include<iostream>

int main()

{

int x=0, y=1;

std::cout<<x<<" ";

std::cout<<y<<" ";

while( y<1000000 )

{

std::cout<<(y+=x)<<" ";

x=y-x;

}

std::cout<<std::endl;

return(0);

}

What is the function of data-compression utility?

The function of a data compression utility is to reduce the file size and thus maximise storage capacity. The compression has to be done in such a way that the original data can be restored by a corresponding decompression utility.

The simplest way to compress data is to use a variable-length encoding rather than a fixed-length encoding. Normally, data is stored using a sequence of symbols with fixed-width codes (typically 8-bit bytes). However, some symbols appear more frequently than others, so by using fewer bits to represent the most frequent symbols and more bits for the less frequent ones, data can be stored much more efficiently.

To achieve this, no two symbols can have the same prefix. This is, if the space character (which is the most common symbol in text data) is represented by the 1-bit code 0, then no other symbol can be prefixed with a 0 -- they must all be prefixed with a 1. The letter 'e' is also quite common, thus we might give it the 2-bit prefix 10, which means all other symbols must have a 11 prefix. The problem with this encoding scheme is that with each new symbol we have to add another bit. If our data contains 127 unique symbols (the entire ANSI character set), then the least-frequent symbol would consume 127 bits! Although we may still be able to reduce the overall size of the data, this is not the most efficient method of doing so. We could achieve very similar savings just by using a 7-bit byte instead (which is sufficient to encode the entire ANSI character set).

Prior to 1951 there was no algorithm capable of producing the most efficient prefix encoding. We knew that binary trees offered the solution, where leaf nodes represented the symbols we wished to encode and the path from the root node of the tree determined the prefix (appending a 0 bit when we branched left and a 1 when we branched right). Thus every symbol had a unique path and therefore a unique prefix. However, building such a tree was a complex process and there was no guarantee the end result would produce the most efficient encoding without a good deal of trial and error.

David A. Huffman came up with the solution in 1951. Instead of building the tree from the root, he built the tree from the bottom up. First, he created a frequency table of all the unique symbols in the data, sorted in descending order of frequency. He then extracted the bottom entry (the least frequent symbol) and attached it to the left node of a parent node. He then took the next least frequent symbol and attached it to the right. The parent's frequency became the sum of its two nodes and it was re-inserted into the table in sorted order. In other words, he removed two entries and inserted one, thus reducing the table by one entry overall. He then repeated this process continually until there was only one entry left. That one entry became the root of the binary tree. As simple as it sounds, this algorithm produces the most efficient prefix encoding, all we have to do is branch left (append a 0) or branch right (append a 1) and when we reach a symbol (which is always a leaf) we have the prefix for that symbol. We can then rescan the original data and output a contiguous sequence of prefixes to create the payload.

In order to decompress Huffman coded data, we must also be able to recreate the original tree that was used to compress it. Since there are only two types of node (parent or leaf) and every parent has two nodes and every leaf holds a symbol, we simply need to traverse the tree in depth-first order from the root. This is a recursive operation where we examine the left node followed by the right node. If we examine the left node and find it is a leaf, we output a 1 bit followed by its 8-bit symbol. But if the left node is a parent, we output a 0, traverse to it, and repeat the process with that node. Eventually we get back to the root where we can examine its right node. Again, if it is a leaf we output a 1 followed by its symbol, otherwise we output a 0 and traverse to it. Eventually we arrive back at the root and the entire tree has been encoded.

The only other piece of information we need is the number of symbols in the payload (equal to the size of the original data in bytes). Once we have all three we can build the compressed data, outputting the size, followed by the encoded tree, followed by the payload. If the final size is not a multiple of 8 bits, we simply pad the remaining bits with zeroes.

To decompress, we read back the size and store it. We then create a parent node for the root of the tree and begin reading the encoded tree one bit at a time. If the bit is a 1, we create a leaf node on the left and read the next 8 bits to determine its symbol, otherwise we create a parent on the left, traverse to it. Eventually we arrive back at the root and can process its right node in the same way. Once that's done we'll have rebuilt the tree.

Then we can process the payload, starting at the root. If we read a 0, we traverse left, otherwise we traverse right. If the node we traverse to is a leaf, we output the symbol, otherwise we read the next bit and traverse left or right until we do reach a symbol. Once we output a symbol, we increment a counter and start again from the root. When the counter is equal to the size we read at the start, we can stop processing; any remaining bits are just padding.

The algorithm is elegant and, even with the added weight of the encoded tree, can produce significant savings in space for any moderate sized file. For example, the Project Gutenberg Etext version of Moby Dick comes in at 1,256,165 bytes, but is only 720,043 bytes when compressed with Huffman encoding (57% of the original). Huffman coding can be found in more complex algorithms which can compress data even further. For instance, RAR (Roshal Archive, by Eugene Roshal) can compress the same file down to just 443,371 bytes (35%)!

Which pair of loops causes a statement or set of statements to repeat as long as a condition is true?

A while loop repeats until the condition becomes false, and may never execute:

int a = 4;

while (a > 5)

{

//Do something

}

or

int a = 4;

while (a > 0)

{

//Do something

a--;

}

Example of an array?

Here is a statical array:

int arr[50]; /* 50 integers in the array */

/* setting some values */

arr[0] = 1;

arr[1] = 2;

...

int matrix[10][10]; /* we have a matrix 10 x 10 of integers */

/* setting some values */

matrix[0][0] = 1;

matrix[1][1] = 2;

matrix[3][5] = 3;

...

You can initialize array at the time of creation:

int arr[3] = { 0, 1, 3 };

/* or could do like this */

int arr[] = { 0, 1, 2, 3, 4, 5 };

int matrix[2][4] = { 0, 0, 0, 01, 1, 1, 1};

/* is the same as: */

int matrix[2][4] = { { 0, 0, 0, 0 }, { 1, 1, 1, 1} };

C Arrays are no limited only to one and two dimensional arrays, you can define N-dimensional arrays too.

int cube[3][3][3]; /* here we have a cube 3 x 3 x 3*/

There are dynamic arrays too, but that requires knowledge of memory management and pointers. There are some links that will explain arrays in C more detailed.

What is Switch Case Statement?

The switch statement immediately passes control to a case label within its body, dependant upon the evaluation of an expression. This allows us to control complex conditional and branching operations.

Each case label contains a constant expression, which must match one of the possible evaluations of the switch expression. No two case labels may hold the same constant. An optional "catch-all" label, the default label, may also be included but it must follow all other case labels.

When control passes to a case label, execution continues from that point on until the end of the switch body unless a break statement is encountered before the end of the switch body. A break statement forces execution to pass to the statement immediately following the switch body. Return and goto statements can be used in place of a break statement where required. Switch statements can also be nested if required.

Although some switch statements can be implemented as a series of if..else if.. statements, switch statements are more efficient as there's only one evaluation required rather than a separate evaluation for each if statement. Moreover, case labels allow greater control over the flow of execution.

As a simple example, suppose we wish to count the number of characters in a string along with the number of times a specific letter is used including how many of them are capitals. One way of doing this is to traverse the string and increment counters using a switch statement:

char str[]="This is a test string";

int chars=0, ts=0, capts=0;

for( int i = 0; i<sizeof(str); ++i )

{

switch( str[i] )

{

case( 'T' ): ++capts;

case( 't' ): ++ts;

default: ++chars;

}

}

printf( "String: "%s"\n", str );

printf( "The string has %d character%s in total\n", chars, chars>1?"s":"" );

printf( "There %s %d letter 't'%s in total\n", ts>1?"are":"is", ts, ts>1?"s":"" );

printf( "There %s %d capital 't'%s in total\n", capts>1?"are":"is", capts, capts>1?"s":"" );

Output:

String: "This is a test string"

The string has 22 characters in total

There are 4 letter 't's in total

There is 1 capital 't' in total

If we wanted separate counts of upper and lower case letters, then we can simply change the switch statement to suit:

switch( str[i] )

{

case( 'T' ): ++capts; break;

case( 't' ): ++ts;

}

++chars;

If were to reproduce the original switch statement using if..else if.. we'd need to use the following instead:

if( str[i] 't' )

++ts;

++chars;

Although this looks fine, we are forced to evaluate str[i] twice if it is not equal to 'T'. In the switch statement, we only need to evaluate str[i] once. If..else if.. should only be used when evaluating completely different expressions. If you're evaluating the same value or need more control over the execution path, then switch is much more efficient.

Note that in complex switch statements, the order of the case labels is important if execution is allowed to flow from once case label to the next. Case labels only indicate where execution will begin. After that they are ignored (just as goto labels are ignored). You must use break, return or goto statements to control where execution ends, otherwise execution will continue through all the following cases. Sometimes that is desirable (as shown in the example above), but often it is not. In most cases, the last statement of a case will be break statement.

Can you use doubly linked list as a circular linked?

Write a program that inputs a series of integers and passes them one at a time to a function even which uses the remainder operator to determine if an integer is even. The function should take an integer argument and return 1 if the integer is even and 0 otherwise.

What is File manager function?

A file manager is just a program that allows you to keep track of all files on your drive . Some will allow you to check drive space and other drive related stuff. It lets you search your drive for files by size ,name , type etc. There are many other features built into some managers.

What are the disadvantage of array implementation of stack?

Some immediate disadvantages:

  1. You'll have dead space in the array (entries which aren't currently used for items) taking up memory
  2. You'll have to keep track of the free entries - after a few insertions and deletions, these free entries could be anywhere.
  3. Using an array will impose an upper limit on the size of the linked list.

Write a c program To count number of words in a given string?

#include<stdio.h>

#include<string.h>

void main()

{

int count,i,len;

char str[100];

printf("enter the sentence");

gets(str);

len=strlen(str);

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

{

while(str)

if(str[i]==' ') count++;

}

printf("the number of words are :\t%d",count+1);

}

Program for print reverse string of given number in java?

public class StringReverseExample

{

public static void main(String[] args)

{

int num=1001;

int n=num, rev;

while(num!=0)

{

int d=num%10;

rev= (rev*10)+d;

num=num/10;

}

System.uot.println(rev);

}

}

How do you make a program in Visual studio To know the number whether its Even or Odd?

using system;

{

public static void main(string[] args)

{

int n;

n=int.parse(console.ReadLine());

if(n%2==0)

console.WriteLine("Even number");

else

console.WriteLine("Odd number");

}

}

When should a switch statement be used?

switch is a loop which is used for checking various conditions and print corresponding matter.

switch(a)//where a is that whose condition you have to check.

#include

main()

{

char a;

printf("enter a char.");

scanf("%c",&a);

switch(a)

{

case 'a':printf("vowel");break;

case 'e':printf("vowel");break;

case 'i':printf("vowel");break;

case 'o':printf("vowel");break;

case 'u':printf("vowel");break;

default:printf("consonent");

}

}

What is a disadvantage of a compiler?

This question cannot be answered in general (cf: what are the disadvantages of fruits), only two (or more) specific compiler can be compared, for example: 'gcc is cheaper than xlc, but supports more platform'

Who developed C programming language?

C language was developed by an American computer programmer Dennis Rithie and kerningham

From jp dhillon moga

c language was developed by an American computer programmer Dennis ritche & kerningham bell laborteries united state of America in 1972....

How do you compile and run C programs on the Macintosh?

The first step is to install the XCode developer tools that comes with OSX. You can do this by looking on your OSX install disc. There should be a folder for optional installs, and a package for XCode. This will automatically install gcc, a C compiler, on your machine. Next, find the Console in the Utilities folder. It opens a UNIX shell. Move to the directory that your c file is in (you can find more information about UNIX elsewhere if you need to). Let's say your C file is named myfile.c. To compile it, type: gcc myfile.c This will compile your program, and name the compiled program a.out. To run it, type: ./a.out I hope that helps. Brina

How do you write a program to scan a number n and then output the sum of the powers from 1 to n using for loop statement?

#includ<iostream.h>

int main ()

{

int n,n-1;

for(n=0);

cout<<"the number is even:"<<endl;

if(n=n-1)

cout<<"the number is prime:"<<endl;

getch 0;

return 0;

}

How do you write a functions using pointers to multiply two matrices and return it to the calling function?

void main()

{

int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;

printf("Enter The Rows And Cloumns And Of The First Matrix:");

scanf("%d %d",&m,&n);

printf("\nEnter The Rows And Cloumns And Of The Second Matrix:");

scanf("%d %d",&p,&q);

printf("\nEnter Elements Of The First Matrix:\n");

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

{

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

{

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

}

}

printf("\nEnter Elements Of The Second Matrix:\n");

for(i=0;i< p;i++) {

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

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

}

printf("The First Matrix Is:\n"); /* Print the first matrix */

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

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

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

printf("\n");

}

printf("The Second Matrix Is:\n"); /* Print the second matrix */

for(i=0;i< p;i++) {

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

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

printf("\n");

}

if(n!=p) {

printf("Aborting./nMultiplication Of The Above Matrices Not Possible.");

exit(0);

}

else {

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

for(j=0;j< q;j++) {

c[i][j] = 0;

for(k=0;k< n;k++) {

c[i][j] = c[i][j] + a[i][k] * b[k][j];

}

}

}

printf("\nThe Product Of The Two Matrices Is:\n\n");

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

for(j=0;j< q;j++) {

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

}

printf("\n");

}

}

return 0;

}

-----------------------------------------------------">-----------------------------------------------------

The above code is not the solution of Matrix multiplication using pointers.

the following code is absolutely correct.

void main()

{int *a,*b,*c,row1,col1,row2,col2;

clrscr();

printf("enter rows of 1at matrix");

scanf("%d",&row1);

printf("enter columns of 1at matrix");

scanf("%d",&col1);

printf("enter rows of 2nd matrix");

scanf("%d",&row2);

printf("enter columns of 2nd matrix");

scanf("%d",&col2);

if(col1!=row2)

{

printf("\nsorry\n");

}

else

{

a = (int *) malloc(row1*col1 * 2);

b = (int *) malloc(row2*col2 * 2);

c = (int *) malloc(col1*row2 * 2);

clrscr();

printf("enter 1st matrix \n");

insert(a,row1,col1);

getch();

clrscr();

printf("enter 2st matrix \n");

insert(b,row2,col2);

getch();

clrscr();

printf("1st matrix is\n");

display(a,row1,col1);

printf("\n2nd matrix is\n");

display(b,row2,col2);

prod(a,b,c,row1,col2);

printf("\nafter multiplication \n\n\n");

display(c,row1,col2);

}

getch();

}

insert(int *q,int r,int c)

{int i,j;

for(i=0;i

{

for(j=0;j

{ printf("\nEnter [%d][%d] element-- ",i,j);

scanf("%d",(q+i*c+j));

}

}

}

display(int *q,int r,int c)

{int i,j;

for(i=0;i

{

printf("\n");

for(j=0;j

{

printf("%d\t",*(q+i*c+j));

}

}

}

prod(int *p,int *q,int *z,int r1,int c2)

{int i,j,k;

for(i=0;i

{

for(j=0;j

{

*(z+i*c2+j)=0;

for(k=0;k

{

*(z+i*c2+j)+=*(p+k*2+j)*(*(q+i*c2+k));

}

}

}

}

//Designed by Asjad Farrukh.......

Write an assembly language programin 8085 to find a square of a given number?

To find the area of a square, the person can simply multiply length times width, which would be two sides of the square. To find area of a circle, the person must use the equation ((pie)(r))(squared).

Can static variables be declared in header file?

Can static variables be declared in a header file?
You can't declare a static variable without defining it as well (this is because the storage class modifiers
static and extern are mutually exclusive). A static variable can be defined in a header file, but this would cause each source file that included the header