Latest Posts

C++ Snake Game

8 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

8 comments:

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

3 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

2 comments:

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

C++ Program to Find Largest Element of Array

No comments:

In this program we are going to find the largest number in the array. All we are doing in this program is that we are taking one array element as check and we are comparing it with other elements of array and storing the result in that check element of array. In my program we are taking a[0] as a check element and we are storing the result in it.

 

 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];
   
    for(int i=0; i<10; i++)
    {
        cout<<"Enter " <<i <<" Element of array: ";
        cin>>a[i];
    }
   
    for(int j=0; j<10; j++)
    {
        if(a[0]<a[j])        /* Change sign '<' to sign '>' if you want to find smallest element of array*/
        a[0]=a[j];
    }
   
    cout<<"The Largest Elemet of Array is: " <<a[0];
   
}

Output:

Note: 
By Changing sign '<' to sign '>' you can find the smallest element of array.
 
Read More

C++ Program of Filling and Printing an Array

1 comment:

In this program we are declaring an array of 10. And then we are using For Loop to take input from user of each element of array. And then we are printing each element of the array using another 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];
   
    for(int i=0; i<10; i++)
    {
        cout<<"Enter " <<i <<" Element of array: ";
        cin>>a[i];
    }
   
    for(int j=0; j<10; j++)
    {
        cout<<j <<" Element of array is: " <<a[j] <<endl;
    }

}

Output:

Read More