Basic Array Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aemado
    New Member
    • Sep 2007
    • 17

    #1

    Basic Array Problem

    I haven't written basic array code in so long, I have forgotten how to do some of it. I have two data files. There is some data that is common to both, and some that is only in the first and not the second. I am reading this data in and storing it in the form of an array. If a user inputs a value, I am trying to determine if it is in one, both, or neither. This is where I am stuck. Here is what I have:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    const int MAX = 100;
    
    void process_data(ifstream &indata, ifstream &indata2, int studentnumber, int snumber[], int studnum[], string sclass[]);
    
    int main()
    {
    	ifstream indata;
    	ifstream indata2;
    	int studentnumber, snumber[MAX], studnum[MAX];
    	string filename, filename2, sclass[MAX];
    
    	filename = "student.data";
    	filename2 = "studentclass.data";
    
    	studentnumber = 0;
    
    	indata.open(filename.c_str());
    	if(!indata.is_open()){
    		cout<<"student.data file cannot be opened."<<endl;}
    	indata2.open(filename2.c_str());
    	if(!indata2.is_open()){
    		cout<<"studentclass.data file cannot be opened."<<endl;}
    
    	process_data(indata, indata2, studentnumber, snumber, studnum, sclass);
    
    	indata.close();
    	indata.clear();
    	indata2.close();
    	indata2.clear();
    }
    
    void process_data(ifstream &indata, ifstream &indata2, int studentnumber, int snumber[], int studnum[], string sclass[]){
    
    	int tempsnumber, tempstudnum;
    	string tempsclass, tempsect, tempgrade, tempslname, tempsfname, tempmajor, tempyear;
    
    	int numberstudents = 0;
    
    	while(indata){
    		indata>>tempstudnum;
    		indata>>tempslname;
    		indata>>tempsfname;
    		indata>>tempmajor;
    		indata>>tempyear;
    
    		studnum[numberstudents] = tempstudnum;
    
    		numberstudents++;
    
    		indata>>tempsnumber;
    	}
    
    	int numberstudentfiles=0;
    
    	indata2>>tempsnumber;
    
    	while(indata2){
    		indata2>>tempsclass;
    		indata2>>tempsect;
    		indata2>>tempgrade;
    
    		snumber[numberstudentfiles] = tempsnumber;
    		sclass[numberstudentfiles] = tempsclass;
    
    		numberstudentfiles++;
    
    		indata2>>tempsnumber;
    	}
    	cout<<"Student: ";
    	cin>>studentnumber;
    
    	for(int i=0; i<numberstudents; i++){
    		if(studentnumber == studnum[i] && studentnumber != snumber[i]){
    			for(int j=0; j<numberstudentfiles; j++){
    				if(studentnumber != snumber[j]){
    					cout<<"is not taking any classes"<<endl;
    					return;
    				}
    			}
    		}
    	}
    
    	for(int i=0; i<numberstudents; i++){
    		if(studentnumber != studnum[i]){
    			for(int j=0; j<numberstudentfiles; j++){
    				if(studentnumber != snumber[j]){
    					cout<<"is not found in student.data"<<endl;
    				}
    			}
    		}
    	}
    
    	cout<<"is taking: "<<endl;
    
    	for(int i=0; i<numberstudentfiles; i++){
    		if(studentnumber==snumber[i])
    			cout<<sclass[i]<<endl;
    	}
    
    	return;
    }
    Sorry it is so long...but I know my problems are in the crazy for/if/for/if loops...but I don't know how to traverse through both arrays without doing this (like I said, it has been a long time!) Any suggestions??
  • krishnabhargav
    New Member
    • Feb 2008
    • 24

    #2
    Write a method isFound....
    Code:
    bool isFound(int[] array,int arraySize, int key)
    {
      // write your search code in here
      for(int i=0;i<arraySize;i++)
       if(array[i] == key) return true;
    
      return false;
    }
    
    int main()
    {
       int key = 123; //assume u have 123 to search for
       //you have two arrays array1,array2
       bool inArray1 = isFound(array1,array1Size,key);
       bool inArray2 = isFound(array2,array2Size,key);
    
       if(inArray1 && inArray2)
       cout<<"Found in both";
       else if(inArray1)
       cout<<"Found in array1";
       else if(inArray2)
        cout<<"Found in array2";
      else
       cout<<"Not found";
    }

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Why are you using arrays in C++ ?? C++ is not C. In C++ you should be using vectors.

      Comment

      • Rajesh V
        New Member
        • Dec 2007
        • 16

        #4
        Originally posted by weaknessforcats
        Why are you using arrays in C++ ?? C++ is not C. In C++ you should be using vectors.
        Why shouldn't we use arrays in C++?

        Comment

        • Andr3w
          New Member
          • Nov 2007
          • 42

          #5
          Because when you have something more convinient to use..unless you are instructed to make your life harder (i.e. your teacher or boss wants you to do it that way) you use it....

          Comment

          • aemado
            New Member
            • Sep 2007
            • 17

            #6
            I was instructed... :(

            Comment

            • hdanw
              New Member
              • Feb 2008
              • 61

              #7
              Vectors are slow, Arrays are built more directly into machine code. Thats why.

              Also C++ is a superset of C. This means it IS C, and (++) so much more.

              Comment

              • krishnabhargav
                New Member
                • Feb 2008
                • 24

                #8
                Originally posted by hdanw
                Vectors are slow, Arrays are built more directly into machine code. Thats why.

                Also C++ is a superset of C. This means it IS C, and (++) so much more.
                I agree, vectors are very slow. If u are good at writing non-leaking code, I would prefer arrays over anything.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by hdanw
                  Vectors are slow, Arrays are built more directly into machine code. Thats why.
                  Not true. Vectors are required to be implementated as arrays. All you have to decide is whether to soak your employer over and over to rewrite the same code over and over in a manner that is not reuseable. Were it reuseable, you would end up with a vector.

                  P J Plauger, who wrote most of the4 STL, said: The templates are optimized for speed. If you think you can write faster cide, then think three times.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by krishnabhargav
                    I agree, vectors are very slow. If u are good at writing non-leaking code, I would prefer arrays over anything.
                    You have this backwards. It is your code that leaks not the library code.

                    Besides, any code that passes addresses around is, by definition, leaky.

                    When you start using handles instread of pointers, you will already be using vectors.

                    Comment

                    Working...