A stack is a data structure in which last item inserted is taken out first . That's why they are known as LIFO (last in first out). Inserting an item in stack is termed as push and taking an item out from stack I s termed as pop. Stack pointer is the pointer that points to the top of the stack or that points the item at the top of the stack and help in adding or deleting the item from the top of stack.
First work out the algorithm for what you want to do and then encode that using the C++ language.
Alternative Answer
In this case, the algorithm will consist of a loop that counts from 1 to 1000, testing each value to see if it can be divided by 3 or 5 without leaving a remainder. If so, it gets added to a running total. When the loop ends, the running total is the final answer.
To determine a remainder in C++, we use the modulo operator (%). That is; 9%5 is 4 because 9 divided by 5 is 1 remainder 4, whereas 9%3 is zero. Therefore if either operation evaluates to zero, the value can be added to the running total.
int DoSum( void )
{
int total=0;
for(int x=1; x<1000; ++x)
if( !(x%3) !(x%5) )
total+=x;
return(total);
}
An alternative (and more efficient) algorithm will perform the same loop twice, first in steps of 3 and then in steps of 5. Since we're no longer testing for invalid values, we simply sum every value.
int DoSum( void )
{
int total=0;
for(int y=0; y<2; ++y)
{
int step=y?5:3; // if y is 0, step is 3, otherwise step is 5
for(int x=step; x<1000; x+=step)
total+=x;
}
return(total);
}
What is the numerical range of a char?
There is no defined range of values in C. The built-in types all have ranges that are defined in <stdint.h>, <limits.h> and <float.h>. These ranges are implementation-defined, they are not defined by the language or by the standard. The standard only defines minimum guarantees such that a char is always at least 8 bits long (CHAR_BIT) and that an int is at least as long as a short which is at least as long as a char.
Write a c program to add 2 no without using plus operator?
You can make use of pointers to achieve this.
void add( int *a, int *b){
(*a) += (*b);
}
Now if two numbers a and b are given and you need to store the value in variable c, then you would perform:
c = a;
add(&c,&b);
How is the getchar function used in a C program?
The getchar() is used in 'C' programming language because it can read the character from the Standard input(i.e..from the user keyboard),and converts in to the ASCII value.
A design technique that programmers use to break down an algorithm into modules is known as?
When a programmer breaks down a problem into a series of high-level tasks and continues to break each task into successively more detailed subtasks, this method of algorithm creation is called:
What does descending order mean?
Ascending means to increase or rise. You can ascend stairs, for example, by climbing them. a pattern of numbers can be ascending if each number increases, such as 1, 2, 3, 4, 5.
Ascending is used to describe the return of Jesus from earth to heaven after his resurrection.
ok thanks!!
Going upwards. In different contexts it could mean increasing, getting bigger or climbing.
How do you write a program in C to convert temperatures between Fahrenheit and Celsius?
Difference between register variable and automatic variables?
Register variables are stored in register of microprocessor/micro-controller. The read/write access to register variable is the fastest because CPU never need any memory BUS operation to access these variable.
Auto variable are stored in stack thus access are much slower. Auto variable can be converted to register by using register keyword before it. It has platform specific limitation. Register variable will work only if free registers are available to hold the variable for a function scope. In case of Microprocessor or microcontrollers having very less number of general purpose registers will never take register variable even if we declare it as register.
An algorithm to Reversing the order of elements on stack S using 1 additional stacks?
// stack to contain content
Stack sourceStack = new Stack();
// ... fill sourceStack with content
// stack to contain reversed content
Stack targetStack = new Stack();
while (!sourceStack.empty())
{
targetStack.push(sourceStack.pop());
}
// targetStack contains the reversed content of sourceStack
What is An array of wavelengths?
It is a spectrum. I know this because i am currently in physical science class and we just discussed it. Unless my teacher and book are wrong, this is right.
Storage classes available in c language?
There are four types of storage class or variable in c.
1) auto storage class.
2) register storage class.
3) static storage class.
4) external storage class.
Write a java program that finds and displays all the prime numbers less than 1000?
CLS
FOR n = 2 TO 100
FOR k = 2 TO n / 2
flag = 0
r = n MOD k
IF r = 0 THEN
flag = 1
EXIT FOR
END IF
NEXT k
IF flag = 0 THEN PRINT n,
NEXT n
END
How do you Convert binary number 10000001 to decimal?
Every digit in a binary number corresponds to power of two. So 0001 0101 is equal to 0*2^7 + 0*2^6 + 0*2^5 + 1*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0, which equals 0+0+0+16+0+4+0+1, which equals 21.
What type of data deals with descriptions?
Descriptions are best represented using a character array (string) data type.
What is the most appropriate datastructure to implement priority queue?
Arrays are the most efficient data structure. Memory is allocated to the entire array as a single operation and the total memory consumed is equal to the product of the element size and the number of elements (all elements being of equal size, in bytes). This means that any element in the array can be accessed using simple pointer arithmetic from the start of the array, with the first element at offset 0. All high level languages hide the pointer arithmetic behind an array suffix operator, such that element [5] will be found 5 * sizeof (element) bytes from the start address of the array (the address where element [0] resides). Multi-dimensional arrays are implemented as an array of arrays, such that a two-dimensional array is a one-dimensional array where every element is itself a one-dimensional array. These can be thought of as being a table of rows and columns where the first dimension access a one-dimensional row array, and the second dimension accesses the column within that row. A three-dimensional array can then be thought of as being an array of tables or a cuboid (a stack of tables). A four-dimensional array can therefore be thought of as being an array of cuboids, a table of tables, or a cuboid of arrays. By imagining arrays in this manner it becomes much simpler to imagine arrays with more than 3 dimensions.
By contrast, a list or a tree structure is less efficient because every element requires at least one additional field to maintain the link from that element to another element, thus defining the structure. You also need to maintain an additional field to refer to the first element in the structure. If you have a structure that can dramatically vary in size, lists may be more efficient because there is no need to reallocate the entire structure; you simply allocate and deallocate memory for individual elements and update the links between elements to maintain the structure. However, you lose constant-time random access because you have to traverse the links in the structure to locate an individual element and the additional level of indirection means it will be slower than an array. However, reallocating an array often means copying the array to new memory. One way to minimise reallocations is to reserve more memory than you actually need, thus allowing you to add new elements more quickly at the cost of some memory. You only need to reallocate when you run out of reserve. You can also minimise the cost of reallocation by storing pointers rather than objects in your array. This adds an extra level of indirection, but speeds up the reallocation process by only copying pointers rather than objects being pointed.
How do you write a C program to find the GCD and LCM of two numbers using a switch statement?
The following function will return the GCD or LCM of two arguments (x and y) depending on the value of the fct argument (GCD or LCM).
enum FUNC {GCD, LCM};
int gcd_or_lcm(FUNC fct, int x, int y) {
int result = 0;
switch (fct) {
case (GCD): result = gcd (x, y); break;
case (LCM): result = lcm (x, y); break;
}
return result;
}
Multiple indirection in c language?
C uses pointers for indirection. So, using a pointer to a pointer would be multiple indirection. For example, the following code uses multiple indirection:
int i = 42;
int *pi = &i;
int **ppi = π
**ppi++;
printf("i is now %d\n", i);
What is the default return type in C and C plus plus?
No. There is no default return type for functions, it must be explicitly specified in both the function declaration and in the definition. To specify no return value, return void. To return a variant type, return void* (pointer to void). Otherwise return the exact type.
What is the C program to check whether a given character is vowel or not using if else?
#include<stdio.h> main() { char key; clrscr(); printf("Enter the key"); scanf("\n %c",& key); if (key>='A' key<='Z' )&&(key>='a' key<='z') { printf("%c is an character \n",key); } else { printf("%c is not character",key); } getch(); }
Why is c referred to as middle level language?
C is called a middle level language since it is a higher language than something like assembler, which communicates to the computer through operations that directly manipulate data and uses machine code.
High level languages, are very close to human readable/speakable languages, such as English and French ( and many more), and are therefore more human-oriented.
Unfortunately, the C programming language is neither a low-level language, such as assembler, or a high level language such as English, but somewhere in between. Thus a middle-level language
By mistake. It is a high-level language.
Reverse of a number using do while?
int RevNum( int num )
{
const int base = 10;
int result = 0;
int remain = 0;
do
{
remain = num % base;
result *= base;
result += remain;
} while( num /= base);
return( result );
}
What are the advantages of arrays?
Advantages:
1. You can use one name for similar objects and save then with the same name but different indexes.
2. Arrays are very useful when you are working with sequances of the same kind of data (similar to the first point but has a different meaning).
3. Arrays use reference type and so.
Disadvantages:
1. Sometimes it's not easy to operate with many index arrays.
2. C environment doesn't have checking machism for array sizes.
3. An array uses reference mechanism to work with memory which can cause unstable behaviour of operating system (unless special methods were created to prevent it) and often even "blus screen" and so on. store the many characters or vales in a single variable.
What is c plus plus data type?
The C++ string classes are derived from the std::basic_string<T> template class, where T is a character type. The standard provides the two most common string types, std::string (std::basic_string<char>) and std::wstring (std::basic_string<wchar_t>). The former is used for ASCII strings (which includes UTF-8 strings) while the latter is used for wide-character strings, particularly UNICODE strings.
All standard library strings are represented by an array of some character type (such as char or wchar_t) however, unlike an ordinary array, the individual character elements cannot be re-ordered other by overwriting each character. If you simply need an array of characters that can be re-ordered then use a std::vector<char> or std::vector<wchar_t> rather than a string.
The standard library strings include a rich set of methods, operations and algorithms that are commonly used with strings, such as std::string::find() to locate a character within a string, std::string::substr() to extract a substring from a string, and operator+= to concatenate one string onto another. Most implementations include the short string optimisation so that short strings (up to around 14 characters or so) may be stored in local memory rather than on the heap. Many strings tend to be short so this can provide enormous performance benefits with little cost in terms of memory.
What is the use of recursion function?
Read the part in your programming manual/text book about recursion.
The short answer while easy does not tell you anything about the power or dangers of recursion. It is the power and dangers of recursion that is important because once understood you can use recursion to good effect without running out of stack space.