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 is linear search algorithm?

The linear search problem relates to searching an un-ordered sequence. Because the data is no ordered, we must start at one end of the sequence and inspect each element in turn tunil we find the value we are looking for. If we reach the one-past-the-end of the sequence, the value does not exist. From this we can see that for a set of n elements, the worst case (the element does not exist) is O(n) time while the best case is O(1) time (the element we seek is the first element). Given that there is a 50/50 chance the element we seek will be closer to the start of the sequence than the end, the average seek time is O(n/2).

When a set is ordered we can reduce search times by starting in the middle of the set. In this way, if the element is not found we can eliminate half of the set because we know which half contains the value (if it exists). We repeat the process until we find the value in the middle of the remaining set or the remaining set is empty. The end result is that search times are reduced to a worst case of O(log n), the binary logarithm of n.

Structure in c?

Structures are a way of storing many different values in variables of potentially different types under the same name. This makes it a more modular program, which is easier to modify because its design makes things more compact. Structs are generally useful whenever a lot of data needs to be grouped together--for instance, they can be used to hold records from a database or to store information about contacts in an address book. In the contacts example, a struct could be used that would hold all of the information about a single contact--name, address, phone number, and so forth.
->A structure is a collection of related elements , possibly of different types , having a single name.

->"each element in a structure is called field".

->A FIELD is a smallest element of named data that has meaning. It has many characteristics of the variable .

->It exits in memory . it can be assigned values, which in turn can be accessed for selection or manipulation.

-> A field differs form variable primarily in that it is a part of structure.

ITS SYNTAX IS:

struct tag name

{

field list;

};

What is the purpose of using loop?

A loop is a section of code that is repeated over and over until some condition is met. There are different flavors:

A for loop:

for (a = 0; a < 25; a++)

{

//code

}

The //code will be executed with a=0, then a=1, etc., until a=25, when it will break out of the loop.

a = false

do

{

//code

} while (a == false)

Here, if there is nothing in //code to change the value of a to true, you will have an infinite loop.

C program to find weather given number is even or not?

Oh good old-fashioned C.

void main()

{

int variable_name = [Any number goes here];

if (variable_name % 2 == 0)

{

printf("%d is even.", variable_name);

} else

{

printf("%d is odd.", variable_name);

}

}

I think I've helped enough, so it's up to you to learn how to get input from the user, if that's what you're working on.

What are the 5 ways of writing an algorithm?

for the most part, programming is writing algorithms. An algorithm is just a sequence of instructions designed to get a desired result.

lets write an algorithm for finding the largest number in a list of numbers:

You have a list called numberList and it has a bunch of random numbers stored in it. The only way you can find the largest number is if you go through every element in numberList. Since we don't know anything about any of the numbers in numberList (they could all be the same) lets just call the first element in the list our currentLargest. As we traverse through numberList, if we come across a number larger than our currentLargest then we assign the new number as our currentLargest. Once we have looked at every element in the numberList, our currentLargest should be the largest number in numberList.

The code for the above program would look something like this [Pseudocode]:

numberList = {1, 39, 8, 109, ...}

currentLarget = numberList0

FOR every element in numberList

. . . IF element > currentLargest

. . . . . . currentLargest = element

PRINT currentLargest

What is difference between structural language and object oriented language?

I think there is no any difference between object oriented programming language. Because somebody have written that vb is object based language because there is no inheritance, but javascript has no classes and no inheritance but javascript is also object oriented scripting language and java is also object oriented language vb has no inheritance but classes is.So vb is object based language This is not clear that difference between object oriented and object based. if i am wrong than what should be your answer and if i am wright than no problem But first i am requesting to the developer of any programming language that please define the difference between object oriented and object based languages. Amit Sinha Dist-Gaya State-Bihar

Write a program in c language to subtract two numbers?

#include<stdio.h>

#include<conio.h>

int main(void)

{

int sum=0,n,i;

clrscr();

printf("\n Enter two no.");

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

sum=n+i;

printf("\n%d+%d=%d,n,i,sum);

getch();

return 0;

}

How do you get C programming to reduce a fraction?

#include "stdio.h"

int gcd(int a, int b);

void reduce(int* numerator, int* denominator);

int main(int argc, char* argv[]) {

int a, b;

fscanf(stdin, "%d/%d", &a, &b);

reduce(&a, &b);

fprintf(stdout, "%d/%d", a, b);

return 0;

}

int gcd(int a, int b) {

int c;

while (b) {

c = a % b;

a = b;

b = c;

}

return a;

}

void reduce(int* numerator, int* denominator) {

int g = gcd(*numerator, *denominator);

*numerator /= g;

*denominator /= g;

}

You get the greatest common divisor between the numerator and denominator and divide them by it.

Reduce uses integer pointers so that changes the numerator and denominator to their reduce form.

Characteristics of nonlinear data structure?

A database is a collection of data organized in a fashion that facilitates updating, retrieving, and managing the data. The data can consist of anything, including, but not limited to names, addresses, pictures, and numbers. Databases are commonplace and are used everyday. For example, an airline reservation system might maintain a database of available flights, customers, and tickets issued. A teacher might maintain a database of student names and grades. Because computers excel at quickly and accurately manipulating, storing, and retrieving data, databases are often maintained electronically using a database management system. Database management systems are essential components of many everyday business operations. Database products like Microsoft SQL Server, Sybase Adaptive Server, IBM DB2, and Oracle serve as a foundation for accounting systems, inventory systems, medical recordkeeping sytems, airline reservation systems, and countless other important aspects of modern businesses. It is not uncommon for a database to contain millions of records requiring many gigabytes of storage. For examples, TELSTRA, an Australian telecommunications company, maintains a customer billing database with 51 billion rows (yes, billion) and 4.2 terabytes of data. In order for a database to be useful and usable, it must support the desired operations, such as retrieval and storage, quickly. Because databases cannot typically be maintained entirely in memory, b-trees are often used to index the data and to provide fast access. For example, searching an unindexed and unsorted database containing n key values will have a worst case running time of O(n); if the same data is indexed with a b-tree, the same search operation will run in O(log n). To perform a search for a single key on a set of one million keys (1,000,000), a linear search will require at most 1,000,000 comparisons. If the same data is indexed with a b-tree of minimum degree 10, 114 comparisons will be required in the worst case. Clearly, indexing large amounts of data can significantly improve search performance. Although other balanced tree structures can be used, a b-tree also optimizes costly disk accesses that are of concern when dealing with large data sets. Databases typically run in multiuser environments where many users can concurrently perform operations on the database. Unfortunately, this common scenario introduces complications. For example, imagine a database storing bank account balances. Now assume that someone attempts to withdraw $40 from an account containing $60. First, the current balance is checked to ensure sufficent funds. After funds are disbursed, the balance of the account is reduced. This approach works flawlessly until concurrent transactions are considered. Suppose that another person simultaneously attempts to withdraw $30 from the same account. At the same time the account balance is checked by the first person, the account balance is also retrieved for the second person. Since neither person is requesting more funds than are currently available, both requests are satisfied for a total of $70. After the first person's transaction, $20 should remain ($60 - $40), so the new balance is recorded as $20. Next, the account balance after the second person's transaction, $30 ($60 - $30), is recorded overwriting the $20 balance. Unfortunately, $70 have been disbursed, but the account balance has only been decreased by $30. Clearly, this behavior is undesirable, and special precautions must be taken. A b-tree suffers from similar problems in a multiuser environment. If two or more processes are manipulating the same tree, it is possible for the tree to become corrupt and result in data loss or errors. The simplest solution is to serialize access to the data structure. In other words, if another process is using the tree, all other processes must wait. Although this is feasible in many cases, it can place an unecessary and costly limit on performance because many operations actually can be performed concurrently without risk. Locking, introduced by Gray and refined by many others, provides a mechanism for controlling concurrent operations on data structures in order to prevent undesirable side effects and to ensure consistency. For a detailed discussion of this and other concurrency control mechanisms, please refer to the references below. reference http://www.bluerwhite.org/btree/

What are the differences between Break Continue and Exit?

break - The break statement is used to jump out of loop. After the break statement control passes to the immediate statement after the loop.

continue - Using continue we can go to the next iteration in loop.

exit - it is used to exit the execution of program.

note: break and continue are statements, exit is function.

Write a programme to find simple interest?

int main()

{

int p,n,count;

float r,si;

count=1;

while(count<=3)

{

printf("\n enter values of p,n,andr");

scanf("%d %d %f",&p, &n, &r);

si=(float)p * (float)n * r / 100;

printf("simple interest =rs. %f",si);

count=count+1;

}

return(0);

}

Nishant:this will give S.I...

-----------------------------------------------------------------------------------------------------

//mycfiles.wordpress.com

//Program for Calculate Simple Interest

#include<stdio.h>

#include<conio.h>

void main()

{

float p,r,n,si;

clrscr();

printf("\nEnter the profit, rate & no of yr\n\n ");

scanf("f%f",&p,&r,&n);

si=(p*r*n)/100;

printf("\nSimple Intrest=%f",si);

getch();

}

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