// a code to produce matrix style affect
#include "stdafx.h"
#include<iostream>
using namespace std;


int modulus(int Number, int number);

	char getchar(int lowerlimit, char a, int upperlimit);



int modulus(int Number, int number)



{

int q=Number/number;

return Number-(q*number);

}




char getchar(int lowerlimit, char a, int upperlimit)
{

return (a+modulus(lowerlimit,upperlimit));


}


int _tmain(int argc, _TCHAR* argv[])
{
	

char arr[80];


int j=7;
int k=2;
int l=5;
int m=1;

while(true)
{
int i=0;
while(i<40)
{
	if(arr[i]!=' ')
	{
arr[i]=getchar(j+i*i,33,30);

	}

  cout<<getchar(j+i*i,33,30);
cout<<getchar(7+3*3,33,30);    // just to check i added this statement and ran the 

program without including the statement just above it and figured out  modulus function doesn't seem to work for some reason even though it should, what it does is simply add 7+9+33 and prints out the character representing that value which is (,  even though what it should have done is that it should have applied modulus function to evaluate ((7+9)%30) and then should have added it to 33 as per stated in the function and then should have printed out the character represented by that value, atleast thats what i think.

i++;

}

j=(j+31);
k=(k+17);
l=(l+47);
m=(m+67);
arr[modulus(j,80)]=' ';  // this is supposed to start a new streak how?when it doesnt          evaluate to zero?

arr[modulus(k,80)]='';   // // this is supposed to end a streak how?
arr[modulus(l,80)]=' ';  // and this is supposed to start a new streak? how?
arr[modulus(m,80)]=''';  // // this is supposed to end a streak how? 
                  //how can they end or begin a streak when they are not even printed out in the code?
i=0
while(i<3000000)
{
	getchar(1,1,1);
	i++;
}

}
return 0;

}

First of all, why make a modulus function. There is a pretty little operator for that in c++ (%). Also I would suggest avoiding infinite loops and relying on the system to stop your program. Instead use a boolean to wait for... what? I have no Idea what the goal is, the program does not make much sense (the variable names are pretty bad). For the matrix letter effect you would need some ability to seek in the console. Then you can do something like this:

class MatrixStreak;
int main()
{
     vector<MatrixStreak> streaks;
     bool continuing=true;
     while (continuing)
     {
         if (randomNumber()==0)
         {
             streaks.add(MatrixStreak();
         }
         for (int i=0; i<streaks.size(); ++i)
         {
             streaks[i].draw();
         }
         continuing=keyIsPressed(ENTER);//or whatever you want
    }
    return 0;
}
class MatrixStreak
{
    private:
    int x,y,ylimit;
    public:
    MatrixStreak()    
    {
        x=randomNumber();
        y=randomNumber();
        ylimit=y+randomNumber();
    }
    bool draw()//return false if dead
    {
        putAt(x,y++,randomCharacter());
        return (y<ylimit);
    }
};

Of course that was just pseudo-code. The necessary functions would be platform specific.

Hi I was actually following a video tutorial, and the code was more or less a copy of that code in the video that was being explained, and I don't go to school to learn programming. I just find it very interesting, so I decided to learn it through watching video tutorial, and I am really thankful to you that you took time out to help me find answers to some of the questions Labdabeta. Can I ask you another favor please, can you just have a look at the youtube video link posted below, its less than five mins, so that I can refer to the part of the code I didn't understand please?

http://www.youtube.com/watch?annotation_id=annotation_167353&src_vid=h1Y48mPXaEw&v=Iw5wOszsXIo&feature=iv

And after looking at the link can you tell me what is happening in the following lines that are copied from the video?

j=j+31; // and what's happening in these four lines, I don't understand it too help pls
k=k+17;
l=l+47;
m=m+67;

caRow[Modulus(j,80)]='-';
caRow[Modulus(k,80)]=' ';
caRow[Modulus(l,80)]='-';
caRow[Modulus(m,80)]=' ';

The guy explaining the program said that caRow[Modulus(j,80)]='-' and caRow[Modulus(l,80)]='-' are used to start the character streaks whereas caRow[Modulus(k,80)]=' ' and caRow[Modulus(m,80)]=' ' are used to end the character streaks what does that mean? Thats the only part of the program I don't understand, can anyone help me understand it please? thanks

The link makes it clear.
The code that you posted is not the code on the video. The code in video has this block of code

while(true){
     int i = 0;
     while (i<80){
        if(caRow[i]!=' ')
        {
           caRow[i]=GetChar(j+i*i,33,30);
        }
        std::cout << caRow[i];
        i++;
     }
     j = (j+31);
     k = (k+17);
     l = (l+47);
     m = (m+67);
     caRow[Modulus(j,80)] = '-';
     caRow[Modulus(k,80)] = ' ';
     caRow[Modulus(l,80)] = '-';
     caRow[Modulus(m,80)] = ' ';
     // Delay

The if check if(caRow[i]!=' ') is used to not replace a column of spaces. When not a space we replace the old character with a new random character that will be printed out. After the loop of 80 we update the values in the caRow in four positions. I think it is four different and the reason is because we are adding a prime number to all of the other the other prime numbers, except m which is 1. We replace the values in the caRow with new values that start and stop a streak of characters. The space ' ' stop the printing of a column of characters because the if check on ' ' does that. The '-' character could be any character to start a new column of random character. So, the if caRow[i]!= ' ' is what is used to not replace a column of space and the bottom randomly starts and stops columns of space.

thanks a lot histrungalot, let me just read what u wrote,thanks a lot

Could you please elaborate this by giving an example, because I still don't understand what's happening there in the last few lines. And also can I ask a few questions as to what you explained up there please? Firstly I do agree that in the following lines characters ' ' and '-' are being stored, but how can they start or end the streak when they are not actually outputed in the code? Secondly how can two characters/values start a code and two characters/values end the code considering that program begins at one point and ends at just one point too? Thirdly, what difference does it make whether the values being added to j, k, l, m are prime numbers or j,k,l,m all themselves are prime numbers? And lastly how can there be a column of spaces as the value of i is incremented several times in the loop, even though I do agree, that at some points spaces would be printed out due to if statement or otherwise, but I still don't understand how there would an entire column of spaces? Please illustrate with an example if you could? I would be really grateful to you Thanks

lines being referred to are:

caRow[Modulus(j,80)] = '-';
caRow[Modulus(k,80)] = ' ';
caRow[Modulus(l,80)] = '-';
caRow[Modulus(m,80)] = ' ';

how can they start or end the streak when they are not actually outputed in the code?

The ' ' and '-' are output. The space is what is giving the breaks in characters. The value of '-' is 45 decimal and that is allow in your set of [33-63].

How start and stop

It is the if(caRow[i]!=' ') . If the character is a space then don't change it. It is not until the lower part of the code runs after the while(i<80) runs that the value of a element that had a space in it could change. Using the

caRow[Modulus(j,80)] = '-';
caRow[Modulus(k,80)] = ' ';
caRow[Modulus(l,80)] = '-';
caRow[Modulus(m,80)] = ' ';

an element is change and if it happens to hit some where there was a space with the '-' assignment that column would now start to print out character values. If it hit an element in the array that was a character and now is a ' ' then the print in that column would now stop.

Thirdly

It is about not setting the same index in the caRow array twice and the incrementing with primes and mod 80 do that. Run the code below to see that you don't get the same element in the array set twice in one loop and that it just loops back to the starting conditions.

#include <iostream>
int main() {
  int j(7),k(2),l(5),m(1),i(0),cnt(0);
  unsigned long long totIt(0);
  char caRow[80] = {0};
  while(totIt++ < 100000000000){
     j = (j+31)%80;
     k = (k+17)%80;
     l = (l+47)%80;
     m = (m+67)%80;
     caRow[j] = 1;
     caRow[k] = 1;
     caRow[l] = 1;
     caRow[m] = 1;
     for (int p(0); p < 80; p++){
         if ( caRow[p] ) 
            cnt++;
         caRow[p] = 0;
     }
     // Check to see if the same element in the array gets
     // set twice
     if ( cnt != 4 ) { 
        std::cout << "Yes: " << j << "," << k << "," << l << "," << m << std::endl;
        break;
     }
     // Check to see if we have cycled back to the starting condistions
     if ( j == 7 && k == 2 && l == 5 && m == 1 ) {
        std::cout << "Iter(" << totIt << "):  Back to initial condistions " << j << "," << k << "," << l << "," << m << std::endl;
        break;
     }
     if ( !(totIt%1000000))
        std::cout << "Iter: " << totIt << std::endl;
     cnt = 0;
  }
  return 0;
}

lastly

Because that are not being replaced by the if check again.

I still don't understand how there would an entire column of spaces?

There is an assumption that the width of your terminal is 80 characters in wide (the default for windows command window). If your terminal is not 80 characters then you are right no columns of space.

thanks a lot mate, you have been very helpful my man

in response to thirdy, yes i am getting the same values in one array set when i output i see this . . . then next row when i press enter i again see . . . and so on ?

in response to how start and end, Now I do understand that a column of space would be printed when i<80 ends and comes out of the loop and reaches caRow[Modulus(k,80)] = ' '; as it would evaluate to zero, but what are others j,l,m doing? And also k wont always be zero, eventually the value would become greater than 80 and so caRow[Modulus(k,80)] = ' '; wont evaluate to zero then, so why are there j,k,l,m there then, and how they start and end the streak is still a big question, and I am sorry, I know I am bothering you a lot with all these questions, cause maybe I am not a very intelligent person, but I just don't give up, once again thanks for walking me through this, and helping me out, and if possible, please help me understand it further thanks

what are others j,l,m doing? How they start and end the streak is still a big question?

To see what is going on, start with a know initial condition, all of the elements in caRow[0..79] = ' ' (space).
Using the code below this is what you get (I added a newline so just make you terminal wider than 80 to see it better)

-             -                           
                   +                  .             .                !          
                   ,5                 /             /             1  "          
                   -6            5    0            #0             2  #          
- 1                .7            6    1            $1             3  $          
. 2                /8            7    2        #   %2             4  %          
/ 3           !    09            8    3        $   &3           ! 5  &          
0 4           "!   1:            9    4        %   '4        1  " 6  '          
1 5           #"   2;       5    :    5       #&   (5        2  # 7  (          
2 6           $#   3<       6    ;    6       $'   )6        3  $ 8  )     # '  
3 7           %$   4=       7    <    7   -   %(   *7        4  % 9  *     $ (  
4 8      +    &%   5>       8    =    8   .    )   +8      5 5  & :  +     % )  
5 9      ,!   '&   6!       9    >    9   /    *   ,9   '  6 6  ' ;  ,     & *  
  :      -"   ('   7"  +    :    !    :  70    +   -:   (    7  ( <  -     ' +  
  ;      .#   )(   8#  ,    ;    "    ;  81    ,   .;   )    8  ) =  .# 1  ( ,  
  <      /$   *)   9$  -    <        -<  92    -   /<   *    9  * >  /$ 2  ) -  
  = +    0%   +*   :   .    =        .=  :3    .    = ! +    :  + !  0% 3  * .  
  > ,5   1&   ,+   ;   /    >        />  ;4    /   1> " ,    ;  , "  1& 4  + /

You can see that the first row is all spaces. That is because the if(caRow[i]!=' ') is false for all characters and none of the elements in caRow get replaced with GetChar. The next thing that happens is the chunk of code with the j,k,l,m runs to "start & stop" streaks. The added of prime numbers to the values of j,k,l,m are to get to a new "random" element in caRow. But we need to insure that we don't index outside the range of caRow (80 character in size) and that is where the Modulus functions comes in. The Modulus(x,80) insures that what ever number x is Modulus will return a number from 0 to 79. So the j,k,l,m are incremented to get to an new element (printing column) in caRow and we set two values to ' ' (stop a streak variables k & m) and two values to '-' (start a streak variables j & l). This can be seen in the second row of the output above. Only two columns now start printing "random" values. Theses are the elements (columns) that j & l started. The rest are all still spaces and don't get replaced by GetChar because again spaces don't pass the if(caRow[i]!=' ') check. Now comes the third row and two more columns are set to stop and two set to start. Four columns are now printing, that's two from the last time and two new ones that were just started by j, l. Columns/streaks are being started and stopped at every iteration but we only see the starts because we started with all colums/streaks stopped to help understand what is happening. It is not until row 12 column 47 that we see a column/streak that was started on row 10 column 47 now stop because one of the variable k or m has set that element/column in caRow to be a ' '. Which now disables the printing of that column/streak.
In the end it's all about the if(caRow[i]!=' ') which controls starts and holds stops, and the j,k,l,m are just used to find a new "random" element/column/streak to set it's value to ' ' to stop if it was running and '-' to start or continue if it was running.

#include <iostream>

int Modulus(int Number, int number) {
  int q=Number/number;
  return Number-(q*number);
}

char GetChar(int lowerlimit, char a, int upperlimit) {
   return (a+Modulus(lowerlimit,upperlimit));
}

int main() {

  int j(7),k(2),l(5),m(1),i(0),cnt(0);
  unsigned long long totIt(0);
  char caRow[80] = {0};
  // This will make it easer to see streaks starting and stopping
  memset(caRow,' ',sizeof(caRow));

  while(true){
     int i = 0;
     while (i<80){
        if(caRow[i]!=' ')
        {
           caRow[i]=GetChar(j+i*i,33,30);
        }
        std::cout << caRow[i];
        i++;
     }
     std::cout << std::endl;
     j = (j+31);
     k = (k+17);
     l = (l+47);
     m = (m+67);
     caRow[Modulus(j,80)] = '-';
     caRow[Modulus(k,80)] = ' ';
     caRow[Modulus(l,80)] = '-';
     caRow[Modulus(m,80)] = ' ';
     // Delay
     while(i++ < 10000000);
  }
  return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.