Click here to Skip to main content
15,902,634 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include <iostream>
#include <ctime>
#include <windows.h>
#include <stdlib.h>
using namespace std;
 
int FINISH;
void drawhorses(int * Horses,int m)
{
	int a=0;
	for (int i = 0; i <=m; i++)
	{
		if (i % 2 == 1)
		{
			cout << "[" << a << "]";
			++a;
			for (int k = Horses[i]; k < FINISH; k++)
			{
				cout << " | ";
			}
			for (int k = 0; k < Horses[i]; k++)
			{
				cout << " ";
			}
			cout << endl;
		}
		else
		{
			for (int k = 0; k <= FINISH; k++)
			{
				cout << " | ";
			}
			cout << endl;
		}
	}
}
void draw(int * Horses, int m)
{
	int b = 0;
	system("cls");
	for (int i = 0; i<=m; i++)
	{
		if (i % 2 == 1)
		{
			for (int k = Horses[i]; k>0; k--)
			{
				cout << "   ";
			}
			cout << "[" << b << "]";
			++b;
			for (int k = Horses[i]; k < FINISH; k++)
			{
				cout << " | ";
			}
			cout << endl;
		}
		else
		{
			for (int k = 0; k <= FINISH; k++)
			{
				cout << " | ";
			}
			cout << endl;
		}
	}
}
 
int main()
{
	int Nohorses,n=0;
	cout << "Enter the number of horses: ";
	cin >> Nohorses;
	system("cls");
	int dist;
	cout << "Enter the distance in forlong form 6 to 12: ";
	cin >> dist;
	FINISH = dist * 3;
	n = Nohorses * 2;
	system("cls");
	int *ptr;
	ptr = new int[n];
	srand(time(NULL));
	for (int i = 0; i<n; i++)
	{
		ptr[i] = 0;
	}
	drawhorses(ptr, n);
	int x = 0;
	bool end = false;
	while (!end)
	{
		Sleep(800);
		system("cls");
		for (int i = 0; i<n; i++)
		{
			x = i;
			ptr[i] += rand() % 2;
			if (ptr[i] >= FINISH)
			{
				end = true;				
			}
		}
		cout << "\n\n";
		draw(ptr, n);
	}
	delete ptr;
	system("cls");
	cout << "Horse number " << x/2 << " Won the race" << endl;
	system("pause");
	return 0;
}


What I have tried:

C++
#include <iostream>
#include <ctime>
#include <windows.h>
#include <stdlib.h>
using namespace std;
 
int FINISH;
void drawhorses(int * Horses,int m)
{
	int a=0;
	for (int i = 0; i <=m; i++)
	{
		if (i % 2 == 1)
		{
			cout << "[" << a << "]";
			++a;
			for (int k = Horses[i]; k < FINISH; k++)
			{
				cout << " | ";
			}
			for (int k = 0; k < Horses[i]; k++)
			{
				cout << " ";
			}
			cout << endl;
		}
		else
		{
			for (int k = 0; k <= FINISH; k++)
			{
				cout << " | ";
			}
			cout << endl;
		}
	}
}
void draw(int * Horses, int m)
{
	int b = 0;
	system("cls");
	for (int i = 0; i<=m; i++)
	{
		if (i % 2 == 1)
		{
			for (int k = Horses[i]; k>0; k--)
			{
				cout << "   ";
			}
			cout << "[" << b << "]";
			++b;
			for (int k = Horses[i]; k < FINISH; k++)
			{
				cout << " | ";
			}
			cout << endl;
		}
		else
		{
			for (int k = 0; k <= FINISH; k++)
			{
				cout << " | ";
			}
			cout << endl;
		}
	}
}
 
int main()
{
	int Nohorses,n=0;
	cout << "Enter the number of horses: ";
	cin >> Nohorses;
	system("cls");
	int dist;
	cout << "Enter the distance in forlong form 6 to 12: ";
	cin >> dist;
	FINISH = dist * 3;
	n = Nohorses * 2;
	system("cls");
	int *ptr;
	ptr = new int[n];
	srand(time(NULL));
	for (int i = 0; i<n; i++)
	{
		ptr[i] = 0;
	}
	drawhorses(ptr, n);
	int x = 0;
	bool end = false;
	while (!end)
	{
		Sleep(800);
		system("cls");
		for (int i = 0; i<n; i++)
		{
			x = i;
			ptr[i] += rand() % 2;
			if (ptr[i] >= FINISH)
			{
				end = true;				
			}
		}
		cout << "\n\n";
		draw(ptr, n);
	}
	delete ptr;
	system("cls");
	cout << "Horse number " << x/2 << " Won the race" << endl;
	system("pause");
	return 0;
}
Posted
Updated 12-Oct-17 13:38pm
v3

1 solution

Quote:
Please tell me the dry run of this code in comments

Why don't you see by yourself what the code is doing ?
Use the debugger to see what this code is doing.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900