473,394 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Basic Array Problem

17
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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. const int MAX = 100;
  8.  
  9. void process_data(ifstream &indata, ifstream &indata2, int studentnumber, int snumber[], int studnum[], string sclass[]);
  10.  
  11. int main()
  12. {
  13.     ifstream indata;
  14.     ifstream indata2;
  15.     int studentnumber, snumber[MAX], studnum[MAX];
  16.     string filename, filename2, sclass[MAX];
  17.  
  18.     filename = "student.data";
  19.     filename2 = "studentclass.data";
  20.  
  21.     studentnumber = 0;
  22.  
  23.     indata.open(filename.c_str());
  24.     if(!indata.is_open()){
  25.         cout<<"student.data file cannot be opened."<<endl;}
  26.     indata2.open(filename2.c_str());
  27.     if(!indata2.is_open()){
  28.         cout<<"studentclass.data file cannot be opened."<<endl;}
  29.  
  30.     process_data(indata, indata2, studentnumber, snumber, studnum, sclass);
  31.  
  32.     indata.close();
  33.     indata.clear();
  34.     indata2.close();
  35.     indata2.clear();
  36. }
  37.  
  38. void process_data(ifstream &indata, ifstream &indata2, int studentnumber, int snumber[], int studnum[], string sclass[]){
  39.  
  40.     int tempsnumber, tempstudnum;
  41.     string tempsclass, tempsect, tempgrade, tempslname, tempsfname, tempmajor, tempyear;
  42.  
  43.     int numberstudents = 0;
  44.  
  45.     while(indata){
  46.         indata>>tempstudnum;
  47.         indata>>tempslname;
  48.         indata>>tempsfname;
  49.         indata>>tempmajor;
  50.         indata>>tempyear;
  51.  
  52.         studnum[numberstudents] = tempstudnum;
  53.  
  54.         numberstudents++;
  55.  
  56.         indata>>tempsnumber;
  57.     }
  58.  
  59.     int numberstudentfiles=0;
  60.  
  61.     indata2>>tempsnumber;
  62.  
  63.     while(indata2){
  64.         indata2>>tempsclass;
  65.         indata2>>tempsect;
  66.         indata2>>tempgrade;
  67.  
  68.         snumber[numberstudentfiles] = tempsnumber;
  69.         sclass[numberstudentfiles] = tempsclass;
  70.  
  71.         numberstudentfiles++;
  72.  
  73.         indata2>>tempsnumber;
  74.     }
  75.     cout<<"Student: ";
  76.     cin>>studentnumber;
  77.  
  78.     for(int i=0; i<numberstudents; i++){
  79.         if(studentnumber == studnum[i] && studentnumber != snumber[i]){
  80.             for(int j=0; j<numberstudentfiles; j++){
  81.                 if(studentnumber != snumber[j]){
  82.                     cout<<"is not taking any classes"<<endl;
  83.                     return;
  84.                 }
  85.             }
  86.         }
  87.     }
  88.  
  89.     for(int i=0; i<numberstudents; i++){
  90.         if(studentnumber != studnum[i]){
  91.             for(int j=0; j<numberstudentfiles; j++){
  92.                 if(studentnumber != snumber[j]){
  93.                     cout<<"is not found in student.data"<<endl;
  94.                 }
  95.             }
  96.         }
  97.     }
  98.  
  99.     cout<<"is taking: "<<endl;
  100.  
  101.     for(int i=0; i<numberstudentfiles; i++){
  102.         if(studentnumber==snumber[i])
  103.             cout<<sclass[i]<<endl;
  104.     }
  105.  
  106.     return;
  107. }
  108.  
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??
Feb 7 '08 #1
9 2688
Write a method isFound....
Expand|Select|Wrap|Line Numbers
  1. bool isFound(int[] array,int arraySize, int key)
  2. {
  3.   // write your search code in here
  4.   for(int i=0;i<arraySize;i++)
  5.    if(array[i] == key) return true;
  6.  
  7.   return false;
  8. }
  9.  
  10. int main()
  11. {
  12.    int key = 123; //assume u have 123 to search for
  13.    //you have two arrays array1,array2
  14.    bool inArray1 = isFound(array1,array1Size,key);
  15.    bool inArray2 = isFound(array2,array2Size,key);
  16.  
  17.    if(inArray1 && inArray2)
  18.    cout<<"Found in both";
  19.    else if(inArray1)
  20.    cout<<"Found in array1";
  21.    else if(inArray2)
  22.     cout<<"Found in array2";
  23.   else
  24.    cout<<"Not found";
  25. }
  26.  
Feb 7 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Why are you using arrays in C++ ?? C++ is not C. In C++ you should be using vectors.
Feb 7 '08 #3
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++?
Feb 7 '08 #4
Andr3w
42
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....
Feb 7 '08 #5
aemado
17
I was instructed... :(
Feb 7 '08 #6
hdanw
61
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.
Feb 8 '08 #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.
I agree, vectors are very slow. If u are good at writing non-leaking code, I would prefer arrays over anything.
Feb 8 '08 #8
weaknessforcats
9,208 Expert Mod 8TB
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.
Feb 8 '08 #9
weaknessforcats
9,208 Expert Mod 8TB
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.
Feb 8 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: KellyH | last post by:
Hi, I hope someone can point me in the right direction. I'll get it out of the way: Yes, I am a college student. No, I am not looking for anyone to do my homework, just looking for help. I have...
4
by: solartimba | last post by:
I am a stats man that uses C++ to analyze stock data, but I have run into a problem regarding memory usage. First, though, I need to give a little background info. I want to write a program...
5
by: K. Shier | last post by:
when attempting to edit code in a class file, i see the bug "Visual Basic ..NET compiler is unable to recover from the following error: System Error &Hc0000005&(Visual Basic internal compiler...
4
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give...
1
by: bruce | last post by:
hi... i have the following test python script.... i'm trying to figure out a couple of things... 1st.. how can i write the output of the "label" to an array, and then how i can select a given...
5
by: svancouw | last post by:
I just started teaching myself Perl a couple days ago, and am looking forward to what this will give me for my Linux administration skills. Any help on this will be appreciated. I ran into a...
5
by: Newbie19 | last post by:
I'm trying to break up a sql query from being on long line, so it is easier to maintain for future users. currently I have this: Dim QrtTest As String QrtTest = "SELECT dbo.Issues.ID,...
3
by: Erwin Moller | last post by:
Hi all, I want to create an URL to use with fopen($url). The URL needs basic authentication, but I have no CURL on this particular machine. (I need to access an app that uses REST.) I tried...
12
by: sheldonlg | last post by:
This is kind of basic, and I googled but didn't find much help. I have an array of text element fields. They all need to have the same name. So, let the name be either all with a or build...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.