Introduction to C++ - Applications solved
1) Make a program that exemplifies solving global domain validity (using operator is required resolution).
#include <iostream> #include <fstream> using namespace std; int name=10; void main() { int name=5; //local variable cout<<"Local variable value "<<name<<endl; //endl is used for switching to new line //=5 cout<<"Global variable value "<<::name<<'\n'; //=10 int g; cin >>g; }
2) Make a program alias.cpp which creates two aliases and use them to display values and variables specified addresses.
#include <iostream> #include <fstream> using namespace std; void main() { int a=10; int& a_alias=a; float b=42.76; float& b_alias=b; cout<<"The value of a is "<<a<<", alias is "<<a_alias<<endl; cout<<"Address of a is "<<&a<<", and of alias is "<<&a_alias; cout<<"\nThe value of b is "<<b<<", alias is "<<b_alias; a_alias++; cout<<"\nThe value of a is "<<a<<", alias is "<<a_alias; int g; cin >>g; }
3) Make a program that creates three functions for the three types of transmission parameters received two values as parameters to try and swap between them.
#include <iostream> #include <fstream> using namespace std; void value(int,int); void address(int*,int*); void reference(int&,int&); void main() { int a,b; cout<<"Enter a: "; cin>>a; cout<<"Enter b: "; cin>>b; cout<<"a and b are: "<<a<<" "<<b<<endl; value(a,b); cout<<"a and b are: "<<a<<" "<<b<<endl; address(&a,&b); cout<<"a and b are: "<<a<<" "<<b<<endl; reference(a,b); cout<<"a and b are: "<<a<<" "<<b<<endl; int g; cin >>g; } void value(int m, int n) { int t; t=m; m=n; n=t; } void address(int* m, int* n) { int t; t=*m; *m=*n; *n=t; } void reference(int& m, int& n) { int t; t=m; m=n; n=t; }
4) Write a program that reads and displays complex numbers.
#include <iostream> #include <fstream> #include<math.h> using namespace std; typedef struct { double x; double y; }complex; void main() { complex z; cout<<"Enter real value (a): "; cin>>z.x; cout<<"Enter imaginary value (b): "; cin>>z.y; cout<<endl;//leave a blank line cout<<'\t'<<"a+bi="<<z.x; if (z.y>=0) cout<<"+"<<z.y<<"*i"<<endl; else cout<<z.y<<"*i"<<endl; int g; cin >>g; }
5) Exemplify the static specifier.
#include <iostream> #include <fstream> using namespace std; int& atrib() { static int a=8; a++; return a; } void main() { cout<<atrib()<<endl; atrib()=7; cout<<atrib()<<endl; int g; cin >>g; }
6) Make an expression of the form: 1-2 3-4 ...... +-n, where n is an integer given by the keyboard. Function will send the result by line parameters by reference.
#include <iostream> #include <fstream> using namespace std; void sum(int &s,int n) { s=0; int sign=1; for(int i=1;i<=n;i++) { s+=sign*i; sign=-sign; } } void main() { int s,n; cout<<"Enter n: "; cin>>n; sum(s,n); cout<<"Sum is: "<<s<<'.'<<endl; int g; cin >>g; }
7) Make an expression of the form: 1-2 3-4 ...... +-n, where n is an integer given by the keyboard. Function will send the result by type result by reference.
#include <iostream> #include <fstream> using namespace std; int& sum(int n) { static int s=0; int sign=1; for(int i=1;i<=n;i++) { s+=sign*i; sign=-sign; } return s; } void main() { int s,n; cout<<"Enter n: "; cin>>n; s=sum(n); sum(n)=2; cout<<"Sum is: "<<s<<'.'<<endl; cout<<"Sum is: "<<sum(n)<<'.'<<endl; int g; cin >>g; }