Program for row wise sort for a square matrix in C?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],i,j,k,m,n;
printf("enter the order");
scanf("%d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(k=0;k<m;k++)
{
for(i=0;i<=m-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[k][j]>a[k][j+1])
temp=a[k][j];
a[k][j]=a[k][j+1];
a[k][j+1]=temp;
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("sorted matrix=%d",a[i][j]);
}
}
getch();
}
Source code for prim's algorithm in C plus plus programming?
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}
Why you use C when C plus plus is there?
C is a much simpler language than C++, with fewer keywords. The resultant machine code maps very closely to the source code, thus C is more low level than C++, but is sufficiently abstract that even assembler language programmers can develop highly efficient code much more easily than they can with assembler alone. C++ evolved from C, but has a far greater degree of abstraction and its object-oriented programming support is ideally suited to solving highly complex problems with more complex data structures more easily but every bit as efficiently as with C. However, since C is much older, there is still a wealth of useful and highly efficient C code that can still be used by C++ programmers to this day, thus it is still worthwhile learning C even if you already know C++, as the transition to C is much easier than the transition from C to C++, unless you are familiar with object-oriented principals.
What are the manipulator in c plus plus?
What is a manipulator?
C++ manipulators are functions or operators that are used specifically with input and output streams in order to manipulate those streams or to insert or extract special characters. Many manipulators apply to both input and output streams but some are specific to one or the other.
The following list briefly describes all the manipulators available in C++, including those only available in C++11. Most manipulators can be found in the
Output stream manipulators
std::flush - synchronises the output buffer with the controlled output sequence
std::endl - inserts a newline character (\n) and flushes the output stream
std::ends - inserts a null character (\0) without flushing the output stream
Input stream manipulators
std::ws - extracts and discards whitespace characters from the input stream
Numerical base format manipulators ("basefield" flags)
std::dec - inserts/extracts integral numeric data with decimal notation
std::hex - inserts/extracts integral numeric data with hexadecimal notation
std::oct - inserts/extracts integral numeric data with octal notation
Floating-point format manipulators ("floatfield" flags)
std::fixed - inserts/extracts floating point values with fixed notation
std::scientific - inserts/extracts floating point values with scientific notation
std::hexfloat - inserts/extracts floating point values with hecadecimal notation (C++11 only)
std::defaultfloat - default behaviour (C++11 only)
Adjustment manipulators ("adjustfield" flags)
std::internal - output is padded to the field width by inserting fill characters at a specified internal point
std::left - output is padded to the filed width by appending fill characters
std::right - output is padded to the field width by prepending fill characters
Flags (on)
std::boolalpha - generates text values "true" and "false" for boolean values
std::showbase - generates the base for basefield values
std::showpoint - generates a decimal point for "floatfield" values
std::showpos - generates positive sign to positive values
std::skipws - ignores whitespace characters
std::unitbuf - flushes the buffer after every insertion
std::uppercase - generates upper-case letters for generated letters
Flags (off)
std::noboolalpha - does not generate text values for boolean values
std::noshowbase - does not generate the base for basefield values
std::noshowpoint - does not generate decimal point for "floatfield" values unless the decimal portion is non-zero
std::noshowpos - does not generate positive sign for positive values
std::noskipws - does not ignore whitespace characters
std::nounitbuf - does not flush the buffer afer every insertion
std::nouppercase - does not generate upper-case letters for generated letters
Parameterised manipulators
std::setiosflags - set format flags
std::resetiosflags - reset format flags
std::setbase - set basefield flag
std::setfill - set fill character
std::setprecision - set floatfield precision
std::setw - set field width
Many of these manipulators work together. For instance, the std::internal, std::left and std::rightmanipulators all work in conjunction with the std::setwand std::setfill manipulators.
All manipulators are implemented as operators which can be chained together using the insertion (<<) or extraction (>>) operators as appropriate to the stream. Excluding the parameterised manipulators, most manipulators are also implemented as functions (passing the streram as an argument). Others, including all parameterised manipulators, are implemented as members of the stream. Some, such as std::flush, are implemented all three ways; as an operator, a function and a member function.
Some examples of manipulator usage:
// set field width to 8 characters for console output
std::cout << std::setw (8); // operator
std::cout.width (8); // member method
// set precision to 16 places for floatfields
std::cout << std::setprecision (16); // operator
std::cout.precision (16); // member method
// insert newline and flush stream
std::cout << std::endl; // operator
std::endl (std::cout); // function
// flush stream
std::cout << std::flush; // operator
std::flush (std::cout); // function
std::cout.flush (); // member method
// generate hexadecimal values with base prefix
std::cout << std::showbase << std::hex; // operator
std::showbase (std::cout); // function
std::hex (std::cout); // function
More information on manipulators can be found in the sources and related links section, below.
A Bell Labs researcher named Dennis Ritchie (who was one of the driving forces behind Unix) created the C programming language.
What are c plus plus statements?
Statements are how we tell the compiler what we want our program to do. In other words, they are the instructions written in C++ code. A statement may be a simple instruction to invoke a function call, such as:
foo();
Or to perform an operation, such as adding two integers:
x+=y;
Note that all statements end with a semi-colon.
We can also group individual statements together to form a compound statement. For instance, when we use an if statement to evaluate a condition, we might want more than one statement to execute if the condition were true. We use curly braces to create a compound statement:
if (x==42)
{
for(int i=0; i<100; ++i)
std::cout<<i<<std::endl;
foo();
}
In the above example, if x were 42, then we'd print the number 0 to 99 and then call foo(). If x were not 42, then we'd skip over the entire compound statement and execute the next statement instead.
Compound statements may also be nested. In the above example, for instance, the for loop might contain a compound statement:
if (x==42)
{
for(int i=0; i<100; ++i)
{
std::cout<<i<<std::endl;
foo(i);
}
}
In this case, we print the value 0 and then call foo(0) on the first iteration of the loop, then print 1 and call foo(1), and so on. But since the for loop constitutes the entire compound of the if statement, we can eliminate the outer set of braces completely:
if (x==42)
for(int i=0; i<100; ++i)
{
std::cout<<i<<std::endl;
foo(i);
}
We can also create compound statements using commas to separate the individual statements. For instance, when we delete a pointer we will typically nullify the pointer straight away. Like so:
if (p)
delete (p), p=NULL;
The above is simply a shorthand for the following compound statement:
if(p)
{
delete(p);
p=NULL;
}
Program to count number of leaf node in binary tree in c plus plus?
Add the following recursive method to your binary tree's node class:
size_t Node::count_leaves()
{
if (!left && !right) return 1; // this node is a leaf
size_t count = 0;
if (left) count += left-count_leaves(); // count leaves on left
if (right) count += right-leaves(); // count leaves on right;
return count; // return total leaves.
}
To count the leaves of the entire tree, call the method on the root node of the tree. To count the leaves of a subtree, call the method on the root node of the subtree.
What is the purpose of getch()?
getch() is a way to get a user-inputted character. It can be used to hold program execution, but the "holding" is simply a side-effect of its primary purpose, which is to wait until the user enters a character. getch() and getchar() are used to read a character from screen.
What is called pointers-c plus plus?
Yes, C++ has pointers, which are references to memory locations. which are variables that store memory addresses, or NULL (zero). If the pointer is non-NULL, the pointer is said to dereference the object (or variable) residing at the stored memory address, which permits indirect access to that object so long as the object remains in scope.
Characteristics of constructor?
Contains an access modifier followed by the name of the class and some parameters. More specifically:
public class MyClass {
//Constructor
public MyClass() {
}
}
Yes you can use the same function name for a member function and an external function. They are primarily distinguished by the number and type of arguments they accept (the function signature). If they match exactly, then the scope resolution operator (::) is used to differentiate them by namespace. The class namespace is the class name itself. The external function uses global scope unless scoped to another namespace. When the scope is not explicitly stated, then the scope is implied by the call site.
Note that whenever there is any ambiguity about which function is implied, the compiler will emit an error indicating where the ambiguity lies, and the program will ultimately fail to compile.
What key word is use as identifire in c programming?
A keyword identifier is one whose meaning has already been declared to the C compiler and we can't use it as variable,because if we want to do so we are trying to establish a new meaning to the keywords which is not permitted.
Who is the founder of C programming language?
C was developed by Dennis Ritchie at AT &T's Bell Laboratories of USA in 1972.
No, C is its own language. However, C++ is another language that is based on C, but isn't really a "superset" of it. C++ introduces object-oriented-programming, which facilitates development much more easily than the original C. In fact, there are a whole class of languages based on C, including C# and C++. Note: The (ancient) predecessor of C was B.
There are several ways to determine if a string is a palindrome or not. Typically, you must first ignore all spacing, capitalisation and punctutation within the string, which can be done by copying the string, character by character, ignoring all non-alphanumeric characters. You then point to the first and last characters in the modified string and, so long as both characters under the pointers are the same, work the pointers towards the middle of the string until the pointers either meet in the middle or they pass each other. That is, if the string has an odd count of characters, the pointers will meet at the middle character, otherwise they will pass each other. At that point you can say with certainty the string is a palindrome. If the characters under the pointers differ at any time, then the string is not a palindrome. This is fairly straightforward to program.
A more interesting problem is when you have to locate the longest palindrome within a string which is not itself a palindrome. For instance, the string "Madam, I'm Adam is a palindrome" is not a palindrome, but it does contain one: "Madam I'm Adam". In this case we cannot point to the first and last characters and work towards the middle. Instead, we have to test every possible substring of the string. We do this by starting at the first character and treat it as if it were actually the middle character of a palindrome, and then move our pointers to the left and right of this character while the characters match. When they no longer match, or one of the pointers has reached either end of the string, we store the longest palindrome found up to that point and then move onto the next character and treat it as the middle character. If we continue in this manner, treating every character as if it were the middle character of a palindrome, we will eventually locate the longest palindrome.
The problem with this approach is when the longest palindrome has an even number of characters instead of an odd number. To get around this we simply place a single space between each character, and treat each of those as being the middle character as well. When a palindrome is found, we simply remove the spaces. In this way we can use exactly the same algorithm to cater for both odd and even character palindromes.
The only remaining problem is when we wish to print the palindrome itself. Since this will be a substring of the original string, we cannot use the modified string we used to locate the palindrome. One way to get around that is to store the original positions of each letter in an array of indices, and use that array to determine where the substring lies with in the original string.
The following program demonstrates this technique in full. The key function is the ispalindrome() function, which accepts a lower-case copy of the string (including the original spacing an punctuation), and a vector that contains the indices of each letter within the string (ignoring puctuation and spacing), separated by -1 values (representing the implied spaces between each letter). The pos value tells the function which index of the vector is to be treated as the middle character of the potential palindrome, while x and y are output parameters that determine the start and end of the palindrome within the vector. The function returns true if a palindrome was found, and the x and y values can be used to extract the palindrome from the original string, using the indices stored in the vector. Note that when the search for a palindrome fails, we step back the x and y indices by one, and if the vector index is -1, then we step back another index. We then test the x and y values to see if they indicate a palindrome was found or not.
The strip() function is another key function. This generates the vector from the lower case copy of the original string. Although we could eliminate the -1 values at the start and end of the vector, it's simpler to just leave them in.
You will note that the program can cater for strings that are themselves palindromes, as well as strings that contain palindromes.
#include<iostream>
#include<string>
#include<vector>
using namespace std;
string input_string(string prompt)
{
cout<<prompt<<":\t";
string input;
getline(cin, input, '\n');
return(input);
}
void convert_tolower(string& s)
{
for(string::iterator i=s.begin(); i!=s.end(); ++i)
*i=tolower(*i);
}
vector<int> strip(const string& s)
{
vector<int> v;
v.push_back(-1);
for(int i=0; i<s.size(); ++i)
{
if((s[i]>='a' && s[i]<='z') (s[i]>='0' && s[i]<='9'))
{
v.push_back(i);
v.push_back(-1);
}
}
return(v);
}
bool ispalindrome(const string s, const vector<int> v, int pos, int& x, int& y)
{
for(x=pos,y=pos; x>=0 && y<v.size(); --x, ++y)
if( v[x]!=-1 && ( s[v[x]]!=s[v[y]] ))
break;
++x, --y;
if( v[x]==-1 )
++x, --y;
return(x>=0 && x<y && y-x>1);
}
int main()
{
string input;
while(1)
{
input=input_string("Enter a string");
if(input.size()==0)
break;
string copy(input);
convert_tolower(copy);
vector<int> v=strip(copy);
string pal;
int pos=0;
for(int i=0; i<v.size(); ++i)
{
int start=0, end=0;
if( ispalindrome( copy, v, i, start, end))
{
string tmp( input.substr(v[start],v[end]-v[start]+1));
if( tmp.size() > pal.size() )
{
pal = tmp;
pos = v[start];
}
}
}
if( pal.size() )
{
cout<<"Palindrome:\t";
for(int i=0; i<pos; ++i)
cout<<" ";
cout<<pal<<"\n"<<endl;
}
else
cout<<"The string contains no palindromes!\n"<<endl;
}
return(0);
}
Example output:
Enter a string: Madam, I'm Adam
Palindrome: Madam, I'm Adam
Enter a string: Madam, I'm Adam is a palindrome
Palindrome: Madam, I'm Adam
Enter a string: In girum imus nocte et consumimur igni
Palindrome: In girum imus nocte et consumimur igni
Enter a string: 0123456765432
Palindrome: 23456765432
Enter a string:
Press any key to continue . . .
How do you swap 2 variables without using third variable?
void main() { int a,b; clrscr(); printf("\n\n\t\tenter any two nos..."); scanf("%d%d",&a,&b); a=a+b; b=a-b; a=a-b; printf("\n\n\t\tvalue of a=",a); printf("\n\n\t\tvalue of b=",b); getch(); }
What is the difference between initialisation and definition in C programming?
Variable-declaration is:
extern int x;
extern double y;
extern char a;
Variable-definition is:
int x;
static double y;
auto char a;
Variable-definition with initialization is:
int x = 1;
static double y= 2.3;
auto char a = 'w';
Define polymorphism in c plus plus?
Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.
How do you convert from assembly to binary in c plus plus?
Use inline assembly instructions. Then compile your C++ program to produce the machine code.
What is cputs function in computer c plus plus?
Nothing.
The C language only recognizes a few keywords, like "for" and "if". Most of what's in a C program ... that doesn't reference routines in the C program itself ... are library calls, and cputs() is one of those. What it does is write its argument (which should be a pointer to a character string) to the console... console put string.
C plus plus program for sum of n numbers using class?
#include <iostream>
int main()
{
double num1 = 0.0;
std::cout << "Enter first number: ";
std::cin >> num1;
double num2 = 0.0;
std::cout << "Enter second number: ";
std::cin >> num2;
std::cout << num1 << " + " << num2 << " = " << (num1 + num2);
return 0;
}
HOW to display array elements C plus plus?
In C++, it is better to use vectors than dynamic arrays because a vector always knows its own size. C-style arrays are better suited to static arrays where the size is always known in advance. The following demonstrates how a vector might be used within a class.
#include<iostream>
#include<vector>
class foo
{
public:
std::vector<int>& operator+= (int data){ m_data.push_back(data); return(m_data);}
int operator[] (size_t element)const{
ASSERT(element<m_array.size());
return( m_array[element]; }
const size_t size()const{return(m_data.size());}
private:
std::vector<int> m_data;
}
int main()
{
foo f;
f += 42;
f += 9;
for(int i=0; i<f.size(); ++i)
std::cout<<f[i]<<std::endl;
}
Output:
42
9
How do you make C plus plus Program Autorun?
This is not a C++ question. It is an operating system question. Different operating systems have different ways of making programs run on startup. Most of them have several different ways to accomplish this. In MS Windows, one way is to place a shortcut for the program in the "{percent}userprofile{percent}\Start Menu\Programs\Startup" shell folder.
What is the difference between C plus plus and MS Visual C plus plus computer programming?
C++ is simply the generic implementation, based upon the version originally developed by Bjarne Storustrup, and which is considered the standard implementation. Visual C++ is Microsoft's implementation of the language, which follows much of the standard, but is not 100% compliant. However, VC++ includes an extensive and exclusive library that is specific to Windows programming. Competitors such as Embarcadero's C++ Builder have similarly extensive Windows libraries but which are not compatible with Microsoft's.
Can you have inline virtual functions in a class?
No, inlining is done at compile time whereas virtual functions are resolved at run time(late binding). So, virtual functions can't be inlined. Both properties are orthogonal.Inlining is a mere suggestion the compiler may ignore it if it is declared with virtual function.