How do you write a c program to find out whether the given input string is an identifier or not?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,flag=0,m;
char s[5][10]={"if","else","goto","continue","return"},st[10];
clrscr();
printf("\n enter the string");
gets(st);
for(i=0;i<5;i++)
{
m=strcmp(st,s[i]);
if(m==0)
flag=1;
}
if(flag==0)
printf("\n it is not keyword");
else
printf("\n it is a keyword");
getch();
}
C language program to print Pascal Triangle?
#include <iostream.h>
double fact(double n)
{
return (n > 1) ? n * fact(n - 1) : 1;
}
double ncr(int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
int main()
{
for (int i = 0; i < 15; i++)
{
for (int j = 0; j <= i; j++)
cout <<ncr(i, j) << ' '; cout << endl;
}
return 0;
}
How do you write an algorithm to find the number of permutations or combinations?
Permutations and combinations are two separate things. Although we often use them interchangeably in English, we need a more precise definition in mathematics, such that ABC and CBA are regarded as being two different permutations of the same combination. In other words, the order of the elements is important in a permutation but is completely irrelevant in a combination.
First we have to define what it means to create a combination or permutation. Typically we have a set from which we must make a subset. The number of elements in the set is typically defined using the variable n while the number of elements in the subset is r. Thus we can formally define a permutation mathematically using the function P(n,r) and a combination as C(n,r).
We must also consider whether elements may be repeated within a combination or a permutation. For instance, when selecting numbers for a lottery, no number may be repeated in any combination but in a 4-digit combination lock, any digit may be repeated in a permutation.
Note that a combination lock is really a permutation lock in mathematics and is the perverse way of remembering the mathematical difference between a combination and a permutation.
Thus we have 4 possible variations to cater for. In order of difficulty, they are:
Let's deal with them one at a time.
1. Permutations with repetition
To calculate P(n,r) with repetitions, for every selection, r, there are always n possibilities, thus we have n^r permutations.
In a 3-digit combination lock, each digit has ten possibilities, 0 through 9, so there are 10^3=10x10x10=1000 permutations. This stands to reason because the permutations form all of the numeric values from 000 through to 999, which is 1000 different values.
In C, we can write this function as:
unsigned long long permutations (unsigned long long n, unsigned long long r) {
return pow(n,r); /* use standard library function */
}
2. Permutations without repetition
To calculate P(n,r) without repetitions we must reduce the set by one element each time we make a selection. If we go back to our 3-digit combination lock, we have 10 choices for the first digit which leaves 9 choices for the next and 8 for the next. So instead of 10x10x10=1000 permutations we only have 10x9x8=720 permutations.
Although fairly simple to work out in this case, we need a formula that is generalised to cater for all cases, just as n^r works for all permutations with repetitions.
We can see that 10x9x8 is the initial product of 10! (factorial 10) which is 10x9x8x7x6x5x4x3x2x1. So we need a formula that ignores everything after the 8. The portion after the 8 is 7x6x5x4x3x2x1 which is 7! and we can calculate that from (n-r)!=(10-3)!=7!
Having determined the portion we need to ignore, the rules of multiplication and division state that if we multiply by x and subsequently divide by x, then the two x's must cancel each other out. Thus we get:
10!/7!=(10x9x8x7!)/7!=10x9x8=720
Using formal notation, P(n,r) without repetition is therefore n!/(n-r)!
In C, we must first write a function to calculate factorials:
unsigned long long factorial (unsigned long long n) {
return (n>1)?factorial(n-1):1; /* recursive function */
}
With that in place, we can now write a function to handle permutations without repetitions:
unsigned long long permutations_norep (unsigned long long n, unsigned long long r) {
return factorial(n)/factorial(n-r);
}
3. Combinations without repetition
C(n,r) without repetition is simply an extension of P(n,r) without repetition. Every combination of r has r! permutations, so if we divide P(n,r) by r! we will get C(n,r). Expressing this formally, C(n,r) without repetition is n!/((n-r)!r!)
Going back to our 3-digits from 10, there are 10!/((10-3)!3!)=10!/(7!3!)=(10x9x8x7!)/(7!3!)=(10x9x8)/3!=720/6=120 combinations without repetition.
Using the factorial function shown above, we can write a C function to handle combinations without repetition:
unsigned long long combinations_norep (unsigned long long n, unsigned long long r) {
return factorial(n)/(factorial(n-r)*factorial(r));
}
4. Combinations with repetition
Combinations with repetition is the hardest concept to wrap your head around.
Going back to our 3-digits from 10, let's begin enumerating all the combinations so we can verify the answer at the end. We start by enumerating all those that combinations that begin with a 0:
000, 001, 002, 003, 004, 005, 006, 007, 008, 009
011, 012, 013, 014, 015, 016, 017, 018, 019
022, 023, 024, 025, 026, 027, 028, 029
033, 034, 035, 036, 037, 038, 039
044, 045, 046, 047, 048, 049
055, 056, 057, 058, 059
066, 067, 068, 069
077, 078, 079
088, 089
099
Note that there is no 010 because it is a permutation of 001. Similarly with 021 which is a permutation of 012. As a result of this, each row has one less combination than the one above. Thus there are 10+9+8+7+6+5+4+3+2+1=55 combinations.
If we now enumerate all those that begin with a 1, we see a similar pattern emerges:
111, 112, 113, 114, 115, 116, 117, 118, 119
122, 123, 124, 125, 126, 127, 128, 129
133, 134, 135, 136, 137, 138, 139
144, 145, 146, 147, 148, 149
155, 156, 157, 158, 159
166, 167, 168, 169
177, 178, 179
188, 189
199
This time we have 9+8+7+6+5+4+3+2+1=45 combinations.
Following the same logic, the next section must have 8+7+6+5+4+3+2+1=36 combinations, followed by 28, 21, 15, 10, 6, 3 and finally 1. Thus there are 220 combinations in total.
The formula to work this out is quite complex, however it becomes simpler when we look at the problem in a different way. Suppose we have 10 boxes and each box holds at least 3 of the same digit. We can number these boxes 0 through 9 according to those digits. Let us also suppose that we can only move in one direction, from box 0 to box 9, and we must stop at every box along the way. This means we must make 9 transitions from one box to the next.
While we along the row, we carry a tray with 3 slots. Whenever we stop at a box (including box 0 where we start from) we can either pick a number from the box or we can move onto the next box. if we pick a number, we place it in the first slot. We can then pick another or we can move on. When we have filled all the slots, we simply move on until we reach box 9. If we reach box 9 and still have slots available, we must pick as many 9s as we need to fill the remaining slots.
It probably sounds far more complex than it really is. By imagining a selection being done this way we can create a convenient binary notation. For instance, if we say that 1 means pick a number and 0 means move onto the next box, the sequence 101010000000 would tell us we selected the combination 123 while the sequence 000000000111 tells us we selected 999. Every combination is therefore reduced to 12-bit value containing exactly three 1s and nine 0s, and it is these specific combinations we are actually looking for.
C(n,r) with repetition is formally expressed as (r+n-1)!/(r!(n-1)!)
If we plug in the actual numbers we find:
=(3+10-1)!/(3!(10-1)!)
=12!/(3!9!)
=(12x11x10x9!)/(3!9!)
=(12x11x10)/3!
=1320/(3x2x1)
=1320/6
=220 combinations with repetition.
This type of problem might be expressed in other ways. For example, how many different ways can we fill a box with 100 sweets from 30 different sweets. C(n,r) is C(30,100) thus we find:
=(100+30-1)!/(100!(30-1)!)
=129!/(100!29!)
=(129x128x127x...x101x100!)/(100!29!)
=(129x128x127x...x101)/29!
=5.3302324527079900778691094496787e+59/8,841,761,993,739,701,954,543,616,000,000
=60,284,731,216,266,553,294,577,246,880 combinations with repetition.
In C we can use the following function in conjunction with the factorial function shown earlier:
unsigned long long combinations (unsigned long long n, unsigned long long r) {
return factorial(n+r-1)/(factorial(r)*factorial(n-1));
}
We might also have similar problems with an additional restriction. For instance, we might be asked to select 100 sweets from 30 different sweets selecting at least 1 of each type. This reduces the number of slots to 100-30=70 but we have the same number of transitions, so we get:
=(70+30-1)!/(100!(30-1)!)
=99!/(70!29!)
=(99x98x97x...x71x70!)/(70!29!)
=(99x98x97x...x71)/29!
=7.7910971370578048745872324992773e+55/8,841,761,993,739,701,954,543,616,000,000
=8,811,701,946,483,283,447,189,128 combinations with repetition.
To accommodate this caveat, we can use the following function instead:
unsigned long long combinations2 (unsigned long long n, unsigned long long r) {
return factorial(n-1)/(factorial(r)*factorial(n-1));
}
Pre increment and post increment?
Both increment the value of the variable by one. The difference is the value of the increments expression itself. With preincrement value is taken after incrementing, and with postincrement value is taken before incrementing.
Example:
Let x have value 5.
y = ++x;
Both y and x are assigned value 6.
Again let x have value 5.
y = x++;
y is assigned value 5. x is assigned value 6.
extern "C"
{
int printf(const char *format,..);
}
int main()
{
printf("Hello world");
}
coz all the include statement does is copy the requested file at the asked location.
What is difference between stack pointer and program counter?
Both of them are pointers, but otherwise they are completely unrelated. The former points to the current position of the stack, the latter points to the current instruction of the program.
import java.util.Scanner;
public class NumberSystem
{
public void displayConversion()
{
Scanner input = new Scanner(System.in);
System.out.printf("%-20s%-20s%-20s%-20s\n", "Decimal",
"Binary", "Octal", "Hexadecimal");
for ( int i = 1; i <= 256; i++ )
{
String binary = Integer.toBinaryString(i);
String octal = Integer.toOctalString(i);
String hexadecimal = Integer.toHexString(i);
System.out.format("%-20d%-20s%-20s%-20s\n", i,
binary, octal, hexadecimal);
}
}
// returns a string representation of the decimal number in binary
public String toBinaryString( int dec )
{
String binary = " ";
while (dec >= 1 )
{
int value = dec % 2;
binary = value + binary;
dec /= 2;
}
return binary;
}
//returns a string representation of the number in octal
public String toOctalString( int dec )
{
String octal = " ";
while ( dec >= 1 )
{
int value = dec % 8;
octal = value + octal;
dec /= 8;
}
return octal;
}
public String toHexString( int dec )
{
String hexadecimal = " ";
while ( dec >= 1 )
{
int value = dec % 16;
switch (value)
{
case 10:
hexadecimal = "A" + hexadecimal;
break;
case 11:
hexadecimal = "B" + hexadecimal;
break;
case 12:
hexadecimal = "C" + hexadecimal;
break;
case 13:
hexadecimal = "D" + hexadecimal;
break;
case 14:
hexadecimal = "E" + hexadecimal;
break;
case 15:
hexadecimal = "F" + hexadecimal;
break;
default:
hexadecimal = value + hexadecimal;
break;
}
dec /= 16;
}
return hexadecimal;
}
public static void main( String args[])
{
NumberSystem apps = new NumberSystem();
apps.displayConversion();
}
}
What is overloading function in c and explain with example?
Short answer: You can't.
Long answer: Function overloading is when you define two functions with the same name that take different arguments, e.g.:
void pickNose(int fingerId);
void pickNose(Finger finger);
This is a valid construct in C++, which supports function overloading. If you try this in C, you'll get some error about function already defined.
What do you mean by a low level language?
A low-level programming language is one that has little to no abstraction between the source code and the machine code produced by the language translator. Machine code has no abstraction whatsoever and is the lowest possible level of coding (machine code is the native language of the machine). Assembly language has very little abstraction because each mnemonic either maps 1:1 with a specific machine operation code (opcode), or maps to one of several opcodes that only differ by the operand types and can be implied from those operands.
Given the lack of abstraction, low-level code is machine-dependent code and is therefore non-portable. That is, code is written specifically to suit the assembler and thus the machine it was intended to execute upon. Conversely, high-level code has a high-level of abstraction and is generally portable. That is, code is written to suit the language compiler or interpreter rather than underlying hardware. High-level languages generally provide a much more convenient method of producing low-level code that is much easier for humans to read, write and maintain, largely due to the high-level of abstraction these languages provide.
Yes and no. It really depends on what the programmer decides. A GUI interface is usually composed of many objects, some of which may be part of the operating system's development kit (common controls), some of which may be user-defined and others which may be defined by a third-party. However, all objects respond to messages whenever a mouse cursor enters or leaves an object, or otherwise interacts with the object (moves or clicks upon the object). A programmer can intercept these messages and decide how the cursor should change, if at all.
How do you access and store the elements of array?
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a[10],i;//array declaration
clrscr();
printf("\n enter the elements of array");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\n the elements you enter into the array");
for(i=0;i<10;i++)
printf("%5d",a[i]);
getch();
return 0;
}
How procedural oriented programming language is interdependency?
Function which uses other function as part of it programming is known as function interdependent
How do you handle object array?
Exactly as you would any other type of array. An object's size is determined in the same way a structure's size is determined, by the sum total size of its member variables, plus any padding incurred by alignment.
However, you cannot create arrays of base classes. Arrays of objects can only be created when the class of object is final; a class that has a private default constructor, otherwise known as a "leaf" class. This is because derived classes can vary in size; array elements must all be the same size.
To create an array of base classes you must create an array of pointers to those base classes instead. Pointers are always the same size (4 bytes on a 32-bit system).
Static arrays are ideally suited to arrays of leaf objects where the number of objects never changes, or the maximum number of objects is finite and fixed. Although you can use dynamic arrays of leaf objects, you will incur a performance penalty every time the array needs to be resized, because every object's copy constructor must be called during the reallocation. Dynamic arrays are better suited to arrays of pointers to objects -- only the pointers need to be copied during resizing, not the objects they point to.
Write a program in c to draw a star and rotate it?
// asterisk pyramid
#include<iostream>
using namespace std;
int main()
{
int count = 0;
for (int width = 0; width <= 10; width++)
{
for (int alignLeft = width; alignLeft <= 15; alignLeft++)
{
cout << " ";
}
for (int space = 1; space < count; space++)
{
cout << "*";
}
cout << endl;
count += 2;
}
return 0;
}
What is do while loop in VB 6?
Structure:
do (while(expression) or until(expression))
.
.
.
loop (while(expression) or until(expression))
This is called a loop in VB and it is used to do something more than one times.
It may be used without any of the parameters "while" or "until" but in such case you have to make your code exit of the loop or most likely your program is going to stop responding.
The while parameter is used when we want the code in the loop to be executed WHILE the expression is True.
Example:
variable = variable + 1
The until parameter is used when we want the code in the loop to be executed until the expression gets True.
Example:
variable = variable + 1
How do you make chess game in c plus plus programming?
This question cannot be answered here. Go to amazon.com and find a book about chess-programming.
What is difference between c and oops?
C is a programming language, oops is what you say when you realize you were wrong in something. Note: Some programming languages are known as object-orient languages, C is not one of them, but some derivatives of it (C++, C#, Java) are.
Many researches show that relationship between watching violence on TV and violent behavioral patterns is positively correlated. Can we statistically say from this information that viewing violence on television causes children to behave in a violent way?
Explai the nature of the various types queues in data structures?
The queue is a linear data structure where operations of insertion and deletion are performed at separate ends also known as front and rear. Queue is a FIFO structure that is first in first out. A circular queue is similar to the normal queue with the difference that queue is circular queue ; that is pointer rear can point to beginning of the queue when it reaches at the end of the queue. Advantage of this type of queue is that empty location let due to deletion of elements using front pointer can again be filled using rear pointer. A priority queue is a queue in which each element is inserted or deleted on the basis of their priority. A higher priority element is added first before any lower priority element. If in case priority of two element is same then they are added to the queue on FCFS basis (first come first serve). Mainly there are two kinds of priority queue: 1) Static priority queue 2) Dynamic priority queue A double ended queue (or deque ) is a queue where insertion and deletion can be performed at both end that is front pointer can be used for insertion (apart from its usual operation i.e. deletion) and rear pointer can be used for deletion (apart from its usual operation i.e. insertion)