Changing a word to all uppercase letters in c plus plus?
To change any string to uppercase, loop through each character in the string. If the character is in the range 'a' through 'z', decrement the character by decimal 32 (the difference between ASCII value 'a' and 'A'). The following function shows an example of this:
void to_upper(std::string& str)
{
for(int i=0; i<str.size(); ++i)
if(str[i]>='a' && str[i]<='z')
str[i]-=32;
}
C plus plus program for inverse of matrix?
#include<stdio.h>
int main(){
int a[2][2],b[2][2],c[2][2],i,j;
int m1,m2,m3,m4,m5,m6,m7;
printf("Enter the 4 elements of first matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
printf("Enter the 4 elements of second matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
printf("\nThe first matrix is\n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe second matrix is\n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",b[i][j]);
}
m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
m2= (a[1][0]+a[1][1])*b[0][0];
m3= a[0][0]*(b[0][1]-b[1][1]);
m4= a[1][1]*(b[1][0]-b[0][0]);
m5= (a[0][0]+a[0][1])*b[1][1];
m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
c[0][0]=m1+m4-m5+m7;
c[0][1]=m3+m5;
c[1][0]=m2+m4;
c[1][1]=m1-m2+m3+m6;
printf("\nAfter multiplication using \n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
}
return 0;
}
Sample output:
Enter the 4 elements of first matrix: 1
2
3
4
Enter the 4 elements of second matrix: 5
6
7
8
The first matrix is
1 2
3 4
The second matrix is
5 6
7 8
After multiplication using
19 22
43 50
What is data encapsulation in c plus plus?
Data encapsulation refers to a means of limiting data access only to those functions that actually require direct access. Object-oriented programming languages such as C++ use access specifiers (public, private and protected) to determine the accessibility (or visibility) of an object's data or, more specifically, its representation.
For most classes, data members are declared private, thus fully encapsulating the data.
How does Prim's algorithm differ from Kruskal's and Dijkstra's algorithms?
First a vertex is selected arbitrarily. on each iteration we expand the tree by simply attaching to it the nearest vertex not in the tree. the algorithm stops after all yhe graph vertices have been included.. one main criteria is the tree should not be cyclic.
How do you implement red black trees in c plus plus?
#include<iostream.h>
#include<conio.h>
class add
{
int a,b,c;
public:
void input()
{
cout<<"\n *** Enter the value of a & b *** ";
cin>>a>>b;
}
void output()
{
c=a+b;
cout<<" The value of "<<a<<"+ "<<b<<" = "<<c;
}
};
void main()
{
add a1;
clrscr();
a1.input();
a1.output();
getch();
}
How do you write a C plus plus program that uses Bubble Sort?
template<class T>
void exch( T& x, T& y )
{
T tmp=x;
x=y;
y=tmp;
}
template<class T>
void bubble_sort( T A[], size_t size )
{
size_t last_exch, left, right;
while( size )
{
for( left=0, right=1, last_exch=left; right<size; ++left, ++right)
if( A[right]<A[left] )
exch( A[left], A[last_exch=right] );
size = last_exch;
}
}
for (int n = 0; n <=100; n++) // you may start n = 1, same result, 1 less iteration
{
if (n % 6) // or, n % 6 != 0
printf(n);
}
Why is c plus plus regarded as being a hybrid language?
C++ is regarded as hybrid because it is both procedural and objected oriented. A pure c program can be compiled and run on a c++ platform. At the same time, c++ also provides object oriented features like classes, polymorphism, encapsulation, abtraction, etc.
Why upperbound of array in c plus plus overflow during runtime?
An array is simply a contiguous block of memory that is divided into one or more elements of equal size. The array name is itself a reference to the start address of the array, which has the same address as the first element in the array (the element with index 0). The index is essentially an offset from the start of the array, multiplied by the size of an element. However, there is no built-in mechanism in C to prevent you from accessing elements beyond the upper bound of the array at runtime -- essentially overflowing the array. Since C++ inherits from C, the same problem exists in C++.
For instance, in a 10 element array, the upper bound is 9. If you attempt to write to element 10, you are overflowing the array, the buffer, because that memory does not belong to the array. You then introduce undefined behaviour. At best, nothing bad will happen. At worst, people could die. Once you introduce undefined behaviour there's simply no telling what could happen -- it's a time-bomb waiting to go off.
The only way to avoid such problems is to ensure all your array offsets remain within the bounds of the array. That is, the onus is upon the C++ programmer -- just as it still is with the C programmer.
Which version is use of c plus plus in window7?
No. You have to jump through a great many hoops just to get it running on Vista. On windows 7 you can, at best, install the baseline system but you won't be able to apply any of the service packs, rendering the entire package obsolete. Bear in mind that VC++ 6.0 was released in June 1998 and has gone through six major updates since. Moreover, Windows NT4 has been updated five times (Windows 2000, Windows XP, Windows Vista, Windows 7 and now Windows 8) not counting the server editions. If you want to stick with VC++ 6.0 then you'll need to install Windows XP within a virtual machine and target your programs towards the XP platform. However, if you want to write programs for Windows 7 or 8, upgrading your IDE is long overdue. At 15 years old, I think you've had your money's worth.
When a language has the capability to produce new data type is also called?
New, compared to what? I guess you meant user-defined data-types, which exist in almost every modern programming language.
What is string in c plus plus?
A String is a variable that represents a list, or array, of characters. They are normally assigned using double quotes (""). When using strings you must import string header file
#include
and then an example of a string
string mystring = "This is a string";
This give the variable 'mystring' a value of "This is a string".
A string can also be referred to as an array of characters
char string [10];
Would make the variable string to an array of characters with length ten.
Write a c plus plus program to find the largest and second largest number from an array?
#include <iostream.h>
#include <conio.h>
class largest
{
public:
int a[10],i,b,s;
void get();
};
void largest :: get()
{
cout<<"Enter the numbers"<<endl;
for(i=0;i<10;i++)
{
cin>>a[i];
}
b=0;
for(i=0;i<10;i++)
{
if(b>=a[i])
b=b;
else
b=a[i];
}
cout<<"Largest No-"<<b<<endl;
s=0;
for(i=0;i<10;i++)
{
if((s>=a[i])&&(s!=b))
s=s;
else
s=a[i];
}
cout<<"Second Largest No-"<<s<<endl;
}
void main()
{
clrscr();
largest l;
l.get();
getch();
}
When do you need to use default arguments in a function?
We declare (not use) default arguments in a function whenever the default values cover the majority of calls to that function. We use default arguments in order to simplify those calls and thus reduce the verbosity of our calling code, thus making it easier to call the function.
How many bytes are allocated to an int in C?
16 bit and 32 bit are the most common values. See sizeof.
What is the C program for byte stuffing with the output?
//Sender
#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#define SIZE 40
struct frame
{
char str[SIZE+1];
char tmp[2*SIZE];
char flag;
char e;
char final[2*SIZE];
}s;
main()
{int fd,len1;
int i,j,len;
fd=open("b1",O_WRONLY);
printf("\nEnter flag character and escape character for byte-stuffing:");
scanf("%c %c",& s.flag,&s.e);
printf("\nEnter string:");
scanf("%s",s.str);
len=strlen(s.str);
for(i=0,j=0;i<len;i++,j++)
{
if(s.str[i]==s.flag)
{
s.tmp[j]=s.e;
s.tmp[j+1]=s.flag;
j++;
continue;
}
else if(s.str[i]==s.e)
{
s.tmp[j]=s.e;
s.tmp[j+1]=s.e;
j++;
continue;
}
else
{
s.tmp[j]=s.str[i];
}
}
printf("\nAppended string is==>%s \n",s.tmp);
len1=strlen(s.tmp);
for(i=0,j=0;i<=len1;i++,j++)
{
if((i==0)(i==len1))
{
s.final[j]=s.flag;
s.final[j+1]=s.tmp[i];
j++;
continue;
}
else
{
s.final[j]=s.tmp[i];
}
}
printf("\nFianal string is==>%s\n",s.final);
write(fd,&s,sizeof(s));
}
//Reciver
#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#define SIZE 40
struct frame
{
char str[SIZE+1];
char tmp[2*SIZE];
char flag;
char e;
char final[2*SIZE];
}r;
main()
{
int fd,len1;
int i,j,len;
mknod("b1",010666,0);
fd=open("b1",O_RDONLY);
read(fd,&r,sizeof(r));
printf("\nFlag character is==>%c\n",r.flag);
printf("\nEscape character is ==>%c\n",r.e);
printf("\nAnd actual message was ==>%s\n",r.str);
printf("\nReceived message is %s\n\n",r.final);
}
/*****************
[mca222@rcclinux mca222]$ cc -os byte_s.c
[mca222@rcclinux mca222]$ cc -or byte_r.c
[mca222@rcclinux mca222]$ ./r&
[1] 1570
[mca222@rcclinux mca222]$ ./s
Enter flag character and escape character for byte-stuffing:#
@
Enter string:sim#andh@ar
Sending message is==>#sim@#andh@@ar#
Flag character is==>#
Escape character is ==>@
And actual message was ==>sim#andh@ar
Received message is #sim@#andh@@ar#
C plus plus program using operator overloading?
class foo
{
private:
int m_data;
public:
foo (int data=0): m_data (data) {}
foo (const foo& source): m_data (source.m_data) {}
foo (foo& source): m_data (std::move (source.m_data)) {}
// operator overloads: assign
foo& operator= (const int source) { m_data = source; return *this; }
foo& operator= (const foo& source) { m_data = source.m_data; return *this; }
foo& operator= (foo& source) { m_data = std::move (source.m_data); return this; }
// compound operator overloads: increment and assign
foo& operator+= (const foo& rhs) { m_data += rhs.m_data; return *this; }
foo& operator+= (const int rhs) { m_data += rhs; return *this; }
};
There are a few different ways to do this but this one will work just fine for your problem. (note these numbers are not truly random, but they will probably be random enough for your purposes)
you will have to:
import java.util.Random;
then define a new random number generator called Generator:
Random generator = new Random(); Then, make a random integer between 0 and 40 and subtract 20 so it is between -20 and positive 20. int myNumber = generator.nextInt(40) - 20;
[CTRL]+C is achieved by holding CTRL while pressing C.
What are the advantages of constructor and destructor?
Without a copy constructor the only way to copy an object would be to instantiate a new object (or use an existing object) and then assign another object's value to it. However, it would be very odd indeed to have a copy assignment operator without a matching copy constructor. If you have one, you must have both. If you do not need to copy an object of a particular class, however, you can simply delete both the copy constructor and the copy assigment operator for that class. Any attempt to copy or copy assign would then result in a compile-time error.
What is the virtual function and friend function?
I have not used friend concept in C#. Actually it may not exist in C#.
Based on OO principles, the friend feature in C++ violates the privacy or encaptualization of private data memeber.
Would you allow your friend (a friend class) to access your wallet or bank account (private member)?
What is static variable in the class in cpp?
Static member variables of a class are variables that are local to the class within which they are declared. That is, each instance of the class (each object) shares the same common member variable. Changing that variable's value in one instance will change it for all instances.
Contrast with a non-static member variable where each instance of the class has its own independent variable, scoped to the object itself (not the class of object).