- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
A matrix probability question in C?
The matrix probability question Calculates the probability that whether an element will be inside the given matrix after taking N steps and any direction. This means we need to find what is the probability of an element not going out of the scope of the matrix even after moving N positions in any direction.
In this problem, we are free to move the matrix element in all four directions (left, right, up, down). And the probability of moving elements is same 0.25 or 1/4.
The program will return 0 if the element steps out otherwise not.
Example
#include <stdio.h> int isSafe(int x, int y, int m, int n) { return (x >= 0 && x < m && y >= 0 && y < n); } double Probability(int m, int n, int x, int y, int N) { if (!isSafe(x, y, m, n)) return 0.0; if (N == 0) return 1.0; double prob = 0.0; prob += Probability(m, n, x - 1, y, N - 1) * 0.25; prob += Probability(m, n, x, y + 1, N - 1) * 0.25; prob += Probability(m, n, x + 1,y, N - 1) * 0.25; prob += Probability(m, n, x, y - 1, N - 1) * 0.25; return prob; } int main() { int m = 5, n = 5; int i = 2, j = 1; int N = 4; printf("Probability of moving is %lf", Probability(m, n, i, j, N)); return 0; }
Output
Probability of moving is 0.738281
- Related Articles
- A matrix probability question ?
- A Boolean Matrix Question in C++?
- A comma operator question in C/C++ ?
- An interesting time complexity question in C++
- Transpose a matrix in C#
- An Insertion Sort time complexity question in C++
- Question
- Search a 2D Matrix in C++
- Determinant of a Matrix in C++?
- Knight Probability in Chessboard in C++
- Program to convert given Matrix to a Diagonal Matrix in C++
- Python finding programming question in a String
- Probability of a key K present in array in C++
- Airplane Seat Assignment Probability in C++
- Implementation of a Falling Matrix in C++

Advertisements