BTW, see also http://www.cplusplus.com/r
Main Topics
Browse All TopicsGuys .....greetings...
My programme to find the prime number below 10000 is ready to go. But I want to use malloc function in my programme to create memory for 10,000 elements of the array.....So guys can you help me how can I do that........
Tons of thanks......
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
BTW, see also http://www.cplusplus.com/r
Yes thank you
It is really simple to use malloc. I also used assert macro just below the malloc function.I think it is correct to use in that way......
In the output I am getting number 4063608 on the top( before prime number 2). I don't know why I am getting this......
There I want to print something like "There are 1229 prime numbers less than or equal to 10000"
Output is attached below.....
Thank you JKR very much,,
I have one last question for you..............I want to print " There are 1229( what ever it is) prime numbers less than or equal to 10000"
So for this I have to use just fprintf... function but I don't know how to count n[i].......Please tell me how to do that....
Thank you very much......................
You can replace these two lines with a single statement using calloc() as follows.Calloc initializes memory with value 0.
>>num = (int*) malloc(10000 * sizeof(int)); // allocate the memory
>> memset(num,0,10000 * sizeof(int));
num = (int*) calloc(10000, sizeof(int));
>>the out put is wrong it shows very high number...............
This prog given by JKR is correct. I think u r getting garbage due to some changes in the code. Can u provide the exact code that produces this unexpected result!
Thank you very much jkr,
Here is probably last help i want from you... I want to make some modification to my code......I want to use command line arguments to pass the filename and the number. Such as
SeiveErat.exe 10000 Output.txt
I think we have to use something like atoi() to convert 10000 to int.
I don't know how to use the function atoi() in our programme.........
Can you tell me how to do this...........what should we add in our programme...........
Thank you very much in advance..................
It doesn't make a functional difference, but (though I'm not big with C) I believe allocating memory like this is supposed to be done using "calloc" instead of "malloc". They are functionally identical, but using one over the other shows your intent in a program, which is valuable in any language. calloc's signature is as follows:
void* calloc(size_t numElements, size_t elementSize);
So you would put the following where you currently call malloc:
calloc(10000, sizeof(int))
Also, your algorithm is using much more memory than is needed (unless I am misunderstanding your intent). It looks to me like you want to output every prime number between 2 and 10,000, and you also want to output how many of these numbers there are. Again, I'm not big with C, but this is much more efficient (I'm gunna hate myself for this, 'cuz this totally sounds like a homework assignment, but here goes anyway...)
Hope this helps,
Nate
Thank you Internalstatic,
I appreciate your help and support. I am not like you, I am just a beginner in this world. SO, I don't care about how much memory my programme is eating. I am learning and using all differnt kind of things. And your programme is different then mine.
So, can you please update my own programme or code..........In that way it will be way to much easier for me to know whats going on in my programme...........
By the way what do you mean by "countPrimes = 0U;" What is 0U..
Thank you very much......
As Subrat2009 said, the "U" after the number tells the compiler that the number it follows is unsigned (in other words, that the number within it cannot be negative). As I've expressed in a previous post, the point of the "U" suffix is to express intent, while it offers no real functionality in this context (you'll start to see when these things matter as your knowledge progresses). Its use is only valid after a literal numeric constant.
By the way, is this little project a homework assignment, or are you teaching yourself the C language?
// Plz read the comments carefully by which u can be cleared that why something is used!
/* Finding Prime Numbers Using The Sieve Of Eratosthenes*/
#include<stdio.h>
#include <stdlib.h> /* for the malloc() function and exit() function */
#include <assert.h> /* for assert macro */
int main(int argc, char* argv[])
{
FILE *fp = NULL;//Better try to initialize all variables
int i = 0, j = 0, count = 0;
// better use unsigned count = 0U;
int* num = NULL; // instead of the array.
//Better try to initialize all variables
if (argc != 3) {
printf("Usage: SeiveErat.exe upperbound output.txt \n");
exit(1);
}
fp = fopen(argv[2], "w+"); // change this to 2 bcz argv[1] holds upperbound like 10000.So argv[2] will hold filename.argv[0] holds executable name.
int upperBound = atoi(argv[1]);
//num = (int*) malloc(upperBound * sizeof(int)); // allocate the memory
// Use calloc() here . Already told why in the same thread. Else use memset() as described by jkr which initializes the memory
// Please use calloc() bcz it initializes the memory.But malloc dosn't do that , it only allocates memory and allocated memory will hold garbage values. Which is unsafe.As u r in beginning level, u should check urself in these regards to become a very good programmer in future.
// Use num = (int*) calloc(upperBound, sizeof(int));
num = (int*) calloc(upperBound, sizeof(int));
assert(num != NULL);
for(i=1;i<=upperBound-1;i++
num[i]=i+1;
for(i=1;i<=upperBound-1;i++
{
if(num[i]!=0)
{
for(j=(i+1);j<=upperBound-1
{
if(num[j]!=0)
{
if((num[j]%num[i])==0) /*check if num[j]*/
num[j]=0; /*is a multiple of num[i]*/
/*if it is a multiple then set it to 0*/
}
}
}
}
for(i=1;i<=upperBound-1;i++
{
if(num[i]!=0) /*Print all prime numbers*/
{
//printf("\n%d",num[i]);
fprintf(fp, "%d\r\n", num[i]);
++count; // count prime number
}
}
fprintf(fp,"There are %u prime numbers less than or equal to %d\n", count, upperBound);
free(num); // clean up
fclose(fp);
}
First of all guys thanks for your help,
This is not homework assignment, I am teaching myself C and I ask you guys exact questions and problems from books. And I want to do exactly what is said in books.
This is how I am learning......
I have one question again...................
In the end of my output page it says," There are 1229 prime numbers less than or equal to 10000" ................I want to put that message in the begening of the page i,e before the prime number starts..
Guys give me some solid idea please ..........................
I would love to help, but I think this question is quite exhausted. Us feeding you code like this really isn't getting you anywhere--you need to learn how to think like a programmer. Syntax is nothing--hell, I've never written a program in straight C before today, and it took me all of about 15 minutes to figure it out.
When you find a book to read, do not skip anything. If you think you know whatever it is you're reading about, assume you're wrong, because you probably are. If you've done some skipping around on your current book, I suggest you start over. Open up notepad, and start putting bulleted notes about things that don't make sense to you (maybe your first entry should include the "U" suffix I mentioned? If you're not familiar with the difference between "signed" and "unsigned", it's time you discovered Bing.com)
Lastly, I strongly suggest you put your learning of C on hold and take up another, very similar language (called C++). Their syntax is remarkably similar, however, they are fundamentally different. C is what we call a procedural language, whereas C++ is an object-oriented or object-based language. Object-oriented languages are quickly becoming the standard, so if you don't have a set-in-stone reason to learn C, I recommend you move on over to C++ instead. However, there is one more language I would recommend, also syntactically similar to C and C++: The language C#.
C# is another object-oriented language, but it's a little different. The language relies on another "platform" (known as the .NET Framework--perhaps you've heard of it). The advantage of this fact is that the .NET Framework offers unsurpassed code documentation, naming consistency, and the world's truly best IDE (Integrated Development Environment), known as Microsoft's Visual Studio. The disadvantage in using a .NET language (eg C#) is there is a somewhat large performance penatly, though it is rarely noticed anymore, with 2GB of RAM being as cheap as a candy bar. This is still no excuse for poor coding, however.
I apologize for this post be a rather "personal" and "targeted" one (not very useful for the knowledgebase in most cases, I'm afraid), but I hope it will at least get you thinking. If you'd like to continue this conversation, you may e-mail me at InternalStatic@live.com, as I'd rather not clog this question up any more than I already have.
- Nate
Business Accounts
Answer for Membership
by: jkrPosted on 2009-10-23 at 10:50:50ID: 25646690
Using 'malloc()' here is quite simple, just change that to
Select allOpen in new window