473,473 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Counting duplicate number in an array- implimented BST

6 New Member
This code is created to find all duplicates in a list of number by using a binary search tree implemented as an array. I want to add a function to count how many numbers in the list are duplicated but i dont know where to put it. It's really confusing. One more, i want to increase my array size to 5,000,000 but the program doesnt work.I dont know how to fix it. Plz help me.Thanks 4 advance.

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h> 
  2. #include <stdio.h> 
  3. #include <conio.h>
  4.  
  5. #define TRUE 1 
  6. #define FALSE 0 
  7. #define TOTAL_SLOTS 500000     
  8.  
  9. struct nodetype 
  10. long int info; 
  11. long int used; 
  12. }node[TOTAL_SLOTS] ; 
  13.  
  14. //initialize a tree
  15. void initialize (); 
  16. void initialize() 
  17.      long int i; 
  18.      for (i = 0; i<TOTAL_SLOTS; i++) 
  19.          node[i].used = FALSE;                 //mark every node as unused
  20.  
  21. //add node to the tree  
  22. void addnode (long int node_id,long int value) ;  
  23. void addnode (long int node_id,long int value ) 
  24.  
  25.  
  26.        node[node_id].info = value;            //assign the value of the number to the appropriate node_id in the tree
  27.        node[node_id].used = TRUE ;            //mark node as used one
  28.  
  29.  
  30.  
  31.  
  32. int main(void) 
  33.  
  34. {
  35.  
  36. //function ask user for the list of numbers      
  37.      printf("To start, please enter the number of elements in the list: ");
  38.      scanf("%d",&n);
  39.  
  40.      for (i=0;i<n;i++)
  41.      {
  42.           printf("Please enter number #%d: ",i+1);
  43.           scanf("%d",&a[i]);
  44.      }
  45. //initialize the tree
  46.      initialize(); 
  47.  
  48. //examine the numbers in the list
  49.      for(i=0;i<n;i++) 
  50.      { 
  51.        node_id=0; 
  52.        while ((node_id < TOTAL_SLOTS)) 
  53.        {
  54.               if (node[node_id].used == FALSE)   //while node_id has not been used
  55.               {
  56.                      addnode(node_id,a[i]);      //add node_id to the binary search tree
  57.                      break;
  58.               } 
  59.               if (a[i] == node[ node_id ].info)   //while current value has already been in the BST
  60.               {
  61.                      printf("\n%d is a duplicate", a[i]); 
  62.                      break; 
  63.               } 
  64.               if (a[i] < node[node_id].info)       //while value is smaller than the root
  65.                      node_id = 2* node_id +1;      //go to the left subtree
  66.  
  67.               else   
  68.                      node_id = 2* node_id +2;      //go to the right subtree
  69.        } 
  70.  
  71.        if (node_id >=TOTAL_SLOTS) 
  72.        {
  73.               printf("overflow error\n"); 
  74.               exit(1); 
  75.        } 
  76.  
  77. getch ();
  78.  
  79.  
Dec 10 '07 #1
3 5518
Laharl
849 Recognized Expert Contributor
If a long int is 4 bytes, then an array of 5,000,000 of nodes would use up about 8*5000000=40,000,000 bytes. Thus, simply allocating memory for this program would require at least 40MB. Your compiler probably doesn't want to do that.

You can add the function in the same general area as your other functions, after the struct is defined and before main().

Or, you can simply count up how many times <value> "is a duplicate" prints during your main and print that, no function required.
Dec 10 '07 #2
IZZI
6 New Member
Thank u very much!
About the function count, I mean that I dont know where to put it in the main function.
Dec 11 '07 #3
Laharl
849 Recognized Expert Contributor
From the looks of it, a function is really not what you want here, unless you want unnecessary overhead associated with calling it. Simply initialize and use an integer to count the number of times you have to print "duplicate value" when you try to insert.
Dec 11 '07 #4

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

Similar topics

3
by: Giloosh | last post by:
Hello, i need some help if possible... i have a payments table with over 500 records i want to run a query that searches through the table spotting out any duplicate ID#'s and Dates. So basically...
9
by: NiQ | last post by:
Hello every one, this is about an array of string and may be have done several time before but i need it and wasnt able to find it so here is the problem i have an array of strings with contains...
8
by: mgm | last post by:
hello, I have a query that is supposed to return only 1 record, however I recently found that because of an error in the database it can return more than 1. So what I need to do is capture if it...
2
by: SunMan | last post by:
Hello! I am trying to create a program that will ask for a user to enter a series of letters (codes) and then print out a table that shows the codes in decending frequency. Only letters will be...
14
by: Dan | last post by:
Is this discouraged?: for line in open(filename): <do something with line> That is, should I do this instead?: fileptr = open(filename) for line in fileptr: <do something with line>
9
by: tman88g | last post by:
I've been trying to learn arrays, and I was wondering how one would go about making a program that would take from the user as many single digit numbers (0 - 9) as the user wants to enter, and then...
2
by: ericnyc | last post by:
Hi, I am a newbee in C++ Please review this is what I wrote , what so ever I understood so far, My question is that I have to " Write a program C++ array that reads in an integer number and...
5
by: k4m135h | last post by:
Hi, I've come across a problem where i seem to be doing the counting of numbers in a inefficient way i've got a 200x200 array with numbers ranging from 101-600 and also 0 and 1. and i need to count...
2
by: raphael001 | last post by:
In my Visual Basic program I'm just trying to find duplicate values entered into an array from an inputbox, but i can't seem to get the coding right on the final part to check for duplicate values...
2
by: dawn123 | last post by:
I have a 2-d array that the user enters tress species and diameter so it looks something like: sm 10 rm 18 bf 12 sm10 sm10 rm18 and so on.. I need to be able to seach though my array and find...
0
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
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.