What Example of structured programming?
C is a structured programming language. PHP, COBOL is also a structured programming language. These languages follow a top down approach.
switch (expression)
{
case constant-value1:
statements
break [optional];
case constant-value2:
statements
break [optional];
.
.
.
default:
statements;
}
How do you use strlen function in c language?
It is very difficult, because it has as many as oneparameter, so it is very easy to get confused...
try this:
const char *s= "example";
printf ("length of '%s' is %d\n", s, (int)strlen (s));
strlen just tells you how many charters there are before the the first zero value. If you are going to create a buffer based on strlen then you must add 1 to the value that strlen returnes. The needs to be a place to store the terminating zero.
char *s= "example";
char *buffer;
buffer = (char*)malloc(strlen(s)+1);
Why Two pointers cannot be added in c language?
Because no-one knows what the sum of two pointers should be...
of course you can convert them to integers and then sum them, but why on earth would you do that?
What is single line comments as used in C programming?
single line comment are comment written in single line.in c there are two types of comment single line and multiple line.single line comment is written using // and multiple line comment is written between /*comment*/.compiler does not compile comments.it is used for better understanding of program.
What does it mean when a variable is static?
A static variable is a variable that is allocated at compile time and that remains in memory for the entire duration the program remains in memory. Contrary to the misleading answer given below, static variables ARE variables, they are NOT constants; the value is initialised at compile time but can be modified at runtime whenever the variable is within scope. Static constants are also possible, but they must be explicitly marked constant -- otherwise they are implicitly variable.
As with ordinary variables, the scope of a static variable is dependant upon where it is declared. This could be at global scope, file scope, function scope, namespace scope or class scope. However, the scope only determines the visibility of a static variable. The variable exists at all times even when not in scope so its value can persist across scopes, but the value can only be modified when the variable is currently in scope.
Global scope means the static variable has external linkage and is accessible to all code with access to the definition. File scope limits visibility to the translation unit that defines it. Function scope limits visibility to the function that defines it. Namespace scope limits visibility to the namespace that defines it, but you can still gain external access via the scope resolution operator (::).
Class scope is the most complex scope because as well as scope resolution, the variable is also subject to the class access specifier (public, protected and private) associated with it. However, another key difference is that static class member variables are scoped to the class itself, not to any instance of the class. Thus they are visible (if not accessible) even when no instances of the class exist.
***************PREVIOUS ANSWER*********************
A common misconception about and misuse of the static qualifier is:
A static variable is program variable that does not vary... go figure.
Static variables have the same value throughout run time. They can be changed at design time only.
This is actually a better description of the const modifier.
'static' is used in C programs to declare a variable that should exist throughout the lifetime of the program. When the program starts executing, all allocations are made for static variables. It has the added side-effect of making the variable not visible outside the scope of the module in which is declared. The explanation in the answer below this one describes this well.
For more information specific to java see: http://mindprod.com/jgloss/static.html Answerstatic is an access qualifier that limits the scope but causes the variable to exist for the lifetime of the program. This means a static variable is one that is not seen outside the function in which it is declared but which remains until the program terminates. It also means that the value of the variable persists between successive calls to a function. The value of such a variable will remain and may be seen even after calls to a function. One more thing is that a declaration statement of such a variable inside a function will be executed only once.
For usage try following: void fun() { static int fnvalue=0;//Executed once printf(" \n%d",fnvalue++);// Value changed retains for next call }
this code behaves like a sequence generator.
One more advantage of static variables is that, since they are created once and then exist for the life of the program, the address of the variable can be passed to modules and functions that aren't in the same C file for them to access the variable's contents. This has several advantages in programming.
For further confusions or details write me: rupesh_joshi@sify.com,rupesh.joshi@gmail.com
C++ and Java In Object-oriented languIages like C++ and Java, the static keyword has additional practical value. If you define a class-level variable as static, that variable is accessible whether there is an instance of the class or not. Furthermore, all instances of the class share the same single value. This is used in a number of ways.
Sometimes you'll have a class where you want all the instances to share a single variable (EG a bus class where all buses in the system shared the same totalRiders variable.) Often you'll define constants as static final, because they are frequently used as parameters for the constructor of the class.
Methods can also be static, which means they can be called without an instance of the class. Most often this is used to make a class act like an old-school function library. The Java Math class is a great example of this. You never instantiate the Math class, but all of its methods are static.
A static member is almost always called with the class name rather than the instance name.
"static" in programming and databases means "constant--never changing". So, a static variable could be the "node name" or "database name". Once those are set, they cannot be changed.
False. OO programs are usually slightly bigger and slower than 'standard' programs.
How do you distinguish between a text file and a binary file?
You can distinguish between binary and text files, and for the most part even identify what type of binary file, by using the "file" command. For example:
~$ file unknownfile
unknownfile: PNG image data, 155 x 155, 8-bit/color RGBA, non-interlaced
This tells you that the file is a PNG file by reading metadata and/or magic numbers in the file. When used on a text file, the command will return "ASCII text" or "Unicode text."
C program to arrange 7 numbers in ascending order?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp,a[7];
clrscr();
printf("Enter 7 integer numbers: \n");
for(i=0;i<7;i++)
scanf("%d",&a[i]);
for (i=0;i<7;i++)
{
for(j=i+1;j<7;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n\nThe 7 numbers sorted in ascending order are: \n");
for(i=0;i<7;i++)
printf("%d\t",a[i]);
getch();
}
How do you copy a string without using string functions?
There is almost nothing to explain if you know C Language. Here is the program:
#include
void copyString(const char *src, char *dest);
int main() {
char str1[100];
char str2[100];
printf("Enter the string: ");
gets(str1);
copyString(str1, str2);
printf("Copied string: %s\n", str2);
return 0;
}
void copyString(const char *src, char *dest) {
while (*dest++ = *src++);
}
As you can see actually copying is done in just one line of code. While loop stops after it reaches zero value and all strings in C language are null-terminated strings (ending with 0x00 byte at the end of string, which is zero).
Testing:
Enter the string: Hello world, we have copy function for string!
Copied string: Hello world, we have copy function for string! Note: You should not be using gets() in real application, because it is not possible to limit number of the characters to be read thus allowing to overflow buffer. You might get a warning in line with our while loop if you are compiling with -Wall option in GCC, what does -Wall is that it checks for questionable places. This place for compiler is questionable because we have assignment operation inside while loop expression. Most of the times this is common mistake in programming, but not in this situation. Compiler just give a notice for developer that we should be careful.
Flow chart for addition of two matrices?
For the resulting matrix, just add the corresponding elements from each of the matrices you add. Use coordinates, like "i" and "j", to loop through all the elements in the matrices. For example (for Java; code is similar in C):
for (i = 0; i <= height - 1; i++)
for (j = 0; j<= weidht - 1; j++)
matrix_c[i][j] = matrix_a[i][j] + matrix_b[i][j]
Write an algorithm for Knapsack Problem?
The pseudocode listed below is for the unbounded knapsack problem.
operation ub-ks (n, K)
// n is the total number of items, K is the capacity of the knapsack
{
for (int h = 0; h < K; h++)
V[0, h] = 0; // initializes the bottom row of the table
for (int i = 0; i < n; i++) {
for (int kp = 0; kp < K; kp++) {
ans = V[i-1, kp]; // case 1: item i not included
if (size[i] <= kp) { // if the ith item's size is less than kp...
other = val[i] + V[i-1, kp - size[i]];
// ...then case 2: item i is included
if (other > ans) // case 3: both are possible, so take the max
ans = other;
V[i, kp] = ans;
}
}
}
return V[n, K];
} // end ub-ks
Program to check palindrome using recursion?
#include <stdio.h>
#include <conio.h>
void main()
{
int num,rev=0,m,r;
clrscr();
printf("enter any number");
scanf("%d",&num);
m=num;
while(num>0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
if(rev==m)
printf("given number is palindrome");
else
printf("given number is not palindrome");
getch();
}
this is the answer.
Are primitive traits typical of broader it smaller clades?
Traits that evolved early, such as the hole in the hip socket, are called primitive traits.
What is a wildcard for more than one character?
In Windows and UNIX-based systems, while specifying filenames, ? is a wildcard that substitutes for exactly one character.
In SQL databases, the underscore (_) matches exactly one character.
What is the algorithm to convert miles to kilometers?
Multiply miles by 1.609344 to get kilometres:
double mile2km (double miles) { return miles * 1.609344; }
Multiply kilometres by 0.62137119 to get miles:
double km2mile (double km) { return km * 0.62137119; }
A c program to generate even numbers up to 50?
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=50;i+=2)
{
printf("\t %d", i);
}
getch();
}
What is the default value of register storage class?
Register storage class is a compiler hint that the variable will be often used, and that it should generate code, if it can, to keep the variable's value in a register.
Keyword represent something like reserved words. This includes syntactic words etc...
For eg: in C if, else, while , printf, static,extern all these are key words.
Some editors show these words in different color to make the suer understand that these are key words
What is the importance of recursion?
In computer science, complex problems are resolved using an algorithm. An algorithm is a sequence of specific but simple steps that need to be carried out in a procedural manner. Some steps may need to be repeated in which case we will use an iterative loop to repeat those steps. However, sometimes we encounter an individual step that is actually a smaller instance of the same algorithm. Such algorithms are said to be recursive.
An example of a recursive algorithm is the factorial algorithm. The factorial of a value, n, is the product of all values in the range 1 to n where n>0. If n is zero, the factorial is 1, otherwise factorial(n)=n*factorial(n-1). Thus the factorial of any positive value can be expressed in pseudocode as follows:
function factorial (num)
{
if num=0 then return 1
otherwise;
return num * factorial (num-1);
}
Note that any function that calls itself is a recursive function. Whenever we use recursion, it is important that the function has an exit condition otherwise the function would call itself repeatedly, creating an infinite recursion with no end. In this case the exit condition occurs when n is 0 which returns 1 to the previous instance. At this point the recursions begin to "unwind", such that each instance returns its product to the previous instance. Thus if num were 3, we get the following computational steps:
factorial(3) = 3 * factorial(3-1)
factorial(2) = 2 * factorial(2-1)
factorial(1) = 1 * factorial(1-1)
factorial(0) = 1
factorial(1) = 1 * 1 = 1
factorial(2) = 2 * 1 = 2
factorial(3) = 3 * 2 = 6
Note how we cannot calculate 3 * factorial(3-1) until we know what the value of factorial(3-1) actually is. It is the result of factorial(2) but, by the same token, we cannot work out 2 * factorial(2-1) until we know what factorial(2-1) is. We continue these recursions until we reach factorial(0) at which point we can begin working our way back through the recursions and thus complete each calculation in reverse order. Thus factorial(3) becomes 1*1*2*3=6.
Although algorithms that are naturally recursive imply a recursive solution, this isn't always the case in programming. The problem with recursion is that calling any function is an expensive operation -- even when it is the same function. This is because the current function must push the return address and the arguments to the function being called before it can pass control to the function. The function can then pop its arguments off the stack and process them. When the function returns, the return address is popped off the stack and control returned to that address. All of this is done automatically, Behind the Scenes, in high-level languages. Compilers can optimise away unnecessary function calls through inline expansion (replacing the function call with the actual code in the function, replacing the formal arguments with the actual arguments from the function call). However, this results in increased code size, thus the compiler has to weigh up the performance benefits of inline expansion against the decreased performance from the increased code size. With recursive functions, the benefits of inline expansion are quickly outweighed by the code expansion because each recursion must be expanded. Even if inline expansion is deemed beneficial, the compiler will often limit those expansions to a predetermined depth and use a recursive call for all remaining recursions.
Fortunately, many recursive algorithms can be implemented as an iterative loop rather than a recursive loop. This inevitably leads to a more complex algorithm, but is often more efficient than inline expansion. The factorial example shown above is a typical example. First, let's review the recursive algorithm:
function factorial (num)
{
if num=0 then return 1
otherwise;
return num * factorial (num-1);
}
This can be expressed iteratively as follows:
function factorial (num)
{
let var := 1
while 1 < num
{
var := var * num
num := num - 1
}
end while
return var;
}
In this version, we begin by initialising a variable, var, with the value 1. We then initiate an iterative loop if 1 is less than num. Inside the loop, we multiply var by num and assign the result back to var. We then decrement num. If 1 is still less than num then we perform another iteration of the loop. We continue iterating until num is 1 at which point we exit the loop. Finally, we return the value of var, which holds the factorial of the original value of num.
Note that when the original value of num is either 1 or 0 (where 1 would not be less than num), then the loop will not execute and we simply return the value of var.
Although the iterative solution is more complex than the recursive solution and the recursive solution expresses the algorithm more effectively than the iterative solution, the iterative solution is likely to be more efficient because all but one function call has been completely eliminated. Moreover, the implementation is not so complicated that it cannot be inline expanded, which would eliminate all function calls entirely. Only a performance test will tell you whether the iterative solution really is any better.
Not all recursive algorithms can be easily expressed iteratively. Divide-and-conquer algorithms are a case in point. Whereas a factorial is simply a gradual reduction of the same problem, divide-and-conquer uses two or more instances of the same problem. A typical example is the quicksort algorithm.
Quicksort is ideally suited to sorting an array. Given the lower and upper bounds of an array (a subarray), quicksort will sort that subarray. It achieves this by selecting a pivot value from the subarray and then splits the subarray into two subarrays, where values that are less than the pivot value are placed in one subarray and all other values are placed in the other subarray, with the pivot value in between the two. We then sort each of these two subarrays in turn, using exactly the same algorithm. The exit condition occurs when a subarray has fewer than 2 elements because any array (or subarray) with fewer than 2 elements can always be regarded as being a sorted array.
Since each instance of quicksort will result in two recursions (one for each half of the subarray), the total number of instances doubles with each recursion, hence it is a divide-and-conquer algorithm. However, it is a depth-first recursion, so only one of the two recursions is executing upon each recursion. Nevertheless, each instance of the function needs to keep track of the lower and upper bounds of the subarray it is processing, as well as the position of the pivot value. This is because when we return from the first recursion we need to recall those values in order to invoke the second recursion. With recursive function calls we automatically maintain those values through the call stack but with an iterative solution the function needs to maintain its own stack instead. Since we need to maintain a stack, the benefits of iteration are somewhat diminished; we might as well use the one we get for free with recursion. However, when we invoke the second recursion, we do not need to recall the values that we used to invoke that recursion because when that recursion returns the two halves of the subarray are sorted and there's nothing left to do but return to the previous instance. Knowing this we can eliminate the second recursion entirely because, once we return from the first recursion, we can simply change the lower bound of the subarray and jump back to the beginning of the function. This effectively reduces the total number of recursions by half.
When the final statement of a function is a recursive call to the same function it is known as a "tail call". Although we can manually optimise functions to eliminate tail calls, compilers that are aware of tail call recursion can perform the optimisation for us, automatically. However, since the point of tail call optimisation is to reduce the number of recursions, it pays to optimise the call upon those recursions that would normally result in the greatest depth of recursion. In the case of quicksort, the deepest recursions will always occur upon the subarray that has the most elements. Therefore, if we perform recursion upon the smaller subarray and tail call the larger subarray, we reduce the depth of recursion accordingly.
Although recursions are expensive, we shouldn't assume that iterative solutions are any less expensive. Whenever we have a choice about the implementation, it pays to do some performance tests. Quite often we will find that the benefits of iteration are not quite as significant as we might have thought while the increased complexity makes our code significantly harder to read and maintain. Wherever possible we should always try to express our ideas directly in code. However, if more complex code results in measurable improvements in performance and/or memory consumption, it makes sense to choose that route instead.
Give example of a language which uses more than one pass for compiling a program?
FORTRAN, Assembler, to name two. Effectively, any language that allows you to reference symbols before they are declared.
How many simple data types are there?
There are a total of 8 simple or primitive data types in Java. They are:
Write a c program to accept the range of all prime numbers from 1 to n by using while loop?
#include<iosys.h>
#include<math.h> // for sqrt()
bool is_prime (unsigned num) {
if (num<2) return false; // 2 is the first prime
if (!(num%2)) return num==2; // 2 is the only even prime
// largest potential factor is square root of num
unsigned max = (unsigned) sqrt ((double) num) + 1;
// test all odd factors
for (unsigned factor=3; factor<max; factor+=2) if (!(num%factor)) return false;
return true; // if we get this far, num has no factors and is therefore prime
}
int main (void) {
// test all nums from 0 to 100 inclusive
for (unsigned num=0; num<=100; ++num) {
if (is_prime (num))
printf ("%d is prime\n", num);
else if (num>0)
printf ("%d is composite\n", num);
else printf ("%d is neither prime nor composite\n", %d);
}
return 0;
}