vector - problems
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: vector - problems

  1. #1
    Join Date
    Nov 2013
    Posts
    3

    vector - problems

    I need do a program in c++ by vector library, but I have any problems. Can you help me?
    The code source is:

    PHP Code:
    #include <iostream>
    #include <stdexcept>
    #include <fstream>
    #include <vector>

    using namespace std;

    const 
    string menu="\nScegli una operazione op tra\n\
    - ? show menu\n\
    - q quit menu\n\
    - = i k Rk=Ri\n\
    - A i <nome> legge dal file <nome> una matrice e la salva nel registro Ri\n\
          (errore se il file non esiste o non e' una matrice n*n di naturali)\n\
    - B i <nome> scrive nel file <nome> la matrice nel registro Ri\n\
    - C i j k Rk=Ri+Rj\n\
    - D i j k Rk=Ri*Rj\n\
    - E i m k Rk=somma(Ri^j | j=1...m), con m intero positivo e\n\
              M^j definita per induzione M^1=M e M^{j+1}=M^j*M\n"
    ;

    typedef unsigned int Nat;
    typedef vector<NatMat;
    bool LeggiDaFile (const Nat nMatM, const stringnome) {
        
    int i 0;
        
    cout<<"Nome del File da leggere: ";
        
    ifstream file(nome.c_str());
        for (
    unsigned int u=0;u<n;u++) {
                for (
    int v=0;v<n;v++) {
                        
    file>>M[i][u][v];
                        }
        };
                    
    file.close();
                    }

    bool ScriviSuFile (const Nat n, const MatM, const stringnome) {
        
    cout<<"Nome del file su cui scrivere: > ";
        
    ofstream file(nome.c_str());
        for (
    unsigned int i 0i<ni++) {
            for (
    unsigned int u =0;u<n;u++) {
                for (
    unsigned int v=0;v<n;v++) {
                        
    file<<M[i][u][v]<<"\t";
                        }
                        
    file<<endl;
        }
    };

    file.close();
                }
    Mat Sum (const Nat n, const MatM1, const MatM2) {
        const 
    Mat M
        
    Nat k,j;
         
        for (
    unsigned int u=0;u<n;u++) {
                    for (
    int unsigned v=0;v<n;v++)
                        
    M[k][u][v]=M1[i][u][v]+M2[j][u][v];
        
    }
    }
    Mat Prod (const Nat n, const MatM1, const MatM2) {
        const 
    Mat M;
        
    Nat kj;
        
    vector<MatR(r,M);
         
                for (
    unsigned int u=0;u<n;u++)
                    for (
    unsigned int v=0;v<n;v++)
                    for (
    Nat w=0;w<n;w++)
                    
    M[u][v]+=M1[i][u][w]*M2[j][w][v];
                
    R[k]=M;

    }
    Mat SumProd (const Nat n, const Nat m, const MatM) {
        
        
    //Codice
    }

    int main()
    try {
        
    Nat r,n;
        
    cout<<"Inserisci la dimensione delle matrici n=";
        
    cin>>n;
        if (!
    cin) throw runtime_error("input error");
        
    cout<<"Inserisci il numero di registri r=";
        
    cin>>r;
        if (!
    cin) throw runtime_error("input error");
        if (
    r<1||n<1) throw runtime_error("parametri out-of-range");
        
        
    cout<<menu<<"dove i,j,k in [0,"<<r<<") e n="<<n;
        
        const 
    Mat M0(n*n,0);//matrice nulla, usata per inizializzare R
        
    vector<MatR(r,M0);//registri
        
    char op;//operazione
        
    Nat i,j,k;//indici <r per R
        
    Nat m;
        
    string nome;
        
        
    //loop di esecuzione
        
    do {
            
    cin.clear(); cin.ignore();//reset di precedenti errori di input
            
    cout<<"\nscegli op> ";
            
    cin >> op;
            switch (
    op)
            {
            case 
    '?':
                
    cout<<menu<<"dove i,j,k in [0,"<<r<<") e n="<<n;
                break;
            case 
    'q':
                
    cout<<"fine!\n";
                return(
    0);
            case 
    '=':
                
    cin>>i>>k;
                if (!
    cin||k>=r||i>=r)
                    throw 
    runtime_error("out-of-range");
                if (
    k!=iR[k]=R[i];
                break;
            case 
    'A':
                
    cin>>i;
                if (!
    cin||i>=r) throw runtime_error("out-of-range");
                
    cin>>nome;
                if (!
    cin) throw runtime_error("input error");
                if (!
    LeggiDaFile(n,R[i],nome)) throw runtime_error("read error");
                break;
            case 
    'B':
                
    cin>>i;
                if (!
    cin||i>=r) throw runtime_error("out-of-range");
                
    cin>>nome;
                if (!
    cin) throw runtime_error("input error");
                if (!
    ScriviSuFile(n,R[i],nome))  throw runtime_error("write error");
                break;
            case 
    'C':
                
    cin>>i>>j>>k;
                if (!
    cin||k>=r||i>=r||j>=r)
                    throw 
    runtime_error("out-of-range");
                
    R[k]=Sum(n,R[i],R[j]);
                break;
            case 
    'D':
                
    cin>>i>>j>>k;
                if (!
    cin||k>=r||i>=r||j>=r)
                    throw 
    runtime_error("out-of-range");
                
    R[k]=Prod(n,R[i],R[j]);
                break;
            case 
    'E':
                
    cin>>i>>m>>k;
                if (!
    cin||k>=r||i>=r||m<1)
                    throw 
    runtime_error("out-of-range");
                
    R[k]=SumProd(n,m,R[i]);
                break;
            default:
                
    cout<<op<<" operazione inesistente!";
            };
        } while (
    true);
    } catch
    (
    runtime_error &msg) {
        
    cerr<<msg.what();
    } catch (...) {
        
    cerr<<"crash!\n";


  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,365

    Re: vector - problems

    1) Please use code tags for formatting code and not php tags. Go advanced, select the code and click '#'
    2) What are the problems you are having? What is your c++/vector question? Are they compile errors or run time issues you are having? If run-time, what debugging of the code have you done?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,427

    Re: vector - problems

    Quote Originally Posted by linux67 View Post
    I need do a program in c++ by vector library, but I have any problems. Can you help me?
    The code source is:
    1) The functions that are supposed to return Mat objects do not return anything. Look at your Sum, Prod, and SumProd functions.

    2)
    Code:
     const Mat M; 
      //...
     M[u][v]+=
    M is declared as a const, so why are you trying to change the value?

    3) Design -- In C++, it isn't normal practice to throw exceptions if the person types in the wrong input.
    Code:
    cin>>i; 
    if (!cin||i>=r) throw runtime_error("out-of-range"); // not traditional C++ style or reason to throw exception
    The reason is that typing wrong input is not an exceptional case. People type wrong information all the time, so why is this an exception throwing situation? An exception should be thrown when something totally unexpected happens (for example, a file that must exist on a system for some reason doesn't exist) or something catastrophic occurs that makes the program unusable (for example, disk space used up, memory exhausted, a resource doesn't initialize and your program relies on such a resource, etc.)

    Maybe in other languages exceptions are thrown for anything "wrong" (like in Java), but this is not necessarily the normal way C++ programmers do these things. Leave exceptions for those exceptional situations.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 12th, 2013 at 10:24 AM.

  4. #4
    Join Date
    Nov 2013
    Posts
    3

    Re: vector - problems

    I need do a program in c++ by vector library, but I have any problems. Can you help me?
    When I running the program, it is an error about i variable.

    The code source is:

    Code:
    #include <iostream>
    #include <stdexcept>
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    const string menu="\nScegli una operazione op tra\n\
    - ? show menu\n\
    - q quit menu\n\
    - = i k Rk=Ri\n\
    - A i <nome> legge dal file <nome> una matrice e la salva nel registro Ri\n\
          (errore se il file non esiste o non e' una matrice n*n di naturali)\n\
    - B i <nome> scrive nel file <nome> la matrice nel registro Ri\n\
    - C i j k Rk=Ri+Rj\n\
    - D i j k Rk=Ri*Rj\n\
    - E i m k Rk=somma(Ri^j | j=1...m), con m intero positivo e\n\
              M^j definita per induzione M^1=M e M^{j+1}=M^j*M\n";
    
    typedef unsigned int Nat;
    typedef vector<Nat> Mat;
    bool LeggiDaFile (const Nat n, Mat& M, const string& nome) {
    	int i = 0;
    	cout<<"Nome del File da leggere: ";
    	ifstream file(nome.c_str());
    	for (unsigned int u=0;u<n;u++) {
    			for (int v=0;v<n;v++) {
    					file>>M[i][u][v];
    					}
    	};
    				file.close();
    				}
    
    bool ScriviSuFile (const Nat n, const Mat& M, const string& nome) {
    	cout<<"Nome del file su cui scrivere: > ";
    	ofstream file(nome.c_str());
    	for (unsigned int i = 0; i<n; i++) {
    		for (unsigned int u =0;u<n;u++) {
    			for (unsigned int v=0;v<n;v++) {
    					file<<M[i][u][v]<<"\t";
    					}
    					file<<endl;
    	}
    };
    
    file.close();
    			}
    Mat Sum (const Nat n, const Mat& M1, const Mat& M2) {
    	const Mat M; 
    	Nat k,j;
    	 
    	for (unsigned int u=0;u<n;u++) {
    				for (int unsigned v=0;v<n;v++)
    					M[k][u][v]=M1[i][u][v]+M2[j][u][v];
    	
    }
    }
    Mat Prod (const Nat n, const Mat& M1, const Mat& M2) {
    	const Mat M;
    	Nat k, j;
    	vector<Mat> R(r,M);
    	 
    			for (unsigned int u=0;u<n;u++)
    				for (unsigned int v=0;v<n;v++)
    				for (Nat w=0;w<n;w++)
    				M[u][v]+=M1[i][u][w]*M2[j][w][v];
    			R[k]=M;
    
    }
    Mat SumProd (const Nat n, const Nat m, const Mat& M) {
    	
    	//Codice
    }
    
    int main()
    try {
    	Nat r,n;
    	cout<<"Inserisci la dimensione delle matrici n=";
    	cin>>n;
    	if (!cin) throw runtime_error("input error");
    	cout<<"Inserisci il numero di registri r=";
    	cin>>r;
    	if (!cin) throw runtime_error("input error");
    	if (r<1||n<1) throw runtime_error("parametri out-of-range");
    	
    	cout<<menu<<"dove i,j,k in [0,"<<r<<") e n="<<n;
    	
    	const Mat M0(n*n,0);//matrice nulla, usata per inizializzare R
    	vector<Mat> R(r,M0);//registri
    	char op;//operazione
    	Nat i,j,k;//indici <r per R
    	Nat m;
    	string nome;
    	
    	//loop di esecuzione
    	do {
    		cin.clear(); cin.ignore();//reset di precedenti errori di input
    		cout<<"\nscegli op> ";
    		cin >> op;
    		switch (op)
    		{
    		case '?':
    			cout<<menu<<"dove i,j,k in [0,"<<r<<") e n="<<n;
    			break;
    		case 'q':
    			cout<<"fine!\n";
    			return(0);
    		case '=':
    			cin>>i>>k;
    			if (!cin||k>=r||i>=r)
    				throw runtime_error("out-of-range");
    			if (k!=i) R[k]=R[i];
    			break;
    		case 'A':
    			cin>>i;
    			if (!cin||i>=r) throw runtime_error("out-of-range");
    			cin>>nome;
    			if (!cin) throw runtime_error("input error");
    			if (!LeggiDaFile(n,R[i],nome)) throw runtime_error("read error");
    			break;
    		case 'B':
    			cin>>i;
    			if (!cin||i>=r) throw runtime_error("out-of-range");
    			cin>>nome;
    			if (!cin) throw runtime_error("input error");
    			if (!ScriviSuFile(n,R[i],nome))  throw runtime_error("write error");
    			break;
    		case 'C':
    			cin>>i>>j>>k;
    			if (!cin||k>=r||i>=r||j>=r)
    				throw runtime_error("out-of-range");
    			R[k]=Sum(n,R[i],R[j]);
    			break;
    		case 'D':
    			cin>>i>>j>>k;
    			if (!cin||k>=r||i>=r||j>=r)
    				throw runtime_error("out-of-range");
    			R[k]=Prod(n,R[i],R[j]);
    			break;
    		case 'E':
    			cin>>i>>m>>k;
    			if (!cin||k>=r||i>=r||m<1)
    				throw runtime_error("out-of-range");
    			R[k]=SumProd(n,m,R[i]);
    			break;
    		default:
    			cout<<op<<" operazione inesistente!";
    		};
    	} while (true);
    } catch
    (runtime_error &msg) {
    	cerr<<msg.what();
    } catch (...) {
    	cerr<<"crash!\n";
    }

  5. #5
    Join Date
    Apr 1999
    Posts
    27,427

    Re: vector - problems

    You didn't read my post?
    Code:
    bool ScriviSuFile (const Nat n, const Mat& M, const string& nome) 
    {
        cout<<"Nome del file su cui scrivere: > ";
        ofstream file(nome.c_str());
        for (unsigned int i = 0; i<n; i++)
        {
            for (unsigned int u =0;u<n;u++)
            {
                for (unsigned int v=0;v<n;v++)
                {
                    file<<M[i][u][v]<<"\t";
                }
                file<<endl;
            }
        };
    
        file.close();
    }
    
    Mat Sum (const Nat n, const Mat& M1, const Mat& M2) 
    {
        const Mat M; 
        Nat k,j;
    
        for (unsigned int u=0;u<n;u++)
        {
            for (int unsigned v=0;v<n;v++)
                M[k][u][v]=M1[i][u][v]+M2[j][u][v];
        }
    }
    
    Mat Prod (const Nat n, const Mat& M1, const Mat& M2) 
    {
        const Mat M;
        Nat k, j;
        vector<Mat> R(r,M);
    
        for (unsigned int u=0;u<n;u++)
            for (unsigned int v=0;v<n;v++)
                for (Nat w=0;w<n;w++)
                    M[u][v]+=M1[ i ][u][w]*M2[j][w][v];
        R[k]=M;
    }
    First, do you see what is in red? That is the return type of the function, so why do your functions never return anything?

    Second, look at the Prod function. Do you see what is in green? Where is "i" declared in that function?

    Third, your Mat variable is declared const -- you cannot change the value of const variables.
    Code:
    const Mat M;
    //...
    M[k][u][v] = M1[i][u][v] + M2[j][u][v];  // Error -- trying to change the value of the M const variable!
    This is basically what I stated to you in my first post, but it seems as if you didn't read it or understand it.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 12th, 2013 at 01:43 PM.

  6. #6
    Join Date
    Nov 2013
    Posts
    3

    Re: vector - problems

    Sorry! I try so, but there are problems.
    My new code is:

    Code:
    bool LeggiDaFile (const Nat n, Mat& M, const string& nome) {
    	cout<<"Nome del File da leggere: ";
    	ifstream file(nome.c_str());
    	for (unsigned int i = 0; i <n; i++) { 
    for (unsigned int u=0;u<n;u++) {
    			for (int v=0;v<n;v++) {
    					file>>M[i][u][v];
    					}
    	}};
    				file.close();
    if (!file)
    return false;
    return true;
    				}
    
    bool ScriviSuFile (const Nat n, const Mat& M, const string& nome) {
    	cout<<"Nome del file su cui scrivere: > ";
    	ofstream file(nome.c_str());
    	for (unsigned int i = 0; i<n; i++) {
    		for (unsigned int u =0;u<n;u++) {
    			for (unsigned int v=0;v<n;v++) {
    					file<<M[i][u][v]<<"\t";
    					}
    					file<<endl;
    	}
    };
    file.close();
    if (!file)
    return false;
    return true;
    }
    Mat Sum (const Nat n, const Mat& M1, const Mat& M2) {
    Mat M;
    Nat k,j;
    for (unsigned int i=0; i<n; i++) {
    for (unsigned int u=0;u<n;u++) {
    for (int unsigned v=0;v<n;v++) {
    					M[k][u][v]=M1[i][u][v]+M2[j][u][v];
    	
    }
    }
    }
    }
    return M[k][u][v];
    }
    Mat Prod (const Nat n, const Mat& M1, const Mat& M2) {
    	Mat M;
    	Mat R;
    	Nat k, r, j;
    for (unsigned int i = 0; i<n; i++) {	
    for (unsigned int u=0;u<n;u++) {
    				for (unsigned int v=0;v<n;v++) {
    				for (Nat w=0;w<n;w++) {
    				M[u][v]+=M1[i][u][w]*M2[j][w][v];
    			R[k]=M;
    }}};
    return M;
    }

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,365

    Re: vector - problems

    It would help to understand your code if you formatted it properly with code blocks indented etc. Your code as posted is just about unreadable.

    So what errors are you obtaining now?
    Code:
    Mat Sum (const Nat n, const Mat& M1, const Mat& M2) {
    Mat M;
    Nat k,j;
    
         for (unsigned int i=0; i<n; i++) {
              for (unsigned int u=0;u<n;u++) {
                   for (int unsigned v=0;v<n;v++) {
                        M[k][u][v]=M1[i][u][v]+M2[j][u][v];
    	       }
              }
         }
    }
    return M[k][u][v];
    }
    Where are k and j initialised and updated?
    If you had formatted your code properly you would realise there is an extra }
    To return an array just do
    Code:
    return M;
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center