 Okay. Continuing with this series, if you have not yet watched the intro to this series, please click on the annotation that's hopefully up on the screen that will bring you to the playlist and watch at least the first intro video, which explains the concept of the dice game called Toss Up. If you have never played Toss Up or you haven't watched that intro video, you may not understand what we're trying to do here. Basically, we're trying to figure out our odds on when we're rolling the dice, what our odds are of losing or winning. This is also part of a series where I'm doing the same program in a few different languages this week, and today we're going to be doing it in C, which I am NOT a very good C programmer. So although my code here will work, there probably are better ways of writing it up, and I hope that I explain things properly, but my code does work. So let's just jump into it. I'm going to use Vim as my text editor, but use whatever editor you want, and I'll create a C file called Toss Up. Next, we will import some header files. So include the standard input output library or header, and include standard lib, and also include the time header, which I believe mainly I have for generating random numbers. Next, we're going to type define char asterisk string, and we're also going to start our main function here, and we will give it an output of return zero. Because I always forget to do that, I'm going to do that first. Next, we're going to also want to be able to pass arguments to this program. So once again, if you watched the previous video on doing this in bash with shell scripts, you can either run the command, it will ask you how many times you want to roll, and the number of dice you want to roll, because that's the two things we need from the input of the user. You can either put that as arguments or it will ask you if you don't. So we're going to have to pass the arguments, we'll do that here, integer arg c comma constant character, the argument V, like that. I think I typed that all right. Now we're going to create a bunch of variables, the number of rolls, the number of dice to roll, the, and a few other variables here, and probably could put all these on one line, but I'm just going by my notes. Once again, I am not a very good C programmer, and I don't want to vary from my notes because things probably won't work if I do what I think will work, but haven't tested. We got integers here for wins and loses, that's to count the number of times you win and number of times you lose. We're also going to do for number of greens and number of reds. In this game, we rolled dice with six sides, summer green, summer yellow, summer red. We're not making integer for the yellow because in what we're doing here, yellows are kind of irrelevant. Next, we're going to create a random number. We need to run this command from the timer header. We're going to set this to null here. Okay. There we go. Next, we're going to say string dice six. That's creating our array, which will hold strings, and there will be six options because there's six sides to the die. We're going to say dice zero, which will be the first side of the die. We're going to say equals the string red. We're going to say dice one, so the next side of the die equals yellow. To save some time, I am going to copy and paste that, change this to two, and let's see. Oops. We'll change this to a three. Once again, we're just creating each side of the die, and there are on each die one red side, two yellow sides, and three green sides. Okay. Now that we have our die created or dice, depending on how many times we loop through this, we're going to check to see if the user put in any arguments, basically the number of times two roll and the number of dice to roll. So we're going to say argc, the argument count, if that's less than two, then we are going to print f. How many times do you want to roll and create a new line after that? Basically, if there's less than two arguments, so basically if the user did not give us all the information we need at the time of running the program, we will ask for the information we need. And we're going to use scanf to get the user input, and we will put that into a variable called rolls. Then we need to find out print f, the number of dice to roll. So we'll say how many dice do you want to roll new line character there, and we'll grab the user input with scanf. And we will say to put the user input into a variable called dice num. Both these we created up here, but did not set values. So if the user did not pass those as arguments, we're going to ask them for it now. Then we're going to say else. So if there are at least two arguments, we're going to use those arguments as the variables. The first one will be rolls equals ATIO. I have no clue what ATIO stands for. I could guess though. Argument time of input. I have no clue. Don't quote me on that. That's just a complete guess. But we're going to check the arguments. The arguments are given kind of like an array. So we got argument one there will be the number of rolls and dice, number of dice will equal ATIO, or OI. And we're going to say once again to check the arguments variables input there is the second one, which would be the number of dice. Really, if you're writing out this program for mass usage, you would want to have a man file or some sort of output when you run the program telling the user how to put in the variables, the arguments through the variables. But since it's a tutorial, we're not going to get into that. Now we're going to do a loop here. We're going to create a variable called I, which will equal the number of rolls. And then we will say while I is greater than zero, we're going to say I minus minus. So basically we're taking whatever the number of rolls are. So let's say we roll 100 times each time it loops. It's going to subtract one. So I minus minus means subtract one from that value and do this as long as it's greater than zero. We'll put our brackets here for the for loop. And we are now going to do another for loop. We'll say Y will equal the dice number. So the number of dice will say as long as Y is greater than zero. Loop and each time we loop subtract one from Y. So we are going to, you know, have a die. Say we have four dice. We're rolling each time we roll. We're going to roll each die one time. Now we're going to say R, which is a variable we created earlier. We're going to say set that to a random number. And we're going to limit that to six sides. So zero through five in this case for our array, which is once again created right here with its values. So we're creating R so that we can check the array. So now we're going to say set die equal to dice R. So basically we are setting die and then we're looking at our array and we're putting in here a number between zero and five, which is the random number generated here. And from that we're getting the string that that array number equals. Now we're going to say print F. We're going to print out a string here with a comma after it. And that string will be whatever the value of die is. Next we are going to do an if then statement. We're going to say if die equals green, then we're going to take the value of green, which we set up of. We started it at zero because we had had zero greens when we start the program. And we'll add one to it if there is a green. Next we're going to say else if die equals red, then we're going to take the value of red and add one to it. The reason we say else if in this case rather than just else is because there are yellow rolls, which we don't care about. But we need to have in there in the array to get an accurate number. Okay, next we're going to basically print a new line character because basically every time that loops were saying this color was rolled comma this color was rolled comma and when we're done with that role, then we need to create a new line. So we're just going to print F new line character. That's all that does. They're gonna say if green equals zero and red is less than zero, then we're going to take the number of losses and add one to it. So lose, which starts at zero and if that's true, we'll now equal one. Else we will add one to win. And I'm going to explain all this hopefully a little bit better here in a moment. We're going to say red equals zero, green equals zero, which is just resend them back to zero before we loop again. So we're mostly done. We've got all our roles. We just now need to print our output. So I'm going to say print F. I'm going to say wins percent D and for new line losses colon percent D, which are the placeholders for the variables we're about to put in there, which are win comma lose. And that's our whole program. So let's quickly look over it. Here we're starting our main function, getting the arguments passed from users. If there are any, creating a bunch of variables up here. Next, we're resetting our time null to null for for our random generator. Then we're creating our die, which is six sides. Here are the six sides of it. One red, two yellow, three greens. And then we're going to say, well, if the user gave us, didn't give us argument arguments, or didn't give us enough arguments, then we're going to ask them for the information we need, which is number of roles, number of dice, and placing them in these variables here. Now if they did give us at least two arguments, hopefully they're proper ones, we're going to get those arguments and put them into the variables here. Next, we're going to start our main for loop here. We're going to say, create a variable called I set it to the number of roles. And then as long as I is over zero, we're going to do this loop. But each time it loops subtract one from the number. So if we say we're running 100 times, we're going to say it's 100. Next time it loops, it's going to be 99, the 98 all the way down to zero. Then we're going to say, you know, for each number of die we're rolling, so for each die we're rolling, as long as it's over zero. So we're going to start off, let's say we're going to roll five dice, it will equal five, we'll roll one, then roll the next, then roll the next, roll next, very quickly here, putting the output on a line separated by commas. And and we're counting the number of greens on that line and the number of reds on that line. And then we're going to check if there's zero green, but red is greater than zero. Well, then you lost because any time you have a red with no greens, you've lost. But as long as you have at least one green, you're good. Or if there's no reds, you're good. So we add to the wind column there. So and then we have to reset our values for red and green before we loop back again to the beginning of the loop because this loop, each time it loops is one roll of a certain number of dice. Once we've rolled the number of rolls that we've inputted, we're going to get our output, which is number of losses and number of wins. Let's save that. I'm going to use GCC, our program, the output will say toss up. And I type something wrong. Let's go in here and see what I typed wrong. And hopefully I can figure it out because, well, let's look at the, it's saying line 37, character 15, I believe, is the way you read that. So line 37. Oh, that should be a comma, not a period. Okay, let's try compiling it again. It compiled properly. Whoo. Okay, now we're going to run the program. It's going to ask how many times you want to roll, we'll say 100. And it says how many dice do you want to roll? We'll say five. So as you can see, there should be 100 lines with five dice rolled each line. And down here, it will tell you you won 98% of the time on those 100 rolls. Of course, you'll, you can, oops, let's control C that, give the input, we'll say 100 rolls here to dice. We'll roll that here. We won 86% of the time and lost 14. So as I roll, obviously, you're going to get different numbers each time. But it looks like if you have two dice to roll, I would say looks like you have 10 to 15% chance of losing. Let's roll it 1000 times. It's 131, which is 13% 15% chance of losing 18% chance of losing 14% chance of losing 12% chance of losing. Let's try one die 1000 times. Now you also notice that compared to if you watch the bash video, this runs a whole lot faster. In fact, here in a second, I'll give you and it will compare the two. So 16 16. If I run this program twice in a row, you see I get the same output twice in a row. I think that's because of the way I do random numbers using the time library. And so it generates a random number based on time because computers can't really generate random numbers. So there's probably a better way. In fact, I think I've done better ways in other scripts, other programs. But since it's using the time, I think if you run something within the same second, it's going to give you the same output. So probably could have done the random number a little bit better. But it is giving us good output here. Let's let's now compare this to our bash script. So I'm going to use the time command. Well, let's run oral 10 dice 1000 times and time it. Here you can see that system 4% user. It's running really fast. Let's let's go back and go into our bash script here. And we'll run the same thing. We'll say time, then the name of our script here will roll 1000 times 10 dice. You can see it takes a little bit longer to run. Here, it says that it took, let's see, user system. I forget which time I want to say that this is probably the most accurate time. This this total here CPU time is probably the time that we're going for because it probably took just under three seconds. Let's run again. 123. Yep. So looking here, it took 2.9 seconds to run the bash script. We'll run it again. Of course, this will vary on system system, depending on the speed of your system, but consistently, just under three seconds. If we go back into our C here and we say time, oops, what am I doing? Time toss up 1000 times 10 dice. You can see it took less than a tenth of a second. So this is this is one of those examples where crunching these numbers when you're doing something like this. It's still pretty fast, but you can see why a compiled program like C is definitely faster than a bash script or a shell script of some sort. Obviously, both programs probably could have been written better, but both seem to work. That was pretty good odds. If you're running, if you have full 10 dice, you got pretty good chance of winning. So that time we got zero losses, zero losses. So whenever you have the full 10 dice, you got a pretty good chance of winning. There you have 2%, actually not 2%, 0.02%, right? Because we're running 1000 times or 0.2%. So if you're playing toss up and you have all 10 dice, I would recommend rolling. Otherwise, you're not really playing. Anyway, thank you for watching. I hope this tutorial wasn't too long and boring. And I hope you're enjoying these, seeing how I do the same thing in different programming languages. The next tutorial will probably be on doing the same program I'm thinking in JavaScript, which will be even slower than the bash script I can almost guarantee. Okay, I thank you for watching. Please visit filmsbychrist.com. That's Chris at the K. There should be a link in the description. Also in the description, if I did not forget, should be a link to this C code. So you can download it or copy and paste it at least both. But I also recommend typing it out if you're trying to learn. And also if you are a C programmer and you're just banging your head against the wall watching how I program and see because I know I'm a horrible C programmer. Sorry. At least it works. But I would love to see your solutions. So go ahead and post your solutions and then put a link in the comments if you can to it on pasted bin or something like that. Anyway, thank you for watching. And I hope that you have a great day.