Advertisement
HristoBaychev

students

Mar 26th, 2023
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.21 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Създайте функция, която приема асоциативен масив от имена на ученици и техните
  5. оценки като параметър и изчислява средната оценка за всички ученици в масива.+ още много условия :D
  6. + Средна оценка на всеки студент
  7. + име на най-добрият и най-слабият студент
  8. + условие ако въведеният резултат е невалиден
  9. */
  10.  
  11. function studentsCalculation($Evaluation){
  12.     $sumForStudent = 0;
  13.     $totalSum = 0;
  14.     $final_sum = 0;
  15.     $countForAllEvaluation = 0;
  16.     $count = 0;
  17.     $totalFinalCount = 0;
  18.     $bestStudent = 0;
  19.     $worstStudent = 0;
  20.     $bestStudentName = "";
  21.     $worstStudentName = "";
  22.     foreach ($Evaluation as $row){           // start from 1 --> [0] -- in this case some name  ["Georgi", .......
  23.         foreach ($row as $col) {            // take all value on row [".......",  5, 6, 2, 3, 4, 2]
  24.             if ($col <= 0 || $col > 6){                 // if some number is negative or 0 we cannot give real result
  25.                 print "The evaluations is not correct! Please check for correct evaluations! Exist 0 or negative number in array!".PHP_EOL;
  26.                 print "Unable to proceed with the check for $name!".PHP_EOL;
  27.                 $sumForStudent = 0;   // return value of sum for one student
  28.                 $count = 0;          // return value of counter
  29.                 $totalSum = 0;       // return value total sum to 0
  30.                 $countForAllEvaluation = 0;   // return counter
  31.                 continue 2;                 // pass the student --> going to the next
  32.             }
  33.             if (is_string($col)){              // check if is string
  34.                 print "$col =>".PHP_EOL;        // print the string
  35.                 $name = $col;                  // safe the string
  36.                 continue;                      // continue iteration from next in this case 5 .....
  37.             }
  38.             else{
  39.                 $count++;  
  40.                 $countForAllEvaluation++;                       // incr count ++
  41.                 $sumForStudent += $col;             // calculate sum for one student
  42.                 $totalSum += $col;                  // calculate sum for all students
  43.             }
  44.         }
  45.  
  46.  
  47.         $averageForStudent = $sumForStudent / $count;        // average for only one student
  48.         $final_sum += $totalSum;
  49.         $totalFinalCount += $countForAllEvaluation;
  50.  
  51.  
  52.         if ($averageForStudent >= $bestStudent) {           // assign the name of best students
  53.             $bestStudent = $averageForStudent ;
  54.             $bestStudentName = $name;
  55.         }
  56.         if ($averageForStudent <= $worstStudent || $worstStudent === 0) {   // assign the name of worst students
  57.             $worstStudent = $averageForStudent;
  58.             $worstStudentName = $name;
  59.         }  
  60.  
  61.  
  62.         print $averageForStudent.PHP_EOL;                       // print average for one student
  63.         $newAverageForBestAndWorstStudent[] = $averageForStudent;   // new array for assign the results from students
  64.  
  65.  
  66.         $sumForStudent = 0;   // return value of sum for one student
  67.         $count = 0;          // return value of counter
  68.         $totalSum = 0;      // return value of total sum to 0
  69.         $countForAllEvaluation = 0;  // return counter
  70.  
  71.     }
  72.    
  73.     $bestStudent = max($newAverageForBestAndWorstStudent);    // take result for best student (calculate who is + )
  74.     $worstStudent = min($newAverageForBestAndWorstStudent);   // take result for worst student (calculate who is - )
  75.     $totalAverage = $final_sum / $totalFinalCount;  // return total average
  76.     $format =  number_format($totalAverage, 2, '.', '');                     // format the result
  77.     echo "Best result is: $bestStudent on $bestStudentName".PHP_EOL;
  78.     echo "Worst result is: $worstStudent on $worstStudentName".PHP_EOL;
  79.     echo "total average is: ";
  80.     return $format;
  81.  
  82. }
  83.  
  84.  
  85.  
  86. $myArr = [
  87.     ["Georgi", 3, 6, 2, 3, 4, 0],
  88.     ["Petyr",  4, 3, 2, 1, 2, 3],
  89.     ["Desi",   3, 6, 6, 6, 6, 6]
  90. ];
  91.  
  92. echo studentsCalculation($myArr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement