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 are generalised linked list?

A generalized linked list contains structures or elements with every one containing its own pointer. It's generalized if the list can have any deletions, insertions, and similar inserted effectively into it.

How do you overcome the disadvantages of arrays?

There are many limitations of arrays. The greatest advantage of an array of course is performance. Arrays are by far the fastest means of referencing collection of items.

That said, arrays suffer incredible limitations since they are generally very slow for adding items in a sorted manor. Unless you preallocate a large enough area of memory to store all possible items you wish to use, arrays have to be reallocated over and over. There are some hackish functions which can be used to exploit the MMU of the system to grow arrays by creating multiple logical contiguous regions of memory, but if this is performed on a fine grain, the logical descriptor tables of the system will grow and overall system performance will be negatively impacted.

Even though you appear to want to program plain-C, there is no reason you can't develop in an object oriented manor. There are many libraries available for C which provide high-performance variable length collections. I am not familiar with all of them, but as part of the GNOME toolkit, there is GLib (if I recall) which provides excellent implementations of many of them. Alternatively, if you're interested in learning how they work in order to correctly choose the right one for each task, you may want to get a copy of Donald Knuth's "The Art of Computer Programming, Volume 1" which is effectively a bible of sorts on this specific topic and is quite easy to read.

If you want to learn the ABCs and 123s without an overly prolonged investment in time, I recommend experimenting with an object oriented language with good class libraries like Java or my favorite, C#. They are both great starting points for experimenting with data structures.

What is the difference between a database and word processing?

Authors are far more concerned with the actual content than the layout and will use word processors to produce that content. Desktop publishing is more concerned with layout than with content.

How do you write a C program to check for Armstrong numbers in three digit values from 100 to 999 using do while loop?

import java.io.*;

public class Arms

{

public static void main(String args[]) throws Exception

{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the number to be printed: ");

int n=Integer.parseInt(br.readLine());

int num,sum,r,temp;

System.out.println("Armstrong numbers upto given limit are:\n");

for(num=1;num<n;num++)

{

temp=num;

sum =0;

while (temp!=0)

{

r=temp%10;

sum=sum+(r*r*r);

temp=temp/10;

}

if(sum==num)

{

System.out.println(num);

}

}

}

}

Regards

Ramakrishna Nallapati

What type of data is gender?

Important. (Please, be more specific with your question, or else no-one could fathom what you wants to know.)

There needs to be a category for questions that as asked defy a sensible answer.

Is function always return a value?

no, every function can not return a value. for example

void name()

{

cout<<"Hello world";

}

this function does not return any value due to the key word void that tells the compiler that the function does not returns a value.

What is difference between Single pass compiler and double pass compiler?

i think a pass is reading the input file, i.e. the file in which the code is written in the source language.so in a single pass compiler, the input file is read only once and in doublepass compiler this is done 2 times

.

Why c is called high level language?

Because C is one of the high level programming languages. Other examples: Pascal, Fortran, List, JavaScript, APL

How do you write a c program for performing DFS and BFS operations?

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<process.h>

#include<string.h>

#define MAX 20

typedef struct queue

{

int data;

struct queue *next;

}queue;

typedef struct stack

{

int data;

struct stack *next;

}stack;

typedef struct adj_list

{

int vertex;

struct adj_list *next;

}adj_list;

adj_list *G[MAX];

int queue_empty(queue *front,queue *rear)

{

if(front==NULL && rear==NULL)

return 1;

else

return 0;

}

queue* getnode()

{

queue *newnode;

newnode = (queue*)malloc(sizeof(queue));

newnode->next = NULL;

return newnode;

}

void enqueue(queue **front, queue **rear, int data)

{

queue *newnode;

newnode = getnode();

newnode->data = data;

if(queue_empty(*front,*rear))

{

*front = *rear = newnode;

}

else

{

(*rear)->next = newnode;

(*rear) = newnode;

}

}

int dequeue(queue **front,queue **rear)

{

int data;

queue *temp;

if(queue_empty(*front,*rear))

return 0;

temp = *front;

data = (*front)->data;

if(*front==*rear)

{

*front = *rear = NULL;

}

else

{

*front = (*front)->next;

}

free(temp);

return data;

}

stack* create_node()

{

stack *newnode;

newnode = (stack*)malloc(sizeof(stack));

newnode->next = NULL;

return newnode;

}

void push(stack **top, int data)

{

stack *newnode;

newnode = create_node();

newnode->data = data;

newnode->next = *top;

*top = newnode;

}

int pop(stack **top)

{

stack *temp;

int data;

temp = *top;

data = (*top)->data;

*top = (*top)->next;

free(temp);

return data;

}

int stack_empty(stack *top)

{

if(top==NULL)

return 1;

else

return 0;

}

void insert(int vi, int vj)

{

adj_list *temp,*newnode;

newnode=(adj_list*)malloc(sizeof(adj_list));

newnode->vertex = vj;

newnode->next=NULL;

if(G[vi]==NULL)

{

G[vi]=newnode;

}

else

{

temp = G[vi];

while(temp->next!=NULL)

{

temp = temp->next;

}

temp->next = newnode;

}

}

void create(int count,char location[MAX][MAX])

{

int i,edges, vi,vj;

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

{

printf("\nEnter the name of node %d\t",i);

flushall();

scanf("%s",location[i]);

G[i] = NULL;

}

printf("\nEnter number of edges\t");

scanf("%d",&edges);

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

{

printf("\nEnter the edge (node u,node v)\t");

scanf("d",&vi,&vj);

insert(vi,vj);

insert(vi,vj);

}

}

void BFS(int v,int count,char location[MAX][MAX])

{

int w,i,visited[MAX];

queue *front=NULL,*rear=NULL;

adj_list *temp;

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

{

visited[i]=0;

}

enqueue(&front,&rear,v);

printf("\n%s",location[v]);

visited[v]=1;

while(!queue_empty(front,rear))

{

v = dequeue(&front,&rear);

for(temp=G[v];temp!=NULL;temp=temp->next)

{

w = temp->vertex;

if(visited[w]==0)

{

enqueue(&front,&rear,w);

visited[w]=1;

printf("\n%s",location[w]);

}

}

}

}

void DFS(int v,int count,char location[MAX][MAX])

{

adj_list *temp;

int visited[MAX],j,w;

stack *top=NULL;

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

{

visited[j]=0;

}

push(&top,v);

visited[v]=1;

while(!stack_empty(top))

{

w = pop(&top);

printf("\n%s",location[w]);

for(temp=G[w];temp!=NULL;temp=temp->next)

{

if(visited[temp->vertex]==0)

{

push(&top,temp->vertex);

visited[temp->vertex]=1;

}

}

}

}

int search(char location[MAX][MAX], char item[MAX])

{

int i;

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

{

if(stricmp(item,location[i])==0) /*string found*/

return i;

}

return -1;

}

void main()

{

int ch,count,i;

char location[MAX][MAX],start[MAX];

do

{

clrscr();

printf("\n*** MENU ***");

printf("\n1.Create");

printf("\n2.Depth First Search");

printf("\n3.Breadth First Search");

printf("\n4.Exit");

printf("\n\nEnter your choice\t");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("\nEnter number of nodes\t");

scanf("%d",&count);

create(count,location);

break;

case 2:

printf("\nEnter starting place\t");

scanf("%s",start);

i = search(location,start);

if(i==-1)

printf("\nInvalid Location Entered");

else

{

printf("\nDepth First Search...\n");

DFS(i,count,location);

}

break;

case 3:

printf("\nEnter starting place\t");

scanf("%s",start);

i = search(location,start);

if(i==-1)

printf("\nInvalid Location Entered");

else

{

printf("\nBreadth First Search...\n");

BFS(i,count,location);

}

break;

case 4:exit(0);

default:printf("\nPlease enter proper choice!!!");

}

getch();

}while(1);

}

This program performs both DFS and BFS operations.

Why not you simply put the decimal integer into the original string as compared to the format specifier?

You can certainly do that ...

printf ("This is a number: 12345\n");

... but that does not have the same value as placing the value in a variable and converting the variable into a string ...

int i = 12345;

printf ("This is a number: %d\n", i);

That's the whole point of format specifiers - to initiate a conversion from one place to another.

Write a program to read four floating points numbers and find their sum and average?

#include<stdio.h>

#include<conio.h>

int main(void)

{

float a,b,c,d,avg=0;

clrscr();

printf("\n Enter the four floating point nos.\n");

scanf("%f%f%f%f",&a,&b,&c,&d);

avg=(a+b+c+d)/4;

printf("\n the average is=%10.5f",avg);

getch();

return 0;

}

How are two dimensional arrays represented in memory. Explain how address of an element is calculated in a two dimensional array?

The memory of a computer is linear and not a matrix like a 2D array. So, the elements of the array are stored either by row, called "row-major", or by column, called "column-major". Row-major order is used most notably in C and C++ during static declaration of arrays.

In C, since the length of each row is always known, the memory can be filled row one row at a time, one after the other. Example:

a[i][j] =

1 2 3 4 5 6 7 8 9

Representation in the memory: In row-major: 1 2 3 4 5 6 7 8 9 In column-major: 1 4 7 2 5 8 3 6 9

Address calculation of an element: Row-Major : addr (i,j) = B + W * ( Nc * (i - Lr) + (j-Lc) ) Col-Major : addr (i,j) = B + W * ( (i - Lr) + Nr * (j-Lc) ) i,j = subscript number. B = Base address W = width (size) of each element Nc = Number of Columns Nr = Number of Rows Lc = Lower-bound of Column Lr = Lower-bound of Row

In above example, for element (6), i.e., a(1,2) in row-major or a(2,1) in col-major, B = 200 (say) W = 2 Lr=Lc=0 Nc=Nr=3

addr (1,2) = 210; addr (2,1) = 210

Future of c language?

The future of the C language is very secure. This is because the choice of programming language for any application depends on the nature of the application itself. C 's biggest assets are its performance and speed of execution. For performance critical applications there is no beating C. Besides, C has been around for many years and has a dedicated community of users. It also has a lot of online resources, a clean syntax and is easy to learn and use. So it is safe to say that C isn't going anywhere.

What is syntax of structure?

struct tag_name

{

data type v1;

data type v2;

__________

__________

__________

}s1;

What type of loop uses Boolean expression to control the number of times that it repeats a statement set of statements?

A counted loop. Typically we use a for loop for counted loops:

// loop 10 times...

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

// ...

}

We can also use while and do-while loops to do the same thing, however a for loop provides all the information up front where it belongs and we can localise the control variable. With while and do-while loops, the control variable must be declared outside the loop, and the increment is usually specified at the end of the loop. This makes while and do-while loops harder to read because the information that controls the loop is separated:

// loop 10 times...

int i = 0;

while (i<10) {

// ...

++i;

}

A do-while loop is similar to a while loop, but the control expression is placed at the end of the loop thus the loop always executes at least once. This also upsets the logic of a counted loop because the control variable is off-by-one.

// loop 10 times...

int i = 0; do {

// ...

++i;

} while (i<11);

What are the applications of array in data structure?

The most obvious reason is to store a string within a data structure. After all, a string is a null-terminated array of type char. And if we can store a simple character array in a data structure, there's nothing to stop us storing any type of array in a data structure.

As a more practical example, consider a structure that stores a name and an address. An address might be represented by 3, 4 or 5 string fields, but we could just as easily use an array of strings instead and thus cater for any number of lines (a null terminator can be used to mark the end of the array or we can store the line count in the structure itself).

As well as variable-length arrays we can also store fixed-length arrays in a structure. For instance, suppose we wished to create a record of a particular Sudoku puzzle with fields denoting the publisher, the date of publication, the level of difficulty and so on. The puzzle itself can be represented as a 9x9 array of integers within the structure itself.

Why we have to compile a c program?

Programming Languages are a form of comunication between a programmer and the hardware. So the code written in C has to be compiled/transformed into machine code (similar to Assembler) so that the hardware can understand what to do. Compilation leaves your code ready to excecute. Withought it you only have the recipe of what the program does.

Why different types of programming languages have been developed?

There are different uses for programs. The main variances occur in the level in which the language goes. That is you can go from writing a program in binary to using a language where all you have to right is "Hello world" and it does a set task.

Given this there is also the fact there is always something who thinks that "i can do better then this".

How do you add two numbers in C programming?

#include<iostream>

int main

{

int x=40, y=2;

std::cout<<"The sum of "<<x<<" and "<<y<<" is "<<x+y<<std::endl;

}

How do you intialize graphic mode in c?

Platform-dependent. For Turbo C, enter initgraph and press Ctrl+F1.

What are dynamic memory allocation advantages?

Often an application needs additional memory for the temporary storage of data. For example, the C programming language allows the programmer to use the MALLOC (memory allocate) function to grab a chunk of memory suitable for the applications needs. Failure to release the memory after it is used by using the FREE function can result in problems. It is called dynamic memory allocation because the memory is allocated at run-time, as needed. Unlike variables created within functions - the memory is not allocated on the processor stack, instead, when using MALLOC or the 'new' keyword, memory is allocated in the applications virtual address space.

What are the function of compiler and an interpreter?

Interpreters translate source code into machine while it is executing. The machine code is not saved, thus the code must be translated on every execution. As a result, the code is larger and very much slower than an equivalent machine code executable would be. However, the source code is highly portable.

Compilers translate the whole program at once to produce a machine code executable that requires no further translation. However, the machine code is non-portable and must be recompiled for each supported platform.

Some language compile to byte code rather than machine code. Byte code is best thought of as being the native language of a virtual machine rather than a physical one. The virtual machine then interprets the byte code to produce the machine code. Like all interpreted languages, the byte code is highly portable, and while quicker to interpret than source code, is still very much larger and slower than compiled native machine code.

Define and give three examples of primitive data types?

A primitive variable can be one of eight types: char, boolean, byte, short, int, long, double, or float. Once a primitive has been declared, its primitive type can never change, although in most cases its value can change. they are used to store values like name, age, salary etc.

Which data type would you assign to an address field?

For mailing address: in some computer languages, strings or text will be the answers. For OO language, a class Address should be designed to be the template to hold onto any address information. (an address is a class with a lot of string fields for address lines, city, zip, state, county, country, province, zone, district, etc)

For internet address: some languages such as C# already provides you with Url class