Thread: Parallel Array Problem

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    Parallel Array Problem

    I'm not sure if I did this properly. The program works fine but I don think that is what the problem is asking for.

    Any other ways to accomplish this task?

    Thanks in advance!

    Here's the problem
    Code:
    Name and load two parallel arrays for the following table.  Write a search routine (not a whole program) 
     that uses the input variable deptName to extract the access code.
    
    
    Department	Access Code
    Accounting	 1123X5
    Receiving	   1154V2
    Sales	             8823Q1
    Management    5412R7
    and my solution:
    Code:
    #include<iostream.h>
    
    int main()
    {
    	
    
    	char department[4][15] = {"Accounting", "Receiving", "Sales", "Management"};
    	char access_code [4][15] = {"1123X5", "1154V2", "8823Q1", "5412R7",};
    
    
    cout << "\n Please enter department number to retreive the access code:";
    cout << "\n Accounting = 1";
    cout << "\n Receiving = 2";
    cout << "\n Sales = 3";
    cout << "\n Management = 4" << endl;
    int deptName;
    cin >> deptName;
    
    
    
    		cout << department[deptName-1] << " ";
    		cout << "\nAccess code is: " << access_code[deptName-1] << endl;
    
    	
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    since this is C++, use 'iostream' instead of 'iostream.h'. instead of using char[]'s use 'string's (youll need to #include<string> first) unless your required to use char[]'s or want to. after your include statements do "using namespace std;".

    the program looks like it will do what is asked. but it doesnt really 'search' for it. maybe he wants you to retrieve a string for the department name and use a for loop to loop through each element in department and compare the strings, then return the access code at that index.

    i think it would be either this way or the way you have it, but the way i described sounds more like 'searching', but i guess its up to the teacher and what he's been teaching you.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well, the assignment says to load the data, maybe implying input. It also sounds like deptName could be a string that you ask the user for, which might imply string comparison. You should probably just also check whether what the user enters is actually a valid department (in your solution, I could give it a number like 841875 and it would crash).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM