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

Wap to merge two arrays using c?

#include<stdio.h>

void main()

{

int a[5],b[5];

int c[10];

int i,j,temp,k;

printf("\n enter the elements of array A:=\n");

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

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

printf("\n enter the elements of array B:=\n");

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

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

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

{

for(j=0;j<5-i;j++)

{

if(a[j]>a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

for(j=0;j<5-i;j++)

{

if(b[j]>b[j+1])

{

temp=b[j];

b[j]=b[j+1];

b[j+1]=temp;

}

}

}

printf("\n the elements of the array A:=\n");

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

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

printf("\n the elements of the array B:=\n");

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

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

i=0,j=0,k=0;

while(i<5&&j<5)

{

if(a[i]>b[j])

c[k++]=b[j++];

if(a[i]<b[j])

c[k++]=a[i++];

}

if(i<5&&j==5)

while(i<5)

c[k++]=a[i++];

if(i==5&&j<5)

while(j<5)

c[k++]=b[j++];

printf("\n the elements of the sorted merged array C:=\n");

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

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

}

How will you Write a c program to find the kth smallest element in an array in c?

//This is for kth largest element. (So this is for n-k smallest element) //Sudipta Kundu [Wipro Technologies] #include <stdio.h> //Input: array with index range [first, last)

//Output: new index of the pivot. An element in the middle is chosen to be a pivot. Then the array's elements are

//placed in such way that all elements <= pivot are to the left and all elements >= pivot are to the right.

int positionPivot(int* array, int first, int last); //Input: array with index range [first, last) and integer K (first <= K < last)

//Output: array whose Kth element (i.e. array[K]) has the "correct" position. More precisely,

//array[first ... K - 1] <= array[K] <= array[K + 1 ... last - 1]

void positionKthElement(int* array, int first, int last, int k); int main() {

int array[] = {7,1,8,3,1,9,4,8};

int i;

for (i = 0; i < 8; i++) {

positionKthElement(array, 0, sizeof(array) / sizeof(array[0]),i);

printf("%d is at position %d\n", array[i], i);

}

return 0;

}

int positionPivot(int* array, int first, int last) {

if (first last)

return first; int tmp = (first + last) / 2;

int pivot = array[tmp];

int movingUp = first + 1;

int movingDown = last - 1;

array[tmp] = array[first];

array[first] = pivot;

while (movingUp <= movingDown) {

while (movingUp <= movingDown && array[movingUp] < pivot)

++movingUp;

while (pivot < array[movingDown])

--movingDown;

if (movingUp <= movingDown) {

tmp = array[movingUp];

array[movingUp] = array[movingDown];

array[movingDown] = tmp;

++movingUp;

--movingDown;

}

}

array[first] = array[movingDown];

array[movingDown] = pivot;

return movingDown;

} void positionKthElement(int* array, int first, int last, int k) {

int index;

while ((index = positionPivot(array, first, last)) != k) {

if (k < index)

last = index;

else

first = index + 1;

}

}

Types of data in reseach methodology?

experimental, survey,non creative and secondary analysis research, last analysis of quantitative data.

C program to calculate percentage of 3 subject marks?

#include
#include
void main()
{
float avg(int m1,int m2,int m3);
clrscr();
printf("\nAverage of 3 Subjects: %.2f",avg(78,85,95));
getch();
}
float avg(int m1,int m2,int m3)
{
return (m1+m2+m3)/3;
}

Implementation of stack using recursion?

public void reverse(Stack st)

{

int m = (int)st.Pop();

if (st.Count != 1)

reverse(st);

Push(st , m);

}

public void Push(Stack st , int a)

{

int m = (int)st.Pop();

if (st.Count != 0)

Push(st , a);

else

st.Push(a);

st.Push(m);

}

What are the Advantages of files in C language?

data files are permanent storage. where as normal data types are volatile, they will save the values as long as the program runs. saving a file will provide us the flexibility to recover the saved data whenever required.

What is the maximum number of nodes that you can have on a stack linked list?

You can have as many as you can fit in memory, which is dependent on size of each node, OS, amount of RAM, etc.

Two linked list in a array is possible?

Like this:

#define MAXLIST 100

int first, next [MAXLIST];

/* let the list be: #0 ---> #2 ---> #1 */

first= 0;

next[0]= 2;

next[2]= 1;

next[1]= -1; /* means end of list */

Note: you should track which elements are unused,

in the beginning every elements are unused:

int first_unused= 0;

for (i= 0; i<MAXLIST; ++i) next[i]= i+1;

next[MAXLIST-1]= -1; /* means end of list */

C program to find the lowest digit of a number?

#include<stdio.h>

#include<conio.h>

void main()

{

int n,d[10],i,k,s;

clrscr();

printf("enter the number..:");

scanf("%d",&n);

for(i=0,t=n;t>0;i++)

{

d[i]=t%10;

t=t/10;

}

k=i;

s=d[0];

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

{

if(d[i]<s)

s=d[i];

}

printf("\n The smallest digit is...%d",s);

getch();

}

Program to drow graphical circle in c?

Standard C does not include any graphical functions thus there is no standard method for drawing circles. For that you will need a graphics library suited to your specific hardware and operating system. Specific methods will vary according to which library you use, however there are also generic library's available depending on your hardware's capabilities. Consult your library's documentation; there will typically be simple examples to demonstrate the library's core features, including drawing circles.

What kind of diagram graphically illustrates the structure of a program?

A flowchart is used to illustrate the logic of the program. To illustrate the structure we typically use a hierarchical tree, where the main function serves as the root.

How do you write a c program to swap the values of two variables in unix?

#include<stdio.h>

void main()

{

int a=2,b=4;

printf("Program for swapping two numbers ");

printf("Numbers before swapping");

printf("a=%d and b=%d",a,b);

a=((a+b)-(b=a));

printf("Numbers after swapping");

printf("a=%d and b=%d",a,b);

getch();

}

How do you write a C program to print the table from 1 to n side by side?

#include
#include
void main()
{ int
printf("\nEnter the no. upto which the multiplication table is to be displayed:");
scanf("%d",&n);

for(i=1;i<=n;i++)
{ printf("\n%d TABLE:\n",i);
for(j=1;j<=10;j++)
printf("d=%d\n",i,j,i*j);
}
getch();
}
If the ans helps you,plz increase the trust point.

How do you make a C plus plus program that arrange the numbers from highest to lowest?

Use the std::vector template class (in header <vector>) to store the numbers, then apply the std::vector.sort() algorithm. The default sort order is ascending. The elements must support the less-than operator (<). All the built-in primitives (int, char, double and float) already support this operator.

How do you create our own header file in c plus plus?

1. open word processing software. (Notepad or wordpad)

2. Write the desired function for the header.

3. Save it with .h as extension. Like "myheader.h"

4. Run cpp.

5. It should be like this... #include "myheader.h" 1. A header file is not any different to the compiler than ordinary code

2. Its just a convenient way to have commonly "included" code

3. It is primarily used for prototypes, not actual code, such as declaring an API

4. Be care with redeclarations. The #ifdef / #endif pragmas can help

5. The location of "user defined" headers is different than system headers

6. They are normally in the build directory, whereas system headers are elsewhere

Write a c program that reverse a given integer number and check whether the number is palindrome or not?

#include <stdio.h>

#include <string.h>

#define N 100

#define PALINDROME 0

#define NONPALINDROME 1

/*Program that tells you whether what you enter is a palindrome or not*/

char pal[N]; //input line

int i; //counter

int k; //counter

int tag;

int flag = PALINDROME; /*flag=0 ->palindrome, flag =1 ->nonpalindrome*/

int main()

{

printf("Enter something: \n");

scanf("%s", pal);

tag = strlen(pal); /*determine length of string*/

/* pointer running from the beginning and the end simultaneously

looking for two characters that don't match */

/* initially assumed that string IS a palindrome */

/* the following for loop looks for inequality and flags the string as

a non palindrome the moment two characters don't match */

for (i=0,k=tag-1; i=0; i++,k--) {

if(pal[i] != pal[k]) {

flag=NONPALINDROME;

break;

}

}

if(flag == PALINDROME) {

printf("This is a palindrome\n");

}

else {

printf("This is NOT a palindrome\n");

}

return 0;

}

#include <stdio.h>

#include <string.h>

#define N 100

#define PALINDROME 0

#define NONPALINDROME 1

/*Program that tells you whether what you enter is a palindrome or not*/

char pal[N]; //input line

int i; //counter

int k; //counter

int tag;

int flag = PALINDROME; /*flag=0 ->palindrome, flag =1 ->nonpalindrome*/

int main()

{

printf("Enter something: \n");

scanf("%s", pal);

tag = strlen(pal); /*determine length of string*/

/* pointer running from the beginning and the end simultaneously

looking for two characters that don't match */

/* initially assumed that string IS a palindrome */

/* the following for loop looks for inequality and flags the string as

a non palindrome the moment two characters don't match */

for (i=0,k=tag-1; i=0; i++,k--) {

if(pal[i] != pal[k]) {

flag=NONPALINDROME;

break;

}

}

if(flag == PALINDROME) {

printf("This is a palindrome\n");

}

else {

printf("This is NOT a palindrome\n");

}

return 0;

}

Characteristics of an algorithm?

An algorithm is written in simple English and is not a formal document. An algorithm must:

- be lucid, precise and unambiguous

- give the correct solution in all cases

- eventually end

Also note it is important to use indentation when writing solution algorithm because it helps to differentiate between the different control structures.

1) Finiteness: - an algorithm terminates after a finite numbers of steps. 2) Definiteness: - each step in algorithm is unambiguous. This means that the action specified by the step cannot be interpreted (explain the meaning of) in multiple ways & can be performed without any confusion. 3) Input:- an algorithm accepts zero or more inputs 4) Output:- it produces at least one output. 5) Effectiveness:- it consists of basic instructions that are realizable. This means that the instructions can be performed by using the given inputs in a finite amount of time.

What is difference between c plus plus and data structures?

Data structure is nothing but a way to organize and

manipulate any given set of data in a specific and reusable

format/structure hence simplifying the manipulation of data.

Some of the commonly and frequently used data structures

are Stack, Queue, List, Set, e.t.c. But this list is not

limited to what we see here, rather we can invent our own

as long as there is a definite structure and better

efficiency by using it than work with raw data.

What does the header file in c consists of?

Headers are primarily used to separate interfaces from implementations in order to provide modularity and organisation of code. Headers typically contain declarations of related data types, classes and functions while corresponding source files contain the implementations or definitions for those types. The only real exceptions are template functions and classes which must be fully-defined within the header.

By separating the interfaces from the implementations, other source files can make use of those interfaces simply by including the appropriate headers. All headers must use header guards to ensure each is only included once in any compilation.

Headers can also include other headers, however this is only necessary if the header requires access to the interface contained therein. For instance, if the header declares a derived class, the base class header must be included as the derived class declaration needs to know the interface details of its base class. Similarly if a class contains an embedded class member, the interface for that member must be known. Pointers and references to types do not require interfaces, thus a forward declaration suffices. However, if a header includes an inline implementation that requires an interface (such as an accessor that returns a type by value, or that invokes a method of a type), the appropriate header for that type must be included.

All types that can be forward declared in a header must be included in the header's corresponding source file.

The one exception to separating interface from implementations is when creating template functions or classes. Templates must be fully-defined thus all implementation details must be available from the header alone. One way of maintaining separation is to have the header include the source file rather than the other way around. However, the inclusion must come after the interface declaration, and the source must not include the header.

How do you write a program to arrange the numbers in ascending order using 8085 microprocessor?

MVI B, 09 : Initialize counter

START : LXI H, 2200H: Initialize memory pointer

MVI C, 09H : Initialize counter 2

BACK: MOV A, M : Get the number

INX H : Increment memory pointer

CMP M : Compare number with next number

JC SKIP : If less, don't interchange

JZ SKIP : If equal, don't interchange

MOV D, M

MOV M, A

DCX H

MOV M, D

INX H : Interchange two numbers

SKIP:DCR C : Decrement counter 2

JNZ BACK : If not zero, repeat

DCR B : Decrement counter 1

JNZ START

HLT : Terminate program execution

How do you declare a two dimensional array with initialiser in C programming?

#include <iostream>

#include <iomanip>

using std::cout;

using std::endl;

using std::setw;

int main()

{

static const char *Rush[3][2] = {

{ "Geddy Lee", "Vocals, bass and keyboards" },

{ "Alex Lifeson", "Lead/rhythm guitar" },

{ "Neil Peart", "Drums & percussion" }};

cout << "Personnel:" << endl << endl;

for (int nPerson=0; nPerson<3; ++nPerson)

cout << setw(16) << Rush[nPerson][0] << " : " << Rush[nPerson][1] << endl;

cout << endl;

return( 0 );

}

Write an 8086 assembly language program to compare if two strings are of the same length?

org 100h

.data

str1 db "Computer"

str2 db "computer"

mes1 db "string are same $"

mes2 db "string are different $"

.code

assume cs:code,ds:data

start: mov ax,@data

mov ds,ax

mov es,ax

mov si,offset str1

mov di,offset str2

cld

mov cx,8

repe cmpsb

mov ah,9

jz skip

lea dx,mes2

jmp over

skip: lea dx,mes1

over: int 21h

mov ax,4c00h

int 21h

end start

ret

What is the binary search tree worst case time complexity?

Binary search is a log n type of search, because the number of operations required to find an element is proportional to the log base 2 of the number of elements. This is because binary search is a successive halving operation, where each step cuts the number of choices in half. This is a log base 2 sequence.

C program for prime number?

A number is a prime number if it is divisible by only 1 and itself. So make use of for loop and check for this condition.

int i,num,flag = 0;//num is the number to check

scanf("%d", &num);

for( i=2; i

if( num%i == 0)//it has other divisors

flag = 1;

}

if( !flag)

printf("The number is prime\n");

else

printf("The number is not prime\n");