I have been working on a basic accounting program, the accounts were supposed to be stored in a array, but I never got that part of the program working. Is there anyway I can get a sample of hash code that would help me understand what I need to do? In my case, I am trying to get the account numbers to be stored in the hash array (that's how he worded it in class). I have searched through my book for the class and I did not find anything about hashing.

Any tips, tricks, or code sample would be appreciated.

Thanks in advance!

Recommended Answers

All 4 Replies

Tip: Pick a hashing algorithm such as MD5, SHA-2, Tiger, etc and look it up

I am not exactly sure what you meant by "MD5, SHA-2, Tiger, etc". Even our professor in class said this was not in the book. His lectures are recorded and I never heard anything about any of that... Basically I am learning all this stuff on my own, with the help of this forum and other research. Here is the code I currently have, I don't know if it helps or not, but I know for a fact I am very lost.

/*
Current Problems which need to be fixed:
Input account number and be able to change the account balance accordingly.
How do I get the user to input an account number, and the program to recognize it as a different account number each time?
Then adjust the account balance accordingly?
How do I save the balance to run through for all 31 days, increasing and decreasing accordingly?
Also, when trying to change from account to account, it will not change.
Not sure how to fix this problem.  
*NOTE - I am not trying to save to a file yet, for now we just want the balance to be saved only when the program is 
running, when the  program is closed and then reinitialized the account starts with a 0 balance again.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define DAY 31
#define MAXACCT 25
#define BUF_SIZE 80
#define SIZE 51

long hash(int acct [SIZE], long key);
long find(int acct[SIZE], long key);


int main(void)
{
	printf("Basic Accounting Program\n\n");

/*declared variables*/	
	int a = 1;
	int b = 2;
	int c = 0;
	int count, factorial;
	int choice = 0;
	int credit = 0;
	int debit = 0;
	int j = 0;
	int bal = 0;
	int table[51] = {0};
	long acct;
	long key;
	char rtrn;
	int acntlookup (const int acntnum);
/*end of declared variable*/

	for (count = 1; count < DAY ; count++)/*for loop for 31 days*/
	{
		printf ("Day %d\n\n",count);/*print the day*/

here1:/*user wants to make adjustments to new account*/

		for (j = 1; j < SIZE; j++)/*for loop for 25 accounts*/
		{
			printf ("Input account number, thank you!\n");
			rtrn = scanf ("%d", &acct);/*user inputs string of numbers*/
			key = acct;
			key = hash(table, acct);
			table[key] = acct;
			key = find(table, acct);
			acct++;

here:/*user has been returned to the main menu*/

			printf ("\nPlease choose an option below\n");/*main menu title*/
			printf ("\t1. Deposit (CR)\n");/*main menu 1*/
			printf ("\t4. Account Balance\n");/*main menu 4*/
			printf ("\t5. Choose another account\n");/*main menu 5*/
			printf ("\t7. Exit\n");/*main menu 7*/
			printf ("Please enter your choice: \n");/*main menu user input prompt*/
			scanf ("%d", &choice);/*main menu user chooses*/
			if (choice == 1)/*main menu choice 1*/
			{

here5:/*user has been returned to charge card menu from invalid entry*/		

				printf ("There is a $1 charge for Bank Card Transactions.\n");/*charge card menu*/
				printf ("Do you wish to proceed?\n");/*charge card menu*/
				printf ("\t1. Yes\n");/*charge card menu*/
				printf ("\t2. No\n");/*charge card menu*/
				scanf ("%d", &choice);/*charge card menu user chooses*/
				if (choice == 1)/*charge card menu choice 1 from main menu 1*/
				{
					printf ("Input Credit:\n");/*credit prompt*/
					scanf ("%d", &credit);/*user inputs credit*/
					bal = bal + credit - 1;/*balance is adjusted*/
					goto here;/*returns user to main menu*/
				}
				else if (choice == 2)/*charge card menu choice 1 from main menu 1*/
				{
					printf ("Please make another selection from the main menu. Thank you.\n");/*tells user what's going to happen*/
					goto here;/*returns user to main menu*/
				}
				else if (choice != 1 || choice != 2)/*user input wrong information in charge card menu*/
				{
					printf ("OOPS!! Invalid entry, please try again.\n");/*tells user they entered bad information*/
					goto here5;/*returns user to charge card menu from main menu 1*/
				}
			}
			else if (choice == 4)/*main menu choice 4*/
			{
				printf ("Your account number %d, and your balance is %d\n", rtrn, bal);/*prints out account number and balance*/
				goto here;/*returns user to the main menu*/
			}
			else if (choice == 5)/*main menu choice 5*/
			{
				printf("You must select a different account! Thank you!\n\n");
				goto here1;/*returns user to input account*/
			}
			else if (choice == 7)/*main menu choice 7*/
			{
				return (0);/*exit program*/
			}
			else if (choice != 1 || choice != 2 || choice != 3 || choice != 4 || choice != 5 || choice != 6 || choice != 7)
				/*invalid entry for main menu*/
			{
				printf ("OOPS!  Invalid Entry!\n");
				goto here;/*returns the uuser to main menu*/
			}
		}

here2:/*starts new day for the user*/;	

	}

return 1;/*EOF*/

}
long hash(int acct[SIZE], long key)
{
	int hashkey;
	int quotient;
	hashkey = key % SIZE;
	if (acct[hashkey] == 0)
	{
		return hashkey;
	}
	quotient = key/SIZE;
	do 
		hashkey = (hashkey + quotient) % SIZE;
	while (acct[hashkey] != 0);
	return hashkey;
}

long find(int acct[SIZE], long key)
{
	int hashkey;
	int quotient;
	int counter = 0;

	hashkey = key % SIZE;
	if (acct[hashkey] == key)
	{
		return hashkey;
	}
	quotient = key/SIZE;
	while (1)
	{
		hashkey = (hashkey + quotient) % SIZE;
		counter++;
		if (acct[hashkey] == 0)
		{
			return - 1;
		}
		else if(acct[hashkey] == key)
		{
			return hashkey;
		}
		else if (counter > SIZE)
		{
			return -2;
		}
	}
}

Why does the professor want you to use a hash? Hashing is for when you want to store something and then later check if a second input is the same as the first input while keeping the first input secret.

Each different sting of integers is put in a different spot and it runs through the whole program. My problem now is that if I put in $20 for account 1, then change to account 2, the balance of $20 carries over into the new account. I think that it will continue to do that until I everything is being stored in a file. I am so frustrated. But thanks to the help from this site I was able to get my program to come close to what the professor wants.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.