Advertisement
pan7nikt

zad5.cpp

Nov 6th, 2023
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. #define nstudent 100
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int student[nstudent];
  11.     int arraySize = sizeof(student) / sizeof(int);
  12.     int total = 0;
  13.     float avg = 0;
  14.     cout << "arraySize: " << arraySize << "\n";
  15.  
  16.     for(int i=0; i<arraySize; i++)
  17.         {
  18.             srand(time(NULL) + i);
  19.             student[i] = rand() % 11;
  20.             //cout << i << "\n";
  21.             cout << student[i] << "\n";
  22.         }
  23.  
  24.     for(int i=0; i<arraySize; i++)
  25.         {
  26.             total += student[i];
  27.         }
  28.         cout << "\ntotal: " << total << "\n";
  29.  
  30.         avg = (float)total / (float)arraySize;
  31.  
  32.         cout << "\navg: " << avg << "\n";
  33.  
  34.     //Sort
  35.     for(int i=0; i<arraySize; i++)
  36.         {
  37.             int m = student[i];
  38.             int id = 0;
  39.             for(int j=i+1; j<arraySize; j++)
  40.                 {
  41.                     if(student[j]>m){m=student[j]; id = j;}
  42.                 }
  43.             cout << "iteration " << i << " switching " << student[i] << " for " << student[id] << "\n";
  44.             cout << "student[0]: " << student[0] << "\n";
  45.             student[id] = student[i];
  46.             student[i] = m;
  47.  
  48.  
  49.         }
  50.  
  51.         for(int i=0; i<arraySize; i++)
  52.             {
  53.                 cout << i << ". " << student[i] << "\n";
  54.             }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement