![]() |
|
#1
|
|||
|
|||
c: array comparisoncan 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
|
||||
|
||||
|
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.
Here is a program that will read the numbers and check for duplicates. CPP / C++ / C Code:
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:
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:
You should be able to modify this to fit your particular need. |
|
#3
|
|||
|
|||
urgent : c: array comparisonThe 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
|
||||
|
||||
|
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.
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:
Routine to load numbers from a file CPP / C++ / C Code:
Routine to save numbers to a file CPP / C++ / C Code:
Routine to print the list to the screen CPP / C++ / C Code:
And finally your modified random function CPP / C++ / C Code:
Here is the includes & defines that I needed CPP / C++ / C Code:
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:
|
|
#5
|
||||
|
||||
|
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:
|
|
#6
|
|||
|
|||
pointersfor 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
|
||||
|
||||
|
Quote:
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:
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:
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
|
||||
|
||||
|
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:
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 GIDBlog
Observations of Iraq by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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