473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Array help

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 1785
D_C
293 100+
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 Expert Mod 8TB
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
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 =>...
2
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...
8
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...
7
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...
5
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...
8
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...
3
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...
3
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...
23
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...
5
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" ,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.