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 algorithm to calculate the area of a circle?

#include<stdio.h>

main()

{

int r;

float area;

clrscr();

printf("enter the value of r\n");

scanf("%d",&r);

area=3.142*r*r;

printf("area of circle=%f\n",area);

getch();

}

Write a program to accept two string and display weather they are identical or not?

I assume the program should be case-sensitive. Here is a code of such program:

#include

#include

int main() {

char str1[100];

char str2[100];

printf("Please enter first string: ");

gets(str1);

printf("Please enter second string: ");

gets(str2);

if (strcmp(str1, str2) == 0) {

printf("Strings are Equal.\n");

} else {

printf("Strings are Not Equal.\n");

}

return 0;

}

Testing:

Please enter first string: A

Please enter second string: A

Strings are Equal.

Please enter first string: a

Please enter second string: A

Strings are Not Equal.

If you want to make not case-sensitive comparing before checking you should make both string in lowercase or uppercase.

Here is how it should look:

#include

#include

#include

void upString(char *str);

int main() {

char str1[100];

char str2[100];

printf("Please enter first string: ");

gets(str1);

printf("Please enter second string: ");

gets(str2);

upString(str1);

upString(str2);

if (strcmp(str1, str2) == 0) {

printf("Strings are Equal.\n");

} else {

printf("Strings are Not Equal.\n");

}

return 0;

}

void upString(char *str) {

register int ind = 0;

while (str[ind]) {

str[ind] = toupper(str[ind]);

ind++;

}

}

Testing:

Please enter first string: aaa

Please enter second string: AAA

Strings are Equal.

Please enter first string: aaa

Please enter second string: aAa

Strings are Equal.

Note: You should not be using gets() function in real-world application. There is no way you can limit number of characters to read thus allowing to overflow buffer. It was used only for example.

Can somebody give me the program to find simple interest using friend function in c plus plus?

Here is an example program:

class obj{

public:

float p,n,r,si;

friend void calc( obj temp);

};

void calc( obj temp){

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

}

The initialization and function calling is essential.

What is the condition for the overflow in the linked lists?

In linked list if there is no any element inside it than we can say linked list is underflow.

Vmm moves 4k segments called what?

Memory manager works with pages and segments;these are different things, don't confuse them.

Can you delete Microsoft visual c plus?

To delete Microsoft Visual C/C++, like any other Windows program, simply uninstall it using Control Panel / Add-Remove Programs. For additional information, consult the readme that came with the original program.

Write a program to input two numbers and interchange there value using the third variable Give anawers in Basic method?

Public Sub Swap()

dim inta as integer

dim intb as integer

dim intc as integer

inta=4

intb=5

intb=inta

intc=intb

inta=intc

End Sub

Why in c language initgraph doesn't work?

Initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver) then putting the system into

graphics mode.Initgraph also resets all graphics settings (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.

How do you correct syntax error logical error?

Your IDE should include syntax checking, which highlights errors as they occur (similar to a grammar/spell checker in a word-processor). Attempting to compile a program that contains a syntax error will fail to compile, but it should provide a list of all the errors that need to be fixed. If the error is an obvious one, the error list may include a solution to the problem, but you must make the necessary changes manually -- the syntax checker won't modify any code for you, even if the error is an obvious one, such as using . instead of -> on a pointer.

Can you make private class in c plus plus?

yes it is possible to make a private class in C++ but this class will not solve any purpose.................

When an array name is passed to a function the function?

All array names will implicitly convert to a pointer to the first element in the array. Note that when passing an array to a function, you must also pass the array length as a separate argument because the pointer alone cannot tell you how many elements were actually allocated to the array, let alone how many are currently in use. However, there are some exceptions. For example, a null-terminated string argument does not require a length argument as the null-terminator denotes the end of the character array. User-defined arrays can use a similar technique, using any "unused" or "invalid" value or token to denote the end of the array.

Note that the following function signatures are identical:

void f (int* a, unsigned len);

void f (int a[], unsigned len);

The latter is more readable as it makes it clear the pointer refers to an array.

When passing multi-dimensional arrays, you must add an extra level of indirection for each additional dimension:

void g (int* a[], unsigned rows, unsigned cols); // two-dimensional array

void h (int** a[], unsigned width, unsigned height, unsigned depth); // three-dimensional array

Multi-dimensional arrays can also be null-terminated or terminated by a designated token value. The canonical example of this is a global main function which accepts command line arguments. These arguments are passed through a null-terminated array of null-terminated strings (a two-dimensional array of type char).

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

assert (argc>=1); // always at least one element

assert (argv[0] != NULL); // the first element is always the executable name (non-NULL)

assert (argv[argc-1] != NULL); // the last element is always non-NULL

assert (argv[argc] == NULL); // the one-past-the-end element is always NULL

return 0;

}

What is the program structure of C language and C plus plus?

There is no single structure to a C++ program. C++ is multi-paradigm and allows programmers to use any combination of C-style programming, object-oriented programming and template metaprogramming using a mixture of primitive built-in types, standard library types and user-defined types.

Which of the following types of memory contain data that cannot be modified by the user?

Pick one:

ROM, PROM, EPROM

write-protected magnetic disk/tape, CD-ROM, DVD-R

write-protected partition/file, other user's or sysadmin's file

code-segment, read-only data-segment, other user's or kernel's code- or data-segment

Write a c program to implement tower of hanoi moves?

/* hanoi.c */

#include <stdio.h>

#include <stdlib.h>

static long step;

static void Hanoi (int n, int from, int to,int spare)

{

if (n>1) Hanoi (n-1,from,spare,to);

printf ("Step %ld: move #%d %d-->%d\n", ++step, n, from, to);

if (n>1) Hanoi (n-1,spare,to,from);

}

int main (int argc, char **argv)

{

int n;

if (argc==1 (n= atoi(argv[1]))<=0) n= 5;

step= 0;

Hanoi (n, 1, 2, 3);

return 0;

}

What is a dosh header file in c?

Not sure what dosh is but dos.h was used by Turbo C/C++ to handle DOS interrupts way back in the early 90s. DOS has largely been consigned to the history books now that Windows is an OS in its own right. Until 1995 it ran on top of DOS, the actual OS, but no-one in their right mind would consider running DOS programs in Windows in this day and age. Console applications are not DOS programs, they are fully-fledged Windows programs but without a fancy GUI.

How do you write a function that counts the number of characters in a string?

As this is probably a homework question, I will give you some pseudo code:

[code]

num_chars = 0

READ ch FROM string

WHILE ch IS NOT END OF STRING

num_chars = num_chars + 1

READ ch FROM string

END WHILE

[/code]

Remember that in C, we use what are called "C-strings". C-strings are a pointer to a continuous group of characters in memory which are terminated by a null character. The null character is '\0', and has an integer value of 0.

The C-string generally points to the first character in the string. To access the value of this character, you must use the dereferencing operator, *. If you want to move to the next character, you simply add 1 to the pointer.

So if you have a C-string:

char *str = "abcd";

then:

*str '\0'

Anything past the null character is undefined. Trying to access this data is considered to be a buffer overflow, and is very dangerous.

Note that c-strings created as pointers should always be treated as immutable, as trying to change them might produce errors. Many compilers will allocate the above string inside the static data area, along with any constants or literals which can not fit inside the immediate field of an instruction.

If you want a mutable string, then declare it as a character array:

char str[] = "abcd";

This method of declaration will explicitly allocate memory on the stack to store the c string in, and as such, the string can be safely manipulated without fear of unintended side effects.

How do you find the square root of a number in C?

You write a function that evaluates the square root of its argument and returns the result to the caller.

You can also use the run-time library functions in math.h ...

double sqrt (double x);

double pow (double x, (double) 0.5);

Difference between fgets and gets?

gets is an insecure function, its careless use can lead to errors. If you

want to use gets, consider using fgets instead, supplying stdin as

the file reference parameter.

The gets function waits until a line of input is available (unless one is already

available), and consumes the whole line including the ENTER/newline at the end.

The characters on the line are stored in the string parameter, except for the

ENTER/newline, which is discarded.

returns NULL on end-of-file, otherwise the parameter s.

The parameter given to gets must be an already allocated array of characters, not an

uninitialised char * pointer; gets will never allocate memory.

{ char a[100]; gets(line); // This is correct

{ char a[100]; char *s; s=a; gets(s); // This is correct

{ char *s; s=new char[100]; gets(s); // This is correct

{ char *s; gets(s); // This is WRONG

The array given to gets must be big enough to hold any line that could conceivably be

input. C++ and C are incapable of telling how long an array is. If it is not long enough

for the data that is read, other data (and perhaps program code) will be overwritten.

Thus gets is not a safe function for use in critical applications.

What is the C plus plus program for regula falsi method?

#include

#include

#include

/* define prototype for USER-SUPPLIED function f(x) */

double ffunction(double x);

/* EXAMPLE for "ffunction" */

double ffunction(double x)

{

return (x * sin(x) - 1);

}

/* -------------------------------------------------------- */

/* Main program for algorithm 2.3 */

void main()

{

double Delta = 1E-6; /* Closeness for consecutive iterates */

double Epsilon = 1E-6; /* Tolerance for the size of f(C) */

int Max = 199; /* Maximum number of iterations */

int Satisfied = 0; /* Condition for loop termination */

double A, B; /* INPUT endpoints of the interval [A,B] */

double YA, YB; /* Function values at the interval-borders */

int K; /* Loop Counter */

double C, YC; /* new iterate and function value there */

double DX; /* change in iterate */

printf("-----------------------------------------------------\n");

printf("Please enter endpoints A and B of the interval [A,B]\n");

printf("EXAMPLE : A = 0 and B = 2. Type: 0 2 \n");

scanf("%lf %lf", &A, &B);

printf("The interval ranges from %lf to %lf\n", A,B);

YA = ffunction(A); /* compute function values */

YB = ffunction(B);

/* Check to see if YA and YB have same SIGN */

if( ( (YA >= 0) && (YB >=0) ) ( (YA < 0) && (YB < 0) ) ) {

printf("The values ffunction(A) and ffunction(B)\n");

printf("do not differ in sign.\n");

exit(0); /* exit program */

}

for(K = 1; K <= Max ; K++) {

if(Satisfied 0) { /* first 'if' */

Satisfied = 1; /* Exact root is found */

}

else if( ( (YB >= 0) && (YC >=0) ) ( (YB < 0) && (YC < 0) ) ) {

B = C; /* Squeeze from the right */

YB = YC;

}

else {

A = C; /* Squeeze from the left */

YA = YC;

}

if( (fabs(DX) < Delta) && (fabs(YC) < Epsilon) ) Satisfied = 1;

} /* end of 'for'-loop */

printf("----------------------------------------------\n");

printf("The number of performed iterations is : %d\n",K - 1);

printf("----------------------------------------------\n");

printf("The computed root of f(x) = 0 is : %lf \n",C);

printf("----------------------------------------------\n");

printf("Consecutive iterates differ by %lf\n", DX);

printf("----------------------------------------------\n");

printf("The value of the function f(C) is %lf\n",YC);

} /* End of main program */

Why you use linked list instead of arrays?

You would use linked lists instead of arrays in two instances:

1) You don't know how long your list will be and it is apt to dramatically change length.

2) You will make lots of additions and removals in the middle of your list.

Why is c language said to be portable?

Well, C is not platform dependent. You can compile C into source code on a Windows, Mac, Unix or any other operating system as long as you are using that type of computer. You could write code that can be compiled on almost any operating system. But the programs you write may or may not be able to move from system to system based on whether or not you use tools specific to that operating system. Java is not actually platform independent either because you need JVM to run it. It's just that most computers come with JVM installed.

Both of the above are wrong. The C language specification itself is platform-dependent, as there are numerous places where ambiguities (both intentional and unintentional) cause different behavior according to how both the platform AND the C-compiler writer chose to behave. Thus, while it is possible to write a C program which is highly-portable, that program is still dependent on the exact implementation of the C compiler and OS it runs on. So, the behavior of a C program depends on the platform.

The Java Language is platform INDEPENDENT, since it does NOT have the implementation ambiguities of C, and has a completely-standardized interface to all platforms (the JVM spec). Naturally, the JVM program is plaform dependent, as creating it to conform to the Java VM specification requires knowledge of the peculiarities of the platform.

Why to use header file to easy know?

in the java as we use the inheritance property in the same way we can get the the inheritance property in c by using the prepared header files( .h files).

there a single program in c use the many methods of many header files like math.h give us to use the use of floor(), sqrt() e.t.c. functions..

Write a program to display the multiplication table for a given integer up to 10 using a 'for' loop?

The for loop looks like this:

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

{

...

}

This will repeat anything inside ten times; the variable "i" will have the values 1, 2, ...10. The first part is the initial assignment, the second part specifies a condition - while it is true, the loop should continue, and the third part increments the variable(s).

Replace the "..." with anything you want to repeat 10 times, for example, a System.out.println(...) that involves the variable "i".