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

A computer program must be free of errors before you can execute it?

There are three kinds of errors. One type won't allow the program to compile. One type will make it exit due to error while running. And the last type, the hardest to find, won't cause any problems for the program, but it will cause the program to do something the programmer didn't intend for it to do. These, logic errors, may still exist in a program that seems to run fine.

Write a c plus plus program to add two matrix using arrays?

Matrix Add

/* Program MAT_ADD.C

**

** Illustrates how to add two 3X3 matrices.

**

** Peter H. Anderson, Feb 21, '97

*/

#include <stdio.h>

void add_matrices(int a[][3], int b[][3], int result[][3]);

void print_matrix(int a[][3]);

void main(void)

{

int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} };

int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} };

int r[3][3];

add_matrices(p, q, r);

printf("\nMatrix 1:\n");

print_matrix(p);

printf("\nMatrix 2:\n");

print_matrix(q);

printf("\nResult:\n");

print_matrix(r);

}

void add_matrices(int a[][3], int b[][3], int result[][3])

{

int i, j;

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

{

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

{

result[i][j] = a[i][j] + b[i][j];

}

}

}

void print_matrix(int a[][3])

{

int i, j;

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

{

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

{

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

}

printf("\n");

}

}

Can you connect to database in unix environment using c or c plus plus languages?

At the simplest level, a database is simply a data container. As such an array can be considered a database. However, when we think of a database we usually imagine a container that combines one or more related tables of data, that allows us to easily modify data, to search for data, and so on. Databases are typically disk-based centralised repositories (data servers) containing a massive amount of data, while end-users (data clients) are really only concerned with a small amount of that data (a subset). Data can also be generated by the database, such as when returning the number of records in the database that match a specified criteria.

Database Management Systems (DBMS) are the simplest way to make and use a database as all the functionality you need is readily available, all you need do is connect to the DBMS and query it, typically using a text-based script such as SQL (structured query language). MySQL is a popular choice because it is open source and can be used under the terms of the Gnu Public Licence (GPL).

What do you mean by infix?

The operator is between the two operands, like 4+6

What are the different programming languages?

There are literally thousands of programming languages - some for special purpuses, some of a more general nature. Some popular languages are the Microsoft dotnet languages (C#, Visual Basic .NET, and others); Java; PHP; Python; and lots of others. You can get more information:

* In the Wikipedia article on "programming language"

* The TIOBE website has been keeping track of the most "popular" programming languages, for the last few years.

What is the differences between a logical error and syntax error?

Answer:

Syntax Error - Occurs when the code isn't formatted or typed correctly. i.e. In python, typing If instead of if because it only recognizes lowercase.

Logical Error - Occurs when there is a fallacy of reasoning. i.e. In python, typing if x < 0 and x > 5. Since a value can't be less than 0 and greater than 5, a logical error will occur.

Answer:

a) Syntax Error

Definition : An error cause by violation of the programming language used.

Symptoms : Code fails to compile (error message from compiler)

b) Logical Error

Definition : An error caused by violation of logic (range, comparison, etc.). This error will NOT crash the program.

Symptoms : Unexpected output

c) Runtime Error/Execution Error

Definition : Any error, normally logical error that cause the program to crash.

Symptoms : Program crashes.

What is the difference between function and operator overloading in c plus plus?

A class is a type while an object is an instance of a class. This can be likened to the way in which an int is a type while an int variable is an instance of an int:

int x; // x is an instance of int type.

myClass c; // c is an instance of myClass type.

Design a non recursive algorithm for the towers of hanoi puzzle?

public class TowersOfHanoi{

public static void main(String []args){

new TowersOfHanoi().start();

}

public void start(){

String []tOH=showSteps(4);//if there are 4 disks

System.out.println("Towers of Hanoi step by step!");

for(int k=0;k<tOH.length;k++){

System.out.println("Step "+(k+1)+": Move a disk from "+tOH[k].charAt(0)+" to "+tOH[k].charAt(1));

}

}

public String []changeString(String []array,char a, char b){

for(int i=0;i<array.length;i++){

for(int j=0;j<array[i].length();j++){

if(array[i].charAt(j)==b){

array[i]=array[i].substring(0,j)+a+array[i].substring(j+1);

} else if(array[i].charAt(j)==a){

array[i]=array[i].substring(0,j)+b+array[i].substring(j+1);

}

}

}

return array;

}

public String []showSteps(int n){//how many n disks are there?

String []data={"A","B","C"};

String []Array=new String[(int)(Math.pow(2,n))-1];

for(int i=1;i<=Array.length;i=i*2+1){

int middle=(i-1)/2;

Array[middle]="AC";

String []tempArray=new String[middle];

for(int left=0;left<middle;left++){

tempArray[left]=Array[left];

}

tempArray=changeString(tempArray,'C','B');

for(int o=0;o<middle;o++){

Array[o]=tempArray[o];

}

tempArray=changeString(tempArray,'B','A');

tempArray=changeString(tempArray,'A','C');

for(int o=middle+1;o<i;o++){

Array[o]=tempArray[o-middle-1];

}

}

return Array;

}

}

What is the space complexity of shell sort?

average case worst case

LSD Radix sort O(n.k/s) O(n.k/s)

MSD Radix sort O(n.k/s) O(n.k/s.2^s)

n=no of items to be sorted

k=size of each key

s=chunk size used by implementation

LSD=Least Significant Digit

MSD=Most Significant Digit

What objects exhibit two dimensional motion?

Two vectors that do not lie along the same line.

I wish someone would have posted this for me. ^_^

Circular queue in linear data structure?

The queue is a linear data structure where operations of insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. A circular queue is similar to the normal queue with the difference that queue is circular queue ; that is pointer rear can point to beginning of the queue when it reaches at the end of the queue. Advantage of this type of queue is that empty location let due to deletion of elements using front pointer can again be filled using rear pointer.

How do you compare two numbers without using any operators in c?

You cannot compare 2 numbers without using relational operators. Certainly, you could subtract them, but you still need to test the result, and that is a relational operator in itself.

Arrays in c?

1>an array is a static data structure.after declaring an array it is impossible to change its size.thus sometime memory spaces are misused.

2>each element of array are of same data type as well as same size.we can not work with elements of different data type.

3>in an array the task of insertion and deletion is not easy because the elements are stored in contiguous memory location.

4>array is a static data structure thus the number of elements can be stored in it are somehow fixed.

Difference between Java and the C plus plus?

Classes are the basic unit of code in Java, whereas the basic unit of code in C++ are functions.

All objects in Java descend from a common class (Object), which allows more generic code. While this can also be (manually) implemented within C++, generic programming is best achieved using templates and concepts.

All methods in Java (including destructors) are virtual by default. In C++, methods must be explicitly declared virtual if they are expected to be overridden, or explicitly declared final if they must not be overridden.

Java does not permit operator overloading, thus we cannot operate upon objects as intuitively as we can in C++.

C plus plus programming language program for hybrid inheritance?

There's no such thing as hybrid inheritance in C++. Hybrid inheritance implies two or more different types of inheritance but there are really only two types of inheritance in C++ and they are mutually exclusive: single inheritance and multiple inheritance.

A class that inherits directly from one class uses single inheritance.

A class that inherits directly from two or more classes uses multiple inheritance.

The only way to combine these two inheritance patterns is through multi-level inheritance, where a class inherits directly from one or more derived classes. However, whenever we create a derivative, we're only concerned with the base class or classes we are directly inheriting from. The fact they may or may not be derivatives themselves is largely irrelevant from the viewpoint of the derivative. Indeed, the only time we really need to consider one of the lower bases classes is when we need to explicitly invoke a virtual function of that particular class, as opposed to implicitly invoking the most-derived override of that function as we normally would. However, this is really no different to a derived class override invoking its direct base class method.

Virtual base classes are also thought of as being a type of hybrid inheritance, however virtual base classes merely determine which class is responsible for the construction of those classes. Normally, a derived class is responsible for the construction of all its direct base classes, which must be constructed before the derived class can begin construction. In turn, those base classes are responsible for the construction for their own base classes. In this way, derived classes are automatically constructed from the ground up, base classes before derived classes, in the order declared by the derived class.

For example, consider the following hierarchy:

struct X {};

struct Y : X {};

struct Z : Y {};

Z inherits from Y so in order for a Z to exist we must first construct a Y. By the same token, Y inherits from X so in order for a Y to exist we must first construct an X. Thus when we initiate construction of a Z, that initiates construction of a Y which initiates construction of an X.

Now consider a virtual base class:

struct X {};

struct Y : virtual X {};

struct Z : Y {};

The construction sequence is exactly the same as before (X before Y before Z), the only difference is that when we now instantiate a Z, as the most-derived class in the hierarchy it becomes responsible for the construction of the virtual X. Z is also (still) responsible for the construction of a Y, but Y no longer needs to construct an X because a (virtual) X already exists.

Virtual base classes become more relevant in multiple inheritance, where two or more base classes share a common base class:

struct W {};

struct X : virtual W {};

struct Y : virtual W {};

struct Z : X, Y {};

Here, Z uses multiple inheritance from X and Y. Both X and Y use single inheritance from W. Without virtual inheritance, Z would inherit two separate instances of W, specifically X::W and Y::W. But by declaring W as a virtual base of X and Y, the most-derived class, Z, becomes responsible for the construction of W, as well as its direct base classes, X and Y. Neither X nor Y need to construct a W because a W will already exist. Thus X::W and Y::W now refer to the same instance of W.

Note that we do not need to write any additional code for this mechanism to work. The virtual keyword alone is all we need. Even if X or Y provided explicit initialisation of W, those initialisers would be ignored by the compiler since initialisation of W is automatically the responsibility of the most-derived class. The only time those explicit initialisers would be invoked is if we explicitly instantiate an instance of X or Y, because then X or Y become the most-derived class.

Program for count the total number of node in binary tree?

The number of nodes in any subtree is the number of nodes in its left subtree, plus the number of nodes in its right subtree, plus one, so you can use a recursive algorithm and start at the root.

unsigned intbinarytree_count_recursive(const node *root)

{

unsigned int count = 0;

if (root != NULL) {

count = 1 + binarytree_count_recursive(root->left)

+ binarytree_count_recursive(root->right);

}

return count;

}

What are range of character data type in c plus plus?

The range of character data types in C++ is the set of characters in the host's character set.

It is inappropriate to consider the numerical range of such things as characters, because that depends on the particular codeset involved, such as ASCII, EBCDIC, UNICODE, KANJI, etc. Doing that leads to non-portable code, and lazy programming practices. Do not write code that depends on the collating sequence of the character set, or the numerical difference between two characters.

Type char can be signed or unsigned, the value range is -128..127 or 0..255 respectively.

Find the prime nofrom one to ten using 'for' or 'while' loop in c language?

I don't know C, but the process of finding a prime is the same in any programming language # Use a sequential list of numbers from two to some maximum. (10 in your case) # Delete all multiples of 2 greater than 2 from the list. # The next lowest, uncrossed off number in the list is a prime number. # Delete all multiples of this number from the list. This can be started at the square of the number, as lower multiples have already been crossed out in previous steps. # Repeat steps 3 and 4 until you reach a number greater than the square root of the highest number in the list; all the numbers remaining in the list are prime

Why do you need c language?

C is often referred to as a high level assembly language. There are few languages with less overhead (in terms of run-time support). When you are coding to meet certain constraints (performance, real-time time constraints, memory limitations, etc.), C can provide you with code that meets those constraints but which is also (relatively) portable.

Note: Of course C and Assembly are not similar at all.

Write a recursive function to find sum of even numbers from 2 to 50 using C?

I can't imagine a useful reason to have a recursive function to find this, but here you go:

int sumEvens(int start, int end) {

// end condition

if (start > end) {

return 0;

}

// correction if we start on an odd number

if (start % 2 == 1) {

return sumEvens(start + 1, end);

}

// actual work

return start + sumEvens(start + 2, end);

}

Invoke with sumEvens(2, 50) to get the sum of all even numbers in the range [2,50]

Write a programe for calculating the area of circle in C programming?

#include

#include

#include

using std::cout;

using std::cin;

using std::endl;

using std::setw();

int main()

{

const double PI = 3.14153;

double radius = 0.0;

cout << "Enter radius of circle: ";

cin >> radius;

unsigned short precision = 5;

cout << endl <<"Enter precision you want have (not more than 6 digits): ";;

cin >> precision;

cout << setw(precision) <

system("PAUSE");

return 0;

}

Is main a keyword?

No. Main is not a keyword in C or C++. However, your program, when linked, must provide one and only one externally scoped entry point to main(). If you use main in some other context, and you do not provide one and only one entry point main(), then your program will not link nor run.

What are the various operators available in c language?

There are eight types of operators which are used in C language.These are-

1.Arithmetic operator

2.Assignment operator

3.Relational operator

4.Increment/Decrement operator

5.Bitwise operator

6.Logical operator

7.Conditional operator

8.Additional operator

1.Arithmetic operator:Arithmetic operators are mathmetical operator.These are addition,Subtraction,Multiplication and divison.

2.Assignment operator:Assignment operators are used to store the result of an expression to a variable.

Which year C language was invented?

Dennis MacAlistair Ritchie and Kenneth Lane Thompson at Bell Laboratories in 1972.

What are the different reserve words in c-language?

Reserve words, also known as keywords are words whose meaning are already defined by a compiler. C language has a total of 32 reserve words. Short, union, else, for, goto, unsigned, enum, extern, char, continue, switch, struct, typedef are some examples.