Skip to content

Instantly share code, notes, and snippets.

@faytey
Created August 2, 2024 20:48
Show Gist options
  • Select an option

  • Save faytey/3228816516bdb07c04116c7190616bcd to your computer and use it in GitHub Desktop.

Select an option

Save faytey/3228816516bdb07c04116c7190616bcd to your computer and use it in GitHub Desktop.
cairo learnings
use core::option::OptionTrait;
use core::traits::TryInto;
use core::array::ArrayTrait;
use core::traits::Into;
use core::fmt::{Display, Formatter, Error};
fn mul(x: u16, y: u16) -> u32 {
let z: u32 = x.into() * y.into();
z
}
// ASSIGNMENT
// 1. Create a subtraction and an addition function
// 2. Create a 6 member array,
// 2a. Add all items in the array.
// 2b. Subtract all items in the array.
fn mul_arr() -> u32 {
let mut test: Array<u16> = ArrayTrait::new();
let a: u16 = 2;
let b: u16 = 3;
let c: u16 = 4;
let d: u16 = 5;
let e: u16 = 6;
test.append(a);
test.append(b);
test.append(c);
test.append(d);
test.append(e);
println!("The test array: {:?}", test);
let mut i = 0;
let mut stored = 0;
let mut new_arr = array![];
while !test.is_empty() && test.len() > 1 {
println!("The test@i: {:?}", *test.at(i));
println!("The test@i + 1: {:?}", *test.at(i + 1));
// println!("The test array: {:?}", test);
stored = mul(*test.at(i), *test.at(i + 1));
println!("The stored value is: {:?}", stored);
test.pop_front().unwrap();
test.pop_front().unwrap();
println!("The test array remains: {:?}", test);
while !test
.is_empty() {
new_arr.append(test.pop_front().unwrap());
println!("The test array remain: {:?}", test);
println!("The new array contains: {:?}", new_arr);
};
test.append(stored.try_into().unwrap());
println!("The test array has: {:?}", test);
while !new_arr
.is_empty() {
test.append(new_arr.pop_front().unwrap());
println!("The test array remains: {:?}", test);
println!("The new array contains: {:?}", new_arr);
};
};
println!("stored value is: {:?}", stored);
stored
}
fn main() {
mul_arr();
}
@Ucheokolo
Copy link

`
use core::array::ArrayTrait;
fn multiply() {

let mut test: Array<u16> = ArrayTrait::new();
let a = 2_u16;
let b = 3_u16;
let c = 4_u16;
let d = 5_u16;
let e = 6_u16;

test.append(a);
test.append(b);
test.append(c);
test.append(d);
test.append(e);
let mut i = 0;
let l = test.len();
let mut result: u16 = 1;
println!("l is: {}", l);
while i < l {
    println!("i at this point is: {}", i);
    println!("result at this point is: {}", result);
    result = result * *test[i];
    i += 1;
};
println!("result is: {}", result );

}

fn addition() {
let mut add_arry: Array = ArrayTrait::new();
let a = 2_u16;
let b = 3_u16;
let c = 4_u16;
let d = 5_u16;
let e = 6_u16;

add_arry.append(a);
add_arry.append(b);
add_arry.append(c);
add_arry.append(d);
add_arry.append(e);

let l = add_arry.len();
let mut result: u16 = 0;
let mut i = 0;

while i < l {
    result = result + *add_arry[i];
    i += 1;
};
println!("the sum of the array is: {}", result);

}

fn subtraction() {
let mut sub_arry: Array = ArrayTrait::new();
let a = 2_i16;
let b = 3_i16;
let c = 4_i16;
let d = 5_i16;
let e = 6_i16;

sub_arry.append(a);
sub_arry.append(b);
sub_arry.append(c);
sub_arry.append(d);
sub_arry.append(e);

let l = sub_arry.len();
let mut result: i16 = *sub_arry[0];
let mut i = 1;

while i < l {
    result = result - *sub_arry[i];
    i += 1;
};
println!("the difference of the array is: {}", result);

}

fn main() {
multiply();
addition();
subtraction();

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment