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 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

How to print prime numbers in C language program?

#include <stdio.h> void main() { int n,i=1,j,c; clrscr(); printf("Enter Number Of Terms"); scanf("%d",&n);

printf("Prime Numbers Are Following");

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

{ c=0; for(j=1;j<=i;j++) { if(i%j==0) c++; } if(c==2) printf("%d",i); } getch(); }

Which language has the simpliest syntactic structure?

I believe the correct answer is Hawaiian. The Hawaiian alphabet has only 12 letters in it, and therefore will contain the lowest number of syntactic combinations.

What is a heap?

Answer:- A sorting algorithm that works by first organizing the data to be sorted into a special type of binary tree called a heap. The heap itself has, by definition, the largest value at the top of the tree, so the heap sort algorithm must also reverse the order. It does this with the following steps:

1. Remove the topmost item (the largest) and replace it with the rightmost leaf. The topmost item is stored in an array.

2. Re-establish the heap.

3. Repeat steps 1 and 2 until there are no more items left in the heap.

The sorted elements are now stored in an array.

A heap sort is especially efficient for data that is already stored in a binary tree. In most cases, however, the quick sort algorithm is more efficient.

GOURAV KHARE (CHANDIGARH)

gouravsonu89@gmail.com

What are the differences between Macros and Functions in C Language?

Subroutine:

1.When a task is to be done repeatedly then it is written as subroutine and this subroutine will be called each time to perform that task.

2.Subroutine program will be stored in some memory location and program control will be transfered to that location each time.

3.where as in macro the number of instructions will be less than subroutine.Here each time u call a macro that set of instructions will be inserted in that location.

4.macro doesn't have return statement while subroutine has.

5.memory requirement for macro is higher.

6.execution time of macro is lesser than subroutine.

Programs to implement operations on doubly linked list in C?

Stack is an abstract data type that allows you to input and output data in a way that the first data which was placed in the stack will be the last one to get out. We use physical examples of stack in our daily lives such as the stack of dishes or stack of coins where you only add or remove objects from the top of the stack.

You can see the whole source code implementing the stack in related links, below.

What are the benefits of using Abstract Data Types?

  • The implementor of the class can change the implementation for maintenance, bug fixes or optimization reasons, without disturbing the client code.
  • The data type is defined as a set of high-level services with a semantic contract defining both the output that is provided and the input that is required from the client. This actually correspond to the general definition of a type in programming: a type is defined as a set of possible values and a set of operations available on these values.

Why should an object hide its data?

As my AP Computer Science teacher eloquently said:

"You can't let strangers touch your private parts"

Therefore, if data was not hidden, a client class can modify all the variables(given that it is not a final constant) by simply calling:

className.variableName = newWrongData;

By using special methods to access the variables, the class can change its internal implementation, without breaking compatibility with other classes that are already using it.

What is inbound coach operator do?

someone who sorts out coaches for tourists

What are the Characteristics of primitive mode of production?

Primitive mode of production was characterized by the following features or characteristics.

Ownership of the means of production was collective

It was a classless society

Tools used in production were crude

Firstly people lived by hunting and gathering

No exploitation

People started living in caves