Skip to main content

Posts

Showing posts from April, 2015
Use of Pointers in C++ A pointer is a variable that holds a memory address. Declaring a pointer Eg:- int *k or int* k  Pointer operators *<PointerName>  - to obtain the value of the variable the pointer is pointed to Eg: *k &<Pointer/VariableName>  - to obtain the memory address of the pointer or the variable Eg: &k Assigning a value int *m; //declaring the pointer int num=2; //declaring the variable m=&num; //assigning the memory address of 'num' to the pointer 'm' Consider the following program #include <iostream> using namespace std; int main () {         int var1=11;         int var2=12;         int *ptr1, *ptr2, *ptr3;         ptr1=&var1; //memory address of var1 is assigned to ptr1 or ptr1 is pointed to var1's location          ptr2=&var2;  //memory ...
Using Pass by Reference in a Simple Function in C++ This program will allow you to input two numbers. A function will be called to set the smallest number out of the two to 0 and to display the output in the main program. #include <iostream> void zeroSmaller(int &n1,int &n2); using namespace std; int main () {         int num1,num2;         cout<<"Input number 1: ";         cin>>num1;         cout<<"Input number 2: ";         cin>>num2;         zeroSmaller(num1,num2);         cout<<"The new values for number 1 and 2 are "<<endl<<num1<<" and "<<num2<<endl;         return 0; } void zeroSmaller(int &n1,int &n2) {         if(n1<n2)         {          ...
A C++ program to calculate the power of any number using a function eg:1:- eg:2:- #include <iostream> double power(double n,int p); using namespace std; int main () {         double num,pwr;         int p;         cout<<"Input the number: "<<endl;         cin>>num;         cout<<"Input the power: "<<endl;         cin>>p;         pwr=power(num,p);         cout<<"The answer is "<<pwr<<endl;         return 0; } double power(double n,int p) {         double pwr=1;         int i=1;         while(i<=p)         {                i++;                pwr=pwr*n; //The nu...
A C++ program to calculate the area of a circle when the radius is given using a simple function #include <iostream> float circarea(float radius); //Function prototype using namespace std; int main () {         float r,cArea;         cout<<"Enter the radius of the circle: ";         cin>>r;         cArea=circarea(r); //Calling function         cout<<"The area of the circle is "<<cArea<<endl;         return 0; } float circarea(float radius) //Called function {         const double pi=22/7.0;         float area=pi*radius*radius;         return area; }
Rotating the contents of the columns in a 2D array in C++    The values are inputted to the 1st column of the 2D array. As shown in the second image, the content is rotated and displayed. The C++ program for this is as below. #include <iostream> void displayArray(int arr[][3],int row,int col); using namespace std; int main () {         int data[4][3];          cout<<"Please input the number"<<endl;         for(int i=0;i<4;i++)         {                cin>>data[i][0]; //Values are inserted to the 1st column of the array         }          for(int j=0;j<4;j++)         {              for(int k=1;k<3;k++)             {       ...
A program to insert values to the first column of a 2D array of 4X3 and to print the 2D array as a grid assigning 0 to other elements using a function #include <iostream> void displayArray(int arr[][3],int row,int col); using namespace std; int main () {         int data[4][3];         for(int i=0;i<4;i++)         {                 cout<<"Please input the number"<<endl;                 cin>>data[i][0];         }         displayArray(data,4,3);         return 0; } void displayArray(int arr[][3],int row,int col) {         for (int k=0;k<row;k++)         {                 cout<<arr[k][0]; //print the first element of the row         ...
Printing patterns using 'for' loops in C++ #include <iostream> using namespace std; int main () {         for (int i=0;i<=10;i++) //No.of rows         {                 for (int j=0;j<=10;j++) // No. of columns                 {                         cout<<"*";                 }                 cout<<endl;         }         return 0; } #include <iostream> using namespace std; int main () {         for (int i=0;i<=10;i++)         {                 for (int j=0;j<=i;j++)               ...
A C++ program that is terminated when a certain value is entered (using a simple 'while' loop)

Program to print out the cubes of numbers from 1 to 10 using a simple 'for' loop

A C++ Program to print out the cubes of numbers from 1 to 10 using a simple 'for' loop