Thread: Guidance

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    5

    Question Guidance

    Alright the following things is for homework however I am not asking from someone to do it. I am merely asking for a guidance i've already asked my tutort but he just keeps saying that "i've provided you with everything you need" He hasn't even taught us anything all he does is give a code to look at then a week after a new homework. - Sorry for the rant.

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main(int argc, char *argv[])
    {
                 //Enter the values into the array
     	 int arnSalaries[10][2] = {21, 10000, 22, 15600, 23, 10000, 24, 56000,
     	 	  							   25, 13250, 26, 24750, 27, 18750, 28, 56250,
     	 	  							   29, 22450, 30, 27500};
        int nRow, nCol;
        int nEmSal;
        char cFound = 'N';
       	nCount = 0;
    	nMin = 9999;
    	nMax = 0;
    	nInputVal = 0;
     	 printf(" Array Task 5\n\n");
     	 
     	 //Display the array
     	 for(nRow = 0; nRow < 10; nRow++)
     	 {
    		 printf("%4d %6d\n",arnSalaries[nRow][0], arnSalaries[nRow][1]);
        }
        printf("\n\n");
        
        printf("Please enter the salary : ");
    	scanf("%d", &nEmSal);
    	
    	
        
    	funcSearch (arnSalaries, nEmSal);
    
    
      system("PAUSE");	
      return 0;
    }
    
    
    int funcSearch(int arnSalaries[10][2],int nEmSal)
    {
    	int nRow, nCol;
        char cFound = 'N';
        
    	 for(nRow = 0; nRow < 10; nRow++)
    	 {
    	  			 if(nEmSal == arnSalaries[nRow][1])
    	  			 {
    		  			printf("\nEmployee found - has an employee number of %d\n", arnSalaries[nRow][0]);
    		 		 	cFound = 'Y';
    		 		 	nRow = 10; /* This is to break out of the loop */
    		        }
        }
        
        if(cFound == 'N')
        {
    	  			  printf("Sorry, employee not found - please try again\n");
        }
    }
    okay i need hints or something to work with on how can i go about modifying the search function i created so that i can input two set of numbers (for minimum and maximum) then it will display the salary within that range.

    p.s I'm not at all that clever so please explain things in Standard english

    Thank you!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Have your function take two values, and then on line 46 (as posted) check that the salary value is bigger than the low bound and smaller than the high bound (instead of checking equal to).

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First, some notes.

    nCount, nMin, nMax, and nInputVal have not been declared. You need to declare (and optionally initialize) those variables as the ones above it. Assuming they're integer values:

    Code:
    int nCount = // ...
    int nMin = // ...
    // etc
    You can either put your function before or after "main()". If after, you need to declare a function prototype first above "main()".

    Code:
    // example of putting a function before "main()"
    void function(void)
    {
        // do something
    }
    
    int main(void)
    {
        // main program
    
        return 0;
    }
    Code:
    // example of putting a function after "main()"
    void function(void);  // function declaration/prototype - note the semicolon
    
    int main(void)
    {
        // main program
    
        return 0;
    }
    
    void function(void)  // function definition - no semicolon here
    {
        // do something
    }
    Your "funcSearch()" function is declared to return an integer. In that case, you need to return a value from the function. If you're not returning a value from the function, it should be declared as void:

    Code:
    void funcSearch(int arnSalaries[10][2],int nEmSal)
    Also, it would help a lot to give your variable clearer names. "nEmSal" takes longer to decipher than "employee_salary", for example. You don't want the variable names too long, but you also don't want to trim a few letters to make it shorter, thereby making interpretation of its purpose more difficult.



    Now on to the question at hand.

    You didn't explain the data in your two-dimensional array. However, I gleaned from the code that [0] is the employee number and [1] is their salary. (In the future, it would be helpful to include this information in your post to reduce the time we have to spend figuring it out).

    So, to make the modification you described, first you have to read two salaries, the min and the max, in "main()".

    Then, you have to send those two values to the function. Perhaps call these "employee_salary_min" and "employee_salary_max".

    Within the function, instead of checking for equality (==), you would have to check for greater than (>) and less than (<). (Or greater-than-or-equal-to and less-than-or-equal-to.)

    That should be enough to get you started. Let us know if you need clarification on these suggestions, but at least try them out for yourself first.

  4. #4
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    Thank you!! omg thank you! you don't know how much you've helped me!
    Sorry about the code i sent, didn't realize i copied the old instead of the latest.

    Just one last thing, I manage to improve it by having a switch case at the start. 1 where you can search by putting min and max value 2 by putting Employee number which gives you salary and 3 by putting a specific salary which gives you the employee number.

    So 1 last question around the 3rd option a really stupid one. If you input a salary there is a chance that there will be 2 or more employee which has that salary. However it only show 1 employee number instead of 2 or 3. How can i go about and change that so it shows all the employee number who has that salary.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Thank you!! omg thank you! you don't know how much you've helped me!
    That makes me feel good.

    Sorry about the code i sent, didn't realize i copied the old instead of the latest.
    That, not so much.

    Anyway, in regard to your question, we'd have to see the code for that portion to help troubleshoot it.

    I suspect you're using a similar (if not copy/paste) version of the other search function. That function looks for an employee number, which is ideally unique, so once it's found, you can exit the loop, knowing all the work is done.

    If you're looking for which employees have a particular salary, you would need to loop through all employees, checking each one, and printing out the results as they're found.

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    5
    Yup its basically the same search function. I didn't realize there was a task before my first question (on how to add the min and max).
    So i just need to modify the code (the one i copied) so that it can show all employees number that have the same salary.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Excon View Post
    So 1 last question around the 3rd option a really stupid one. If you input a salary there is a chance that there will be 2 or more employee which has that salary. However it only show 1 employee number instead of 2 or 3. How can i go about and change that so it shows all the employee number who has that salary.
    Have you ever wondered why your code says
    Code:
    /* This is to break out of the loop */

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Yup its basically the same search function.
    I figured as much.

    So i just need to modify the code (the one i copied) so that it can show all employees number that have the same salary.
    Copied from whom?

    I already explained what you need to do, and even asked for code which you didn't post. In response, you just re-iterated the question. In the words of your tutor, "I've provided you with everything you need".

    I am not asking from someone to do it. I am merely asking for a guidance
    You got it - the ball is in your court. Try it yourself and, if you get stuck, show the code and ask specific questions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In need of guidance
    By ajdspud in forum C++ Programming
    Replies: 7
    Last Post: 06-01-2016, 02:23 AM
  2. Need Some Guidance
    By Ocean Chow in forum C Programming
    Replies: 6
    Last Post: 03-03-2013, 05:37 PM
  3. Need some guidance.
    By Kinto in forum C Programming
    Replies: 10
    Last Post: 05-31-2009, 12:02 AM
  4. Need some guidance
    By Meikj in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2009, 11:35 AM
  5. I am in need of help, need C++ guidance
    By Chessman.exe in forum C++ Programming
    Replies: 51
    Last Post: 08-24-2007, 03:23 PM

Tags for this Thread