Arrays und Funktionen



  • Hi,

    Ich wühle mich nun schon seit einer Stunde durch das Internet und kann die Lösung für mein Problemchen einfach nicht finden. Ich bin ziemlicher Newbie in C++, habe aber vorher bereits ziemlich viel in Perl programmiert und darum ist meine Sicht mit der Handhabung von Variablen, Arrays, etc. in C++ vermutlich sehr eingeschrenkt. 😉

    Hier ist mein Code:

    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include "random.h"
    
    //b function declarations
    void readFile(int &n_inp, double &xa_inp, double &xb_inp, char &switch_inp, int &randomseed_inp);
    
    void printHeader(int &n_inp, double &xa_inp, double &xb_inp, char &switch_inp, int &randomseed_inp);
    
    int validateFile(int &n_inp, double &xa_inp, double &xb_inp, char &switch_inp, int &randomseed_inp);
    
    double* generateNumbers(int &n_inp, double &xa_inp, double &xb_inp, char &switch_inp, int &randomseed_inp);
    //e function declarations
    
    //b global variables
    
    //e global variables
    
    //b main
    using namespace std;
    
    int main() {
    	double xa_inp, xb_inp, numbers;
    	int n_inp, randomseed_inp;
    	char switch_inp;
    
    	readFile(n_inp, xa_inp, xb_inp, switch_inp, randomseed_inp);
    
    	numbers = generateNumbers(n_inp, xa_inp, xb_inp, switch_inp, randomseed_inp);
    
    	printHeader(n_inp, xa_inp, xb_inp, switch_inp, randomseed_inp);
    
    }
    //e main
    
    //b functions
    void readFile(int &n_inp, double &xa_inp, double &xb_inp, char &switch_inp, int &randomseed_inp) {
    	ifstream file("ex3C.inp");
    
    	file >> n_inp;
    	file >> xa_inp;
    	file >> xb_inp;
    	file >> switch_inp;
    	file >> randomseed_inp;
    
    	file.close();
    }
    
    void printHeader(int &n_inp, double &xa_inp, double &xb_inp, char &switch_inp, int &randomseed_inp) {
    	if ( validateFile(n_inp, xa_inp, xb_inp, switch_inp, randomseed_inp) == 0 ) {
    		cout << "Input data from ex3C.inp:" << endl
    			 << "N\t\t" << n_inp << endl
    			 << "Xa\t\t" << xa_inp << endl
    			 << "Xb\t\t" << xb_inp << endl
    			 << "switch\t\t" << switch_inp << endl
    			 << "seed\t\t" << randomseed_inp << endl;
    	}
    	else if ( validateFile(n_inp, xa_inp, xb_inp, switch_inp, randomseed_inp) == 1 ) {
    		cerr << "Your input is not sensible!" << endl;
    	}
    	else {
    		cerr << "Error!" << endl;
    	}
    }
    
    int validateFile(int &n_inp, double &xa_inp, double &xb_inp, char &switch_inp, int &randomseed_inp) {
    //	if () {
    //		return 1;
    //	}
    //	else {
    		return 0;	
    //	}
    }
    
    double* generateNumbers(int &n_inp, double &xa_inp, double &xb_inp, char &switch_inp, int &randomseed_inp) {
    	double* numbers = new double[n_inp];
    
    	if (switch_inp == 'a') {
    		for(int i = 0; i < n_inp; i++) {
    			numbers[i] = rando(randomseed_inp);
    		}
    	}
    	else if (switch_inp == 'b') {
    		for(int i = 0; i < n_inp; i++) {
    			numbers[i] = gauss(xa_inp, xb_inp, randomseed_inp);
    		}
    	}
    	else if (switch_inp == 'c') {
    		for(int i = 0; i < n_inp; i++) {
    			if (i%2 == 1) {
    				numbers[i] = xa_inp;
    			}
    			else if (i%2 == 0) {
    				numbers[i] = xb_inp;
    			}
    		}
    	}
    
    	return numbers;
    }
    //e functions
    

    Folgender Fehler spuckt gcc aus:

    problem.cc: In function 'int main()':
    problem.cc:30: error: cannot convert 'double*' to 'double' in assignment

    Wer weiss, was hier falsch läuft?

    Herzlichen Dank!



  • Ändere Zeile 24 in:

    double xa_inp, xb_inp, *numbers;
    

    und füg am Ende der main noch ein delete [] numbers; ein.



  • Wow... Das ist ja eine kleine Änderung. 😉 Nun funktionierts!

    Danke vielmals!


Anmelden zum Antworten