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 c program to check whether a no is prime or not?

#include<stdio.h> #include<conio.h> int i=1, count=0,n; clrscr(); printf("Enter Any Number"); scanf("%d", &n); while(i<n) if(n%i==0) { count++; i++; } if(count==2) printf("Given Number is Prime"); else printf("Given Number is not Prime"); getch(); }

How input device transfers data to the CPU?

Every input device attached to the motherboard bus lines has a direct line of communication with the CPU. These lines are known as interrupt request lines (IRQs) which can be prioritised. By signalling the CPU that an input is pending, the CPU can alert the operating system to deal with the request, which will then be prioritised accordingly.

What is a two dimensional array in c?

its simple

just do this swappping

for(i=0;i<m;i++) /*A*/

for(j=0;j<i;j++) /*B*/

{

x=a[i][j];

a[i][j]=a[j][i];

a[j][i]=x;

}

I think A and B need a change :

/*Permutation : */

for ( i = 0 ; i <= lig ; i++ ) /*A*/

for ( j = 0 ; j <= i ; j++ ) /*B*/

{

int permut = MatA[i][j] ;

MatA[i][j] = MatA[j][i] ;

MatA[j][i] = permut ;

}

/*End of permutation */

printf("\nDISPLAY MATRIX : \n") ;

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

{

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

{

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

}

printf("\n") ;

}

What is arrays of structure?

A simple array has of basic data type such as char, int, float... arrays of structure has the type of structure.

struct student std[12];

Here std is an arrays of structure.

Difference between internal and external sorting?

Internal sorting

it means we are arranging the number within the array only which is in computer primary memory.

External sorting

it is the sorting of numbers from the external file by reading it from secondary memory.

Why is the JVM platform dependent?

That depends on how you look at it. The point of the JVM is to allow Java bytecode to be executed on any platform, regardless of what machine it was compiled on.

The actual implementation of the JVM, however, must be platform-specific.

What is a finite and infinite set?

A set which containing $and pi are the end blocks are the finite and without these are infinite

Why are pointers needed in C programming?

pointer is used when we want to retain the change in values between the function calls.

Similarly double pointer is used when we want to retain the change in pointers between the function calls. If you want to modify pointers than you need double pointer to retain the change.

What do you call the loop that never ends?

That kind of loop is referred to as an Infinite Loop. It occurs when the exit conditions can never be met or when the programmer fails to supply an exit.

For example:

Do while (1>0)

(that will loop forever because 1 is always greater than 0)

What are the effectiveness of algorithm?

a measure of the amount of resources consumed in solving a problem of size n-time

-space

•Benchmarking: implement algorithm,

-run with some specific input and measure time taken

-better for comparing performance of processors than for comparing performance of algorithms

•Big Oh (asymptotic analysis)

-associates n, the problem size,

-with t, the processing time required to solve the problem

Matrix addition using 2-D array in turbo c?

#include<stdio.h>

#include<conio.h>

int main(void)

{

int a[100][100]={0}; /* initializing matrices to '0' */

int b[100][100]={0};

int c[100][100]={0}; /*matrix-c for multiplication*/

/*r1,c1 are rows and coloumns for matrix-a.r2,c2 for matrix-b.*/

int r1,c1,r2,c2;

int i,j,k;

clrscr();

printf("enter the no of ROWS and COLOUMNS for MATRIX-A\n");

scanf("%d %d",&r1,&c1);

printf("enter the no of ROWS and COLOUMNS for MATRIX-B\n");

scanf("%d %d",&r2,&c2);

if(c1==r2)

{

printf("matrix multiplication possible\n\nenter numbers in MATRIX-A\n");

/* to enter numbers in matrix-a*/

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

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

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

printf("enter numbers in MATRIX-B\n");

/* to enter numbers in matrix-b*/

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

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

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

/*for matrices multiplication*/

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

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

for(k=0;k<c1;k++)

*(*(c+i)+j)+=*(*(a+i)+j)*(*(*(b+k)+j));

printf("result of multiplication of matrices is \n");

/*to display as matrix format*/

for(i=0;j<r1;i++)

{

printf("\n");

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

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

}

} //if

else

printf("MATRIX multiplication not Possible\n");

getch();

return 0;

}

/* TWINKLING TWINS */

Write a program to count the length of a string without using built in functions?

In JavaScript: To find the length of the string we you length method.

__

__

__

Click the button to return the number of characters in the string "Hello World!".

click here

__

__

function myFunction() {

var str = "Hello World!";

var n = str.length;

document.getElementById("demo").innerHTML = n;

}

__

__

__

Hope this helps. Thank you

Do while in c plus plus?

The do while loop is useful for the special loops that they must circulate at least one time.

#include <iostream>

using namespace std;

int main()

{

int num,digit;

cout<<"Enter a #\n";

cin>>num;

cout<<"Invers is : ";

do

{

digit=num/10;

cout<<digit;

num/=10;

} while(num != 0);

return 0;

}

What is sorting in C language?

Quicksort in any programming language is an algorithm for sorting data sequences. It is typically implemented as follows (example demonstrates a quicksort of an array of type int). Note that a half-open range, [begin:end), includes the one-past-the-end of the range and is the conventional means of defining a range of contiguous array values. When begin==end, the range is deemed empty.

// forward declarations

void qsort (int* unsigned); // this is the function you will actually invoke

void qsort_rec (int*, int*); // recursive function

int* partition (int*, int*); // utility functions...

int* select_pivot (int*, int*);

void swap (int*, int*);

int count (int*, int*);

// sort a data sequence of length size

void qsort (int* data, unsigned size) {

qsort_rec (data, data + size);

}

// recursively sort the half-open range [begin:end)

void qsort_rec (int* begin, int* end) {

if (count (begin, end) < 2) return; // end point of recursion

int* pivot = partition (begin, end);

qsort_rec (begin, pivot);

qsort_rec (++pivot, end);

}

// divide the half-open range [begin:end) into two around a pivot value

int* partition (int* begin, int* end) {

if (count (begin, end) < 2) return begin;

int* pivot = select_pivot (begin, end);

swap (begin, pivot);

--end;

while (begin != end) {

while (*(begin) <= *pivot && begin != end) ++begin;

while (*pivot < *(end) && begin != end) --end;

if (begin!=end) swap (begin, end);

}

assert (begin==end); // sanity check!

swap (pivot, begin);

return begin;

}

// select the median of three from a half-open range [begin:end)

int* select_pivot (int* begin, int* end) {

int* mid = begin + (count (begin, end) / 2);

if (*end<*begin) swap (begin, end);

if (*mid<*begin) swap (begin, mid);

if (*end<*mid) swap (mid, end);

return end;

}

// swap the values referred to by p and q

void swap (int* p, int* q) {

if (!p !q) return; // sanity check!

int t = *p;

*p = *q;

*q = t;

}

// count the elements in the half-closed range [begin:end)

int count (int* begin, int* end) {

int cnt = 0;

int add;

if (begin>end) { // swap pointers if the range is reversed

int* t = begin;

begin = end;

end = t;

add = -1; // count will be negative

} else {

add = 1; // count will be positive

}

while (begin++!=end) cnt+=add;

return cnt;

}

How do you declare a pointer to a structure in C?

It depends on whether you've declared a type alias or not. Without an alias, you must include the struct keyword:

struct record {

// ...

};

struct record* ptr; // declare a pointer to struct record

With an alias, the struct keyword can be omitted from the type name. By convention, we use the _t suffix to denote the type name. Typically, aliases are declared as part of the type declaration:

typedef struct record_t { // type name

// ...

} record; // alias

record* p; // declare a pointer to record

Note that record is the alias while struct record_t is the data type associated with that alias. We can use the type or the alias as we see fit, however type aliases are more convenient.

We can also declare pointer aliases in the type declaration:

typedef struct record_t {

// ...

} record, *record_ptr;

record_ptr p; // declare a pointer to record

However, this latter technique is best avoided because we can often end up inadvertently declaring pointer to pointer types instead of pointers:

record_ptr* p; // equivalent to: record** p; -- possibly erroneous

Note that you cannot use a type alias within a self-referential data type, such as a node with embedded pointers to other nodes (as used in linked list implementations):

typedef struct node_t {

int data;

node* next; // error: node is not (yet) defined

node* prev; // error: node is not (yet) defined

} node;

To fix these errors, use the actual type name:

typedef struct node_t {

int data;

struct node_t* next;

struct node_t* prev;

} node;

What are the characteristics of a structured program?

A structured program is not OO, yet still exhibits low coupling and high cohesion.

What is a parallel array?

a parallel is data structure for representing array of records.

What is string.h?

With the string.h header file you can do special string functions such as

----

strcat() Sticks two strings together, one at the end of another.

strncat() Sticks a given amount of characters from two strings together, one at the end of another.

strchr() Returns the location of a character in a string from the beginning.

strrchr() Returns the location of a character in a string from the end.

strcmp() Compares 2 strings and returns the value 0 if both match.

strcasecmp() Compares 2 strings ignoring case and returns the value 0 if both match.

strncasecmp() Compares only a set number of characters in 2 strings ignoring case.

strcpy() Copies one string into another.

strncpy() Copies only a set number of characters from one string to another.

strlen() Returns the length of the string (Except the final NULL character).

The null character is \0 which dignifies the end of a string.

strstr() Locates one string inside of another.

Remember to use these you MUST include the string.h header file by typing #include <string.h>.

Difference between function and recursive variable?

A function can map for sets with infinite elements. Recursive variables, being 'algorithms of algorithms', are restricted to finite elements.

What is flowchart and explain its symbols?

it is diagram which illustrate the flow of goods ,information ,people and transport at a given time and place

What are the merits and demerits of while loop and do while loop in c plus plus?

You use a while() loop when you want to test a condition before entering a loop for the first time, which may bypass the loop completely. The condition is also tested before beginning each iteration.

A do..while() loop always executes the loop at least once, and tests the condition at the end of each iteration before beginning a new iteration.

Algorithm to find whether given string is palidrome?

Let L = length of the string.

greatest integer (L/2) gives the number of pairs of characters that must be compared.

For example if there are 10 letters 10/2 = 5.

abcdeedcba

If there are 11 letters, 11/5= 5.5, also 5. The 6th letter won't matter in this palindrome because it doesn't have to match another character.

abcdefedcba

Once we have the number of pairs [L/2], we need only run a loop,

as 'i' goes from 1 to [L/2]

check that "character i" = "character L+1-i"

C program for linear search using non recursive functiontion?

void main(){

int a[10],i,n,m,c=0;

clrscr();

printf("Enter the size of an array");

scanf("%d",&n);

printf("\nEnter the elements of the array");

for(i=0;i<=n-1;i++){

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

}

printf("\nThe elements of an array are");

for(i=0;i<=n-1;i++){

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

}

printf("\nEnter the number to be search");

scanf("%d",&m);

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

{

if(a[i]==m)

{

c=1;

break;

}

}

if(c==0)

printf("\nThe number is not in the list");

else

printf("\nThe number is found");

getch();

}

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

Answer by Jatinder Pal Singh


#include
#include
void main()
{
clrscr();
int i,n,item,a[20];
printf("\nEnter no of elements=");
scanf("%d",&n);
printf("\nEnter %d elements=",n);
for(i=0;i{
scanf("%d",&a[i]);
}
printf("\nEnter the element to be search=");
scanf("%d",&item);
for(i=0;i{
if(a[i]==item)
{
printf("\n\t%d is present at position %d",a[i],i+1);
break;
}
}
if(i==n)
printf("\nElement is not Present");
getch();
}