NEWTON
Asked
6 months ago
631
views
7
Can you help how to change the code so that it outputs 400 as well?
// The following code outputs the numbers 100, 200, 300.
// 1. Click on "Run" to see the output.
// 2. Change it to output 400 as well.
// Use the output builtin.
%builtins output
// Import the serialize_word() function.
from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*}() {
// Output 100 by calling serialize_word.
serialize_word(100);
// Output 200.
serialize_word(200);
// Output 300.
serialize_word(300);
// Return.
return ();
}
Answers to this question are a part of the ✨ 17 days of Cairo Lang with Playground & Newton. ✨
Vote for your favorite answer - the best answer will win a $10 award. A new day – a new reward! During the next 17 days, our goal is to attract more developers to the Cairo language and to systematize the knowledge of Cairo lang. Read rules
Newton
asked
6 months ago
5
Accepted answer
Just add serialize_word(400);
The function serialize_word(): To write the value x to the output, we can use the library function serialize_word(x). serialize_word gets one argument (the value we want to write) and one implicit argument output_ptr (which means that behind the scenes it also returns one value). In fact it’s quite simple: it writes x to the memory cell pointed by output_ptr (that is, [output_ptr]) and returns output_ptr + 1. Now the implicit argument mechanism kicks in: in the first call to serialize_word() the Cairo compiler passes the value of output_ptr as the implicit argument. In the second call it uses the value returned by the first call.
Source: https://www.cairo-lang.org/docs/hello_cairo/intro.html
// The following code outputs the numbers 100, 200, 300.
// 1. Click on "Run" to see the output.
// 2. Change it to output 400 as well.
// Use the output builtin.
%builtins output
// Import the serialize_word() function.
from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*}() {
// Output 100 by calling serialize_word.
serialize_word(100);
// Output 200.
serialize_word(200);
// Output 300.
serialize_word(300);
// Output 400.
serialize_word(400); // << Solution
// Return.
return ();
}
Program output:
100
200
300
400
Number of steps: 22
Program hash: 0x0542e6604d143454ef2a63b5e6762616c9999e2224bc7d6098a1b9617fa9dd72
Program output:
100
200
300
400
Number of steps: 22
Program hash: 0x0542e6604d143454ef2a63b5e6762616c9999e2224bc7d6098a1b9617fa9dd72
juancito.eth
answered
6 months ago
0
This is the most classic 1st programming challenge.
People are awesome though, some answers go to such length, it's great!
✅ Solution
serialize_word(z);
serialize_word is like printf, console.log, and so many others...
victorforissier.eth
answered
5 months ago
0
// We need to add this line after serialize_word(300)
serialize_word(400);
0xac3d...5d2902
answered
6 months ago
0
I have created a repository for the cairo challenge https://github.com/chococrypto/17daysOfCairo
here is my answer
// Use the output builtin.
%builtins output
// Import the serialize_word() function.
from starkware.cairo.common.serialize import serialize_word
func print_hundredths{output_ptr: felt*}(max, init) {
serialize_word(init);
if(init == max) {
return();
}
print_hundredths(max, init = init + 100);
return();
}
func main{output_ptr: felt*}() {
print_hundredths(400, 100);
return ();
}
chocolaite
answered
6 months ago
4
%builtins output
from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*}() {
print(4, 100, 100);
return ();
}
func print{output_ptr: felt*}(times: felt, start, step: felt){
if(times == 0){
return();
}
serialize_word(start);
print(times-1, start+step, step);
return();
}
Toni Tabak
answered
6 months ago
4
Please have a look at this link.
As you can see, the way to write to the output in Cairo is to actually write your value to the output_ptr
, which you have provided as an implicit argument to your main
function (read here for implicit arguments). So what is serialize_word
doing in the background?
You find the code for it here, where you can see that the function simply stores your value in the output_ptr
and moves the pointer forward by one.
So to ouput 400 you could add a serialize_word(400);
to your function, or you could do the below also:
%builtins output
func main{output_ptr: felt*}() {
// write straight to the output pointer
// move it by one each time you write in it
assert [output_ptr] = 100;
assert [output_ptr + 1] = 200;
assert [output_ptr + 2] = 300;
assert [output_ptr + 3] = 400;
// reassign the output pointer by the amount of
// times you wrote in it
let output_ptr = output_prt + 4;
return ();
}
Just compile the above code with cairo-compile main.cairo --out compiled_cairo.json
and run it using cairo-run --program compiled_cairo.json --layout=small --print_output
, you should be getting:
Program output:
100
200
300
400
Number of steps: 10
where you can see that we reduced the numbers of step for the execution by more than 2!
answered
6 months ago
1
// The following code outputs the numbers 100, 200, 300.
// 1. Click on "Run" to see the output.
// 2. Change it to output 400 as well.
// Use the output builtin.
%builtins output
// Import the serialize_word() function.
from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*}() {
// Output 300.
serialize_word(400);
// Return.
return ();
}
Program output:
400
Number of steps: 7
Program hash: 0x032772c76f12387460a127b421a49bbe955b8fc9dd67b1f3a3dbcbabe76eb34b
0x9aa4...7B759A
answered
6 months ago
0
// Use the output builtin. %builtins output
// Import the serialize_word() function. from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*}() { // Output 100 by calling serialize_word. serialize_word(100); // Output 200. serialize_word(200); // Output 300. serialize_word(300); // Output 400. serialize_word(400); // Return. return (); }
0x94C4...834517
answered
6 months ago
0
// The following code outputs the numbers 100, 200, 300, 400.
// 1. Click on "Run" to see the output.
// 2. Change it to output 400 as well.
// Use the output builtin.
%builtins output
// Import the serialize_word() function.
from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*}() {
// Output 100 by calling serialize_word.
serialize_word(100);
// Output 200.
serialize_word(200);
// Output 300.
serialize_word(300);
// Output 400.
serialize_word(400);
// Return.
return ();
}
Asten
answered
6 months ago
0
// The following code outputs the numbers 100, 200, 300. // 1. Click on "Run" to see the output. // 2. Change it to output 400 as well.
// Use the output builtin. %builtins output
// Import the serialize_word() function. from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*}() { // Output 100 by calling serialize_word. serialize_word(100); // Output 200. serialize_word(200); // Output 300. serialize_word(300); // Output 400. serialize_word(400); // Return. return (); }
Asten
answered
6 months ago
0
// The following code outputs the numbers 100, 200, 300.
// 1. Click on "Run" to see the output.
// 2. Change it to output 400 as well.
// Use the output builtin.
%builtins output
// Import the serialize_word() function.
from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*}() {
// Output 100 by calling serialize_word.
serialize_word(100);
// Output 200.
serialize_word(200);
// Output 300.
serialize_word(300);
// Outpit 400
****serialize_word(400);****
// Return.
return ();
}
0xfC2F...EC0d87
answered
6 months ago
1
serialize_word
call takes 5 stepsserialize_word
adds the passed felt to the output pointer// Use the output builtin. %builtins output // Import the serialize_word() function. from starkware.cairo.common.serialize import serialize_word func main{output_ptr: felt*}() { // Output 100 by calling serialize_word. serialize_word(100); // Output 200. serialize_word(200); // Output 300. serialize_word(300); // Output 400. serialize_word(400); // Return. return (); }
First run
Program output:
100
200
300
Number of steps: 17
Program hash: 0x06dfe1be49c593f8b693c70a6e9ce50ea84272d76360a46dfb26d6b536d94b77
Second run
Program output:
100
200
300
400
Number of steps: 22
Program hash: 0x0542e6604d143454ef2a63b5e6762616c9999e2224bc7d6098a1b9617fa9dd72
barrant
answered
6 months ago
0
// The following code outputs the numbers 100, 200, 300. // 1. Click on "Run" to see the output. // 2. Change it to output 400 as well.
// Use the output builtin. %builtins output
// Import the serialize_word() function. from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*}() { // Output 100 by calling serialize_word. serialize_word(100); // Output 200. serialize_word(200); // Output 300. serialize_word(300); // Solution - Output 300 serialize_word(400); // Return. return (); }
0xf2c1...A516b0
answered
6 months ago
1
// Use the output builtin.
%builtins output
// Import the serialize_word() function.
from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*}() {
// Output 100 by calling serialize_word.
serialize_word(100);
// Output 200.
serialize_word(200);
// Output 300.
serialize_word(300);
// Output 400.
serialize_word(400);
// Return.
return ();
}
You only need to pass the value that you want to output, as an argument to the serialize_word() function. In this case 400:
// Output 400.
serialize_word(400);
serialize_word() appends a single word to the output pointer, and returns the pointer to the next output cell.
For more reference: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/cairo/common/serialize.cairo
0x68Db...576a29
answered
6 months ago
0
func main{output_ptr: felt*}() { // Output 100 by calling serialize_word. serialize_word(100); // Output 200. serialize_word(200); // Output 300. serialize_word(300);
//Solution
serialize_word(400);
return ();
}
0x0129...93AE11
answered
6 months ago
4
Explanation of components used in code:
builtins: Cairo supports the implementation of predefined operations called builtin. Our program communicates with the external world by making use of the output builtin,which is specified by the directive %builtins output.
Implicit arguments: in curly brackets are required for the storage operations. it stores the addresses of the output. This automatically adds an argument and a return value to the function.
serialize_word: The library function serialize_word can be used to print the value to the output. serialize_word takes one argument and one implicit argument output ptr
Add one more serialize_word function which accepts 400 as an argument.
// Use the output builtin.
// inscribed in the CPU(preprocessor)
%builtins output
// Import the serialize_word() function.
from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*}() {
// Output 100 by calling serialize_word.
serialize_word(100);
// Output 200.
serialize_word(200);
// Output 300.
serialize_word(300);
// Output 400.
serialize_word(400);
// Return.
return ();
}
Ishita Rastogi
answered
6 months ago
1
The solution is to add one extra serialize_word
call in the end:
%builtins output
from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*}() {
// Output 100.
serialize_word(100);
// Output 200.
serialize_word(200);
// Output 300.
serialize_word(300);
// Output 300.
serialize_word(300);
// Solution: Output 400.
serialize_word(400);
return ();
}
0xB883...B7956a
answered
6 months ago
1
The function serialize_word
gets one argument (x
the value we want to write) and one implicit argument output_ptr
(which means that behind the scenes it also returns one value).
It will write x
to the memory cell pointed by output_ptr
(that is, [output_ptr]
) and returns output_ptr + 1
.
To outputs 400, you only need to add the following call before return:
serialize_word(400);
rob
answered
6 months ago
2
%builtins output
from starkware.cairo.common.serialize import serialize_word
func main{output_ptr: felt*}() {
serialize_word(100);
serialize_word(200);
serialize_word(300);
// Solution Output 400.
serialize_word(400);
return ();
}
0xB677...2a4AA6
answered
6 months ago
1
The serialize_word library function abstracts the need to work directly with the output pointer (see the felt* passed into the main function, it permits accessing segments of memory to output for use/need by the verifier) All you need to do is pass 400 to the serialize_word function and the main will in turn output 400.
manakin.eth
answered
6 months ago
How to divide felt in Cairo Lang using unsigned_div_rem?
How to make recursive function in Cairo Lang?
What is the difference between tempvar/let in Cairo Lang? How to use allow_locals and local?
How to use get_fp_and_pc in Cairo Lang?
How to submit a StarkNet contract?
What is SHARP in Cairo Language?
Cairo Lang / StarkNet: What are Revoked references? What is alloc_locals?
What is Starknet.js?
why does keccak256 replace sha256 in warp?
Can tempvar be of a different type, say U256?
ApeWorX: How can I test on multi-chain with both Starknet and Ethereum?
Deploying kakarot evm on starkEx
How can I send a Uint256 amount of ERC20 tokens from L1 to starknet? And how should I build my payload for "sendMessageToL2" to match the Uint256 format of Cairo?
how to send token