Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6. void printArray(int array[], int dim){
  7.     for(int i=0;i<dim;i++)
  8.     {
  9.         cout << array[i]<<" ";
  10.     }
  11.     cout << endl;
  12. }
  13.  
  14. void fillArray(int array[],int dim,int mode){
  15.     if(mode ==1){
  16.         for(int i=0;i<dim;i++){
  17.             array[i]=i+1;
  18.  
  19.  
  20.         }
  21.     }else if(mode==2){
  22.         srand(time(NULL));
  23.         for(int i =0;i<dim;i++){
  24.             array[i]=rand()%dim +1;
  25.  
  26.         }
  27.     }
  28. }
  29.  
  30. /* Determines minimum and maximum of an array of integers. */
  31. void computeMinMax(int array[], int size, int& min, int& max){
  32.     min = array[0];
  33.     max = array[0];
  34.     for(int i=1;i<size;i++)
  35.     {
  36.         if(array[i]<min)
  37.         {
  38.             min=array[i];
  39.         }else if(array[i]>max)
  40.         {
  41.             max=array[i];
  42.         }
  43.  
  44.     }
  45. };
  46. int compareInt(const void* d1, const void* d2){
  47.     if(*(int*)d1<*(int*)d2)
  48.         return -1;
  49.     else if(*(int*)d1==*(int*)d2)
  50.         return 0;
  51.     else
  52.         return 1;
  53.  
  54. }
  55. void calculateFkt(double& a, double& b,double x[], double y[],int dim){
  56.     double ssxy,ssxx,xquer,yquer;
  57.     xquer=0;
  58.     yquer=0;
  59.     ssxx=0;
  60.     ssxy=0;
  61.     for(int i=0;i<dim;i++)
  62.     {
  63.         xquer+=x[i];
  64.         yquer+=y[i];
  65.     }
  66.     xquer=xquer/dim;
  67.     yquer=yquer/dim;
  68.     cout << xquer<<":"<<yquer<<endl;
  69.     for(int i=0;i<dim;i++)
  70.     {
  71.         ssxy+=(x[i]-xquer)*(y[i]-yquer);
  72.         ssxx+=(x[i]-xquer)*(x[i]-xquer);
  73.     }
  74.     cout << ssxy<<":"<<ssxx<<endl;
  75.     b=ssxy/ssxx;
  76.     a=yquer-b*xquer;
  77. }
  78.  
  79. int main() {
  80.     //aufgabe2
  81.     int intarray[10]={1,2,3,0,9,5,6,7,8,4};
  82.     int min,max;
  83.     printArray(intarray,10);
  84.     computeMinMax(intarray,10,min,max);
  85.     cout << min << ":"<<max<<endl;
  86.     fillArray(intarray,10,1);
  87.     printArray(intarray,10);
  88.     computeMinMax(intarray,10,min,max);
  89.     cout << min << ":"<<max<<endl;
  90.     fillArray(intarray,10,2);
  91.     printArray(intarray,10);
  92.     computeMinMax(intarray,10,min,max);
  93.     cout << min << ":"<<max<<endl;
  94.     cout <<"----------------------------------------------------"<<endl;
  95.     //aufgabe3
  96.     const int dim = 100;
  97.     int qsortarray[dim];
  98.     fillArray(qsortarray,dim,2);
  99.     printArray(qsortarray,dim);
  100.     qsort(qsortarray,dim,sizeof(int),compareInt);
  101.     printArray(qsortarray,dim);
  102.     cout <<"----------------------------------------------------"<<endl;
  103.     //Aufgabe4
  104.     /*int anz;
  105.     cout << "anz:";
  106.     cin>>anz;
  107.     double x[(const int)anz];
  108.     double y[(const int)anz];
  109.     for(int i=0;i<anz;i++)
  110.     {
  111.         cout <<"x["<<i<<"]=";
  112.         cin>>x[i];
  113.         cout <<"y["<<i<<"]=";
  114.         cin>>y[i];
  115.     }
  116.     calculateFkt(a,b,x,y,anz);
  117.     cout<< "fuer x="
  118.     int dummy=0;
  119.     while(cin >> dummy;)
  120.     {
  121.         cout<<(a+b*dummy)<<endl
  122.         cout<< "fuer x="f
  123.     }
  124.     */
  125.     //y=a+bx
  126.     int anz=6;
  127.     double x[6]={1.0,2.0,3.0,4.0,5.0,6.0};
  128.     double y[6]={14.0,37.0,52.0,59.0,83.0,92.0};
  129.     double a,b;
  130.     calculateFkt(a,b,x,y,anz);
  131.     cout << "a:"<<a <<" b:"<<b<<endl;
  132.     cout <<(a+b*7)<<endl;
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement