Jump to content

For and While loops

InVeyd

Need help with this one. Every multiples of 3 should be "Wowza" and "Cowza for 5's.
 

12Wowza4CowzaWowza78WowzaCowza11Wowza1314WowzaCowza16

... etc up to 100

Edited by prolemur
code tags :)
Link to comment
Share on other sites

Link to post
Share on other sites

Need help with this one.

You.. You only want to know the difference?

I believe for loops are like.. For everyone instance of something

(Lets say for example, for every player in a game, it happens once.)

And while loops, happen while something is true.

Example: while there is more than 2 players in game 

 

I believe it works like that

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites


while (condition)

{do this thing}

 

for(initializer; break condition; incrementor)

{do this thing}

Edited by alpenwasser
code tags :)

We can't Benchmark like we used to, but we have our ways. One trick is to shove more GPUs in your computer. Like the time I needed to NV-Link, because I needed a higher HeavenBench score, so I did an SLI, which is what they called NV-Link back in the day. So, I decided to put two GPUs in my computer, which was the style at the time. Now, to add another GPU to your computer, costs a new PSU. Now in those days PSUs said OCZ on them, "Gimme 750W OCZs for an SLI" you'd say. Now where were we? Oh yeah, the important thing was that I had two GPUs in my rig, which was the style at the time! They didn't have RGB PSUs at the time, because of the war. The only thing you could get was those big green ones. 

Link to comment
Share on other sites

Link to post
Share on other sites

any while can be a for, and any for can be a while. for example, a program that counts to 30, and prints what X is.

for(x=0;x != 30;x++){cout << X << endl;} x=0while (x != 30){x++cout << X << endl;}
 

oh, this in in C++ BTW.

in normal C the only change would be that the line

 

cout << X << endl;
would become:

 printf (" %d  \n" ,X );
Edited by alpenwasser
code tags :)

We can't Benchmark like we used to, but we have our ways. One trick is to shove more GPUs in your computer. Like the time I needed to NV-Link, because I needed a higher HeavenBench score, so I did an SLI, which is what they called NV-Link back in the day. So, I decided to put two GPUs in my computer, which was the style at the time. Now, to add another GPU to your computer, costs a new PSU. Now in those days PSUs said OCZ on them, "Gimme 750W OCZs for an SLI" you'd say. Now where were we? Oh yeah, the important thing was that I had two GPUs in my rig, which was the style at the time! They didn't have RGB PSUs at the time, because of the war. The only thing you could get was those big green ones. 

Link to comment
Share on other sites

Link to post
Share on other sites

A while loop does something as long as a condition is true.

 

A for loop goes through a list of sorts until the list finishes.

"My game vs my brains, who gets more fatal errors?" ~ Camper125Lv, GMC Jam #15

Link to comment
Share on other sites

Link to post
Share on other sites

Need help with this one. Every multiples of 3 should be "Wowza" and "Cowza for 5's.

... etc up to 100

 

Follow your Thread.

you need a while loop, with if statements in it.

 

you also need to use the modulus operator "%" which gives the remainder of an operation.

 

so 6%3= 06%4= 266%11= 010%4= 2
 

understand?

your program needs to use the modulus operator to detect when a multiple is divisable by 3 and 5, so the modulus would yield 0 when those conditions are met.

 

if (x%3 = 0){cout<< wowza}
Edited by alpenwasser
code tags :)

We can't Benchmark like we used to, but we have our ways. One trick is to shove more GPUs in your computer. Like the time I needed to NV-Link, because I needed a higher HeavenBench score, so I did an SLI, which is what they called NV-Link back in the day. So, I decided to put two GPUs in my computer, which was the style at the time. Now, to add another GPU to your computer, costs a new PSU. Now in those days PSUs said OCZ on them, "Gimme 750W OCZs for an SLI" you'd say. Now where were we? Oh yeah, the important thing was that I had two GPUs in my rig, which was the style at the time! They didn't have RGB PSUs at the time, because of the war. The only thing you could get was those big green ones. 

Link to comment
Share on other sites

Link to post
Share on other sites

what language?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Ah the good old fizzbuzz test: http://c2.com/cgi/wiki?FizzBuzzTest

 

 

What you'll need to know about for this program is the following:

- If statements

- The modulo operator

- Loops (both for and while loop will work)

 

I always recommend programming in baby steps. How big a baby step is, depends on how experienced you are.

Since I see that you are just beginning let's take really small steps


first let's write a program, that checks if a number is divisible by three.
We can simply use the modulo operator and an if statement for this:
 

int number = 6;if(number % 3 == 0) //check if the number is divisible by 3  printf("This number is divisible by 3!\n");

no let's take that program, and chang it so first we check if it's divisble by 3, and if it's not if it's divisible by 5
 

int number = 10;if(number % 3 == 0) //check if the number is divisible by 3  printf("This number is divisible by 3!\n");else if(number % 5 == 0) //if it's not, check if it's divisible by 5  printf("This number is divisible by 5!\n");

Now a bit more tricky we have to know if it's divisible by both 3 and 5. Naturally you would just add another else if to the end, but that won't work. That is because the program will find it's divisible by 3, and then skip over the other checks, so we have to check for that first.
 

int number = 30;if(number % 15 == 0) //we do the modulo of 15 here, because any number that is divisible by both 3 and 5, is divisible by 15 printf("this number is divisible by both 3 and 5!\n");else if(number % 3 == 0) printf("this number is divisible by 3!\n");else if(number % 5 == 0) printf("this number is divisible by 5!\n");

Now we have to put this in a loop to check a bunch of numbers. I'll show how to do that using a while loop.

int number = 1; //the starting numberwhile(number <= 100) //only run while we haven't printed 100 values{  int number = 30;  if(number % 15 == 0)  //check if the number is divisible by both 3 and 5    printf("this number is divisible by both 3 and 5!\n");  else if(number % 3 == 0) //check if the number is divisible by 3    printf("this number is divisible by 3!\n");  else if(number % 5 == 0) //check if the number is divisible by 5    printf("this number is divisible by 5!\n");   number = number + 1; //increment the number by 1}

This program doesn't fulfill all the requirements yet, but can be easily modified to do so

Edit: code tags

PS: this code is writing in C, but is also valid C++, I am not sure what language you are using, but the concepts will be the same

Link to comment
Share on other sites

Link to post
Share on other sites

Batch script

@[member='Echo'] offfor /l %%i in (1,1,100) do (set /a "is3=%%i%3"set /a "is5=%%i%5"set "return=%%i"if %is5% equ 0 set "return=Wowza"if %is3% equ 0 set "return=Cowza"if %is5%%is3% equ 11 set "return=WowzaCowza"echo %return%)

A riddle wrapped in an enigma , shot to the moon and made in China

Link to comment
Share on other sites

Link to post
Share on other sites

Thought i would do it in PHP for you.

<?php	for($i = 1; $i < 101; $i++)	{		print $i;		if($i%3 == 0) print ' Wowza';		if($i%5 == 0) print ' Cowza';		print '<br />';	}?>
Link to comment
Share on other sites

Link to post
Share on other sites

They hardly teach the modulo operand in school, it's quite funny.

 

The other day my friend wanted to write a program that prints out even numbers, up to however many the for loop condition was. 

 

I was like, errr.

 

if (number%2 ==0) 

  printf("Even\n");

else 

  printf("odd\n");

 

He's in a beginning java class. But either way, I was hardly taught it. I figured it out myself.

Link to comment
Share on other sites

Link to post
Share on other sites

f1=0f=0var array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,43,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]f=array.lengthf1=f-1z=0g=0while (z<f1) {g=array[z]if (g%3==0 && g%5==0) {array[z]="WowzaCowza"} elseif (g%3==0) {array[z]="Wowza"} elseif (g%5==0) {array[z]="Cowza"}document.write(array[z])document.write("</br>")z++} 

I would do it like this in javascript but keep in mind that there is a lot of solution to this problem. you don't need to set all var to 0 just the z it's more of a not for myself

Link to comment
Share on other sites

Link to post
Share on other sites

They hardly teach the modulo operand in school, it's quite funny.

 

The other day my friend wanted to write a program that prints out even numbers, up to however many the for loop condition was. 

 

I was like, errr.

 

if (number%2 ==0) 

  printf("Even\n");

else 

  printf("odd\n");

 

He's in a beginning java class. But either way, I was hardly taught it. I figured it out myself.

 

Most beginners classes assume no prior programming knowledge so all the basics are usually mentioned. The modulo operator is likely shown when the basic operations are shown. It's common to mention it after or along with the division operator. At a minimum they should tell you what it does with examples. They may or may not show common uses for it because they may want students to figure that out on an assignment (like the even/odd example).

 

Problem solving is part of programming after all and many times you need to use what you've learned in ways you weren't taught.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm well past my beginning classes now though. Spring 2016, I'll have my BS in CS with concentrations in Software Eng and Comp info and Secu.

 

I just don't recall using modulo on any assignments/hws. Maybe it's just been a while and my memory sucks. 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm well past my beginning classes now though. Spring 2016, I'll have my BS in CS with concentrations in Software Eng and Comp info and Secu.

 

I just don't recall using modulo on any assignments/hws. Maybe it's just been a while and my memory sucks. 

 

Learning on your own is also an important skill for programmers :P

 

It's entirely possible it just wasn't covered for you, it just seems odd to me. If you had a "Beginners Java/Python/whatever language" textbook for that class, I would guess it was at least covered there.

 

Either way, good luck on the rest of your degree. 

Link to comment
Share on other sites

Link to post
Share on other sites

I definitely agree. I learned it myself because it was useful (at least for the projects I was involved with) a few times.

Link to comment
Share on other sites

Link to post
Share on other sites

if(number % 15 == 0) //we do the modulo of 15 here, because any number that is divisible by both 3 and 5, is divisible by 15

 

This seems like premature optimization. It'd probably be better if you wrote if (number % 3 == 0 && number % 5 == 0). A decent compiler should reuse the value of number % 3 and number %5 in the next two checks.

Link to comment
Share on other sites

Link to post
Share on other sites

f1=0f=0var array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,43,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]f=array.lengthf1=f-1z=0g=0while (z<f1) {g=array[z]if (g%3==0 && g%5==0) {array[z]="WowzaCowza"} elseif (g%3==0) {array[z]="Wowza"} elseif (g%5==0) {array[z]="Cowza"}document.write(array[z])document.write("</br>")z++} 

I would do it like this in javascript but keep in mind that there is a lot of solution to this problem. you don't need to set all var to 0 just the z it's more of a not for myself

 

 

There is so much unneeded excess and confusing code here, this can be achieved in a mere few lines, as i have done.

<script type="text/javascript">	for(var i = 1; i < 101; i++)	{		document.write(i);		if(i%3 == 0) document.write(' Wowza');		if(i%5 == 0) document.write(' Cowza');		document.write('<br />');	}</script>
Link to comment
Share on other sites

Link to post
Share on other sites

 

There is so much unneeded excess and confusing code here, this can be achieved in a mere few lines, as i have done.

<script type="text/javascript">	for(var i = 1; i < 101; i++)	{		document.write(i);		if(i%3 == 0) document.write(' Wowza');		if(i%5 == 0) document.write(' Cowza');		document.write('<br />');	}</script>

I'm usually saving all var for other purpose so I kept that habits sorry xD

Link to comment
Share on other sites

Link to post
Share on other sites

This seems like premature optimization. It'd probably be better if you wrote if (number % 3 == 0 && number % 5 == 0). A decent compiler should reuse the value of number % 3 and number %5 in the next two checks.

 

Sorry, I am a game/graphics programmer. I do these little things without even really thinking about them xD

Link to comment
Share on other sites

Link to post
Share on other sites

any while can be a for, and any for can be a while. for example, a program that counts to 30, and prints what X is.

 

for(x=0;x != 30;x++){cout << X << endl;} x=0while (x != 30){x++cout << X << endl;}

This is assuming you are using the standard namespace, which is a bad idea because it makes the code look messy and hard to read. it would be better to replace 

cout << X << endl;

with

std::cout << X << std::endl;

Now it is clear that you are referring to cout and endl from the standard namespace and not something else. Using namespaces is a bad idea, period.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×