Hi, fairly new to C programming. I have been asked to generate a list of numbers and store the prime ones in an array so that they can be then later printed out in columns.
My generateprime function is working fine and i can list all the prime numbers i need but I need to know how to add the prime numbers found to an array,
eg -
-
-
do {
-
-
if the value is prime
-
{
-
printf(output the found prime to screen);
-
move on to test next numbers along
-
// i believe i need to add the found prime to the array here
-
// but i will be using a function to pass the array through
-
// and im not sure what to do.
-
-
}
-
else
-
{
-
move on to test next numbers along
-
}
-
-
}while (the counter is less than requested amount);
-
//CALL Show prime function array
-
}
I am making a function which the program can call to list the numbers found in columns on a screen.
sort of like this -
-
void showprimes(int array[])
-
{
-
printf(every 5 primes found for example a new line begins)
-
}
Im really just getting very confused. If someone could help me understand how to store the found primes into an array and then call them from a function I would be very grateful.
4 2669 Banfa 9,065
Expert Mod 8TB
One of the problems with storing something like primes in an array is that you have to know how many entries you will need when you create the array.
However you can get round this by allocating memory (function: malloc) and using a pointer to hold the address, you can then use this pointer as an array name (simply speaking). If you runout of space in your allocated array you can simply allocate more memory (function: realloc).
The all you need to do is have a variable that gives the next array index to write to, this would be initialised to 0, everytime you find a prime number you execute the following pseudo code -
if (nextIndex == maxIndex)
-
{
-
reallocate array
-
recalculate maxIndex
-
}
-
-
array[nextIndex] = newPrimaryNumber;
-
nextIndex++;
-
Thanks for the reply. I dont know much about pointers. Im attempting to store 1000 prime numbers found in the array and then call it later using a function to display these 1000 in columns.
Banfa 9,065
Expert Mod 8TB
Pointers aren't that hard but if you have a defined limit (1000) then an array will do fine
the pseudo code I posted modifies to -
if (nextIndex < 1000)
-
{
-
array[nextIndex] = newPrimaryNumber;
-
nextIndex++;
-
}
-
printing is simple, by this time of course nextIndex contains the number of primes in the array (should be 1000) -
for(temp = 0; temp<nextIndex; temp++)
-
{
-
printf("%d ", array[temp]);
-
}
-
I will leave it to you to work out how in insert the new lines but you will probably want to make use of the % operator.
Thank you very much. I will look into it.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: dave |
last post by:
Hello there,
I am at my wit's end ! I have used the following script succesfully to
upload an image to my web space. But what I really want to be able to do is
to update an existing record in a...
|
by: supercomputer |
last post by:
I am using this function to parse data I have stored in an array.
This is what the array looks like:
, , , , , , , , ,
, ,
, , , , , , ,
, , , , , ]
This is the code to parse the array:
|
by: GIMME |
last post by:
My question is ... How do I sort an Array on numeric, not character values ?
In the example below, after sorting the contents are 1,10,2,3 .
How do I get the contents to be 1,2,3,10 ?
...
|
by: paladin.rithe |
last post by:
Is it possible to store classes in an array? I am fairly new to PHP,
and haven't found anything either way yet.
I have a program that where you can have multiple notes attached to a
ticket, which...
|
by: Martin Jørgensen |
last post by:
Hi,
I'm reading a number of double values from a file. It's a 2D-array:
1 2 3 4 5 6 7
-------------
1 3.2
2 0 2.1
3 9.3
4
|
by: Stubert |
last post by:
I have a training module db that stores information about employees
and what training they have carried our or need to carry out. One
table in this database stores what training needs to be carried...
|
by: lim4801 |
last post by:
I am currently in doing a program which is given by my tutor:
Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are...
|
by: thehobbit |
last post by:
Hi,
Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and
pass the result back to a calling program in COBOL? We were able to
add up to 15 digit numbers without any problems,...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |