Running loops independently of each other

Hello

I have two parts of a program. The first part allows characters to flash
at the four corners of a 10 by 10 grid while the other has a cross stepping through the grid every couple of seconds, here is the code:

#include <iostream>
#include <windows.h>


using namespace std;

void printarray(char);
void gotoxy (int x, int y);

int main()
{
int num = 10;
int q,w,e,r;
cout<<"This exercise contains a 10 by 10 grid and four fixation targets.\n";
cout<<"Please enter the target flashing periods(in ms, preferably, multiples of 25).\n";
cout<<"Target 1: "; cin>>q; cout<<"Target 2: "; cin>>w;
cout<<"Target 3: "; cin>>e; cout<<"Target 4: "; cin>>r;
cout<<"When you are ready, \n";
system("pause");

printarray(num);

int qt=0, wt=0, et=0, rt=0;

while (true)
{
if (qt<q)
{
gotoxy(2,8); // move to where we want to output
cout << "q"; // overwrite the current output
}
else
{
gotoxy(2,8); // move back to the start of output
cout << " "; // this will reset the output to blank
}

if (wt<w)
{
gotoxy(56,8); // move to where we want to output
cout << "w"; // overwrite the current output
}
else
{
gotoxy(56,8); // move back to the start of output
cout << " "; // this will reset the output to blank
}

if (et<e)
{
gotoxy(2,35); // move to where we want to output
cout << "e"; // overwrite the current output
}
else
{
gotoxy(2,35); // move back to the start of output
cout << " "; // this will reset the output to blank
}

if (rt<r)
{
gotoxy(56,35); // move to where we want to output
cout << "r"; // overwrite the current output
}
else
{
gotoxy(56,35); // move back to the start of output
cout << " "; // this will reset the output to blank
}

qt+=25; if (qt>=2*q) qt=0;
wt+=25; if (wt>=2*w) wt=0;
et+=25; if (et>=2*e) et=0;
rt+=25; if (rt>=2*r) rt=0;
///sequence

/*
for(int j=8;j<=35;j+=3){
for(int i=2;i<=56;i+=6){
gotoxy(i,j);
cout << "+";
Sleep(3000);//this won't allow my
//program to run properly
gotoxy(i,j);
cout << " ";
}
}
*/
Sleep(25);
}cin.get();

system("pause");
return 0;
}

void printarray(char n)
{
char array[n][n];

for(int x=0, i=1; x<n; x++, i++)
{
for(int y=0; y<n; y++)
{
array[x][y]=' '; //assigning space ' ' to all elements of matrix
}
}

for(int y=0; y<n; y++)
{
for(int x=0; x<n; x++)
{
cout<<"|_"<<array[x][y]<<"_| ";
}
cout<<endl<<endl<<endl;
}
}

void gotoxy (int x, int y)
{
COORD coord; // coordinates
coord.X = x; coord.Y = y; // X and Y coordinates
SetConsoleCursorPosition(
GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
}


The problem is within my FOR loop inside the main function. It works like I
want it to but the multiple sleep function won't allow the program two run properly. So I thought to take it this route:

if ()
{
for(int j=8;j<=35;j+=3){
for(int i=2;i<=56;i+=6){
gotoxy(i,j);
cout << "+";
}}
}
else
{
for(int j=8;j<=35;j+=3){
for(int i=2;i<=56;i+=6){
gotoxy(i,j);
cout << " ";
}}
}

I just don't know what to make the IF statement to allow this to run properly.
unfortunately I don't know threads well enough to apply them here. Any help I can get here to make run correctly would be greatly a appreciated

I don't really have the time to study your code at the moment, but if you want to find some help I suggest using the code tags and format it. People hate scanning through unorganized code.

Just a friendly pointer. :)
In addition to Tresky's comment, I guess you could have shortened your code so that you can concentrate on the problem at hand (the if/else block at the bottom). Makes your original post less intimidating. :-)

Unfortunately, it sounds like you will have to use one of pthreads, OpenMP, or MPI if you want two loops to operate independently. I think you should read up on one of the first two since they are applicable to shared memory. That is, both threads will access the same area of memory. I *think* OpenMP is what you should be looking into, but I'm not 100% sure. I don't have a good grasp of how to solve your problem; but looking at such documentation might be a good start...

If you're working on a game, then GPGPU and/or CUDA might also be of interest. Actually, it doesn't have to be a game, but if you're able to use a graphics card's multi-processor abilities.

Topic archived. No new replies allowed.