|
פתרון 5א/* Module: tr2_q5a.cpp Author: Yariv Amar What? This program read 4 numbers from the keyboard and prints the follows: 1. New max serias of numbers 2. "Down fix" average 3. Exact average 4. Round average Input : seria of 4 integer numbers Output: as described Assumptions on input : integer non negative and the numbers are seprated by spaces. How? Algorithm : reads the numbers using 'cin' and spaces 1. comparing numbers using __max(int a,int b) 2. using divide (/) on integers for "down fix" average 3. using divide (/) and casting to double for the exact average 4. adding to task 2 the result of dividing the remains by 2. */ #include <stdlib.h> #include <iostream.h> void main() { int a1,a2,a3,a4; //Define 4 originl numbers int b1,b2,b3,b4; //define new max numbers // Get the original numbers cout<<"Please enter 4 numbers seperated by spaces:\n"; cout<<"===========================================\n"; cin>>a1>>a2>>a3>>a4; //task 1: making new max seria b1 = a1; // firat number is always the biggest b2 = __max(a1,a2); // max (a1,a2) b3 = __max(b2,a3); // max (a1,a2,a3) b4 = __max(b3,a4); // max (a1,a2,a3,a4) cout<<"The new seria is: "<<b1<<","<<b2<<","<<b3<<","<<b4<<"\n"; //task 2: calculating down fix average of the seria cout<<"The down fix average is:"<<(a1+a2+a3+a4)/4<<"\n"; //task 3: calculate exact average cout<<"The exact average is:"<<double(a1+a2+a3+a4)/4<<"\n"; //task 4: calculate round average while x.5 is rounded upwards cout<<"The round average is:" <<(a1+a2+a3+a4)/4 + (a1+a2+a3+a4)%4/2<<"\n"; } פתרון 5ב/* Module: tr2_q5b.cpp Author: Yariv Amar What? This program read 4 numbers from the keyboard and prints the max serias of numbers using minmum variables Input : seria of 4 integer numbers Output: seria of max numbers fron the original seria. Assumptions on input : valid. How? Algorithm : calling __max in recursive way inside the cout. */ #include <stdlib.h> #include <iostream.h> void main() { int a1,a2,a3,a4; //Define 4 originl numbers //step 1: Get the original numbers cout<<"Please enter 4 numbers seperated by spaces:\n"; cout<<"===========================================\n"; cin>>a1>>a2>>a3>>a4; //step 2: making new max seria cout<<"The new seria is: "<<a1<<","<<__max(a1,a2)<<"," <<__max(__max(a1,a2),a3)<<","<<__max(__max(__max(a1,a2),a3),a4) <<"\n"; } פלטים מיצגיםפלט לדוגמה:
|
© Copyright YAProductions 2000 |