473,775 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array help

1 New Member
I am writing this program that takes a number gives it a hash humber and then put the orginal number in the array spot.

ex. Enter a number: 32
Hash value is 5 so 32 is placed in the 5th spot of the array. If two numbers have the same hash value my program must insert that number in the next available spot. Here is my code. My problem is I can't make the numbers go in a particular arrray location.

#include <iostream>
using namespace std;
struct array{
int num;
int collision;
};

int A[8];

int main()
{
array A[8];
int k;
int number;

for (k = 0; k < 8; k++){
A[k].num=-1;
A[k].collision=0;
}

for (k = 0; k < 8; k++)
{
cout << "Enter eight numbers: ";
cin >> number;
int hash = number % (8 + 1);
if(hash=8)
hash=0;
if(A[hash].num==-1)
A[hash].num=number;
else
{
A[hash].collision++;
for (int l=1; l<=8-hash; l++)
{
if(A[hash+l].num==-1)
{
A[hash+l].num=number;
break;
}
else
A[hash+l].collision++;
}
}
}
for (int i=0; i<8; i++)
{
if(A[i].num!=-1)
cout<<"Number Inserted at Index " << i << " is " << A[i].num << " and " << A[i].collision << " collisions occured at this index." << endl;
}

return 0;
}
Aug 31 '06 #1
2 1798
D_C
293 Contributor
Expand|Select|Wrap|Line Numbers
  1. if(hash=8)
  2. hash=0;
This code always does the same thing. That is, assigns 8 to hash, then probably 0 to hash. It depends whether we assign hash = 8. Make it
Expand|Select|Wrap|Line Numbers
  1. if(hash==8)
  2.   hash = 0;
By the way, it would probably be easier just to do it modulo 8, and possibly multiply your input by a number which is relatively prime to eight (3, 5, or 7).
Sep 1 '06 #2
Banfa
9,065 Recognized Expert Moderator Expert
Also

for (int l=1; l<=8-hash; l++)
{
if(A[hash+l].num==-1)
{
...
}
}

At it's limit l = 8-hash

then you use it as an array index A[hash+l] == A[hash+8-hash] = A[8]

but A declared as array A[8] so A[8] is an out of bounds array access.
Sep 1 '06 #3

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

Similar topics

8
906
by: point | last post by:
Hi there.. I have the folowing array => property = hexonet => property = 2003-12-01 18:46:20.0 => property = hexonet => property = 2003-12-02 02:59:15.0 => property = hexonet => property = 2004-12-01 18:46:20.0
2
1565
by: Antti Nummiaho | last post by:
Consider the following javascript: var temp = new Array(new Array(0)) document.writeln(temp) temp = new Array(new Array(0,1)) document.writeln(temp) One would assume that it would print "0 0" that is the first elements of the arrays, but it prints "undefined 0". Why does temp return
8
2112
by: Gactimus | last post by:
I made the program below. It outputs the smallest number in the array. What I would like to know is how do I output the array location. I am at a loss. For example, since the smallest number in the array is 2, the output should be 2 for the number and 1 for the location. If anyone could help or point me in the right direction that would be great. Thanks. ------------------- #include <iostream>
7
25173
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort (Bubble, insertion, selection..). I have an array of int's and am passing them to each sort function.
5
2218
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as 1,1,2,4. I have narrowed it down to the sort function. I'm almost positive
8
10719
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
3
8900
by: George | last post by:
Sub ExcelToListBox() Dim xRange As Object Dim ary Dim xValue As String xRange = oXL.Range("A1:A9") 'has letters A-H ary = xRange.value xValue = ary(3, 1) 'xValue = C Me.ListBoxPerson.Items.AddRange(ary)
3
3740
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a lot different from what the pros around here use. I really need help with debugging some program errors more than anything, even though my coding style might not be perfect. Anyway here is my code. About the only things that work right are...
23
7417
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
5
1559
by: fluk | last post by:
Hi Guys, I hope someone can help me with this, because i'm getting crazy to find a good way to do that! This is what I got by querying a db. $arr1 = array("site", "description", "area1" , "activity1"); $arr2 = array("site", "description", "area1" , "activity2"); $arr3 = array("site", "description", "area2" , "activity2"); $arr4 = array("site1", "description1", "area3" , "activity2");
0
9622
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10107
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10048
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8939
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6718
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5360
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4017
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2853
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.