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

Logic for Fibonacci series in delphi?

i=0 j=0 sum=1 limit = ARGV[0] while sum < limit print sum i = j j = sum sum = i + j end

How do you read more than one line in C?

#include <stdio.h>

#include <string.h>

/* To read something into a string in C you would do the following: */

char * str;

str = malloc(100 * sizeof(char));

scanf("%s", &str);

/* Another way to do the same thing is to read character by character: */

for(int i = 0; (i < 100) && (*(str + i) != '\n') ; i++)

scanf("%c", str + i);

/* This code told the computer to keep reading into str until the computer reads a '\n'

or until the computer reads 100 characters (which is how much I mallocated for str) */

/* Changing the '\n' to 'q' means keep reading and don't stop until you find a 'q'

Write a program to implement the various operations on string such as length of string concatenation reverse of a string copy of a string to another in c anguage?

These should work, if used correctly.

// stores srcL + srcR into dest

// NOTE: dest must be at least as large as srcL + srcR

void strConcat(const char* srcL, const char* srcR, char* dest) {

const int lengthL = strLength(srcL);

const int lengthR = strLength(srcR);

// copy srcL into front of dest

int i;

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

dest[i] = srcL[i];

}

// copy srcR into the rest of dest

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

dest[lengthL + i] = srcR[i];

}

dest[lengthL + i] = '\0';

}

// copies characters from src to dest

// NOTE: dest must be at least as large as src

void strCopy(const char* src, char* dest) {

const int length = strLength(src);

// copy

int i;

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

dest[i] = src[i];

}

}

// reverses str and puts the result in buff

// NOTE: buff must be at least as large as str

void strReverse(const char* str, char* buff) {

const int length = strLength(str);

// special case for zero-length strings

if( length == 0 ) {

return;

}

// reversify

int i;

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

buff[i] = str[length - i - 1];

}

buff[i] = '\0';

}

// returns the number of characters in str

int strLength(const char* str) {

int length = 0;

// take advantage of the fact that all strings MUST end in a null character

while( str[length] != '\0' ) {

++length;

}

return length;

}

Which of these statements is the most complete description of language?

language is any information communicated in written spoken or symbolic form

Define a complex number using c structure?

Just define two fields (whatever those are called in "C" - the parts of the structure), one for the real part, one for the imaginary part.

Just define two fields (whatever those are called in "C" - the parts of the structure), one for the real part, one for the imaginary part.

Just define two fields (whatever those are called in "C" - the parts of the structure), one for the real part, one for the imaginary part.

Just define two fields (whatever those are called in "C" - the parts of the structure), one for the real part, one for the imaginary part.

How do you RSA algorithm c?

Perform encryption on the following PT using RSA and find the CT

p = 3; q = 11; M = 5

How do you make a speed calculator in c plus plus?

To calculate speed you need to know the distance travelled and the time taken to travel that distance such that Speed = Distance / Time. The result of this calculation gives the average speed without taking into account any periods of acceleration or deceleration. We can implement this using just one function:

long double speed (const long double distance, const long double duration) {

return distance / time; }

Note that we use the term "duration" rather than "time" purely to aid readability; "time" implies a specific moment in time rather than a duration.

We use a long double to cater for the widest possible range of values. However, if we want to ensure the most efficient calculation over a wide variety of numeric types, we would use a function template instead:

template<typename T>

T speed (const T& distance, const T& duration) {

return distance / duration;

}

The distance and duration variables will typically be taken from input:

int main () {

double distance, duration;

std::cout << "Enter the distance and duration: "

std::cin >> distance >> duration;

std::cout << "Speed = " << speed (distance, duration) << '\n';

}

Note that the actual units of measurement are not important to the function because speed is denoted by the given distance per given time unit. That is, if we travel 100 meters in 10 seconds, then the speed is 10 meters per second, whereas if we travel 100 miles in 10 minutes, then we are travelling at 10 miles per minute. Thus when distance is 100 and duration is 10, the speed is 10. It is up to the caller to convert these units to something more meaningful:

int main () { double distance, duration; std::cout << "Enter the distance in kilometers: ";

std::cin >> distance;

std::cout << "Enter the duration in hours: ";

std::cin >> duration;

std::cout << "Speed = " << speed (distance, duration) << " kilometers per hour\n";

}

Note that the examples do not perform any error-checking upon the input. In production code when entering numbers from input you will typically input strings instead. You will then test the strings are valid before typecasting the strings to a numeric format such as double.

How can an individual structure member be accessed in terms of its corresponding pointer variable?

By dereferencing the pointer variable. This can be achieved in two ways:

typedef struct s {

int i;

float f;

};

void f (struct s* p) {

int x = p->i; /* using pointer to member operator */

float y = (*p).f; /* using dereference operator */

}

The two methods are functionally equivalent.

Wap to calculate simple interest using c language?

ques : if principle amount is

>=10,000 then rate of interest is 20%

>=8,000 <=9999 then rate of interest is 18%

<+8,000 then rate of interest is 16%

Ans:

#include<stdio.h>

int main()

{

float si,p,t,r;

printf("enter principle amount & time");

scanf("%f%f",&p,&t);

if(p>=10000)

{

r=20;

}

else if(p>=8,000&&<=9999)

{

r=18;

}

else

{

r=16;

}

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

printf("simple interest + %f",si);

return(0);

}

How can you use an array to find the product of 4 x 13?

You can do 4 rows and 13 columns and you get 52.

product = sizeof (char [4][13]);

of course it is just a joke, in actual code you write

product = 4*13;

What is the function of fast loop?

the flow through by pass flowmeter is known as fast loop or speed loop, to reduced the time lag between sample system and sample point.

What is the worst programming language?

Answer

There is no general answer to this question.

While some languages might be better to solve a certain problem than others, every language has its field of application. Some languages might be educational (LOGO) or entertaining (LOLCODE) but that are applications anyway. Even languages like COBOL or FORTRAN still have their uses. The question is not "better or worse", it's "How does it fit my requirements".

That said, there are language features which are bad in a way which is hardly debatable. For a discussion of some of these see:

What is the worst programming language you ever worked with? (http://stackoverflow.com/questions/961942/what-is-the-worst-programming-language-you-ever-worked-with)

Answer The only true contenders for the title of "worst" are those designed to be the worst, such as INTERCAL, Malbolge and Brainf*ck. INTERCAL, in particular, is not only a terrible language, but its compilers are also designed to be terrible. They're the only compilers I can think of that would pessimize, rather than optimize, programs. The terrible languages are often turing tarpits (http://esolangs.org/wiki/Turing_tarpit), but one doesn't always imply the other.Answer


C# is a general-purpose, block structured, procedural, imperative computer programming language that is just not meant to be used in serious enterprise applications.

Answer

Those good old all-capital languages (FORTRAN, BASIC, COBOL): programs written in them are practically unreadable.

What is interrupt flag?

Interrupt flags are used to interrupt the processor on what it is doing. When the flag is triggered the processor stops what it is doing attends what the flag wants to get done and once that is done it goes back to what it was doing. It is very useful for detect bug.

Why does a program reside in ROM in an embedded system?

If it resided in RAM, it would get lost when you power off the system.

What is a program that calculates the sum of any 5 digit integer number entered by user?

#include<iostream>

unsigned sum_of_digits(unsigned num)

{

unsigned sum = 0;

do

{

sum += num%10;

} while (num/=10);

return sum;

}

int main()

{

unsigned number = 12345;

unsigned sum = sum_of_digits (number);

std::cout << "Sum of digits in " << number << " is " << sum << std::endl;

}

Write a Program to implement scaling transformation?

//write a program to scale a rectangle at given point(h,k) #include<graphics.h> #include<stdio.h> #include<conio.h> #include<math.h> int round(float); void main() { int driver,mode,x1,y1,x2,y2,x3,y3,x4,y4,theeta,h,k,a,b; float x_dash1,y_dash1,x_dash2,y_dash2,x_dash3,y_dash3,x_dash4,y_dash4; char str[10]; clrscr(); printf("ENTER THE UPPER LEFT CORNER OF THE RECTANGLE : (X1,Y1)"); scanf("%d%d",&x1,&y1); printf("ENTER THE UPPER RIGHT CORNER OF THE RECTANGLE : (X2,Y2)"); scanf("%d%d",&x2,&y2); printf("ENTER THE LOWER RIGHT CORNER OF THE RECTANGLE : (X3,Y3)"); scanf("%d%d",&x3,&y3); printf("ENTER THE LOWER LEFT CORNER OF THE RECTANGLE : (X4,Y4)"); scanf("%d%d",&x4,&y4); driver=DETECT; initgraph(&driver,&mode,"c:\\tc\\bgi"); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); line(x4,y4,x1,y1); sprintf(str,"(%d,%d)",x1,y1); outtextxy(x1-75,y1-10,str); sprintf(str,"(%d,%d)",x2,y2); outtextxy(x2+10,y2-10,str); sprintf(str,"(%d,%d)",x3,y3); outtextxy(x3+10,y3+10,str); sprintf(str,"(%d,%d)",x4,y4); outtextxy(x4-75,y4+10,str); getch(); closegraph(); printf("ENTER POINTS OF SCALING \n"); printf("ENTER POINTS FOR X-AXIS :"); scanf("%d",&h); printf("ENTER POINTS FOR Y-AXIS : "); scanf("%d",&k); printf("ENTER SIZE OF SCALING(i.e '2' for twice and '3' for thrice)\n"); printf("ENTER THE VALUE TO BE SCALED FOR x-axis : "); scanf("%d",&a); printf("ENTER THE VALUE TO BE SCALED FOR y-axis : "); scanf("%d",&b); if(a<4 & b<4) { x_dash1=x1*a-h*a+h; y_dash1=y1*b-k*b+k; x_dash2=x2*a-h*a+h; y_dash2=y2*b-k*b+k; x_dash3=x3*a-h*a+h; y_dash3=y3*b-k*b+k; x_dash4=x4*a-h*a+h; y_dash4=y4*b-k*b+k; initgraph(&driver,&mode,"c:\\tc\\bgi"); cleardevice(); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); line(x4,y4,x1,y1); setcolor(GREEN); line(x_dash1,y_dash1,x_dash2,y_dash2); line(x_dash2,y_dash2,x_dash3,y_dash3); line(x_dash3,y_dash3,x_dash4,y_dash4); line(x_dash4,y_dash4,x_dash1,y_dash1); sprintf(str,"(%d,%d)",round(x_dash1),round(y_dash1)); outtextxy(round(x_dash1),round(y_dash1),str); sprintf(str,"(%d,%d)",round(x_dash2),round(y_dash2)); outtextxy(round(x_dash2),round(y_dash2),str); sprintf(str,"(%d,%d)",round(x_dash3),round(y_dash3)); outtextxy(round(x_dash3),round(y_dash3),str); sprintf(str,"(%d,%d)",round(x_dash4),round(y_dash4)); outtextxy(round(x_dash4),round(y_dash4),str); getch(); closegraph(); } else printf("YOU HAVE ENTERED WRONG SCALLING VALUES"); getch(); } int round(float x) { int x1=x; float x2=x-x1; if(x2>0.5) x1++; return x1; }

Implement an assembly code that counts the number of vowels in a given string?

.MODEL SMALL

.STACK 100H

.DATA

PROMPT_1 DB 'Enter a string : $'

PROMPT_2 DB 0DH,0AH,'No. of Vowels = $'

PROMPT_3 DB 0DH,0AH,'No. of Consonants = $'

STRING DB 50 DUP (?)

C_VOWELS DB 'AEIOU'

S_VOWELS DB 'aeiou'

C_CONSONANTS DB 'BCDFGHJKLMNPQRSTVWXYZ'

S_CONSONANTS DB 'bcdfghjklmnpqrstvwxyz'

.CODE

MAIN PROC

MOV AX, @DATA ; initialize DS and ES

MOV DS, AX

MOV ES, AX

LEA DX, PROMPT_1 ; load and display the string PROMPT_1

MOV AH, 9

INT 21H

LEA DI, STRING ; set DI=offset address of variable STRING

CALL READ_STR ; call the procedure READ_STR

XOR DX, DX ; clear DX

LEA SI, STRING ; set SI=offset address of variable STRING

OR BX, BX ; check BX for 0

JE @EXIT ; jump to label @EXIT if BX=0

@COUNT: ; jump label

LODSB ; set AL=DS:SI

LEA DI, C_VOWELS ; set DI=offset address of variable C_VOWELS

MOV CX, 5 ; set CX=5

REPNE SCASB ; check AL is capital vowel or not

JE @INCREMENT_VOWELS ; jump to label @INCREMENT_VOWELS if AL is

; capital vowel

LEA DI, S_VOWELS ; set DI=offset address of variable S_VOWELS

MOV CX, 5 ; set CX=5

REPNE SCASB ; check AL is small vowel or not

JE @INCREMENT_VOWELS ; jump to label @INCREMENT_VOWELS if AL is

; small vowel

LEA DI, C_CONSONANTS ; set DI=offset address of variable

; C_CONSONANTS

MOV CX, 21 ; set CX=21

REPNE SCASB ; check AL is capital consonant or not

JE @INCREMENT_CONSONANTS ; jump to label @INCREMENT_CONSONANTS if AL

; is capital consonant

LEA DI, S_CONSONANTS ; set DI=offset address of variable

; S_CONSONANTS

MOV CX, 21 ; set CX=21

REPNE SCASB ; check AL is small consonant or not

JE @INCREMENT_CONSONANTS ; jump to label @INCREMENT_CONSONANTS if AL

; is small consonants

JMP @NEXT ; otherwise, jump to label @NEXT

@INCREMENT_VOWELS: ; jump label

INC DL ; increment DL

JMP @NEXT ; jump to label @NEXT

@INCREMENT_CONSONANTS: ; jump label

INC DH ; increment DH

@NEXT: ; jump label

DEC BX ; decrement BX

JNE @COUNT ; jump to label @COUNT while BX!=0

@EXIT: ; jump label

MOV CX, DX ; set CX=DX

LEA DX, PROMPT_2 ; load and display the string PROMPT_2

MOV AH, 9

INT 21H

XOR AX, AX ; clear AX

MOV AL, CL ; set AL=CL

CALL OUTDEC ; call the procedure OUTDEC

LEA DX, PROMPT_3 ; load and display the string PROMPT_3

MOV AH, 9

INT 21H

XOR AX, AX ; clear AX

MOV AL, CH ; set AL=CH

CALL OUTDEC ; call the procedure OUTDEC

MOV AH, 4CH ; return control to DOS

INT 21H

MAIN ENDP

READ_STR PROC

; this procedure will read a string from user and store it

; input : DI=offset address of the string variabel

; output : BX=number of characters read

; : DI=offset address of the string variabel

PUSH AX ; push AX onto the STACK

PUSH DI ; push DI onto the STACK

CLD ; clear direction flag

XOR BX, BX ; clear BX

@INPUT_LOOP: ; loop label

MOV AH, 1 ; set input function

INT 21H ; read a character

CMP AL, 0DH ; compare AL with CR

JE @END_INPUT ; jump to label @END_INPUT if AL=CR

CMP AL, 08H ; compare AL with 08H

JNE @NOT_BACKSPACE ; jump to label @NOT_BACKSPACE if AL!=08H

CMP BX, 0 ; compare BX with 0

JE @INPUT_ERROR ; jump to label @INPUT_ERROR if BX=0

MOV AH, 2 ; set output function

MOV DL, 20H ; set DL=20H

INT 21H ; print a character

MOV DL, 08H ; set DL=08H

INT 21H ; print a character

DEC BX ; set BX=BX-1

DEC DI ; set DI=DI-1

JMP @INPUT_LOOP ; jump to label @INPUT_LOOP

@INPUT_ERROR: ; jump label

MOV AH, 2 ; set output function

MOV DL, 07H ; set DL=07H

INT 21H ; print a character

MOV DL, 20H ; set DL=20H

INT 21H ; print a character

JMP @INPUT_LOOP ; jump to label @INPUT_LOOP

@NOT_BACKSPACE: ; jump label

STOSB ; set ES:[DI]=AL

INC BX ; set BX=BX+1

JMP @INPUT_LOOP ; jump to label @INPUT_LOOP

@END_INPUT: ; jump label

POP DI ; pop a value from STACK into DI

POP AX ; pop a value from STACK into AX

RET

READ_STR ENDP

OUTDEC PROC

; this procedure will display a decimal number

; input : AX

; output : none

PUSH BX ; push BX onto the STACK

PUSH CX ; push CX onto the STACK

PUSH DX ; push DX onto the STACK

CMP AX, 0 ; compare AX with 0

JGE @START ; jump to label @START if AX>=0

PUSH AX ; push AX onto the STACK

MOV AH, 2 ; set output function

MOV DL, "-" ; set DL='-'

INT 21H ; print the character

POP AX ; pop a value from STACK into AX

NEG AX ; take 2's complement of AX

@START: ; jump label

XOR CX, CX ; clear CX

MOV BX, 10 ; set BX=10

@OUTPUT: ; loop label

XOR DX, DX ; clear DX

DIV BX ; divide AX by BX

PUSH DX ; push DX onto the STACK

INC CX ; increment CX

OR AX, AX ; take OR of Ax with AX

JNE @OUTPUT ; jump to label @OUTPUT if ZF=0

MOV AH, 2 ; set output function

@DISPLAY: ; loop label

POP DX ; pop a value from STACK to DX

OR DL, 30H ; convert decimal to ascii code

INT 21H ; print a character

LOOP @DISPLAY ; jump to label @DISPLAY if CX!=0

POP DX ; pop a value from STACK into DX

POP CX ; pop a value from STACK into CX

POP BX ; pop a value from STACK into BX

RET ; return control to the calling procedure

OUTDEC ENDP

END MAIN

Where placed keywords in c?

Inside the symbol table of the compiler.

How do yo u program a remote to a 97 ford explorer?

I have a remote that locks and unlockes the doors. Could you plesase explain to me how to programm it plz