מבחן טרימסטר ד' מבוא לאלקטרוניקה
10/12/2017
class Program { public static int Sum(params int[] numbers) { int sum = 0; for (int i = 0; i < numbers.Length; i++) { sum += numbers[i]; } return sum; } static void Main(string[] args) { Console.WriteLine(Sum()); // ידפיס 0 Console.WriteLine(Sum(3, 5)); // ידפיס 8 Console.WriteLine(Sum(3, 5, 10)); // ידפיס 18 } }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Hello"); }
class Program { static void Swap(ref int x, ref int y) { int tmp = x; x = y; y = tmp; } static void Main(string[] args) { int a = 5, b = 10; Swap(ref a, ref b); // :הדפסה Console.WriteLine("a: " + a); // a: 10 Console.WriteLine("b: " + b); // b: 5 } }
double n = Math.Abs(-8.9); Console.WriteLine(n); // ידפיס: 8.9
double n = Math.Pow(10, 2); Console.WriteLine(n); // ידפיס: 100
double n = Math.Sqrt(25); Console.WriteLine(n); // ידפיס: 5
Random r = new Random();
r.Next();
r.Next(1,7);
לדוגמה:Random r = new Random(); Console.WriteLine(r.Next(1,9)); // ידפיס מספר רנדומלי בין 1 ל-8 כולל
Console.WriteLine(str);
str = Console.ReadLine();
str.Length
int[] Array;
מערך זה עדיין לא מאותחל. כרגע Array הוא רק "שם של מערך". בדומה לשפת C - שם של מערך הוא מצביע.Array = new int[5];
כעת Array הוא מערך מאותחל בגודל 5 איברים.int[] Array = new int[5];
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication25 { class Program { static void Main(string[] args) { } } }
void setcolor(unsigned short color){ HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hcon, color); }
#include <iostream> using namespace std; void main(){ int *ptr = new int; *ptr = 5; cout<<*ptr<<"\n"; // ידפיס 5 }
#include <iostream> using namespace std; void main(){ int *ptr = new int; *ptr = 5; cout<<*ptr<<"\n"; delete ptr; }
#include <iostream> using namespace std; void swap(int &a,int &b){ int temp; temp=a; a=b; b=temp; } void main(){ int num1=5,num2=10; swap(num1,num2); cout << num1 << " " << num2 << "\n"; }
#include <iostream> using namespace std; int f1(){ //1 return 5*3; } int f1(int x, int y){ //2 return x*y; } double f1(double x, double y){ //3 return x*y; } double f1(int x, double y, int z){ //4 return x*y*z; } void main(){ cout<<f1()<<"\n"; // 15 cout<<f1(7,8)<<"\n"; // 56 cout<<f1(7.5,8.5)<<"\n"; // 63.75 cout<<f1(2,0.5,5)<<"\n"; // 5 cout<<f1(2,5,6)<<"\n"; // 60 }