Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <stdlib.h>
- using namespace std;
- void printArray(int array[], int dim){
- for(int i=0;i<dim;i++)
- {
- cout << array[i]<<" ";
- }
- cout << endl;
- }
- void fillArray(int array[],int dim,int mode){
- if(mode ==1){
- for(int i=0;i<dim;i++){
- array[i]=i+1;
- }
- }else if(mode==2){
- srand(time(NULL));
- for(int i =0;i<dim;i++){
- array[i]=rand()%dim +1;
- }
- }
- }
- /* Determines minimum and maximum of an array of integers. */
- void computeMinMax(int array[], int size, int& min, int& max){
- min = array[0];
- max = array[0];
- for(int i=1;i<size;i++)
- {
- if(array[i]<min)
- {
- min=array[i];
- }else if(array[i]>max)
- {
- max=array[i];
- }
- }
- };
- int compareInt(const void* d1, const void* d2){
- if(*(int*)d1<*(int*)d2)
- return -1;
- else if(*(int*)d1==*(int*)d2)
- return 0;
- else
- return 1;
- }
- void calculateFkt(double& a, double& b,double x[], double y[],int dim){
- double ssxy,ssxx,xquer,yquer;
- xquer=0;
- yquer=0;
- ssxx=0;
- ssxy=0;
- for(int i=0;i<dim;i++)
- {
- xquer+=x[i];
- yquer+=y[i];
- }
- xquer=xquer/dim;
- yquer=yquer/dim;
- cout << xquer<<":"<<yquer<<endl;
- for(int i=0;i<dim;i++)
- {
- ssxy+=(x[i]-xquer)*(y[i]-yquer);
- ssxx+=(x[i]-xquer)*(x[i]-xquer);
- }
- cout << ssxy<<":"<<ssxx<<endl;
- b=ssxy/ssxx;
- a=yquer-b*xquer;
- }
- int main() {
- //aufgabe2
- int intarray[10]={1,2,3,0,9,5,6,7,8,4};
- int min,max;
- printArray(intarray,10);
- computeMinMax(intarray,10,min,max);
- cout << min << ":"<<max<<endl;
- fillArray(intarray,10,1);
- printArray(intarray,10);
- computeMinMax(intarray,10,min,max);
- cout << min << ":"<<max<<endl;
- fillArray(intarray,10,2);
- printArray(intarray,10);
- computeMinMax(intarray,10,min,max);
- cout << min << ":"<<max<<endl;
- cout <<"----------------------------------------------------"<<endl;
- //aufgabe3
- const int dim = 100;
- int qsortarray[dim];
- fillArray(qsortarray,dim,2);
- printArray(qsortarray,dim);
- qsort(qsortarray,dim,sizeof(int),compareInt);
- printArray(qsortarray,dim);
- cout <<"----------------------------------------------------"<<endl;
- //Aufgabe4
- /*int anz;
- cout << "anz:";
- cin>>anz;
- double x[(const int)anz];
- double y[(const int)anz];
- for(int i=0;i<anz;i++)
- {
- cout <<"x["<<i<<"]=";
- cin>>x[i];
- cout <<"y["<<i<<"]=";
- cin>>y[i];
- }
- calculateFkt(a,b,x,y,anz);
- cout<< "fuer x="
- int dummy=0;
- while(cin >> dummy;)
- {
- cout<<(a+b*dummy)<<endl
- cout<< "fuer x="f
- }
- */
- //y=a+bx
- int anz=6;
- double x[6]={1.0,2.0,3.0,4.0,5.0,6.0};
- double y[6]={14.0,37.0,52.0,59.0,83.0,92.0};
- double a,b;
- calculateFkt(a,b,x,y,anz);
- cout << "a:"<<a <<" b:"<<b<<endl;
- cout <<(a+b*7)<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement