GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Jan-2004, 00:28
jack jack is offline
New Member
 
Join Date: Jan 2004
Posts: 15
jack is on a distinguished road

c: array comparison


can anyone provide me with a solution in c language please

I have a number 123456789 that needs to be stored in an array and a second number 234567890 to be stored to the same array. Now if another number is to be added let say 345678901 it has to check if it already exist if yes it ask for another number and if no it stores it in the array. then this lastly number stored 345678901 is taken from the array and broken and added to a new array for e.g a[0]='3' ,a[1]='4' and so on.
  #2  
Old 25-Jan-2004, 07:57
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Jack (sounds like I am going to commandeer a plane )

I've provided some things for you here, but I had to make some assumptions.
  1. Did you want your storage to be static? (ie an array) Or did you want it to be dynamically allocated? The array is easier but the dynamic allocation is better. The sample provided is the array.
  2. Do you want the broken down array to be the actual charecters of the number? That is what it looks like you have shown.
  3. I didn't understand when you wanted to seperate the number out. I just included a function, you will have to call it where you need it.
  4. Are your numbers always going to have 9 digits. This program also assumes that they will (static storage
  5. Where does the input come from? This sample reads it from the console

Here is a program that will read the numbers and check for duplicates.
CPP / C++ / C Code:
#include <stdio.h>

#define SIZE 10

main(){
	long int 	numbers[SIZE];
	int			index1,
				index2;
	char		same;

	for(index1 = 0; index1<SIZE; index1++){
		do{
			printf("Enter number(#%d): ",index1+1);
			scanf("%ld",&numbers[index1]);
			same = 0;
			//Checks to see if number entered is already in array.
			for(index2 = 0; index2<index1; index2++){
				if(numbers[index1] == numbers[index2]){
					same = 1;
					break;
				}
			}
			//If number was already found then same would equal 1
			if(!same)
				printf("Added #%ld\n",numbers[index1]);
			else
				printf("That number is already there!  Try again.\n");
		}while(same);  //Loop if the number is the same and ask for another number.
	}
}

Okay, now for the number split routine. This is slightly ugly due to the need to typecast about everything. I have tried to comment what each line does.
CPP / C++ / C Code:
#include <math.h>
#include <malloc.h>

#define NUM_CHAR 48					//Number to add to get from a numeric digit to an ascii representation
#define	SIZE_NUM 9

char* sep_number(long int number)
{
	char* 		solution = (char*) malloc((SIZE_NUM+1)*sizeof(char));
	long int	divider;

	for(int index = 0; index<SIZE_NUM; index++){
		//This line calculates the number to divide by in the form of 10^(SIZE_NUM-index)
		//1st run Example: 100000000
		divider = (long int) pow( (double) 10, (double) ((SIZE_NUM-1)-index) );

		//This line does integer division to isolate out the first number left in the large number
		//First run example: 123456789 / 10000000 = 1.23456789, the integer portion is 1.
		*(solution+index) = (char) (number / divider);

		//This line reduces the number to subtract off the first number.
		//Note that this does not change the original number only the copy that we have passed to this function.
		//123456789-100000000 = 23456789
		number -= ( *(solution+index) * divider );

		//This line puts the found char in ASCII MODE.
		*(solution+index) += NUM_CHAR;
	}
return(solution);
}

Note that this routine needs the math library to function. I am not sure what compiler you are using, but if you are using gcc, you will need to tell it to link the math library by using the -lm directive:
Code:
gcc numbers.cpp -lm -o numbers

You should be able to modify this to fit your particular need.
  #3  
Old 25-Jan-2004, 10:46
jack jack is offline
New Member
 
Join Date: Jan 2004
Posts: 15
jack is on a distinguished road

urgent : c: array comparison


The solution for the first part s ok but the number stored must always remain in the array once stored. for e.g if 123465789 was stored today, on 2 days time the number must still be there when compiling.
the next part i think there is a problem when compiling, but i understand your concept.

Here i will give you a brief idea what i'm doing.
I worked out a random function like this

#include <stdio.h>
#include <sys/types.h>
#include <time.h>
time_t t1;


main()
{
int w;
w=ra();
printf("%d",w);

}
int ra(){
int i;
(void) time(&t1);

srand48((long) t1);

r= lrand48()%1000000000;

}



So the random value that is generated will be stored forever in the array. if on generating the random value is already existed it has to regenerate else it is stored in the array. And it is this lastly stored number that is needed to be broken down into a digit. that is 123456789, i have to get 1 separate to the remaining and 2 separate to the remaining and so on.
Can you find a solution for this and all must be in the same program.
  #4  
Old 25-Jan-2004, 13:34
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hey Jack.

This may be rather presumptious as I have only been on these forums for a couple of days, but there are a couple of things that I would like to remind you (and everyone else) about.
  1. PLEASE, PLEASE, PLEASE read this post that describes how to post code. It is extremely simple and it helps so much to be able to read it.
  2. This is my own personal opinion, but please don't ask me to write and post a complete program here on this forum. I have no problem sharing code or even looking at your code to debug it. This forum is a community of people coming together to help each other, not just a place where you can get some one to do your homework or solve your problems for you. (Moderators, please mod me down if I overstepped my bounds.)
[/soapbox]

Now to the task at hand. First of all, all programs run in volatile memory. This means that once they are done running, the stored variables are gone. In order to store something your program must serialize/deserialize or in other words have a load/save function to the hard disk. Second, you asked that the data be there every time you compile. Is this really what you mean? Or do you mean every time that the program is run?

As to the part function not compiling, remember that it is a function, you need to put it into your program file and call it as needed. I don't compile/test all of the code I post here, but I did test that one because it was kind of complex. It both compiled and gave the correct results for what I understood them to be. If you get compilation errors/run errors please post the exact error and I will take a look at it.

Now on to the code that you submitted. I don't think that this would compile, but it does give me a better idea of what you are trying to do. Unless you are going to include a header file to define all of your functions, it is better to put your main function last. Also, it appears to me that this is to be a dynamic allocation program and that means an array won't work. I previously posted some to code to handle a singly-linked list here . Scroll down and find a heading for a file called list1.h. You can either include this entire code in your program or you can save it as list1.h and just include it in your program. The latter is better. Now this does include some c++ functionality, but most compilers these days can handle this. Let me know if yours cant.

For what you are doing, I would use a main function like this.
CPP / C++ / C Code:
main(){
	char*	split;
	long	new_number;
	char	same;
	list*	numbers;

	numbers = load_numbers(FILENAME);
	print_numbers(numbers);


	do{
		new_number = ra();
		same = 0;
		if(!numbers->is_empty()){
			numbers->home();
			do{
				if(*(long*)(numbers->get_data()) == new_number){
					same = 1;
					break;
				}
			}while(numbers->next());
		}
		if(!same){
			numbers->add_data((void*) &new_number);
			split = sep_number(new_number);
			printf("String number is: %s\n",split);
			print_numbers(numbers);
		}
		else
			printf("That number is already there.  Finding new number\n");
	}while(same);

	save_numbers(FILENAME,numbers);

	return 0;
}

Routine to load numbers from a file
CPP / C++ / C Code:
list* load_numbers(char* filename)
{
	list* numbers = new list;
	FILE* fp = fopen(filename,"r");
	long *number = new long;

	if(fp){
		while(fscanf(fp,"%ld",number) != EOF){
			numbers->add_data((void*) number);
			number = new long;
		}
		fclose(fp);
	}

	return(numbers);
}

Routine to save numbers to a file
CPP / C++ / C Code:
void save_numbers(char* filename, list* numbers)
{
	FILE* fp;

	if(!numbers->is_empty()){
		fp = fopen(filename,"w");
		numbers->home();
		do{
			fprintf(fp,"%ld\n",*(long*)(numbers->get_data()));
		}while(numbers->next());
		fclose(fp);
	}
}

Routine to print the list to the screen
CPP / C++ / C Code:
void print_numbers(list* numbers)
{
	int		count = 1;

	printf("Listing of numbers:\n");
	if(!numbers->is_empty()){
		numbers->home();
		do{
			printf("#%d: %ld\n",count,*(long*)(numbers->get_data()));
			count++;
		}while(numbers->next());
	}
	else
		printf("List is empty.\n");
	printf("\n");

}

And finally your modified random function
CPP / C++ / C Code:
ng ra(){
	long number;
	time_t t1;

	(void) time(&t1);

	srand48((long) t1);
	number = lrand48()%1000000000;

	return(number);
}

Here is the includes & defines that I needed
CPP / C++ / C Code:
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <sys/types.h>
#include <time.h>
#include <stdlib.h>
#include "list1.h"

#define FILENAME "numbers.tst"
#define NUM_CHAR 48					//Number to add to get from a numeric digit to an ascii representation
#define	SIZE_NUM 9

Now, with the above function of sep_number, that really is the whole program. Once again, I have no problem in giving you the help you need, but I don't want to complete your projects for you. My hope is that you look at this and understand what it is doing so that you can modify it and adapt it as needed.

This compiled and ran for me just fine. On a side note, it is amazing how often there is a repeat of these nine digit numbers! I thought I wouldn't be able to test for duplicates, but it found one after only like 5 numbers. And when it does find one, it loops quite a bit until the time changes and it can give another number. I found it rather interesting. Here is a printout from the last time I ran it:
Code:
That number is already there. Finding new number ... (* a lot) That number is already there. Finding new number Sting number is: 284437014 Listing of numbers: #1: 103321706 #2: 366506341 #3: 259958695 #4: 98344936 #5: 566863713 #6: 11963854 #7: 604547643 #8: 532431855 #9: 870984350 #10: 593534420 #11: 123155762 #12: 993189481 #13: 715739551 #14: 982176258 #15: 560494753 #16: 283044823 #17: 234347671 #18: 104381389 #19: 826931460 #20: 549481530 #21: 116786802 #22: 839336873 #23: 561886943 #24: 284437014
  #5  
Old 26-Jan-2004, 07:27
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
There is an embarrasing oversite in my sep_number function.

I am surprised that this thing didn't throw up all over itself when I ran it. I forgot to null terminate the string. Even though this is an array of chars, it is the C implementation of a string. I even use it as such in the main program. I have added a line right before the return statement that should take care of this.

CPP / C++ / C Code:
char* sep_number(long int number)
{
	char* 		solution = (char*) malloc((SIZE_NUM+1)*sizeof(char));
	long int	divider;

	for(int index = 0; index<SIZE_NUM; index++){
		//This line calculates the number to divide by in the form of 10^(SIZE_NUM-index)
		//1st run Example: 100000000
		divider = (long int) pow( (double) 10, (double) ((SIZE_NUM-1)-index) );

		//This line does integer division to isolate out the first number left in the large number
		//First run example: 123456789 / 10000000 = 1.23456789, the integer portion is 1.
		*(solution+index) = (char) (number / divider);

		//This line reduces the number to subtract off the first number.
		//Note that this does not change the original number only the copy that we have passed to this function.
		//123456789-100000000 = 23456789
		number -= ( *(solution+index) * divider );

		//This line puts the found char in ASCII MODE.
		*(solution+index) += NUM_CHAR;
	}
  *(solution+SIZE_NUM) = NULL;
return(solution);
}
  #6  
Old 26-Jan-2004, 08:51
jack jack is offline
New Member
 
Join Date: Jan 2004
Posts: 15
jack is on a distinguished road

pointers


for the sep number i'm getting two errors which are:
'for' loop declaration used outside c99 mode.
and the second
warning:assignment makes integer from pointer without a cast
can you solves it.
  #7  
Old 26-Jan-2004, 09:28
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by jack
for the sep number i'm getting two errors which are:
'for' loop declaration used outside c99 mode.
and the second
warning:assignment makes integer from pointer without a cast
can you solves it.

Jack, what compiler are you using. That first error is strange. This is a simple for loop. Mabye it doesn't like the in-line declaration. Try declaring the index outside of the for loop, so you would have:

CPP / C++ / C Code:
int index;

for(index = 0; index<SIZE_NUM, index++){

The other one is a warning, ie, it should still compile. It may need a typecast on the number adjustment line:
Try:
CPP / C++ / C Code:
number -= ( ( (int) *(solution+index) ) * divider );

Also, can you perchance post the function if you have more problems? Also, does your compiler give you line numbers for the errors? If so, can you tell me on what line it is giving the errors?
  #8  
Old 26-Jan-2004, 11:21
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
One more thing that I have been thinking about. If this process is running an a high use machine, cycles may be at a premium. This program loops quite a few times if it gets a similar number, probably until the system time changes. If you want to save CPU cycles you may want to insert a sleep command here:

CPP / C++ / C Code:
else{
      printf("That number is already there.  Finding new number\n");
      sleep(1);
}

This will cause the execution to stop for 1 second (enough time for the system time to change). This will run slightly slower, but will not require nearly as many cpu cycles. A second to a cpu is an eternity.

Just a thought...
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers and arrays jack C Programming Language 4 15-Jan-2004 12:27
Sorting a 2D array c++ mike3340 C++ Forum 4 15-Dec-2003 13:35
Extra null element in an array samtediou MySQL / PHP Forum 2 11-Dec-2003 11:52
deleting from an array ionyka C++ Forum 1 02-Sep-2003 03:09
Array_search on a multidimensional array JdS MySQL / PHP Forum 3 11-May-2003 06:22

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:23.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.