What is the input statement for a program?
Input statements extract data from an input stream. For example:
int x;
std::cin >> x;
Output statements insert data to an output stream. For example:
std::cout << x;
You cannot insert data into an input stream and cannot extract data from an output stream. However, streams that are both input and output streams (such as read-write files) can insert and extract data as required, depending on whether you are reading or writing to the stream.
Difference between simple queue and circular queue?
Simple queue is a linear queue having front & rear var to insert & delete elements from the list.But there is a boundary that we have to insert at rear & have delete from front.For this reason instead of having space in the queue if there is a single element in the rear,the queue is full.the other space is wasted.
To utilize space properly,circular queue is derived.In this queue the elements are inserted in circular manner.So that no space is wasted at all.
Why float value cannot be used id case and switch?
It is by design.
Speaking of floating-point values, please remember that they are never accurate, so be careful. Example:
wrong: if (f==4.1) printf ("Equals to 4.1");
good: if (fabs (f-4.1)<0.0000001) printf ("Equals to 4.1");
What is push operation in data structure?
Push inserts a value onto the top of the stack.
Pop extracts the top value from the stack.
These are the two primary operations that can be performed upon a stack. Prior to popping a value, you will first check the stack is not empty, store the top value, then pop the stack. For a stack of type T, you might use the following:
if (!stack.empty()) {
T value {stack.top()}; // copy top value
stack.pop(); // remove value from stack
// use value...
}
For the greatest common factor, you can use the following to your advantage. As an example, take the numbers 14 and 10 as input.
The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 has been obtained by subtracting 14 - 10 (or, faster, to avoid repeated subtraction, take the remainder of a division: 14 % 10).
If you divide 10 % 4 (or subtract 4 twice, from 10), you get a remainder of 2, so the new set of numbers is 4 and 2.
Next step: 4 % 2 = 0. Once you get a remainder of zero, the previous number is the answer - the number that you should return. In this case, the 2.
For the least common multiple, use the property that (using a numeric example) 14 x 10 = 2 x 70 (14 and 10 are the two parameters, 2 and 70 are the greatest common factor and the least common multiple, respectively).
For the greatest common factor, you can use the following to your advantage. As an example, take the numbers 14 and 10 as input.
The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 has been obtained by subtracting 14 - 10 (or, faster, to avoid repeated subtraction, take the remainder of a division: 14 % 10).
If you divide 10 % 4 (or subtract 4 twice, from 10), you get a remainder of 2, so the new set of numbers is 4 and 2.
Next step: 4 % 2 = 0. Once you get a remainder of zero, the previous number is the answer - the number that you should return. In this case, the 2.
For the least common multiple, use the property that (using a numeric example) 14 x 10 = 2 x 70 (14 and 10 are the two parameters, 2 and 70 are the greatest common factor and the least common multiple, respectively).
For the greatest common factor, you can use the following to your advantage. As an example, take the numbers 14 and 10 as input.
The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 has been obtained by subtracting 14 - 10 (or, faster, to avoid repeated subtraction, take the remainder of a division: 14 % 10).
If you divide 10 % 4 (or subtract 4 twice, from 10), you get a remainder of 2, so the new set of numbers is 4 and 2.
Next step: 4 % 2 = 0. Once you get a remainder of zero, the previous number is the answer - the number that you should return. In this case, the 2.
For the least common multiple, use the property that (using a numeric example) 14 x 10 = 2 x 70 (14 and 10 are the two parameters, 2 and 70 are the greatest common factor and the least common multiple, respectively).
For the greatest common factor, you can use the following to your advantage. As an example, take the numbers 14 and 10 as input.
The greatest common factor of 14 and 10 is the same as the greatest common factor of 10 and 4, where 4 has been obtained by subtracting 14 - 10 (or, faster, to avoid repeated subtraction, take the remainder of a division: 14 % 10).
If you divide 10 % 4 (or subtract 4 twice, from 10), you get a remainder of 2, so the new set of numbers is 4 and 2.
Next step: 4 % 2 = 0. Once you get a remainder of zero, the previous number is the answer - the number that you should return. In this case, the 2.
For the least common multiple, use the property that (using a numeric example) 14 x 10 = 2 x 70 (14 and 10 are the two parameters, 2 and 70 are the greatest common factor and the least common multiple, respectively).
Which operator is use to find address of a variable?
The dereference operator. In C, we use the * operator for this. Note that the * symbol has several meanings in C (multiplication, pointer declaration and pointer dereferencing). The meaning is determined from the context in which it is used.
int x, y; /* integer variables */
int* p; /* pointer variable (pointer to int) */
x = 42; /* assign a value to the x variable */
p = &x; /* assign the address of the x variable to the pointer variable p */
y = *p; /* assign the value pointed to by p (42) to the variable y */
How to Reverse a string letter by letter in C language?
#include
#include
void pr_reverse(char *s);
int main(void)
{
pr_reverse("I like C");
return 0;
}
void pr_reverse(char *s)
{
register int i;
for(i = strlen(s) - 1; i >= 0; i--)
putchar( s [ i ] );
}
Algorithm for implementing push operation on stack?
1. If TOP = MAXSTK THEN [Stack already filled?]
Print "OVERFLOW"
Go to step 4
Endif
2. TOP = TOP + 1 [Increase TOP by 1]
3. Set ST[STOP] = ITEM [Insert ITEM in new TOP position]
4. End
Borland C++ was a C++ development environment originally released by the Borland Software Corporation but is now owned by Embarcadero Technologies who purchased the Borland CodeGear division in 2008.
It is now formally known as Embarcadero C++ Builder, or informally as just C++ Builder. At the time of writing, the latest stable release is C++ Builder XE2.
Write a program in qbasic to print the multiplication table of an input number?
This is a homework assignment. write homework assignments for you because you need to do this yourself or you will not learn the skills that the assignment is trying to teach you.
However if, while trying to do your assignment, you find a specific problem that you need help with, WikiAnswers will help you with these specific questions (e.g is this 'xxxxx' qbasic statement correct).
====
Both the above examples can be adjusted to print out the 15 x tables/or else, the 15 times tables square.
HINT:
A> In the first case the user types in the number 15; then, presses Enter.
B> In the 2nd case you simply change both tablesNo%=/timesNo%= to say 15 instead of 12.
What are the differences between Bresenham's line algorithm and Bresenham's circle algorithm?
These two algorithms are almost completely different. The only real similarity is that they are each designed to use only integer addition/subtraction and multiplication, avoiding expensive division and floating point operations.
What is 2's complement of 10002?
Surprise: it is -10002.
(If you wanted to ask 1000(2), then it is 11111000(2))
How do you write a program to find magic numbers?
#include<stdio.h>
unsigned sum_row (unsigned* sq, const unsigned width, const unsigned row) {
unsigned sum, col;
sum = 0;
for (col=0; col<width; ++col)
sum += sq[row*width+col];
return sum;
}
unsigned sum_col (unsigned* sq, const unsigned width, const unsigned col) {
unsigned sum, row;
sum = 0;
for (row=0; row<width; ++row)
sum += sq[row*width+col];
return sum;
}
unsigned sum_diag (unsigned* sq, const unsigned width) {
unsigned sum, row, col;
sum = 0;
for (row=0, col=0; row<width; ++row, ++col)
sum += sq[row*width+col];
return sum;
}
unsigned sum_anti (unsigned* sq, const unsigned width) {
unsigned sum, row, col;
sum = 0;
for (row=0, col=width-1; row<width; ++row, --col)
sum += sq[row*width+col];
return sum;
}
bool is_magic (unsigned* sq, const unsigned width) {
unsigned magic, row, col;
magic = sum_row (sq, width, 0);
for (row=1; row<width; ++row)
if (magic!=sum_row(sq, width, row))
return false;
for (col=0; col<width; ++col)
if (magic!=sum_col(sq, width, col))
return false;
if (magic!=sum_diag(sq, width))
return false;
if (magic!=sum_anti(sq, width))
return false;
return true;
}
int main () {
const unsigned width = 3;
unsigned a[width][width] {{2,7,6},{9,5,1},{4,3,8}};
unsigned row, col;
printf ("Square:\n\n");
for (row=0; row<width; ++row) {
for (col=0; col<width; ++col) {
printf ("%d ", a[row][col]);
}
printf ("\n");
}
printf ("\n");
if (is_magic((unsigned*)&a, width))
printf ("The square is magic with a magic constant of %d\n", sum_row((unsigned*)&a, 3,0));
else
printf ("The square is not magic\n");
return 0;
}
Binary search using pointer in c?
#include
#include
#include
void main()
{
int a[10]={1,2,3,10,11,13,16,21,46,73};
int mid,lower=0,upper=9,num,flag=1;
clrscr();
printf("enter the number to search");
scanf("%d",&num);
printf("\n the list of the data is");
printf("%-20s","\ndata);
for(num=0;num<=upper;num++)
printf("%3d",a[num]);
printf("%-20s","\nindexno");
for(num=0;num<=upper;num++)
printf("%3d",num);
for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
{
if(a[mid]==num)
{
printf("the number is at position %d in array",mid);
flag=0;
break;
}
if(a[mid]>num)
upper=mid-1;
else
lower=mid+1;
}
if(flag)
printf("element is not present in the array");
getch();
}
What is vector and array processing?
An important part of IDL is the ability to work with data that is organized as vectors and arrays. We will first describe vectors and arrays and then show some tools for constructing them. Finally, we will demonstrate some of their uses. In addition to arrays of numbers, which we will describe here, there are also arrays of strings, structures and objects. You can look up information about them in IDL help if you are eager to find out about them now.
Arrays are especially important in signal image processing. We will find that images are just arrays of values with one value for each pixel. Vectors are important in the representation of one-dimensional functions such as functions of time or one spatial dimension.
Vectors
A vector is a list of data items, and can be constructed with an assignment statement like
A=[1.2, 3.1, 4.6, 5.7]
Note that square brackets are used to contain the list. Try typing a list like that shown above and then using HELP,A to get information about the vector.
Vectors can contain any of the number types that are accepted by IDL. When the assignment is made all of the numbers are converted to one type. The type that is used is determined by the data type hierarchy. Try typing
A=[1, 3B, 4L, 5.7] & HELP,A
IDL responds that A is an array of type FLOAT of length 4. If you were to type
A=[1, 3B, 4L, 5] & HELP,A
you would find that A is now of type LONG. The rule is to convert all of the numbers to the highest number type.
Arrays
We can think of a vector as a list that has one dimension. It is a row of data. An array is a list that is arranged in multiple dimensions. A two-dimensional array is a vector of vectors that are all of the same length.
C=[[1,2,3,4],[5,6,7,8],[7,6,5,4]] & PRINT,C
1 2 3 4
5 6 7 8
7 6 5 4
We see that A has three rows and four columns and that each row is one of the vectors in the list. An array can contain any of the IDL data types. When it is created all of the numbers are converted to the highest number type. We will describe a number of IDL functions for the construction of arrays. By using these functions one can construct arrays of up to eight dimensions.
Indexing
Column and Row Index
It is important to be able to refer to particular elements of an array. An element in column 1 and row 2 can be referred to by C[1,2]. In the above array we would find that C[1,2]=6. In IDL the column index comes before the row index.
Rows and columns are indexed beginning with the number 0. The column indexes of C are 0, 1, 2, 3 and the row indexes are 0, 1, 2. Zero-referenced indexing is a convention that may seem strange at first, but it has programming advantages.
One can refer to an entire row by using an asterisk in the column position. C[*,r] refers to the vector in row r. Try the command PRINT,C[*,2].
One can refer to an entire column by using an asterisk in the row position. C[col,*] refers to the vector in column col. Try the command PRINT,C[3,*].
A part of a row or column can be indexed by providing a list of the elements that are wanted. Try the following
row =[0,1,2] & col = [3,2,1] & PRINT,C[col,row]
Can you explain what is printed?
A contiguous segment of column or row elements can be referred to with the notation a:b. For example, C[0:2,2] refers to the elements in positions 0,1,2 in row 2. Try the command PRINT,C[0:2,2]
Array Index
Every element in an array has an array index. The array index is found by counting each element from the beginning of the array to the end row by row. An array that has four columns and three rows would have the indexes as shown below.
0 1 2 3
4 5 6 7
8 9 10 11
Any element in the array can be referred to by its array index. A list of elements in positions 0, 6 and 8 can be printed by
k=[0,6,8] & C[k]
One can find the value of array index from the row and column indexes. The number of columns, n, has to be known. Then k is simply
k = col + row*n
If k is given then the column and row indexes can be found by integer operations:
row = k/n
col = k - row*n
For an array with n=4 columns, C[6]=C[2,1]. You should try converting in both directions.
Array Creation Tools
IDL provides a number of functions that can be used to create arrays and initialize their values. The list can be found in the section "Array Creation Routines" in IDL Online Help. A subset of these routines are listed below.
BINDGEN Return a byte array with each element set to its subscript.
BYTARRCreate a byte vector or array.
FINDGEN Return a floating-point array with each element set to its subscript.
FLTARRReturn a floating-point vector or array.
INDGEN Return an integer array with each element set to its subscript.
INTARRReturn an integer vector or array.
LINDGEN Return a longword integer array with each element set to its subscript.
LONARRReturn a longword integer vector or array.
IDENTITY Return an identity array.
MAKE_ARRAYGeneral purpose array creation.
REPLICATEForm array of given dimensions filled with a value.
The functions BYTARR, FLTARR, INTARR, LONARR create arrays with of the type indicated by the first letter of the name and with their initial values set to zero. You can then enter data into them by assignment statements. For example, create an integer array of size 3x4 and set the elements of the first row to the value 3.
A=INDGEN(3,4)
A[*,0]=3
PRINT,A
The functions BINDGEN, INDGEN, FINDGEN, LINDGEN create arrays of the type indicated by the first letter of the name and with the initial values set to the array index. For example, create an array of type byte of size 3x4 and set its initial value to the index.
B=BINDGEN(3,4)
PRINT,B
0 1 2
3 4 5
6 7 8
9 10 11
The functions that generate index arrays are very useful in creating a vector of independent variables to be used in calculating and plotting a function. Suppose that you needed to plot the function (1+t2)Cos(5t) over the interval [0,3] in steps of size 0.01. You will then need a vector of 301 values t=[0, .01, .02, ..., 2.99, 3]. You certainly would not want to type all this in. The statements that will do the calculation and plot a graph are given below.
t=FINDGEN(301)/100 ;Generates the desired vector
f=(1+t^2)*cos(5*t) ;Calculates the function
plot,t,f ;Draws the graph
The IDENTITY function returns a square array of type float with the elements on the main diagonal set to 1 and all others set to zero. This function is very useful in many linear algebra computations.
I=IDENTITY(4)
PRINT,I
The REPLICATE function makes an array of the specified size in which a give value placed in all of the positions. To make a 5x2 array in which all of the elements contain the value 3.1 one can use
C=REPLICATE(3.1,5,2)
PRINT,C
The function MAKE_ARRAY is the most general array creation tool. It has many options which are best described by the online help documentation and the reference manual.
When to use inheritance in c plus plus?
You use inheritance whenever you need a more specialised version of an existing class (the base class). Rather than creating a new class entirely from scratch, and therefore duplicating tried and tested code, you simply build upon the existing class, overriding the existing methods and adding more specialised methods, whilst retaining all the generic functionality of the base class.
Complexity of an algorithm in data structure?
* search array => O(1) linked list=> O(n) binary tree=> O(log n) hash=>O(1) * search array => O(1) linked list=> O(n) binary tree=> O(log n) hash=>O(1)
How do you insert a node using doubly linked list in c plus plus?
#include<iostream>
#include<list>
void print_list (std::list<int>& list)
{
for (std::list<int>::const_iterator it=list.begin(); it!=list.end(); ++it)
std::cout << *it << ", ";
std::cout << "\b\b \n";
}
int main()
{
// instantiate a list
std::list<int> list;
// push a new value onto the back of the list:
list.push_back (1);
print_list (list);
// push a new value onto the front of the list:
list.push_front (2);
print_list (list);
// insert a new value at the back of the list
list.insert (list.end(), 3);
print_list (list);
// insert a new value at the front of the list
list.insert (list.begin(), 4);
print_list (list);
// locate value 1.
std::list<int>::const_iterator it=list.begin();
while (it!=list.end() && *it != 1)
++it;
// insert a new value in front of value 1.
list.insert (it, 5);
print_list (list);
}
Output:
1
2, 1
2, 1, 3
4, 2, 1, 3
4, 2, 5, 1, 3
Which is easier 'C' or 'C plus plus'?
Opinion 1:
I think both are easy to use an manipulate although i have never written a c++ program other than hello world but to know c++ you have to know c and c is a standard high level language which is easy to grasp
Opinion 2:
C++ is easier in the end because it is a revised version of the old c language. It is not required to learn C first before learning C++ because you pick up bad habits and when you move to C++, those habits will be hard to get rid of. C++ is a lot more powerful and accurate, it is harder at first but with time it will be a lot easier and more organized than c.
In conclusion, C is easier to learn but not recommended to learn.
I highly recommend people to go straight to C++ to pick up only good habits, then go back to C if you're curious how it works.
foreach can simply replace for loop. for ex in perl, if you need to copy display an array, it will take only foreach $var(@arr). then print $var. no need of incrementing the index of array like for loop.
Which of the following data structure is non liner type?
A non-linear data structure is one in which the elements are not arranged or linked in a linear fashion. Trees, graphs, etc. are non-linear data structure since the elements are arranged in a branching manner.