473,507 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with my array/ search function

1 New Member
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5.    {
  6. /*Array Values*/
  7.         int arnSalaries[10][2] = {21, 10000, 22, 15600, 23, 10000, 24, 56000,
  8.                                            25, 13250, 26, 24750, 27, 18750, 28, 56250,
  9.                                            29, 22450, 30, 27500};
  10.     int nRow, nCol;
  11.     int nEmSal;
  12.     char cFound = 'N';
  13.  
  14.           printf("\nSearching a 2D array\n");
  15.  
  16. /*How to Display the Array*/
  17.       for(nRow = 0; nRow < 10; nRow++)
  18.    {
  19.          printf("%4d %6d\n",arnSalaries[nRow][0], arnSalaries[nRow][1]);
  20.    }
  21.          printf("\n\n");
  22.  
  23.          printf("Please enter the salary : ");
  24.          scanf("%d", &nEmSal);
  25.  
  26.  
  27.  
  28.     funcSearch (arnSalaries, nEmSal);
  29.  
  30.     system("PAUSE");    
  31.     return 0;
  32.    }
  33. /*Search Function*/
  34. int funcSearch(int arnSalaries[10][2],int nEmSal)
  35.    {
  36.     int nRow, nCol;
  37.     char cFound = 'N';
  38.  
  39.      for(nRow = 0; nRow < 10; nRow++)
  40.    {
  41.      if(nEmSal == arnSalaries[nRow][1])
  42.    {
  43.         printf("\nEmployee(s) Found : %d \n", arnSalaries[nRow][0]);
  44.          cFound = 'Y';
  45.         nRow = 10; /* This is to break out of the loop */
  46.    }
  47.    }
  48.  
  49.      if(cFound == 'N')
  50.    {
  51.         printf("Sorry, No Employee(s) Found - Please Try Again\n");
  52.    }
  53.    }
So i wrote this code... However if i type a salary for example 10000 where there's two or more employee who has that salary only 1 result would appear.
What do i do?
Jan 30 '14 #1
1 1152
weaknessforcats
9,208 Recognized Expert Moderator Expert
This function:

Expand|Select|Wrap|Line Numbers
  1. int funcSearch(int arnSalaries[10][2],int nEmSal)
  2. {etc...
has arguments that are a pointer to an array of 2 int and a second argument for the search salary.

The function is supposed to return an int but i don't see a return of any kind in the code. In any case, the function should return a pointer to an int:

Expand|Select|Wrap|Line Numbers
  1. int*   funcSearch(int arnSalaries[10][2],int nEmSal);
  2.  
The pointer returned is the pointer to the int pair that has the nEmSal. The function should return 0 if the salary is not found.

All displays should be in main(). The search function should just search.

In addition, to need to pass in the number of elements. You should not have to hard code a 10 in the function since this locks the function to an array of 10 making it useless for an array of 20. There should be no hard-coded constants in your code.

Expand|Select|Wrap|Line Numbers
  1. int* funcSearch(int arnSalaries[10][2],int num elements, int nEmSal)
  2. {etc...
Hint:

Expand|Select|Wrap|Line Numbers
  1. int arr[10][2];
  2.  
  3. int (*p)[2] = arr;
  4.  
  5. int x = (arr+9) - (arr+3);
When working with arrays, the name of the array is the address of element 0. So arr is &arr[0]. arr+9 is &arr[9].
Therefore, x is set to 6. The array has 10 elements so the last element has the address of arr+9. Therefore, if the search function ever tries to search arr+10, you have gone off the end of the array and the function should return 0 for the int* meaning "not found".

Consider using an argument that is a pointer to an array of 2 int instead of the number of elements and let the function search up to that address:

Expand|Select|Wrap|Line Numbers
  1. int* funcSearch(int arnSalaries[10][2], int end[][2] , int nEmSal)
  2. {etc...
Then you can search this way:

Expand|Select|Wrap|Line Numbers
  1. funcSearch(arnSalaries, arnSalaries +10 , int nEmSal)
  2. {etc...
to search from element 0 to element 9. Now you can search any section of the array you wish by correctly setting the call arguments.
Jan 30 '14 #2

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

Similar topics

3
1742
by: Andy Jacobs | last post by:
Hi all I have a search function on a site of mine. At the moment, the content is delivered using this: <?php echo $row_Recordset1; ?> The search function goes through the table and...
1
7099
by: cyrvb | last post by:
Hello, I'm a very very newbie in C# I did start 2 days ago, I get Visual Stuido 2005 C# I try to understand how to manage the arrays I did write this
1
1764
by: lushh | last post by:
hi.. i am currently creating an employee database.. and there is a form on the database that needs to search on records.. i am planning to create a form with a single textbox (for the keyword...
1
1934
by: midnightblue | last post by:
Please could someone help me. I am a relative newbie, and have the following function on my php search page on my Joomla! site. <?php } } function...
3
2227
markmcgookin
by: markmcgookin | last post by:
Hi Folks, I have a VB app, and I have been working at it for a while, and I am now at the stage where I want to create a search function. Now don't be scared! It is in the .Net compact framework,...
6
3497
by: M Turo | last post by:
Hi, I was wondering if anyone can help. I'm want to pre-load a 'table' of function pointers that I can call using a its arrayed index, eg (non c code example) pFunc = func_A; pFunc = func_B;
3
1425
by: alexmason86 | last post by:
getting a bit stuck here got some code that allows a user to input numbers to a 3x3 array. but next on the tutorial sheet i have to then ask the user for a number to search for and then search the...
0
1417
by: kang jia | last post by:
hi currently i am doing this search function for car booking website. it can search through either car seats or CarModel. it seems it able to do search function, however i small problem occurs to...
1
1214
by: ashraf02 | last post by:
Someone please help! i am writing a code for a search function and everytime i execute the code i get the following error. You have an error in your SQL syntax; check the manual that corresponds...
1
1591
by: David De | last post by:
Need help with my site - search function within my site not working?!? I purchased this template, customized it and found out it has a search function for within the site - the challenge is that...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7319
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
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.