*
**
***
****
*****
******
*******
********
*********
**********
My little brother in high school is in CS and needs to make a while loop that prints that out, I told him I'd help as I took the class myself but I'm a lot rustier than I remember. His teacher said it only needs 2 whiles
You do one big while which will go from line to line and in that loop you put another while which prints a line.
After each smaller loop, add 1 to the variable that contains the lenght of a line.
Think of the computer as a third-world sweatshop worker given a box with two buttons 'print *' and 'print newline'. He can follow instructions, count, and can remember things, but that's about it.
1) what is the sequence of buttons you need him to press?
2) how would you explain this sequence to him in a completely unambiguous way?
>>8426241
>non-CS majors
i'm gonna butcher this because i've only ever worked with shitter tier lua, but couldn't you do something like this?
local st = "*"
while true do
print(st)
st = st .. "*"
wait(1)
end
>>8426294
no hes only allowed to use basic stuff, he's only like a month into it he only knows like system.out.print and while
>>8426298
so somthing like
int x=10, y=10;
while (x>=0){
while (y<x){
system.out.print("*");
y++;
}
x--;
}
>>8426241
Yes anon it's absolutely your "little brother"
you aren't actually 15 and retarded enough to think anyone believes you're trying to help your little brother with retard-tier programming
>>8426322
Wow an anon on 4chan calling someone else a child how original
int line, star;
line = star = 0;
while(line < 10)
{
while(star < 10)
{
cout << "*";
star ++;
}
cout << endline;
line++;
}
this is a basic implementation IF line/star count is limited to 10.
>>8426350
no cout's
// using one while loop
function triangular(n) {
return (n*n + n) >> 1; // n(n+1)/2
}
const MAX_LINES = 10;
const MAX_STARS = triangular(MAX_LINES);
var line = 1, star = 1;
while (star++ < MAX_STARS) {
print("*");
if (triangular(line) == star) {
print("\n");
line++;
}
}
// you can inline triangular function if you haven't used functions before
>>8426241
what language?
>>8426241
I'm in high school (senior don't under age b&) taking AP Computer Science A currently. It's in Java
assuming he already knows how to write the rest of the code, he needs a counter
int counter = 0;
And an endpoint
int end = (number of lines);
while(counter<=end){
system. out. print(*)
Counter++;}
Something like that
// Written in C
#include <stdio.h>
int main(void) {
for (x= 1, x <= 10, x++) {
for (i = 1, <= x, i++) {
printf("*");
}
printf ("\n");
}
return 0;
}
>post incredibly simple problem on /g/
>dozens of pa/g/eets put up some solution to regain a bit of self confidence
>>8426415
>Java
why does anyone use this meme-tier language?
>>8426241
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
while(1) {
printf("*\n**\n***\n****\n*****\n******\n*******\n********\n*********\n**********\n");
break;
}
exit(EXIT_SUCCESS);
}
>>8426241
x = 1
While True:
print('*'*x)
x += 1
>>8426241
for(int n=1; n<x; n++){
for(int m=n; m; m--)
cout<<"*";
cout<<"\n";
}
>CS majors can't do this
>>8426863
Because it's pretty retard proof, making it easier to learn - it's also what the ap test uses and most universities intro courses.
>>8426863
I don't know what I'm talking about: the post.
>>8426241
tell him to figure it out by himself or he will get fucked later when it gets actually hard.
>>8426241
FOR I = 1 TO N
FOR J = 1 TO I
PRINT "*";
NEXT
NEXT
Simple java class:
public class test{
public static void main(String[] a){
String a = "*";
while(a.length()<10){ // <- change 10 to number of lines you want
System.out.println(a);
a += "*";
}
}
}
Is it formatted in the shape of a tree (a common question) or is it just an +1 asterick on a new line?
>>8426241
>needing 2 while loops
#include <iostream>
#include <string>
int main(){
int x;
std::cin>>x;
std::string s="";
for(int i=0; i<x; i++){
s.append("*");
std::cout<<s<<"\n";
}
}
_=[print(i*"*") for i in range(1,11)]
>>8426241
In Java, with only one while:
public static void printStars(int amount){
String stars="*";
int count=0;
int maxCount=amount;
while(count<maxCount){
System.out.println(stars);
stars+="*";
count++;
}
}
While loops are cool and all, but in this case I'd argue that a two for loops work better
Like so:
// C++
#include <iostream>
using namespace std;
int main()
{
for (int stars=1; stars <= 10; stars++)
{
for (int line=0; line < stars; line++)
cout << "*";
cout << endl;
}
return 0;
}
>Using while loops
!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname stars debug verbose
main(_Args) ->
Starlist = lists:map(fun (N) -> repeat("*", N) end, lists:seq(5, 1, -1)),
lists:foreach(fun (Star) -> io:format("~s~n", [Star]) end, Starlist).
repeat(E, N) ->
lists:flatten([E || _ <- lists:seq(1,N)]).