Can you Write a program in c plus plus to display the multiplication table?
void printTable() {
}
printf("\n");
printf(" ");
for (c = 0; c <= 10; ++c) {
printf(" -");
}
printf("\n");
// calculations
int r;
for (r = 0; r <= 10; ++r) {
// row headers
printf("%2d|", r);
// calculation for the current row
for (c = 0; c <= 10; ++c) {
printf("%4d", (r * c));
}
printf("\n");
}
}
How do you Assign an address to an element of pointer array?
Pointers were always difficult subject to understand. I would suggest to read several tutorials and spend some time playing with them.
Explanation:
/* create array of two pointers to integer */
int *aaa[2];
/* setting first pointer in array to point to 0x1212 location in memory */
aaa[0] = (int *)0x1212;
/* allocate memory for the second pointer and set value */
aaa[1] = malloc(sizeof(int));
*aaa[1] = 333;
/* if you want to can make first item to point to the second,
now arr[0] pointer address changed to the same as aaa[1] pointer */
aaa[0] = aaa[1];
/* print aaa[1] address and later print value */
printf("LOCATION: %X\n", (unsigned int)*(aaa + 1));
printf("VALUE: %d\n", **(aaa + 1));
/* exact the same here, but different way of how I wrote pointers */
printf("LOCATION: %X\n", (unsigned int)aaa[1]);
printf("VALUE: %d\n", *aaa[1]);
If you would do the same with 0 element of array, which points to 0x1212 memory location you would get some kind of number or Bus error if you accessed restricted memory place (for example the beginning of memory).
I hope this I did not make you more confused than you are.
What is the role of switch statement in c plus plus?
Switch is an alternative to using a long sequence of if...else if... statements where each conditional expression is essentially the same, evaluating to the same data type but with different values.
For instance:
if(x==1) statement_1;
else if(x==2) statement_2;
else if(x==3) statement_3;
else statement_4;
The above can also be written:
switch(x)
{
case(1): statement_1; break;
case(2): statement_2; break;
case(3): statement_3; break;
default: statement_4;
}
Most would regard the second version as more readable as there's only one conditional expression to evaluate.
What are the disadvantage of conventional programming language?
ali asghari:
It shouldn't be a problem, as long as:
1) The features are well-designed, e.g. having one sort function that takes a few options, rather than many sort functions that do slightly different things
2) The features are namespaced - e.g. mathematical functions are in a Maths module or object, that way user function and variable names are less likely to collide with language base features, and be broken by language version increments
3) The features are properly documented in an organised way - there's no point in offering a facility if the programmer doesn't know it's there or the specs give no clue on how it's supposed to be used
4) The features don't duplicate one another - e.g. different names for the same function, different possible syntaxes for the same meaning, etc. which makes code harder to read for programmers familiar with the "other" syntax
5) The features aren't out of scope for the language (e.g. there's no point in features for 3D graphics acceleration in a language designed for web servers), as this will increase memory requirements, and thin out performance efforts.
So, the disadvantages would be if one of these pitfalls is encountered, and the potential problems caused
An array name is a pointer constant?
An array's name is not a constant pointer; it's not even a pointer! An array name is a reference to the array itself. Unlike pointers, references have no memory of their own, therefore a reference cannot be constant (only what it refers to can be constant). A reference is nothing more than an alias for a memory address. Since there is no separate storage for references, you cannot reassign references while they remain in scope. So, in that sense, it behaves like a constant pointer. But a pointer is a variable, so even if declared const, it requires memory of its own in order to store the memory address it points to.
Example:
int a[10];
int * const p = a; // const pointer
assert( *p &a ); would cause an assertion. Likewise, assert( p != &p); proves that p must reside in a separate memory address from that referred to by a. It has to: pointers are variables even when they are declared const.
Can cin and cout be used in the function definition of user defined functions in C plus plus?
The cin and cout entities (they are not statements) are C++ iostream library objects that allows access to "standard input" and "standard output". In C++, the statement cout << "Hello World!"; is similar to the C statement printf("Hello World!\n"); Similarly, cin >> variable; allows reading from standard input to a variable.
What is a feature in c that is not in c plus?
C is a structured programming language which is feature of C which made it a powerful language when it was new in market.
But after C++ was developed structured programming was considered as disadvantage due to development of Object Oriented Programming Language.
What is the use of using namespace STD in c plus plus programming?
No, the use of 'namespace std' is not compulsory. You can specifiy it on any object reference. Specifying 'namespace' simply provides a default value.
Contrast ...
using namespace std;
cout << "Hello world!" << endl;
... with ...
std::cout << "Hello world!" << std::endl;
How many main functions can a c plus plus program have?
1. In C language, you cannot compile a source-file if it has two (or more) functions with the same name.
2. You cannot link a program if it has two (or more) global (ie: non-static) functions with the same name.
Difference between while loop and do loop?
The difference between "do while" and "do until" is that a "do while" loops while the test case is true, whereas "do until" loops UNTIL the test case is true (which is equivalent to looping while the test case is false).
The difference between a "do ...while" loop and a "while {} " loop is that the while loop tests its condition before execution of the contents of the loop begins; the "do" loop tests its condition after it's been executed at least once. As noted above, if the test condition is false as the while loop is entered the block of code is never executed. Since the condition is tested at the bottom of a do loop, its block of code is always executed at least once.
To further clear your concept on this, understand the syntax and description of the two loop types:
while
The while loop is used to execute a block of code as long as some condition is true. If the condition is false from the start the block of code is not executed at al. The while loop tests the condition before it's executed so sometimes the loop may never be executed if initially the condition is not met. Its syntax is as follows.
while (tested condition is satisfied)
{
block of code
}
In all constructs, curly braces should only be used if the construct is to execute more than one line of code. The above program executes only one line of code so it not really necessary (same rules apply to if...else constructs) but you can use it to make the program seem more understandable or readable.
Here is a simple example of the use of the while loop. This program counts from 1 to 100.
#include
int main(void)
{
int count = 1;
while (count <= 100)
{
printf("%d\n",count);
count += 1; // Notice this statement
}
return 0;
}
Note that no semi-colons ( ; ) are to be used after the while (condition) statement. These loops are very useful because the condition is tested before execution begins. However i never seem to like these loops as they are not as clear to read as the do ...while loops. The while loop is the favorite amongst most programmers but as for me, i definitely prefer the do ...while loop.
do ....while
The do loop also executes a block of code as long as a condition is satisfied.
Again, The difference between a "do ...while" loop and a "while {} " loop is that the while loop tests its condition before execution of the contents of the loop begins; the "do" loop tests its condition after it's been executed at least once. As noted above, if the test condition is false as the while loop is entered the block of code is never executed. Since the condition is tested at the bottom of a do loop, its block of code is always executed at least once.
Some people don't like these loops because it is always executed at least once. When i ask them "so what?", they normally reply that the loop executes even if the data is incorrect. Basically because the loop is always executed, it will execute no matter what value or type of data is supposed to be required. The "do ....while" loops syntax is as follows
do
Note that a semi-colon ( ; ) must be used at the end of the do ...while loop. This semi-colon is needed because it instructs whether the while (condition) statement is the beginning of a while loop or the end of a do ...while loop. Here is an example of the use of a do loop.
include
int main(void)
{
}
Disadvantages of learning c and c plus plus?
* It is pointers that give C its power. but they are highly insecure.
makes it vulnerable.
* c doesn't support inheritance,makes it more complex to use coz everything has to be written from scratch.
AnswerC allows the programmer all the rope the programmer could ever want. It is up to the programmer not to hang him/her self. Hanging your self is the drawback.
Difference of c plus plus and fox pro?
C++ is a generic, general purpose, object-oriented, structured programming language used to produce native machine code programs. FoxPro is a procedural language and Database Management System (DBMS).
What is the language of compiler of c plus plus?
C++ is a general purpose, cross platform, object-oriented programming language that compiles to native machine code. Although it supports OOP, it does not rely on it. You can mix C++ code with C-style code and even Assembly Language routines.
How c plus plus is a procedure based language?
C is both.
The characteristics of a procedural oriented language: assignment operators (:= in C)
The characteristics of a structured programming language: block of codes ({} in C) for if-else, while-, for- loops, subroutines, etc.
What is prototyping in c plus plus?
A prototype in C++, as well as in C, is a declaration of a function and its parameters, including the types of the function and parameters. It does not actually create (define) the code for the function - it only identifies (declares) it to the compiler.
It is used to enforce type checking for functions and parameters, and it is used to declare the function for use in other code prior to the function actually being defined, such as in a different compilation unit or library. Headers, for instance, contain mostly prototypes.
What is the function on endl in c plus plus?
std::endl means end line, and is typically used to insert a new-line character in an output stream using the insertion operator (<<). This is really no different to inserting the '\n' character code to the output stream, however it also serves to flush the output buffer by writing all unwritten characters to the output stream.
When used as a global function, std:endl(ostream& OS) can be used to insert a new-line character and flush the given output stream. The return value is a reference to the output stream itself thus it can be used in compound statements that make use of the return value.
How many bytes can each of the in c plus plus language data type hold?
The only certainty is that a char holds one byte. All other primitives are implementation dependant. However, you can determine the length of any type, including user-defined types, by using the sizeof() operator.
Can you use visual c plus plus for window programming in c language?
... yes?
The question doesn't really make any sense. Any language can be used to program a computer, so long as you have a compiler for it. There definitely are both C and C++ compilers for Microsoft Windows, therefore you can write device drivers in it if you want.
Heck, you could theoretically write device drivers in LISP or BASIC if you wanted; I personally wouldn't, but it could be done.
What is difference between virtual base class and virtual function?
A virtual class allows children classes to inherit multiple classes that all share a common ancestor. Without this property, the child class would contain multiple copies of the same parent class. A virtual function is a function, with a body, defined in a parent class that may be overridden in the parent class.
Why there is only one main function in every C programmes?
Try putting 2 main functions in a program and see what happens rather than asking easy for you to resolve questions here.
Why cin and cout are iostream objects?
Most programs require both input and output. By including iostream you gain access to both, along with the most common datatypes and functions within the standard library. If you don't require all the features provided, you can include only what you need instead.
Difference between procedural programming and modular programming?
Procedural programming is a computer programming technique in which the program is divided into modules like function or subroutine or procedure or subprograms, where as ... "Modular Programming" is the act of designing and writing programs as interactions among functions that each perform a single well-defined function, and which have minimal side-effect interaction between them. Put differently, the content of each function is cohesive, and there is low coupling between functions as happens in procedural programming.
What is function in C plus plus programming language?
Functions are short procedures or subroutines that can be called as and when required. Unlike goto statements, which branch off to a labelled section of code never to return, functions always return back to the caller. Functions may also call other functions. Functions are typically used to reduce the amount of duplicate code, producing more consistent and error-free code and reducing maintenance whenever functions need to be modified. Functions are also used to make code more readable by replacing complex or compound statements with a simpler function call. By using easy to understand function names, code becomes self-documenting, reducing the need to include verbose comments that only serve to interrupt the flow of the code.
Although there is a performance penalty in calling functions as opposed to inline code, the compiler's optimisation routines can inline expand functions whenever there is an advantage in doing so. Typically this applies to short functions with one or two simple statements, but can also apply to functions that are seldom called.
Functions may also return a value back to the caller (a function that does not return a value simply returns void). Functions can modify their behaviour according to arguments supplied to the function. The number and type of arguments is dependant upon the function signature. The signature also differentiates between functions with the same name, thus allowing functions to be overloaded to cater for different arguments types.
If the argument types are references (or pointers), the function can modify the arguments passed to it unless the arguments are declared const. Passing by reference also allows functions to return more than one value to the caller (typically the return value itself is used to provide diagnostics, such as an error code).
If the arguments are passed by value, however, the function creates a local copy of the argument, thus preventing changes to the argument that was actually passed. If the argument being passed is an object, the object's copy constructor is called automatically. Since this can be detrimental to performance, objects should always be passed by reference unless the function actually needs to work on a copy of the object rather than the original object.
In C++, classes make use of functions, known as member methods, to provide the implementation of operations that may be applied to an object's data. Classes may also employ virtual functions that allow derived objects to provide more specialised implementations whilst retaining the existing generic code. This also helps to reduce duplicate code, but also allows objects to behave polymorphically. That is, it is not necessary for a generic class to know any of the implementation details of its derivatives. You simply call the generic method of the base class and the virtual table redirects the call to the most-specialised override of that function, if one exists. If not, the generic method is called instead. Classes can also make use of pure-virtual functions, such that the base class need not provide any generic implementation at all. These classes are abstract classes which cannot be instantiated by themselves. Instead, you are expected to derive new classes from abstract classes, and the derived classes must provide the implementation even if the base class provides a generic implementation. Only classes that provide or inherit a complete implementation of all pure-virtual methods can actually be instantiated.
#include<stdio.h>
main()
{
int i,j,k,n;
printf("Enter the number of lines");
scanf("%d",&n);
for(i=n;i>=0;i--)
{
for(k=n-i;k>0;k--)
{
printf(" ");
}
for(j=i-1;j>=0;j--)
{
printf("%2d",i);
}
printf("\n");
}
}
How do you write a C plus plus program to print the area of rectangle using overloading class?
#include<iostream>
struct point
{
int x;
int y;
double length (const point& p) const;
};
double point::length (const point& p) const
{
int w = x - p.x;
int h = y - p.y;
return std::sqrt ((w*w) + (h*h));
}
struct rectangle
{
size_t width;
size_t height;
};
struct triangle
{
point A, B, C;
double length_a () const { return B.length (C); }
double length_b () const { return A.length (C); }
double length_c () const { return A.length (B); }
double perimeter () const { return length_a() + length_b() + length_c(); }
};
size_t area (triangle& t)
{
const double s = t.perimeter() / 2;
return std::sqrt (s * (s - t.length_a()) * (s - t.length_b()) * (s - t.length_c()));
}
size_t area (rectangle& r)
{
return r.width * r,height;
}
int main()
{
triangle t {{0,10},{10,0},{0,0}};
rectangle r {10,5};
std::cout << "Triangle area: " << area(t) << std::endl;
std::cout << "Rectangle area: " << area(r) << std::endl;
}