[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Basic coding help

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 34
Thread images: 2

File: code-008.jpg (521KB, 2235x1500px) Image search: [Google]
code-008.jpg
521KB, 2235x1500px
*
**
***
****
*****
******
*******
********
*********
**********
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
>>
>>8426329
>
>>
>>8426333
Nice trips,
also >>8426253 and >>8426255
helped him a lot thanks you two
>>
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
>>
File: 1451033246293.png (141KB, 800x600px) Image search: [Google]
1451033246293.png
141KB, 800x600px
>>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
PRINT
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)]
>>
>>8426863
>>8426931
Inform me on how you can beat the JIT Compiler?
"I made a pointer my program must run faster!"
>>
>>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)]).
Thread posts: 34
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.