Latest Posts

Cpp Program of Passing 2D array to Function

1 comment:
Passing multidimensional array to function is almost same as that of passing single dimensional array. In this tutorial i will show you how to pass multidimensional array to function and also in future tutorials i will show you how to pass multidimensional array to function using pointers. So let start.


Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include<iostream>
using namespace std;

void filling(int arr[5][5]);    //Function Prototype
void printing(int arr[5][5]);

int main()
{
int arr[5][5]={};  // Null Array

filling(arr);  //Function Call
printing(arr);

}


void filling(int arr[5][5])
{
//Filling 2D array
cout<<"Filling Data: " <<endl;

for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cin>>arr[i][j];
}
}

}


void printing(int arr[5][5])
{
//Printing 2D array
cout<<"\nPrinting Data: " <<endl;

for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cout<<arr[i][j];
}
}

}



Output:



Read More

Cpp Program of Filling and Printing 2D Array

1 comment:
Filling and printing of Multidimensional array is bit tricky than that of one dimensional array. In this tutorial i am going to show you how to fill and print multidimensional array. As you can see in earlier tutorials that in single dimensional array we just used only single for loop, but in 2D array we will use for loop inside for loop or use can say nested loop. The reason is that in 2D array the data is arranged in rows and columns so for controlling row we use outer for loop and for controlling columns we use nested for loop. So let see how it works.


Program:
// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#include <iostream>
using namespace std;

int main()
{
int arr[5][5]={};  // Null Array

//Filling 2D array

cout<<"Filling Data: " <<endl;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cin>>arr[i][j];
}
}


//Printing 2D array

cout<<"\nPrinting Data: " <<endl;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
cout<<arr[i][j];
}
}

} // End of Program



Output:


Read More

C++ Snake Game

4 comments:


Here is a source code of Snake Game coded in C++ originally by me. In this game i have just used simple functions, loops, Arrays and rand() function. The border of the game is made by using for loop. And the food is generated using rand function. The kbhit() function is used to sense the key pressed and according to that it moves the snake and eat the food. Hope you will like it.

Source Code:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com

#‎include‬<iostream>
#include<string>
#include<conio.h>
#include<windows.h>
using namespace std;

const int width = 40;//FOR WIDTH OF FRAME
const int hight = 20;//FOR HIGHT OF FRAME
int foodx, foody;
int x;//FOR CONTROLLING X AXIS
int y;//FOR CONTROLLING Y AXIS
int score = 0;//FOR ADDING SCORE
int tailx[200] = {0};
int taily[200] = {0};
int tail = 0;
enum direction{ LEFT, RIGHT, UP, DOWN };
direction dir;
bool gameover;//FOR STARTING GAME


void start()
{

  gameover = false;
  x = width / 2;//SET THE VALUE OF X=20
  y = hight / 2;//SET THE VALUE OF Y=10
  foodx = rand() % width;//RANDOMLY GENERATE FOOD OF X AXIS
  foody = rand() % hight;//RANDOMLY GENERATE FOOD OF Y AXIS
}


void frame()
{

  system("cls");

  for (int i = 0; i < width + 2; i++)
  cout << "#";//FOR PRINTING UPPER WALL MEAN CEALING
  cout << endl;
 
  for (int i = 0; i < hight; i++)//FOR PRINTING SIDE WALLS
  {
    for (int j = 0; j < width; j++)
    {
      if (j == 0)//PRINT LEFT SIDE WALL
      {
        cout << "#";
      }
    
     if (i == y&&j == x)//FOR MAKING THE SNAKE HEAD IN CENTRE
     cout << "O";

     else if (i == foody&&j == foodx)//FOR GENERATING RANDOM FOOD IN FRAME
     cout << "*";

    else
    {
    bool print = false;

    for (int k = 0; k < tail; k++)
    {
      if (tailx[k] == j&&taily[k] == i)
      {
        cout << "0";
        print = true;
     }
    }

     if (!print) 
     {
      cout << " ";
     }
   }

    if (j == width - 1)//FOR PRINTING RIGHT SIDE WALL
    {
      cout << "#";
    }
 }

 cout << endl;
}

for (int i = 0; i < width + 2; i++)//FOR PRINTING LOWER WALL MEAN FLOOR
{
  cout << "#";

}
 cout << endl;
 cout << "SCORE: " << score << endl;
}


void direct()
{

  if (_kbhit())//WHEN USER ENTER THE KEYBOARD
  {
   switch (_getch())//IT TAKES CHARACTER
   {
    case'a':
    dir = LEFT;
    break;
    case's':
    dir = DOWN;
    break;
    case'w':
    dir = UP;
    break;
    case'd':
    dir = RIGHT;
    break;
   }
  }
}


void move()
{

  int previousx = tailx[0];
  int previousy = taily[0];
  int prev2x = 0;
  int prev2y = 0;
  tailx[0] = x;//remind first tail value of x axis
  taily[0] = y;//remind second tail value of y-ais
 for (int i = 1; i < tail; i++)
 {//for reminding previous tail position
  prev2x = tailx[i];
  prev2y = taily[i];
  tailx[i] = previousx;
  taily[i] = previousy;
  previousx = prev2x;
  previousy = prev2y;

 }

 switch (dir)
 {
  case LEFT://decrease value of x when a is pressed
  x--;
  break;
  case RIGHT://increase value of x when a is pressed
  x++;
  break;
  case UP:
  y--;//decrease value of y when w is pressed
  break;
  case DOWN:
  y++;//increase value of y when s is pressed
  break;
  default:
  break;
 }

 if (x>width - 1 || x<0 || y > hight - 1 || y<0)//when snake hit the wall game end
 gameover = true;

 for (int i = 0; i < tail; i++)
 {
   if (tailx[i] == x&&taily[i] == y)
      gameover = true;
 }

 if (x == foodx&&y == foody)//WHEN FOODX=x AND FOODY=y then generate new food
 {
  foodx = rand() % width;
  foody = rand() % hight;
  tail++;
  score += 10;//when food is eaten then score added by ten
 }

}

int main()
{

  string a;
  cout << "\n\n\n\t\t\t\t THE SNAKE GAME\n\n";
  cout << "CONTROLS:\n\nw=UP\ns=DOWN\na=RIGHT\nd=LEFT\n\n\n";
  cout << "PLEASE ENTER YOUR NAME: ";
  cin >> a;

  start();

 while (!gameover)//game condition is true
 {

  frame();
  direct();
  move();
  Sleep(100);
 }

 system("cls");

  cout << "\n\n\nOPPS YOU ARE OUT" << "\tTRY AGAIN";
  cout << endl;
  cout << a << " YOUR SCORE IS: " << score << endl;

}

 

Read More

Passing Array to a Function in Different ways

1 comment:

There are three ways to pass an array to a function. Each way and its program examples are give below:

Here i am sharing a simple program that will initialize and array and then will send that array to a function which will make square of elements of that array.

These three method include passing array to function using pointers. Passing array to function simply with name, And passing array to function with size.

 

 Method 1:

#include <iostream>
using namespace std;

void function(int arr[]);


int main()
{
  int arr[10];

  for(int i=0; i<10; i++)
   arr[i]=i+1;

  function(arr);
 
  // New Values of Array
  for(int i=0; i<10; i++)
   cout<<arr[i]<<endl;
    return 0;
}

void function(int arr[])
{
      for(int i=0; i<10; i++)
       arr[i]= arr[i] * arr[i];
}



Method 2:

#include <iostream>
using namespace std;

void function(int *arr);


int main()
{
  int arr[10];

  for(int i=0; i<10; i++)
   arr[i]=i+1;

  function(arr);
 
  // New Values of Array
  for(int i=0; i<10; i++)
   cout<<arr[i]<<endl;
    return 0;
}

void function(int *arr)
{
      for(int i=0; i<10; i++)
       arr[i]= arr[i] * arr[i];
}


 

Method 3:

#include <iostream>
using namespace std;

void function(int *arr, int size);

int main()
{
  int arr[10];

  for(int i=0; i<10; i++)
   arr[i]=i+1;

  function(arr, 10);
 
  // New Values of Array
  for(int i=0; i<=9;i++)
   cout<<arr[i]<<endl;
    return 0;
}

void function(int *arr, int size)
{
      for(int i=0; i<size; i++)
       arr[i]= arr[i] * arr[i];
}






 

Output:

 


Read More

C++ Program to find Transpose of Matrix

No comments:



In this c++ program we are going to take transpose of a matrix. Transpose of matrix include transforming rows into columns and columns into rows.

In this program we will take an array of five rows and five columns and another array named transpose in which we will store the result of transformation.

Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com
 
#include<iostream>
using namespace std;

int main()
{
  int arr[5][5], transpose[5][5];
  int rows=0, columns=0;
 
  cout<<"Enter total number of Rows: ";
  cin>>rows;

  cout<<"Enter total number of Columns: ";
  cin>>columns;
 
 
  for(int i=0;i<rows;i++)
  {
    for(int j=0;j<columns;j++)
    {
       cin>>arr[i][j];
    }
  }
 
 cout<<"Printing Matrix" <<endl;

  for(int i=0;i<rows;i++)
  {
   for(int j=0;j<columns;j++)
   {
     cout<<arr[i][j]<<"     ";
   }
    cout<<endl;
  }

// Finding Transpose of Matrix
   
  for(int i=0;i<rows;i++)
  {
     for(int j=0;j<columns;j++)
     {
        transpose[j][i]=arr[i][j];
     }
        cout<<endl;
  }

//Displaying The Final Result

cout<<"Transpose of Entered Matrix is: " <<endl;   
  for(int i=0;i<columns;i++)
  {
     for(int j=0;j<rows;j++)
     {
        cout<<transpose[i][j]<<"    ";
     }
      cout<<endl;
  }

   return 0;
  
}

Code:

 

Read More

C++ Program of Multiplication of Matrix Using 2D Array

2 comments:

In this program we are going to multiply two matrices. For this purpose we will declare two arrays, we will take input in these arrays and then we will multiply them and store their result in a new array.

 

Program:

// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com
 
#include<iostream>
using namespace std;

int main()
{
    int a[10][10], b[10][10], multi[10][10];
    int row1=0, column1=0, row2=0, column2=0;
  
    cout << "Enter rows for the first matrix: ";
    cin >> row1;
  
    cout << "Enter columns for the first matrix: ";
    cin >> column1;
  
  
    cout << "Enter rows for second matrix: ";
    cin >> row2;
  
    cout << "Enter columns for second matrix: ";
    cin >> column2;

/* If columns of first matrix in not equal to rows of second matrix then error will occur, asking user to re-enter size of matrix. */
 
    while (column1!=row2)
    {
  
    cout << "Error Occured! column of first matrix not equal to row of second.";
      
    cout << "Enter rows for the first matrix: ";
    cin >> row1;
  
    cout << "Enter columns for the first matrix: ";
    cin >> column1;
  
  
    cout << "Enter rows for second matrix: ";
    cin >> row2;
  
    cout << "Enter columns for second matrix: ";
    cin >> column2;
  
    }

/* Filling of first matrix. */

    cout<< "Enter elements of 1st matrix: " << endl;
    for(int i=0; i<row1; i++)
    for(int j=0; j<column1; j++)
    {
        cout << "Enter element a" << i+1 << j+1 << " : ";
        cin >> a[i][j];
    }

/* Filling of second matrix. */
    cout <<"Enter elements of 2nd matrix: " << endl;
    for(int i=0; i<row2; ++i)
    for(int j=0; j<column2; ++j)
    {
        cout << "Enter element b" << i+1 << j+1 << " : ";
        cin >> b[i][j];
    }

/* Initializing array of multi.*/
    for(int i=0; i<row1; ++i)
    for(int j=0; j<column2; ++j)
    {
       multi[i][j]=0;
    }

/* Multiplying matrix a and b and storing in array of multi. */
    for(int i=0; i<row1; i++)
    for(int j=0; j<column2; j++)
    for(int k=0; k<column1; k++)
    {
        multi[i][j]=multi[i][j] + a[i][k] * b[k][j];
    }

/* Displaying the result. */
    cout <<"Output Matrix: " << endl;
    for(int i=0; i<row1; i++)
    for(int j=0; j<column2; j++)
    {
        cout << " " << multi[i][j];
        if(j==column2-1)
            cout << endl;
    }
    return 0;
}

Output:


Read More

C++ Program of Sorting Elements of Array

1 comment:

In this C++ program we will sort an unsorted array. First of all we will take input from user using for loop. And then we will sort that array using if condition in nested for loop.

Program:



// Program By http://solutionscpp.blogspot.com
// Author Mansoor Ahmed
// GMail ID: secguy000@gmail.com
 
#include<iostream>
using namespace std;

int main()
{
    int a[10], i=0 ,j=0, temp=0;
  

    for(i=0; i<10; i++)
    {
       cout<<"Enter " <<i <<" Element of Array: ";
        cin>>a[i];
     }

       
    for(i=1; i<10 ;i++)
    {
        for(j=0; j<(10-i); j++)
       
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
    }
   

    cout<<"Array after bubble sort is:";

    for(i=0 ;i<10; i++)
    {
     cout<<" "<<a[i];
    }
       
    return 0;

}  //end of main function

Output:

Read More